|
From: <sa...@us...> - 2003-11-28 13:24:49
|
Update of /cvsroot/jrobin/src/org/jrobin/core
In directory sc8-pr-cvs1:/tmp/cvs-serv8426/org/jrobin/core
Modified Files:
Archive.java RrdDb.java RrdDef.java RrdToolkit.java Util.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.
Index: Archive.java
===================================================================
RCS file: /cvsroot/jrobin/src/org/jrobin/core/Archive.java,v
retrieving revision 1.6
retrieving revision 1.7
diff -C2 -d -r1.6 -r1.7
*** Archive.java 27 Nov 2003 15:53:33 -0000 1.6
--- Archive.java 28 Nov 2003 13:24:46 -0000 1.7
***************
*** 410,416 ****
int count = parentDb.getHeader().getDsCount();
for(int i = 0; i < count; i++) {
! String dsName = parentDb.getDatasource(i).getDsName();
! if(arc.getParentDb().getDatasource(dsName) != null) {
! int j = arc.getParentDb().getDsIndex(dsName);
states[i].copyStateTo(arc.states[j]);
robins[i].copyStateTo(arc.robins[j]);
--- 410,415 ----
int count = parentDb.getHeader().getDsCount();
for(int i = 0; i < count; i++) {
! int j = Util.getMatchingDatasourceIndex(parentDb, i, arc.parentDb);
! if(j >= 0) {
states[i].copyStateTo(arc.states[j]);
robins[i].copyStateTo(arc.robins[j]);
Index: RrdDb.java
===================================================================
RCS file: /cvsroot/jrobin/src/org/jrobin/core/RrdDb.java,v
retrieving revision 1.6
retrieving revision 1.7
diff -C2 -d -r1.6 -r1.7
*** RrdDb.java 27 Nov 2003 15:53:33 -0000 1.6
--- RrdDb.java 28 Nov 2003 13:24:46 -0000 1.7
***************
*** 660,676 ****
"Cannot copy RrdDb object to " + other.getClass().getName());
}
! RrdDb rrd = (RrdDb) other;
! header.copyStateTo(rrd.header);
for(int i = 0; i < datasources.length; i++) {
! Datasource matchingDatasource = rrd.getDatasource(datasources[i].getDsName());
! if(matchingDatasource != null) {
! datasources[i].copyStateTo(matchingDatasource);
}
}
for(int i = 0; i < archives.length; i++) {
! Archive matchingArchive = rrd.getArchive(
! archives[i].getConsolFun(), archives[i].getSteps());
! if(matchingArchive != null) {
! archives[i].copyStateTo(matchingArchive);
}
}
--- 660,675 ----
"Cannot copy RrdDb object to " + other.getClass().getName());
}
! RrdDb otherRrd = (RrdDb) other;
! header.copyStateTo(otherRrd.header);
for(int i = 0; i < datasources.length; i++) {
! int j = Util.getMatchingDatasourceIndex(this, i, otherRrd);
! if(j >= 0) {
! datasources[i].copyStateTo(otherRrd.datasources[j]);
}
}
for(int i = 0; i < archives.length; i++) {
! int j = Util.getMatchingArchiveIndex(this, i, otherRrd);
! if(j >= 0) {
! archives[i].copyStateTo(otherRrd.archives[j]);
}
}
Index: RrdDef.java
===================================================================
RCS file: /cvsroot/jrobin/src/org/jrobin/core/RrdDef.java,v
retrieving revision 1.4
retrieving revision 1.5
diff -C2 -d -r1.4 -r1.5
*** RrdDef.java 27 Nov 2003 14:15:11 -0000 1.4
--- RrdDef.java 28 Nov 2003 13:24:46 -0000 1.5
***************
*** 319,321 ****
--- 319,332 ----
throw new RrdException("Could not find datasource named '" + dsName + "'");
}
+
+ void removeArchive(String consolFun, int steps) throws RrdException {
+ for(int i = 0; i < arcDefs.size(); i++) {
+ ArcDef arcDef = (ArcDef) arcDefs.get(i);
+ if(arcDef.getConsolFun().equals(consolFun) && arcDef.getSteps() == steps) {
+ arcDefs.remove(i);
+ return;
+ }
+ }
+ throw new RrdException("Could not find archive " + consolFun + "/" + steps);
+ }
}
Index: RrdToolkit.java
===================================================================
RCS file: /cvsroot/jrobin/src/org/jrobin/core/RrdToolkit.java,v
retrieving revision 1.3
retrieving revision 1.4
diff -C2 -d -r1.3 -r1.4
*** RrdToolkit.java 27 Nov 2003 15:53:33 -0000 1.3
--- RrdToolkit.java 28 Nov 2003 13:24:46 -0000 1.4
***************
*** 1,15 ****
! /**
! * 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 {
--- 1,41 ----
! /* ============================================================
! * JRobin : Pure java implementation of RRDTool's functionality
! * ============================================================
! *
! * Project Info: http://www.jrobin.org
! * Project Lead: Sasa Markovic (sa...@jr...);
! *
! * (C) Copyright 2003, by Sasa Markovic.
! *
! * Developers: Sasa Markovic (sa...@jr...)
! * Arne Vandamme (cob...@jr...)
! *
! * This library is free software; you can redistribute it and/or modify it under the terms
! * of the GNU Lesser General Public License as published by the Free Software Foundation;
! * either version 2.1 of the License, or (at your option) any later version.
! *
! * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
! * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
! * See the GNU Lesser General Public License for more details.
! *
! * You should have received a copy of the GNU Lesser General Public License along with this
! * library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
! * Boston, MA 02111-1307, USA.
*/
package org.jrobin.core;
import java.io.IOException;
+ import java.io.File;
/**
! * <p>Class used to perform various complex operations on RRD files. Use an instance of the
! * RrdToolkit class to:</p>
! * <ul>
! * <li>add datasource to a RRD file.
! * <li>add archive to a RRD file.
! * <li>remove datasource from a RRD file.
! * <li>remove archive from a RRD file.
! * </ul>
! * <p>All these operations can be performed on the copy of the original RRD file, or on the
! * original file itself (with possible backup file creation)</p>
*/
public class RrdToolkit {
***************
*** 56,59 ****
--- 82,108 ----
/**
+ * <p>Adds one more datasource to a RRD file.</p>
+ * <p>WARNING: This method is potentialy dangerous! It will modify your RRD file.
+ * It is highly recommended to preserve the original RRD file (<i>saveBackup</i>
+ * should be set to <code>true</code>). The backup file will be created in the same
+ * directory as the original one with <code>.bak</code> extension added to the
+ * original name.</p>
+ * <p>Before applying this method, be sure that the specified RRD file is not in use
+ * (not open)</p>
+ * @param sourcePath path to a RRD file to add datasource to.
+ * @param newDatasource Datasource definition to be added to the RRD file
+ * @param saveBackup true, if backup of the original file should be created;
+ * false, otherwise
+ * @throws IOException Thrown in case of I/O error
+ * @throws RrdException Thrown in case of JRobin specific error
+ */
+ public void addDatasource(String sourcePath, DsDef newDatasource, boolean saveBackup)
+ throws IOException, RrdException {
+ String destPath = Util.getTmpFilename();
+ addDatasource(sourcePath, destPath, newDatasource);
+ copyFile(destPath, sourcePath, saveBackup);
+ }
+
+ /**
* 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
***************
*** 65,69 ****
* @throws RrdException Thrown in case of JRobin specific error
*/
-
public void removeDatasource(String sourcePath, String destPath, String dsName)
throws IOException, RrdException {
--- 114,117 ----
***************
*** 82,85 ****
--- 130,156 ----
/**
+ * <p>Removes single datasource from a RRD file.</p>
+ * <p>WARNING: This method is potentialy dangerous! It will modify your RRD file.
+ * It is highly recommended to preserve the original RRD file (<i>saveBackup</i>
+ * should be set to <code>true</code>). The backup file will be created in the same
+ * directory as the original one with <code>.bak</code> extension added to the
+ * original name.</p>
+ * <p>Before applying this method, be sure that the specified RRD file is not in use
+ * (not open)</p>
+ * @param sourcePath path to a RRD file to remove datasource from.
+ * @param dsName Name of the Datasource to be removed from the RRD file
+ * @param saveBackup true, if backup of the original file should be created;
+ * false, otherwise
+ * @throws IOException Thrown in case of I/O error
+ * @throws RrdException Thrown in case of JRobin specific error
+ */
+ public void removeDatasource(String sourcePath, String dsName, boolean saveBackup)
+ throws IOException, RrdException {
+ String destPath = Util.getTmpFilename();
+ removeDatasource(sourcePath, destPath, dsName);
+ copyFile(destPath, sourcePath, saveBackup);
+ }
+
+ /**
* Creates a new RRD file with one more archive in it. RRD file is created based on the
* existing one (the original RRD file is not modified at all). All data from
***************
*** 106,113 ****
}
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", "sun");
}
}
--- 177,289 ----
}
+ /**
+ * <p>Adds one more archive to a RRD file.</p>
+ * <p>WARNING: This method is potentialy dangerous! It will modify your RRD file.
+ * It is highly recommended to preserve the original RRD file (<i>saveBackup</i>
+ * should be set to <code>true</code>). The backup file will be created in the same
+ * directory as the original one with <code>.bak</code> extension added to the
+ * original name.</p>
+ * <p>Before applying this method, be sure that the specified RRD file is not in use
+ * (not open)</p>
+ * @param sourcePath path to a RRD file to add datasource to.
+ * @param newArchive Archive definition to be added to the RRD file
+ * @param saveBackup true, if backup of the original file should be created;
+ * false, otherwise
+ * @throws IOException Thrown in case of I/O error
+ * @throws RrdException Thrown in case of JRobin specific error
+ */
+ public void addArchive(String sourcePath, ArcDef newArchive, boolean saveBackup)
+ throws IOException, RrdException {
+ String destPath = Util.getTmpFilename();
+ addArchive(sourcePath, destPath, newArchive);
+ copyFile(destPath, sourcePath, saveBackup);
+ }
+
+ /**
+ * Creates a new RRD file with one archive removed. RRD file is created based on the
+ * existing one (the original RRD file is not modified at all). All relevant 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 a new RRD file (will be created)
+ * @param consolFun Consolidation function of Archive which should be removed
+ * @param steps Number of steps for Archive which should be removed
+ * @throws IOException Thrown in case of I/O error
+ * @throws RrdException Thrown in case of JRobin specific error
+ */
+ public void removeArchive(String sourcePath, String destPath, String consolFun, int steps)
+ throws IOException, RrdException {
+ if(Util.sameFilePath(sourcePath, destPath)) {
+ throw new RrdException("Source and destination paths are the same");
+ }
+ RrdDb rrdSource = new RrdDb(sourcePath);
+ RrdDef rrdDef = rrdSource.getRrdDef();
+ rrdDef.setPath(destPath);
+ rrdDef.removeArchive(consolFun, steps);
+ RrdDb rrdDest = new RrdDb(rrdDef);
+ rrdSource.copyStateTo(rrdDest);
+ rrdSource.close();
+ rrdDest.close();
+ }
+
+ /**
+ * <p>Removes one archive from a RRD file.</p>
+ * <p>WARNING: This method is potentialy dangerous! It will modify your RRD file.
+ * It is highly recommended to preserve the original RRD file (<i>saveBackup</i>
+ * should be set to <code>true</code>). The backup file will be created in the same
+ * directory as the original one with <code>.bak</code> extension added to the
+ * original name.</p>
+ * <p>Before applying this method, be sure that the specified RRD file is not in use
+ * (not open)</p>
+ * @param sourcePath path to a RRD file to add datasource to.
+ * @param consolFun Consolidation function of Archive which should be removed
+ * @param steps Number of steps for Archive which should be removed
+ * @param saveBackup true, if backup of the original file should be created;
+ * false, otherwise
+ * @throws IOException Thrown in case of I/O error
+ * @throws RrdException Thrown in case of JRobin specific error
+ */
+ public void removeArchive(String sourcePath, String consolFun, int steps,
+ boolean saveBackup) throws IOException, RrdException {
+ String destPath = Util.getTmpFilename();
+ removeArchive(sourcePath, destPath, consolFun, steps);
+ copyFile(destPath, sourcePath, saveBackup);
+ }
+
+ private static void copyFile(String sourcePath, String destPath, boolean saveBackup)
+ throws IOException {
+ File source = new File(sourcePath);
+ File dest = new File(destPath);
+ if(saveBackup) {
+ String backupPath = destPath + ".bak";
+ File backup = new File(backupPath);
+ deleteFile(backup);
+ if(!dest.renameTo(backup)) {
+ throw new IOException("Could not create backup file " + backupPath);
+ }
+ }
+ deleteFile(dest);
+ if(!source.renameTo(dest)) {
+ throw new IOException("Could not create file " + destPath + " from " + sourcePath);
+ }
+ }
+
+ private static void deleteFile(File file) throws IOException {
+ if(file.exists() && !file.delete()) {
+ throw new IOException("Could not delete file: " + file.getCanonicalPath());
+ }
+ }
+
public static void main(String[] args) throws RrdException, IOException {
! String file = "c:/test.rrd";
! RrdToolkit tool = RrdToolkit.getInstance();
! DsDef dsDef1 = new DsDef("XXX", "GAUGE", 666, -1, Double.NaN);
! DsDef dsDef2 = new DsDef("YYY", "GAUGE", 777, +1, Double.NaN);
! tool.addDatasource(file, dsDef1, true);
! tool.addDatasource(file, dsDef2, false);
! tool.removeDatasource(file, "ftpUsers", false);
! tool.removeDatasource(file, "XXX", false);
! ArcDef arcDef1 = new ArcDef("LAST", 0.22222, 13, 567);
! tool.addArchive(file, arcDef1, false);
! tool.removeArchive(file, "MAX", 6, false);
}
}
Index: Util.java
===================================================================
RCS file: /cvsroot/jrobin/src/org/jrobin/core/Util.java,v
retrieving revision 1.5
retrieving revision 1.6
diff -C2 -d -r1.5 -r1.6
*** Util.java 27 Nov 2003 14:37:20 -0000 1.5
--- Util.java 28 Nov 2003 13:24:46 -0000 1.6
***************
*** 205,208 ****
--- 205,233 ----
}
+ static int getMatchingDatasourceIndex(RrdDb rrd1, int dsIndex, RrdDb rrd2)
+ throws IOException {
+ String dsName = rrd1.getDatasource(dsIndex).getDsName();
+ try {
+ return rrd2.getDsIndex(dsName);
+ } catch (RrdException e) {
+ return -1;
+ }
+ }
+
+ static int getMatchingArchiveIndex(RrdDb rrd1, int arcIndex, RrdDb rrd2)
+ throws IOException {
+ Archive archive = rrd1.getArchive(arcIndex);
+ String consolFun = archive.getConsolFun();
+ int steps = archive.getSteps();
+ try {
+ return rrd2.getArcIndex(consolFun, steps);
+ } catch (RrdException e) {
+ return -1;
+ }
+ }
+
+ static String getTmpFilename() throws IOException {
+ return File.createTempFile("JROBIN_", ".tmp").getCanonicalPath();
+ }
}
|