|
From: <sa...@us...> - 2003-12-08 13:47:44
|
Update of /cvsroot/jrobin/src/org/jrobin/core
In directory sc8-pr-cvs1:/tmp/cvs-serv6289/org/jrobin/core
Modified Files:
ArcDef.java Archive.java Robin.java RrdDef.java
RrdToolkit.java
Log Message:
Finished RRDToolkit class. It is now possible to resize RRAs, but preserving already archived values.
Index: ArcDef.java
===================================================================
RCS file: /cvsroot/jrobin/src/org/jrobin/core/ArcDef.java,v
retrieving revision 1.3
retrieving revision 1.4
diff -C2 -d -r1.3 -r1.4
*** ArcDef.java 10 Nov 2003 08:52:27 -0000 1.3
--- ArcDef.java 8 Dec 2003 13:47:40 -0000 1.4
***************
*** 158,160 ****
--- 158,164 ----
}
+ void setRows(int rows) {
+ this.rows = rows;
+ }
+
}
Index: Archive.java
===================================================================
RCS file: /cvsroot/jrobin/src/org/jrobin/core/Archive.java,v
retrieving revision 1.9
retrieving revision 1.10
diff -C2 -d -r1.9 -r1.10
*** Archive.java 2 Dec 2003 10:18:17 -0000 1.9
--- Archive.java 8 Dec 2003 13:47:40 -0000 1.10
***************
*** 399,405 ****
throw new RrdException("Incompatible number of steps");
}
- if(arc.rows.get() != rows.get()) {
- throw new RrdException("Incompatible number of rows");
- }
int count = parentDb.getHeader().getDsCount();
for(int i = 0; i < count; i++) {
--- 399,402 ----
Index: Robin.java
===================================================================
RCS file: /cvsroot/jrobin/src/org/jrobin/core/Robin.java,v
retrieving revision 1.6
retrieving revision 1.7
diff -C2 -d -r1.6 -r1.7
*** Robin.java 30 Nov 2003 08:14:11 -0000 1.6
--- Robin.java 8 Dec 2003 13:47:40 -0000 1.7
***************
*** 1,160 ****
! /* ============================================================
! * 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.
! *
! * 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.
! *
! * Developers: Sasa Markovic (sa...@jr...)
! * Arne Vandamme (cob...@jr...)
! *
! * 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;
!
! /**
! * Class to represent archive values for a single datasource. Robin class is the heart of
! * the so-called "round robin database" concept. Basically, each Robin object is a
! * fixed length array of double values. Each double value reperesents consolidated archive
! * value for the specific timestamp. When the underlying array of double values gets completely
! * filled, new values will replace the oldest entries.<p>
! *
! * Robin object does not hold values in memory - such object could be quite large.
! * Instead of it, Robin stores all values on the disk and reads them only when necessary.
! *
! * @author <a href="mailto:sa...@jr...">Sasa Markovic</a>
! */
! public class Robin implements RrdUpdater {
! private Archive parentArc;
! private RrdInt pointer;
! private RrdDoubleArray values;
! private int rows;
!
! Robin(Archive parentArc, int rows) throws IOException {
! this.parentArc = parentArc;
! this.rows = rows;
! if(getRrdFile().getMode() == RrdFile.MODE_CREATE) {
! pointer = new RrdInt(0, this);
! values = new RrdDoubleArray(this, rows, Double.NaN);
! }
! else {
! pointer = new RrdInt(this);
! values = new RrdDoubleArray(this, rows);
! }
! }
!
! /**
! * Fetches all Robin archive values from the disk.
! *
! * @return Array of double archive values, starting from the oldest one.
! * @throws IOException Thrown in case of IO specific error.
! */
! public double[] getValues() throws IOException {
! double[] result = new double[rows];
! int start = pointer.get();
! for(int i = start, j = 0; i < start + rows; i++, j++) {
! result[j] = values.get(i % rows);
! }
! return result;
! }
!
! void store(double newValue) throws IOException {
! int position = pointer.get();
! values.set(position, newValue);
! pointer.set((position + 1) % rows);
! }
!
! /**
! * Returns the underlying RrdFile object.
! * @return Underlying RrdFile object
! */
! public RrdFile getRrdFile() {
! return parentArc.getRrdFile();
! }
!
! String dump() throws IOException {
! StringBuffer buffer = new StringBuffer("Robin " + pointer.get() + "/" + rows + ": ");
! int startPos = pointer.get();
! for(int i = startPos; i < startPos + rows; i++) {
! buffer.append(Util.formatDouble(values.get(i % rows), true) + " ");
! }
! buffer.append("\n");
! return buffer.toString();
! }
!
! /**
! * Returns the i-th value from the Robin archive.
! * @param index Value index
! * @return Value stored in the i-th position (the oldest value has zero index)
! */
! public double getValue(int index) throws IOException {
! return values.get((pointer.get() + index) % rows);
! }
!
! /**
! * Returns the Archive object to which this Robin object belongs.
! *
! * @return Parent Archive object
! */
! public Archive getParent() {
! return parentArc;
! }
!
! /**
! * Returns the size of the underlying array of archive values.
! *
! * @return Number of stored values
! */
! public int getSize() {
! return rows;
! }
!
! /**
! * 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());
! }
!
! void filterValues(double minValue, double maxValue) throws IOException {
! for(int i = 0; i < rows; i++) {
! double value = values.get(i);
! if(!Double.isNaN(minValue) && !Double.isNaN(value) && minValue > value) {
! values.set(i, Double.NaN);
! }
! if(!Double.isNaN(maxValue) && !Double.isNaN(value) && maxValue < value) {
! values.set(i, Double.NaN);
! }
! }
! }
! }
--- 1,165 ----
! /* ============================================================
! * 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.
! *
! * 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.
! *
! * Developers: Sasa Markovic (sa...@jr...)
! * Arne Vandamme (cob...@jr...)
! *
! * 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;
!
! /**
! * Class to represent archive values for a single datasource. Robin class is the heart of
! * the so-called "round robin database" concept. Basically, each Robin object is a
! * fixed length array of double values. Each double value reperesents consolidated archive
! * value for the specific timestamp. When the underlying array of double values gets completely
! * filled, new values will replace the oldest entries.<p>
! *
! * Robin object does not hold values in memory - such object could be quite large.
! * Instead of it, Robin stores all values on the disk and reads them only when necessary.
! *
! * @author <a href="mailto:sa...@jr...">Sasa Markovic</a>
! */
! public class Robin implements RrdUpdater {
! private Archive parentArc;
! private RrdInt pointer;
! private RrdDoubleArray values;
! private int rows;
!
! Robin(Archive parentArc, int rows) throws IOException {
! this.parentArc = parentArc;
! this.rows = rows;
! if(getRrdFile().getMode() == RrdFile.MODE_CREATE) {
! pointer = new RrdInt(0, this);
! values = new RrdDoubleArray(this, rows, Double.NaN);
! }
! else {
! pointer = new RrdInt(this);
! values = new RrdDoubleArray(this, rows);
! }
! }
!
! /**
! * Fetches all Robin archive values from the disk.
! *
! * @return Array of double archive values, starting from the oldest one.
! * @throws IOException Thrown in case of IO specific error.
! */
! public double[] getValues() throws IOException {
! double[] result = new double[rows];
! int start = pointer.get();
! for(int i = start, j = 0; i < start + rows; i++, j++) {
! result[j] = values.get(i % rows);
! }
! return result;
! }
!
! void store(double newValue) throws IOException {
! int position = pointer.get();
! values.set(position, newValue);
! pointer.set((position + 1) % rows);
! }
!
! /**
! * Returns the underlying RrdFile object.
! * @return Underlying RrdFile object
! */
! public RrdFile getRrdFile() {
! return parentArc.getRrdFile();
! }
!
! String dump() throws IOException {
! StringBuffer buffer = new StringBuffer("Robin " + pointer.get() + "/" + rows + ": ");
! int startPos = pointer.get();
! for(int i = startPos; i < startPos + rows; i++) {
! buffer.append(Util.formatDouble(values.get(i % rows), true) + " ");
! }
! buffer.append("\n");
! return buffer.toString();
! }
!
! /**
! * Returns the i-th value from the Robin archive.
! * @param index Value index
! * @return Value stored in the i-th position (the oldest value has zero index)
! */
! public double getValue(int index) throws IOException {
! return values.get((pointer.get() + index) % rows);
! }
!
! /**
! * Returns the Archive object to which this Robin object belongs.
! *
! * @return Parent Archive object
! */
! public Archive getParent() {
! return parentArc;
! }
!
! /**
! * Returns the size of the underlying array of archive values.
! *
! * @return Number of stored values
! */
! public int getSize() {
! return rows;
! }
!
! /**
! * 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
! */
! public void copyStateTo(RrdUpdater other) throws IOException, RrdException {
! if(!(other instanceof Robin)) {
! throw new RrdException(
! "Cannot copy Robin object to " + other.getClass().getName());
! }
! Robin robin = (Robin) other;
! int rowsDiff = rows - robin.rows;
! if(rowsDiff == 0) {
! // Identical dimensions. Do copy in BULK to speed things up
! robin.pointer.set(pointer.get());
! robin.values.writeBytes(values.readBytes());
! }
! else {
! // different sizes
! for(int i = 0; i < robin.rows; i++) {
! int j = i + rowsDiff;
! robin.store(j >= 0? getValue(j): Double.NaN);
! }
! }
! }
!
! void filterValues(double minValue, double maxValue) throws IOException {
! for(int i = 0; i < rows; i++) {
! double value = values.get(i);
! if(!Double.isNaN(minValue) && !Double.isNaN(value) && minValue > value) {
! values.set(i, Double.NaN);
! }
! if(!Double.isNaN(maxValue) && !Double.isNaN(value) && maxValue < value) {
! values.set(i, Double.NaN);
! }
! }
! }
! }
Index: RrdDef.java
===================================================================
RCS file: /cvsroot/jrobin/src/org/jrobin/core/RrdDef.java,v
retrieving revision 1.5
retrieving revision 1.6
diff -C2 -d -r1.5 -r1.6
*** RrdDef.java 28 Nov 2003 13:24:46 -0000 1.5
--- RrdDef.java 8 Dec 2003 13:47:40 -0000 1.6
***************
*** 321,329 ****
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;
}
}
--- 321,335 ----
void removeArchive(String consolFun, int steps) throws RrdException {
+ ArcDef arcDef = findArchive(consolFun, steps);
+ if(!arcDefs.remove(arcDef)) {
+ throw new RrdException("Could not remove archive " + consolFun + "/" + steps);
+ }
+ }
+
+ ArcDef findArchive(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) {
! return arcDef;
}
}
Index: RrdToolkit.java
===================================================================
RCS file: /cvsroot/jrobin/src/org/jrobin/core/RrdToolkit.java,v
retrieving revision 1.5
retrieving revision 1.6
diff -C2 -d -r1.5 -r1.6
*** RrdToolkit.java 4 Dec 2003 13:21:03 -0000 1.5
--- RrdToolkit.java 8 Dec 2003 13:47:40 -0000 1.6
***************
*** 357,360 ****
--- 357,414 ----
}
+ /**
+ * Creates new RRD file based on the existing one, but with a different
+ * size (number of rows) for a single archive. The archive to be resized
+ * is identified by its consolidation function and the number of steps.
+ * @param sourcePath Path to the source RRD file (will not be modified)
+ * @param destPath Path to the new RRD file (will be created)
+ * @param consolFun Consolidation function of the archive to be resized
+ * @param numSteps Number of steps of the archive to be resized
+ * @param newRows New archive size (number of archive rows)
+ * @throws IOException Thrown in case of I/O error
+ * @throws RrdException Thrown in case of JRobin specific error
+ */
+ public void resizeArchive(String sourcePath, String destPath, String consolFun,
+ int numSteps, int newRows)
+ throws IOException, RrdException {
+ if(Util.sameFilePath(sourcePath, destPath)) {
+ throw new RrdException("Source and destination paths are the same");
+ }
+ if(newRows < 2) {
+ throw new RrdException("New arcihve size must be at least 2");
+ }
+ RrdDb rrdSource = new RrdDb(sourcePath);
+ RrdDef rrdDef = rrdSource.getRrdDef();
+ ArcDef arcDef = rrdDef.findArchive(consolFun, numSteps);
+ if(arcDef.getRows() != newRows) {
+ arcDef.setRows(newRows);
+ rrdDef.setPath(destPath);
+ RrdDb rrdDest = new RrdDb(rrdDef);
+ rrdSource.copyStateTo(rrdDest);
+ rrdDest.close();
+ }
+ rrdSource.close();
+ }
+
+ /**
+ * Modifies existing RRD file, by resizing its chosen archive. The archive to be resized
+ * is identified by its consolidation function and the number of steps.
+ * @param sourcePath Path to the RRD file (will be modified)
+ * @param consolFun Consolidation function of the archive to be resized
+ * @param numSteps Number of steps of the archive to be resized
+ * @param newRows New archive size (number of archive rows)
+ * @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 resizeArchive(String sourcePath, String consolFun,
+ int numSteps, int newRows, boolean saveBackup)
+ throws IOException, RrdException {
+ String destPath = Util.getTmpFilename();
+ resizeArchive(sourcePath, destPath, consolFun, numSteps, newRows);
+ copyFile(destPath, sourcePath, saveBackup);
+ }
+
private static void deleteFile(File file) throws IOException {
if(file.exists() && !file.delete()) {
|