|
From: <sa...@us...> - 2003-11-27 14:15:19
|
Update of /cvsroot/jrobin/src/org/jrobin/core
In directory sc8-pr-cvs1:/tmp/cvs-serv6628/org/jrobin/core
Modified Files:
ArcState.java Archive.java Datasource.java Header.java
Robin.java RrdDb.java RrdDef.java RrdDoubleArray.java
RrdPrimitive.java RrdUpdater.java
Added Files:
RrdToolkit.java
Log Message:
Added RRDToolkit class to perform some less frequent but very useful operations on a RRD file as whole. So far so good: you can now add and remove datasource definitions to a RRD still preserving already existing data in it.
--- NEW FILE: RrdToolkit.java ---
/**
* Created by IntelliJ IDEA.
* User: Administrator
* Date: Nov 27, 2003
* Time: 12:21:37 PM
* To change this template use Options | File Templates.
*/
package org.jrobin.core;
import java.io.IOException;
/**
* Class used to perform less important but complex operations on RRD files (like adding
* a new datasource to a RRD file).
*/
public class RrdToolkit {
private static RrdToolkit ourInstance;
/**
* Returns an instance of RrdToolkit.
* @return Toolkint instance to work with
*/
public synchronized static RrdToolkit getInstance() {
if (ourInstance == null) {
ourInstance = new RrdToolkit();
}
return ourInstance;
}
private RrdToolkit() {
}
/**
* Creates a new RRD file with one more datasource in it. RRD file is created based on the
* existing one (the original RRD file is not modified at all). All data from
* the original RRD file is copied to the new one.
* @param sourcePath path to a RRD file to import data from (will not be modified)
* @param destPath path to new RRD file (will be created)
* @param newDatasource Datasource definition to be added to the new RRD file
* @throws IOException Thrown in case of I/O error
* @throws RrdException Thrown in case of JRobin specific error
*/
public void addDatasource(String sourcePath, String destPath, DsDef newDatasource)
throws IOException, RrdException {
RrdDb rrdSource = new RrdDb(sourcePath);
RrdDef rrdDef = rrdSource.getRrdDef();
rrdDef.setPath(destPath);
rrdDef.addDatasource(newDatasource);
RrdDb rrdDest = new RrdDb(rrdDef);
rrdSource.copyStateTo(rrdDest);
rrdSource.close();
rrdDest.close();
}
/**
* Creates a new RRD file with one datasource removed. RRD file is created based on the
* existing one (the original RRD file is not modified at all). All remaining data from
* the original RRD file is copied to the new one.
* @param sourcePath path to a RRD file to import data from (will not be modified)
* @param destPath path to new RRD file (will be created)
* @param dsName Name of the Datasource to be removed from the new RRD file
* @throws IOException Thrown in case of I/O error
* @throws RrdException Thrown in case of JRobin specific error
*/
public void removeDatasource(String sourcePath, String destPath, String dsName)
throws IOException, RrdException {
RrdDb rrdSource = new RrdDb(sourcePath);
RrdDef rrdDef = rrdSource.getRrdDef();
rrdDef.setPath(destPath);
rrdDef.removeDatasource(dsName);
RrdDb rrdDest = new RrdDb(rrdDef);
rrdSource.copyStateTo(rrdDest);
rrdSource.close();
rrdDest.close();
}
public static void main(String[] args) throws RrdException, IOException {
DsDef dsDef = new DsDef("XXX", "GAUGE", 666, -1, Double.NaN);
RrdToolkit.getInstance().addDatasource("demo.rrd", "demo2.rrd", dsDef);
RrdToolkit.getInstance().removeDatasource("demo2.rrd", "demo3.rrd", "XXX");
}
}
Index: ArcState.java
===================================================================
RCS file: /cvsroot/jrobin/src/org/jrobin/core/ArcState.java,v
retrieving revision 1.3
retrieving revision 1.4
diff -C2 -d -r1.3 -r1.4
*** ArcState.java 27 Nov 2003 10:54:07 -0000 1.3
--- ArcState.java 27 Nov 2003 14:15:11 -0000 1.4
***************
*** 41,45 ****
private RrdLong nanSteps;
- // create for the first time
ArcState(Archive parentArc) throws IOException {
this.parentArc = parentArc;
--- 41,44 ----
***************
*** 52,57 ****
long nan = (Util.normalize(lastUpdateTime, step) -
Util.normalize(lastUpdateTime, arcStep)) / step;
- nanSteps = new RrdLong(nan, this);
accumValue = new RrdDouble(Double.NaN, this);
}
else {
--- 51,56 ----
long nan = (Util.normalize(lastUpdateTime, step) -
Util.normalize(lastUpdateTime, arcStep)) / step;
accumValue = new RrdDouble(Double.NaN, this);
+ nanSteps = new RrdLong(nan, this);
}
else {
***************
*** 117,119 ****
--- 116,133 ----
}
+ /**
+ * Copies object's internal state to another ArcState object.
+ * @param other New ArcState object to copy state to
+ * @throws IOException Thrown in case of I/O error
+ * @throws RrdException Thrown if supplied argument is not an ArcState object
+ */
+ public void copyStateTo(RrdUpdater other) throws IOException, RrdException {
+ if(!(other instanceof ArcState)) {
+ throw new RrdException(
+ "Cannot copy ArcState object to " + other.getClass().getName());
+ }
+ ArcState arcState = (ArcState) other;
+ arcState.accumValue.set(accumValue.get());
+ arcState.nanSteps.set(nanSteps.get());
+ }
}
Index: Archive.java
===================================================================
RCS file: /cvsroot/jrobin/src/org/jrobin/core/Archive.java,v
retrieving revision 1.4
retrieving revision 1.5
diff -C2 -d -r1.4 -r1.5
*** Archive.java 27 Nov 2003 10:54:07 -0000 1.4
--- Archive.java 27 Nov 2003 14:15:11 -0000 1.5
***************
*** 45,48 ****
--- 45,49 ----
private RrdDouble xff;
private RrdInt steps, rows;
+ // state
private Robin[] robins;
private ArcState[] states;
***************
*** 87,92 ****
rows = new RrdInt(reader.getRows(arcIndex), this);
int dsCount = reader.getDsCount();
- robins = new Robin[dsCount];
states = new ArcState[dsCount];
for(int dsIndex = 0; dsIndex < dsCount; dsIndex++) {
// restore state
--- 88,93 ----
rows = new RrdInt(reader.getRows(arcIndex), this);
int dsCount = reader.getDsCount();
states = new ArcState[dsCount];
+ robins = new Robin[dsCount];
for(int dsIndex = 0; dsIndex < dsCount; dsIndex++) {
// restore state
***************
*** 386,388 ****
--- 387,418 ----
}
+ /**
+ * Copies object's internal state to another Archive object.
+ * @param other New Archive object to copy state to
+ * @throws IOException Thrown in case of I/O error
+ * @throws RrdException Thrown if supplied argument is not an Archive object
+ */
+ public void copyStateTo(RrdUpdater other) throws IOException, RrdException {
+ if(!(other instanceof Archive)) {
+ throw new RrdException(
+ "Cannot copy Archive object to " + other.getClass().getName());
+ }
+ Archive arc = (Archive) other;
+ if(!arc.consolFun.get().equals(consolFun.get())) {
+ throw new RrdException("Incompatible consolidation functions");
+ }
+ if(arc.steps.get() != steps.get()) {
+ throw new RrdException("Incompatible number of steps");
+ }
+ if(arc.rows.get() != rows.get()) {
+ throw new RrdException("Incompatible number of rows");
+ }
+ int count = Math.min(
+ parentDb.getHeader().getDsCount(),
+ arc.parentDb.getHeader().getDsCount());
+ for(int i = 0; i < count; i++) {
+ states[i].copyStateTo(arc.states[i]);
+ robins[i].copyStateTo(arc.robins[i]);
+ }
+ }
}
Index: Datasource.java
===================================================================
RCS file: /cvsroot/jrobin/src/org/jrobin/core/Datasource.java,v
retrieving revision 1.2
retrieving revision 1.3
diff -C2 -d -r1.2 -r1.3
*** Datasource.java 10 Nov 2003 08:52:27 -0000 1.2
--- Datasource.java 27 Nov 2003 14:15:11 -0000 1.3
***************
*** 267,271 ****
}
! private double calculateTotal(long startTime, long boundaryTime) throws IOException {
double totalValue = Double.NaN;
long validSeconds = boundaryTime - startTime - nanSeconds.get();
--- 267,271 ----
}
! private double calculateTotal(long startTime, long boundaryTime) {
double totalValue = Double.NaN;
long validSeconds = boundaryTime - startTime - nanSeconds.get();
***************
*** 288,291 ****
--- 288,314 ----
writer.writeTag("unknown_sec", nanSeconds.get());
writer.closeTag(); // ds
+ }
+
+ /**
+ * Copies object's internal state to another Datasource object.
+ * @param other New Datasource object to copy state to
+ * @throws IOException Thrown in case of I/O error
+ * @throws RrdException Thrown if supplied argument is not a Datasource object
+ */
+ public void copyStateTo(RrdUpdater other) throws IOException, RrdException {
+ if(!(other instanceof Datasource)) {
+ throw new RrdException(
+ "Cannot copy Datasource object to " + other.getClass().getName());
+ }
+ Datasource datasource = (Datasource) other;
+ if(!datasource.dsName.get().equals(dsName.get())) {
+ throw new RrdException("Incomaptible datasource names");
+ }
+ if(!datasource.dsType.get().equals(dsType.get())) {
+ throw new RrdException("Incomaptible datasource types");
+ }
+ datasource.lastValue.set(lastValue.get());
+ datasource.nanSeconds.set(nanSeconds.get());
+ datasource.accumValue.set(accumValue.get());
}
Index: Header.java
===================================================================
RCS file: /cvsroot/jrobin/src/org/jrobin/core/Header.java,v
retrieving revision 1.2
retrieving revision 1.3
diff -C2 -d -r1.2 -r1.3
*** Header.java 10 Nov 2003 08:52:27 -0000 1.2
--- Header.java 27 Nov 2003 14:15:11 -0000 1.3
***************
*** 165,167 ****
--- 165,181 ----
}
+ /**
+ * Copies object's internal state to another Header object.
+ * @param other New Header object to copy state to
+ * @throws IOException Thrown in case of I/O error
+ * @throws RrdException Thrown if supplied argument is not a Header object
+ */
+ public void copyStateTo(RrdUpdater other) throws IOException, RrdException {
+ if(!(other instanceof Header)) {
+ throw new RrdException(
+ "Cannot copy Header object to " + other.getClass().getName());
+ }
+ Header header = (Header) other;
+ header.lastUpdateTime.set(lastUpdateTime.get());
+ }
}
Index: Robin.java
===================================================================
RCS file: /cvsroot/jrobin/src/org/jrobin/core/Robin.java,v
retrieving revision 1.4
retrieving revision 1.5
diff -C2 -d -r1.4 -r1.5
*** Robin.java 27 Nov 2003 10:22:58 -0000 1.4
--- Robin.java 27 Nov 2003 14:15:11 -0000 1.5
***************
*** 125,127 ****
--- 125,148 ----
}
+ /**
+ * Copies object's internal state to another Robin object.
+ * @param other New Robin object to copy state to
+ * @throws IOException Thrown in case of I/O error
+ * @throws RrdException Thrown if supplied argument is not a Robin object or
+ * is a Robin object with different number of rows
+ */
+ public void copyStateTo(RrdUpdater other) throws IOException, RrdException {
+ if(!(other instanceof Robin)) {
+ throw new RrdException(
+ "Cannot copy Archive object to " + other.getClass().getName());
+ }
+ Robin robin = (Robin) other;
+ if(rows != robin.rows) {
+ throw new RrdException("Incompatible number of rows: " + rows +
+ " != " + robin.rows);
+ }
+ robin.pointer.set(pointer.get());
+ // BULK operation, will speed things up
+ robin.values.writeBytes(values.readBytes());
+ }
}
Index: RrdDb.java
===================================================================
RCS file: /cvsroot/jrobin/src/org/jrobin/core/RrdDb.java,v
retrieving revision 1.4
retrieving revision 1.5
diff -C2 -d -r1.4 -r1.5
*** RrdDb.java 27 Nov 2003 10:22:58 -0000 1.4
--- RrdDb.java 27 Nov 2003 14:15:11 -0000 1.5
***************
*** 649,664 ****
}
! public static void main(String[] args) throws RrdException, IOException {
! RrdDb rrd = new RrdDb("demo.rrd");
! long t1 = Util.getTimestamp(2003, 4, 1);
! long t2 = Util.getTimestamp(2003, 4, 2);
! FetchData data = rrd.createFetchRequest("AVERAGE", t1, t2).fetchData();
! data.dump();
! System.out.println("MAX : " + data.getAggregate("sun", "MAX"));
! System.out.println("MAX2: " + data.getAggregate("sun", "MAX", "value,2,*"));
! System.out.println("MIN : " + data.getAggregate("sun", "MIN"));
! System.out.println("MIN2: " + data.getAggregate("sun", "MIN", "value,2,*"));
! System.out.println("AVG : " + data.getAggregate("sun", "AVERAGE"));
! System.out.println("AVG2: " + data.getAggregate("sun", "AVERAGE", "value,2,*"));
}
}
--- 649,673 ----
}
! /**
! * Copies object's internal state to another RrdDb object.
! * @param other New RrdDb object to copy state to
! * @throws IOException Thrown in case of I/O error
! * @throws RrdException Thrown if supplied argument is not a compatible RrdDb object
! */
! public void copyStateTo(RrdUpdater other) throws IOException, RrdException {
! if(!(other instanceof RrdDb)) {
! throw new RrdException(
! "Cannot copy RrdDb object to " + other.getClass().getName());
! }
! RrdDb rrd = (RrdDb) other;
! header.copyStateTo(rrd.header);
! int dsCount = Math.min(header.getDsCount(), rrd.header.getDsCount());
! for(int i = 0; i < dsCount; i++) {
! datasources[i].copyStateTo(rrd.datasources[i]);
! }
! int arcCount = Math.min(header.getArcCount(), rrd.header.getArcCount());
! for(int i = 0; i < arcCount; i++) {
! archives[i].copyStateTo(rrd.archives[i]);
! }
}
}
Index: RrdDef.java
===================================================================
RCS file: /cvsroot/jrobin/src/org/jrobin/core/RrdDef.java,v
retrieving revision 1.3
retrieving revision 1.4
diff -C2 -d -r1.3 -r1.4
*** RrdDef.java 10 Nov 2003 08:52:27 -0000 1.3
--- RrdDef.java 27 Nov 2003 14:15:11 -0000 1.4
***************
*** 308,310 ****
--- 308,321 ----
return dump();
}
+
+ void removeDatasource(String dsName) throws RrdException {
+ for(int i = 0; i < dsDefs.size(); i++) {
+ DsDef dsDef = (DsDef) dsDefs.get(i);
+ if(dsDef.getDsName().equals(dsName)) {
+ dsDefs.remove(i);
+ return;
+ }
+ }
+ throw new RrdException("Could not find datasource named '" + dsName + "'");
+ }
}
Index: RrdDoubleArray.java
===================================================================
RCS file: /cvsroot/jrobin/src/org/jrobin/core/RrdDoubleArray.java,v
retrieving revision 1.1
retrieving revision 1.2
diff -C2 -d -r1.1 -r1.2
*** RrdDoubleArray.java 27 Nov 2003 10:22:58 -0000 1.1
--- RrdDoubleArray.java 27 Nov 2003 14:15:11 -0000 1.2
***************
*** 45,48 ****
--- 45,51 ----
void set(int index, double value) throws IOException {
+ if(index >= length) {
+ throw new IOException("Invalid index supplied: " + index + ", max = " + length);
+ }
RrdFile rrdFile = getRrdFile();
rrdFile.seek(getPointer() + index * RrdDouble.SIZE);
***************
*** 51,54 ****
--- 54,60 ----
double get(int index) throws IOException {
+ if(index >= length) {
+ throw new IOException("Invalid index supplied: " + index + ", max = " + length);
+ }
RrdFile rrdFile = getRrdFile();
rrdFile.seek(getPointer() + index * RrdDouble.SIZE);
Index: RrdPrimitive.java
===================================================================
RCS file: /cvsroot/jrobin/src/org/jrobin/core/RrdPrimitive.java,v
retrieving revision 1.2
retrieving revision 1.3
diff -C2 -d -r1.2 -r1.3
*** RrdPrimitive.java 27 Nov 2003 11:21:22 -0000 1.2
--- RrdPrimitive.java 27 Nov 2003 14:15:11 -0000 1.3
***************
*** 63,67 ****
}
! byte[] getBytes() throws IOException {
byte[] b = new byte[byteCount];
RrdFile rrdFile = getRrdFile();
--- 63,67 ----
}
! byte[] readBytes() throws IOException {
byte[] b = new byte[byteCount];
RrdFile rrdFile = getRrdFile();
Index: RrdUpdater.java
===================================================================
RCS file: /cvsroot/jrobin/src/org/jrobin/core/RrdUpdater.java,v
retrieving revision 1.2
retrieving revision 1.3
diff -C2 -d -r1.2 -r1.3
*** RrdUpdater.java 10 Nov 2003 08:52:27 -0000 1.2
--- RrdUpdater.java 27 Nov 2003 14:15:11 -0000 1.3
***************
*** 26,29 ****
--- 26,31 ----
package org.jrobin.core;
+ import java.io.IOException;
+
/**
* Interface that has to be implemented for all classes which read from or write to
***************
*** 32,36 ****
* @author <a href="mailto:sa...@jr...">Sasa Markovic</a>
*/
! public interface RrdUpdater {
/**
* Returns associated RrdFile object.
--- 34,38 ----
* @author <a href="mailto:sa...@jr...">Sasa Markovic</a>
*/
! interface RrdUpdater {
/**
* Returns associated RrdFile object.
***************
*** 38,40 ****
--- 40,50 ----
*/
public RrdFile getRrdFile();
+
+ /**
+ * Copies the internal object state to another object of the same type.
+ * @param updater Object to copy the internal state to
+ * @throws IOException Thrown in case of I/O error
+ * @throws RrdException Thrown if supplied argument is not an object of a compatible class
+ */
+ public void copyStateTo(RrdUpdater updater) throws IOException, RrdException;
}
|