|
From: <sa...@us...> - 2003-11-27 10:23:01
|
Update of /cvsroot/jrobin/src/org/jrobin/core In directory sc8-pr-cvs1:/tmp/cvs-serv5669/org/jrobin/core Modified Files: Archive.java Robin.java RrdDb.java RrdDouble.java RrdFile.java RrdInt.java RrdLong.java RrdString.java Added Files: RrdDoubleArray.java RrdPrimitive.java Log Message: Related classes (RrdInt, RrdLong, etc) are now properly rooted. RrdPrimtive class introduced as a base class for all classes mapped to a RRD file. Code should run slightly faster. --- NEW FILE: RrdDoubleArray.java --- /* ============================================================ * 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; class RrdDoubleArray extends RrdPrimitive { private int length; RrdDoubleArray(RrdUpdater updater, int length) throws IOException { super(updater, length * RrdDouble.SIZE); this.length = length; } RrdDoubleArray(RrdUpdater updater, int length, double initVal) throws IOException { super(updater, length * RrdDouble.SIZE); this.length = length; for(int i = 0; i < length; i++) { set(i, initVal); } } void set(int index, double value) throws IOException { RrdFile rrdFile = getRrdFile(); rrdFile.seek(getPointer() + index * RrdDouble.SIZE); rrdFile.writeDouble(value); } double get(int index) throws IOException { RrdFile rrdFile = getRrdFile(); rrdFile.seek(getPointer() + index * RrdDouble.SIZE); return rrdFile.readDouble(); } } --- NEW FILE: RrdPrimitive.java --- /* ============================================================ * 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; abstract class RrdPrimitive { private RrdUpdater parent; private long pointer; private int byteCount; RrdPrimitive(RrdUpdater parent, int byteCount) throws IOException { this.parent = parent; // this will set pointer and byteCount parent.getRrdFile().allocate(this, byteCount); } long getPointer() { return pointer; } void setPointer(long pointer) { this.pointer = pointer; } int getByteCount() { return byteCount; } void setByteCount(int byteCount) { this.byteCount = byteCount; } RrdUpdater getParent() { return parent; } RrdFile getRrdFile() { return parent.getRrdFile(); } byte[] getBytes() throws IOException { byte[] b = new byte[byteCount]; int bytesRead = parent.getRrdFile().read(b); if(bytesRead != byteCount) { throw new IOException("Could not read enough bytes (" + byteCount + " bytes requested, " + bytesRead + " bytes obtained"); } return b; } } Index: Archive.java =================================================================== RCS file: /cvsroot/jrobin/src/org/jrobin/core/Archive.java,v retrieving revision 1.2 retrieving revision 1.3 diff -C2 -d -r1.2 -r1.3 *** Archive.java 10 Nov 2003 08:52:27 -0000 1.2 --- Archive.java 27 Nov 2003 10:22:58 -0000 1.3 *************** *** 60,64 **** for(int i = 0; i < n; i++) { states[i] = new ArcState(this, true); ! robins[i] = new Robin(this, rows.get(), true); } } --- 60,64 ---- for(int i = 0; i < n; i++) { states[i] = new ArcState(this, true); ! robins[i] = new Robin(this, rows.get()); } } *************** *** 76,80 **** for(int i = 0; i < n; i++) { states[i] = new ArcState(this, false); ! robins[i] = new Robin(this, rows.get(), false); } } --- 76,80 ---- for(int i = 0; i < n; i++) { states[i] = new ArcState(this, false); ! robins[i] = new Robin(this, rows.get()); } } *************** *** 95,99 **** states[dsIndex].setNanSteps(reader.getStateNanSteps(arcIndex, dsIndex)); // restore robins ! robins[dsIndex] = new Robin(this, rows.get(), true); double[] values = reader.getValues(arcIndex, dsIndex); for(int j = 0; j < values.length; j++) { --- 95,99 ---- states[dsIndex].setNanSteps(reader.getStateNanSteps(arcIndex, dsIndex)); // restore robins ! robins[dsIndex] = new Robin(this, rows.get()); double[] values = reader.getValues(arcIndex, dsIndex); for(int j = 0; j < values.length; j++) { Index: Robin.java =================================================================== RCS file: /cvsroot/jrobin/src/org/jrobin/core/Robin.java,v retrieving revision 1.3 retrieving revision 1.4 diff -C2 -d -r1.3 -r1.4 *** Robin.java 13 Nov 2003 11:45:35 -0000 1.3 --- Robin.java 27 Nov 2003 10:22:58 -0000 1.4 *************** *** 43,59 **** private Archive parentArc; private RrdInt pointer; ! private RrdDouble values; private int rows; ! Robin(Archive parentArc, int rows, boolean newRobin) throws IOException { this.parentArc = parentArc; this.rows = rows; ! pointer = new RrdInt(this); ! values = new RrdDouble(this, rows); ! if(newRobin) { ! pointer.set(0); ! for(int i = 0; i < rows; i++) { ! values.set(i, Double.NaN); ! } } } --- 43,59 ---- 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); } } Index: RrdDb.java =================================================================== RCS file: /cvsroot/jrobin/src/org/jrobin/core/RrdDb.java,v retrieving revision 1.3 retrieving revision 1.4 diff -C2 -d -r1.3 -r1.4 *** RrdDb.java 10 Nov 2003 08:52:27 -0000 1.3 --- RrdDb.java 27 Nov 2003 10:22:58 -0000 1.4 *************** *** 107,111 **** public RrdDb(RrdDef rrdDef) throws IOException, RrdException { rrdDef.validate(); ! initializeSetup(rrdDef.getPath()); // create header header = new Header(this, rrdDef); --- 107,111 ---- public RrdDb(RrdDef rrdDef) throws IOException, RrdException { rrdDef.validate(); ! initializeSetup(rrdDef.getPath(), RrdFile.MODE_CREATE); // create header header = new Header(this, rrdDef); *************** *** 123,127 **** } // finalize ! finalizeSetup(true); Util.debug(rrdDef.getRrdToolCommand()); } --- 123,127 ---- } // finalize ! finalizeSetup(); Util.debug(rrdDef.getRrdToolCommand()); } *************** *** 143,147 **** } try { ! initializeSetup(path); // restore header header = new Header(this); --- 143,147 ---- } try { ! initializeSetup(path, RrdFile.MODE_RESTORE); // restore header header = new Header(this); *************** *** 158,162 **** archives[i] = new Archive(this); } ! finalizeSetup(false); } catch(RuntimeException e) { --- 158,162 ---- archives[i] = new Archive(this); } ! finalizeSetup(); } catch(RuntimeException e) { *************** *** 188,192 **** */ public RrdDb(String rrdPath, String xmlPath) throws IOException, RrdException { ! initializeSetup(rrdPath); XmlReader reader = new XmlReader(xmlPath); // create header --- 188,192 ---- */ public RrdDb(String rrdPath, String xmlPath) throws IOException, RrdException { ! initializeSetup(rrdPath, RrdFile.MODE_CREATE); XmlReader reader = new XmlReader(xmlPath); // create header *************** *** 205,225 **** reader = null; // finalize ! finalizeSetup(true); } ! private void initializeSetup(String path) throws IOException { ! file = new RrdFile(path); ! file.setSafeMode(true); } ! private void finalizeSetup(boolean newFile) throws IOException { ! if(newFile) { ! file.truncate(); } ! else if(!file.isEndReached()) { ! throw new IOException("Extra bytes found in RRD file. Not a RRD file at all?"); } ! file.setSafeMode(false); } --- 205,227 ---- reader = null; // finalize ! finalizeSetup(); } ! private void initializeSetup(String path, int mode) throws IOException { ! file = new RrdFile(path, mode); } ! private void finalizeSetup() throws IOException { ! int mode = file.getMode(); ! if(mode == RrdFile.MODE_CREATE) { ! file.truncateFile(); } ! else if(mode == RrdFile.MODE_RESTORE) { ! if(!file.isEndReached()) { ! throw new IOException("Extra bytes found in RRD file. Not a RRD file at all?"); ! } } ! file.setMode(RrdFile.MODE_NORMAL); } Index: RrdDouble.java =================================================================== RCS file: /cvsroot/jrobin/src/org/jrobin/core/RrdDouble.java,v retrieving revision 1.3 retrieving revision 1.4 diff -C2 -d -r1.3 -r1.4 *** RrdDouble.java 10 Nov 2003 08:52:27 -0000 1.3 --- RrdDouble.java 27 Nov 2003 10:22:58 -0000 1.4 *************** *** 28,81 **** import java.io.IOException; ! class RrdDouble { ! private RrdFile file; ! private long pointer; ! private int count; ! ! private boolean cached = false; ! private double cachedValue; ! ! RrdDouble(RrdUpdater updater, int count) throws IOException { ! this.count = count; ! file = updater.getRrdFile(); ! pointer = file.allocate(RrdFile.DOUBLE_SIZE, count); ! } RrdDouble(RrdUpdater updater) throws IOException { ! this(updater, 1); } ! ! RrdDouble(double initValue, RrdUpdater updater) throws IOException { ! this.count = 1; ! file = updater.getRrdFile(); ! pointer = file.allocate(initValue); ! cached = true; ! cachedValue = initValue; } ! void set(int index, double value) throws IOException { ! assert index < count; ! long readPointer = pointer + index * RrdFile.DOUBLE_SIZE; ! file.writeDouble(readPointer, value); } void set(double value) throws IOException { ! cached = true; ! cachedValue = value; ! set(0, value); ! } ! ! double get(int index) throws IOException { ! assert index < count; ! long readPointer = pointer + index * RrdFile.DOUBLE_SIZE; ! return file.readDouble(readPointer); } ! double get() throws IOException { ! if(!cached) { ! cachedValue = get(0); ! cached = true; ! } ! return cachedValue; } } --- 28,63 ---- import java.io.IOException; ! class RrdDouble extends RrdPrimitive { ! static final int SIZE = 8; ! ! private double cache; RrdDouble(RrdUpdater updater) throws IOException { ! super(updater, SIZE); ! loadCache(); } ! ! void loadCache() throws IOException { ! RrdFile rrdFile = getRrdFile(); ! if(rrdFile.getMode() == RrdFile.MODE_RESTORE) { ! rrdFile.seek(getPointer()); ! cache = rrdFile.readDouble(); ! } } ! RrdDouble(double initValue, RrdUpdater updater) throws IOException { ! super(updater, SIZE); ! set(initValue); } void set(double value) throws IOException { ! cache = value; ! RrdFile rrdFile = getRrdFile(); ! rrdFile.seek(getPointer()); ! rrdFile.writeDouble(cache); } ! double get() { ! return cache; } } Index: RrdFile.java =================================================================== RCS file: /cvsroot/jrobin/src/org/jrobin/core/RrdFile.java,v retrieving revision 1.2 retrieving revision 1.3 diff -C2 -d -r1.2 -r1.3 *** RrdFile.java 10 Nov 2003 08:52:27 -0000 1.2 --- RrdFile.java 27 Nov 2003 10:22:58 -0000 1.3 *************** *** 26,33 **** --- 26,36 ---- package org.jrobin.core; + import java.io.File; import java.io.IOException; import java.io.RandomAccessFile; import java.nio.channels.FileChannel; import java.nio.channels.FileLock; + import java.util.LinkedList; + import java.util.List; /** *************** *** 38,61 **** * @author <a href="mailto:sa...@jr...">Sasa Markovic</a> */ ! public class RrdFile { ! /** Maximum acceptable string length (20).*/ ! public static int STRING_LENGTH = 20; ! static final int STRING_SIZE = STRING_LENGTH * 2; ! static final int INT_SIZE = 4; ! static final int LONG_SIZE = 8; ! static final int DOUBLE_SIZE = 8; static final long LOCK_DELAY = 500; // 0.5sec static int lockMode = RrdDb.NO_LOCKS; - private RandomAccessFile file; - private boolean safeMode = true; - private long bookmark; private String filePath; private FileLock fileLock; ! RrdFile(String filePath) throws IOException { this.filePath = filePath; ! file = new RandomAccessFile(filePath, "rw"); lockFile(); } --- 41,62 ---- * @author <a href="mailto:sa...@jr...">Sasa Markovic</a> */ ! public class RrdFile extends RandomAccessFile { ! static final int MODE_NORMAL = 0; ! static final int MODE_RESTORE = 1; ! static final int MODE_CREATE = 2; static final long LOCK_DELAY = 500; // 0.5sec + static final String FLAGS = "rw"; // R/W access static int lockMode = RrdDb.NO_LOCKS; private String filePath; private FileLock fileLock; + private List primitives = new LinkedList(); + private int mode; ! RrdFile(String filePath, int mode) throws IOException { ! super(filePath, FLAGS); this.filePath = filePath; ! this.mode = mode; lockFile(); } *************** *** 63,67 **** private void lockFile() throws IOException { if(lockMode == RrdDb.WAIT_IF_LOCKED || lockMode == RrdDb.EXCEPTION_IF_LOCKED) { ! FileChannel fileChannel = file.getChannel(); do { fileLock = fileChannel.tryLock(); --- 64,68 ---- private void lockFile() throws IOException { if(lockMode == RrdDb.WAIT_IF_LOCKED || lockMode == RrdDb.EXCEPTION_IF_LOCKED) { ! FileChannel fileChannel = getChannel(); do { fileLock = fileChannel.tryLock(); *************** *** 85,94 **** } ! void close() throws IOException { unlockFile(); ! if(file != null) { ! file.close(); ! file = null; ! } } --- 86,96 ---- } ! /** ! * Closes the underlying RandomAccessFile object. ! * @throws IOException Thrown in case of I/O error ! */ ! public void close() throws IOException { unlockFile(); ! super.close(); } *************** *** 103,233 **** close(); } ! ! void truncate() throws IOException { ! file.setLength(file.getFilePointer()); ! } ! ! boolean isEndReached() throws IOException { ! return file.getFilePointer() == file.length(); ! } ! ! long allocate(int typeSize, int count) throws IOException { ! assert typeSize == STRING_SIZE || typeSize == INT_SIZE || ! typeSize == LONG_SIZE || typeSize == DOUBLE_SIZE; ! long pointer = file.getFilePointer(); ! file.seek(pointer + typeSize * count); ! return pointer; ! } ! ! long allocate(int intVal) throws IOException { ! long pointer = file.getFilePointer(); ! file.writeInt(intVal); ! return pointer; ! } ! ! long allocate(long longVal) throws IOException { ! long pointer = file.getFilePointer(); ! file.writeLong(longVal); ! return pointer; ! } ! ! long allocate(double doubleVal) throws IOException { ! long pointer = file.getFilePointer(); ! file.writeDouble(doubleVal); ! return pointer; ! } ! ! long allocate(String stringVal) throws IOException { ! long pointer = file.getFilePointer(); ! writeStringInternal(stringVal); ! return pointer; ! } ! ! int readInt(long pointer) throws IOException { ! prepareIO(pointer); ! int result = file.readInt(); ! finalizeIO(); ! return result; ! } ! ! long readLong(long pointer) throws IOException { ! prepareIO(pointer); ! long result = file.readLong(); ! finalizeIO(); ! return result; ! } ! ! double readDouble(long pointer) throws IOException { ! prepareIO(pointer); ! double result = file.readDouble(); ! finalizeIO(); ! return result; ! } ! ! String readString(long pointer) throws IOException { ! prepareIO(pointer); ! char[] chars = new char[STRING_LENGTH]; ! for(int i = 0; i < STRING_LENGTH; i++) { ! chars[i] = file.readChar(); ! } ! String result = new String(chars).trim(); ! finalizeIO(); ! return result; ! } ! ! void writeInt(long pointer, int value) throws IOException { ! prepareIO(pointer); ! file.writeInt(value); ! finalizeIO(); ! } ! ! void writeLong(long pointer, long value) throws IOException { ! prepareIO(pointer); ! file.writeLong(value); ! finalizeIO(); ! } ! ! void writeDouble(long pointer, double value) throws IOException { ! prepareIO(pointer); ! file.writeDouble(value); ! finalizeIO(); ! } ! ! void writeString(long pointer, String value) throws IOException { ! prepareIO(pointer); ! writeStringInternal(value); ! finalizeIO(); ! } ! ! private void writeStringInternal(String value) throws IOException { ! for(int i = 0; i < STRING_LENGTH; i++) { ! if(i < value.length()) { ! file.writeChar(value.charAt(i)); ! } ! else { ! file.writeChar(' '); ! } ! } } ! ! private void prepareIO(long pointer) throws IOException { ! if(safeMode) { ! bookmark = file.getFilePointer(); ! } ! file.seek(pointer); } ! ! private void finalizeIO() throws IOException { ! if(safeMode) { ! file.seek(bookmark); } ! } ! boolean isSafeMode() { ! return safeMode; } ! void setSafeMode(boolean safeMode) { ! this.safeMode = safeMode; } --- 105,136 ---- close(); } ! ! RrdPrimitive getPrimitive(int index) { ! return (RrdPrimitive) primitives.get(index); } ! ! void allocate(RrdPrimitive primitive, int byteCount) throws IOException { ! long pointer = getNextPointer(); ! primitive.setPointer(pointer); ! primitive.setByteCount(byteCount); ! primitives.add(primitive); } ! ! private long getNextPointer() { ! long pointer = 0; ! int count = primitives.size(); ! if(count > 0) { ! RrdPrimitive lastPrimitive = getPrimitive(count - 1); ! pointer = lastPrimitive.getPointer() + lastPrimitive.getByteCount(); } ! return pointer; ! } ! void truncateFile() throws IOException { ! setLength(getNextPointer()); } ! boolean isEndReached() throws IOException { ! return getNextPointer() == length(); } *************** *** 239,242 **** --- 142,154 ---- return filePath; } + + /** + * Returns canonical path to RRD file on disk. + * @return RRD file path. + * @throws IOException Thrown in case of I/O error + */ + public String getCanonicalFilePath() throws IOException { + return new File(filePath).getCanonicalPath(); + } /** *************** *** 247,251 **** */ public long getFileSize() throws IOException { ! return file.length(); } --- 159,163 ---- */ public long getFileSize() throws IOException { ! return length(); } *************** *** 257,259 **** --- 169,180 ---- RrdFile.lockMode = lockMode; } + + int getMode() { + return mode; + } + + void setMode(int mode) { + this.mode = mode; + } + } Index: RrdInt.java =================================================================== RCS file: /cvsroot/jrobin/src/org/jrobin/core/RrdInt.java,v retrieving revision 1.3 retrieving revision 1.4 diff -C2 -d -r1.3 -r1.4 *** RrdInt.java 10 Nov 2003 08:52:27 -0000 1.3 --- RrdInt.java 27 Nov 2003 10:22:58 -0000 1.4 *************** *** 28,81 **** import java.io.IOException; ! class RrdInt { ! private RrdFile file; ! private long pointer; ! private int count; ! ! private boolean cached = false; ! private int cachedValue; ! ! RrdInt(RrdUpdater updater, int count) throws IOException { ! this.count = count; ! file = updater.getRrdFile(); ! pointer = file.allocate(RrdFile.INT_SIZE, count); ! } RrdInt(RrdUpdater updater) throws IOException { ! this(updater, 1); } ! ! RrdInt(int initValue, RrdUpdater updater) throws IOException { ! this.count = 1; ! file = updater.getRrdFile(); ! pointer = file.allocate(initValue); ! cached = true; ! cachedValue = initValue; } ! ! void set(int index, int value) throws IOException { ! assert index < count; ! long readPointer = pointer + index * RrdFile.INT_SIZE; ! file.writeInt(readPointer, value); } void set(int value) throws IOException { ! cached = true; ! cachedValue = value; ! set(0, value); ! } ! ! int get(int index) throws IOException { ! assert index < count; ! long readPointer = pointer + index * RrdFile.INT_SIZE; ! return file.readInt(readPointer); } ! int get() throws IOException { ! if(!cached) { ! cachedValue = get(0); ! cached = true; ! } ! return cachedValue; } } --- 28,63 ---- import java.io.IOException; ! class RrdInt extends RrdPrimitive { ! static final int SIZE = 4; ! ! private int cache; RrdInt(RrdUpdater updater) throws IOException { ! super(updater, SIZE); ! loadCache(); } ! ! void loadCache() throws IOException { ! RrdFile rrdFile = getRrdFile(); ! if(rrdFile.getMode() == RrdFile.MODE_RESTORE) { ! rrdFile.seek(getPointer()); ! cache = rrdFile.readInt(); ! } } ! ! RrdInt(int initValue, RrdUpdater updater) throws IOException { ! super(updater, SIZE); ! set(initValue); } void set(int value) throws IOException { ! cache = value; ! RrdFile rrdFile = getRrdFile(); ! rrdFile.seek(getPointer()); ! rrdFile.writeInt(cache); } ! int get() { ! return cache; } } Index: RrdLong.java =================================================================== RCS file: /cvsroot/jrobin/src/org/jrobin/core/RrdLong.java,v retrieving revision 1.3 retrieving revision 1.4 diff -C2 -d -r1.3 -r1.4 *** RrdLong.java 10 Nov 2003 08:52:27 -0000 1.3 --- RrdLong.java 27 Nov 2003 10:22:58 -0000 1.4 *************** *** 28,81 **** import java.io.IOException; ! class RrdLong { ! private RrdFile file; ! private long pointer; ! private int count; ! ! boolean cached = false; ! long cachedValue; ! ! RrdLong(RrdUpdater updater, int count) throws IOException { ! this.count = count; ! file = updater.getRrdFile(); ! pointer = file.allocate(RrdFile.LONG_SIZE, count); ! } RrdLong(RrdUpdater updater) throws IOException { ! this(updater, 1); } ! ! RrdLong(long initValue, RrdUpdater updater) throws IOException { ! this.count = 1; ! file = updater.getRrdFile(); ! pointer = file.allocate(initValue); ! cached = true; ! cachedValue = initValue; } ! void set(int index, long value) throws IOException { ! assert index < count; ! long readPointer = pointer + index * RrdFile.LONG_SIZE; ! file.writeLong(readPointer, value); } void set(long value) throws IOException { ! cached = true; ! cachedValue = value; ! set(0, value); ! } ! ! long get(int index) throws IOException { ! assert index < count; ! long readPointer = pointer + index * RrdFile.LONG_SIZE; ! return file.readLong(readPointer); } ! long get() throws IOException { ! if(!cached) { ! cachedValue = get(0); ! cached = true; ! } ! return cachedValue; } } --- 28,63 ---- import java.io.IOException; ! class RrdLong extends RrdPrimitive { ! static final int SIZE = 8; ! ! private long cache; RrdLong(RrdUpdater updater) throws IOException { ! super(updater, SIZE); ! loadCache(); } ! ! void loadCache() throws IOException { ! RrdFile rrdFile = getRrdFile(); ! if(rrdFile.getMode() == RrdFile.MODE_RESTORE) { ! rrdFile.seek(getPointer()); ! cache = rrdFile.readLong(); ! } } ! RrdLong(long initValue, RrdUpdater updater) throws IOException { ! super(updater, SIZE); ! set(initValue); } void set(long value) throws IOException { ! cache = value; ! RrdFile rrdFile = getRrdFile(); ! rrdFile.seek(getPointer()); ! rrdFile.writeLong(cache); } ! long get() { ! return cache; } } Index: RrdString.java =================================================================== RCS file: /cvsroot/jrobin/src/org/jrobin/core/RrdString.java,v retrieving revision 1.3 retrieving revision 1.4 diff -C2 -d -r1.3 -r1.4 *** RrdString.java 10 Nov 2003 08:52:27 -0000 1.3 --- RrdString.java 27 Nov 2003 10:22:58 -0000 1.4 *************** *** 28,81 **** import java.io.IOException; ! class RrdString { ! private RrdFile file; ! private long pointer; ! private int count; ! ! private boolean cached = false; ! private String cachedValue; ! ! RrdString(RrdUpdater updater, int count) throws IOException { ! this.count = count; ! file = updater.getRrdFile(); ! pointer = file.allocate(RrdFile.STRING_SIZE, count); ! } RrdString(RrdUpdater updater) throws IOException { ! this(updater, 1); } ! ! RrdString(String initValue, RrdUpdater updater) throws IOException { ! this.count = 1; ! file = updater.getRrdFile(); ! pointer = file.allocate(initValue); ! cached = true; ! cachedValue = initValue; } ! void set(int index, String value) throws IOException { ! assert index < count; ! long readPointer = pointer + index * RrdFile.STRING_SIZE; ! file.writeString(readPointer, value); } void set(String value) throws IOException { ! cached = true; ! cachedValue = value; ! set(0, value); ! } ! ! String get(int index) throws IOException { ! assert index < count; ! long readPointer = pointer + index * RrdFile.STRING_SIZE; ! return file.readString(readPointer); } ! String get() throws IOException { ! if(!cached) { ! cachedValue = get(0); ! cached = true; ! } ! return cachedValue; } } --- 28,74 ---- import java.io.IOException; ! class RrdString extends RrdPrimitive { ! private static final int SIZE = 20; ! ! private String cache; RrdString(RrdUpdater updater) throws IOException { ! super(updater, SIZE * 2); ! loadCache(); } ! ! void loadCache() throws IOException { ! RrdFile rrdFile = getRrdFile(); ! if(rrdFile.getMode() == RrdFile.MODE_RESTORE) { ! rrdFile.seek(getPointer()); ! char[] c = new char[SIZE]; ! for(int i = 0; i < SIZE; i++) { ! c[i] = rrdFile.readChar(); ! } ! cache = new String(c).trim(); ! } } ! RrdString(String initValue, RrdUpdater updater) throws IOException { ! super(updater, SIZE * 2); ! set(initValue); } void set(String value) throws IOException { ! cache = value.trim(); ! RrdFile rrdFile = getRrdFile(); ! rrdFile.seek(getPointer()); ! for(int i = 0; i < SIZE; i++) { ! if(i < cache.length()) { ! rrdFile.writeChar(cache.charAt(i)); ! } ! else { ! rrdFile.writeChar(' '); ! } ! } } ! String get() { ! return cache; } } |