Update of /cvsroot/jrobin/src/org/jrobin/core In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv31473/org/jrobin/core Modified Files: RrdDb.java RrdDouble.java RrdDoubleArray.java RrdFile.java RrdInt.java RrdLong.java RrdPrimitive.java RrdString.java Log Message: Slightly faster openning of RRD files Index: RrdString.java =================================================================== RCS file: /cvsroot/jrobin/src/org/jrobin/core/RrdString.java,v retrieving revision 1.6 retrieving revision 1.7 diff -C2 -d -r1.6 -r1.7 *** RrdString.java 20 Apr 2004 08:20:29 -0000 1.6 --- RrdString.java 21 Apr 2004 10:56:55 -0000 1.7 *************** *** 39,45 **** void loadCache() throws IOException { - RrdFile rrdFile = getRrdFile(); if(rrdFile.getRrdMode() == RrdFile.MODE_RESTORE) { ! rrdFile.seek(getPointer()); char[] c = new char[SIZE]; for(int i = 0; i < SIZE; i++) { --- 39,44 ---- void loadCache() throws IOException { if(rrdFile.getRrdMode() == RrdFile.MODE_RESTORE) { ! restorePosition(); char[] c = new char[SIZE]; for(int i = 0; i < SIZE; i++) { *************** *** 59,64 **** value = value.trim(); if(!cached || !cache.equals(value)) { ! RrdFile rrdFile = getRrdFile(); ! rrdFile.seek(getPointer()); for(int i = 0; i < SIZE; i++) { if(i < value.length()) { --- 58,62 ---- value = value.trim(); if(!cached || !cache.equals(value)) { ! restorePosition(); for(int i = 0; i < SIZE; i++) { if(i < value.length()) { Index: RrdLong.java =================================================================== RCS file: /cvsroot/jrobin/src/org/jrobin/core/RrdLong.java,v retrieving revision 1.6 retrieving revision 1.7 diff -C2 -d -r1.6 -r1.7 *** RrdLong.java 20 Apr 2004 08:20:29 -0000 1.6 --- RrdLong.java 21 Apr 2004 10:56:55 -0000 1.7 *************** *** 39,45 **** void loadCache() throws IOException { - RrdFile rrdFile = getRrdFile(); if(rrdFile.getRrdMode() == RrdFile.MODE_RESTORE) { ! rrdFile.seek(getPointer()); cache = rrdFile.readLong(); cached = true; --- 39,44 ---- void loadCache() throws IOException { if(rrdFile.getRrdMode() == RrdFile.MODE_RESTORE) { ! restorePosition(); cache = rrdFile.readLong(); cached = true; *************** *** 54,59 **** void set(long value) throws IOException { if(!cached || cache != value) { ! RrdFile rrdFile = getRrdFile(); ! rrdFile.seek(getPointer()); rrdFile.writeLong(value); cache = value; --- 53,57 ---- void set(long value) throws IOException { if(!cached || cache != value) { ! restorePosition(); rrdFile.writeLong(value); cache = value; Index: RrdPrimitive.java =================================================================== RCS file: /cvsroot/jrobin/src/org/jrobin/core/RrdPrimitive.java,v retrieving revision 1.4 retrieving revision 1.5 diff -C2 -d -r1.4 -r1.5 *** RrdPrimitive.java 29 Mar 2004 14:38:28 -0000 1.4 --- RrdPrimitive.java 21 Apr 2004 10:56:55 -0000 1.5 *************** *** 28,42 **** abstract class RrdPrimitive { ! ! private RrdUpdater parent; private long pointer; private int byteCount; ! protected boolean cached = false; RrdPrimitive(RrdUpdater parent, int byteCount) throws IOException { ! this.parent = parent; // this will set pointer and byteCount ! parent.getRrdFile().allocate(this, byteCount); } --- 28,42 ---- abstract class RrdPrimitive { ! private long pointer; private int byteCount; ! RrdFile rrdFile; ! boolean cached = false; RrdPrimitive(RrdUpdater parent, int byteCount) throws IOException { ! rrdFile = parent.getRrdFile(); // this will set pointer and byteCount ! rrdFile.allocate(this, byteCount); } *************** *** 57,89 **** } - RrdUpdater getParent() { - return parent; - } - - RrdFile getRrdFile() { - return parent.getRrdFile(); - } - byte[] readBytes() throws IOException { byte[] b = new byte[byteCount]; ! RrdFile rrdFile = getRrdFile(); ! rrdFile.seek(pointer); int bytesRead = rrdFile.read(b); ! if(bytesRead != byteCount) { ! throw new IOException("Could not read enough bytes (" + byteCount + ! " bytes requested, " + bytesRead + " bytes obtained"); ! } return b; } void writeBytes(byte[] b) throws IOException { ! if(b.length != byteCount) { ! throw new IOException("Invalid number of bytes supplied (" + b.length + ! "), exactly " + byteCount + " needed"); ! } ! RrdFile rrdFile = getRrdFile(); ! rrdFile.seek(pointer); rrdFile.write(b); } } --- 57,82 ---- } byte[] readBytes() throws IOException { byte[] b = new byte[byteCount]; ! restorePosition(); int bytesRead = rrdFile.read(b); ! assert bytesRead == byteCount: "Could not read enough bytes (" + byteCount + ! " bytes requested, " + bytesRead + " bytes obtained"; return b; } void writeBytes(byte[] b) throws IOException { ! assert b.length == byteCount: "Invalid number of bytes supplied (" + b.length + ! "), exactly " + byteCount + " needed"; ! restorePosition(); rrdFile.write(b); } + final void restorePosition() throws IOException { + rrdFile.seek(pointer); + } + + final void restorePosition(int unitIndex, int unitSize) throws IOException { + rrdFile.seek(pointer + unitIndex * unitSize); + } } Index: RrdInt.java =================================================================== RCS file: /cvsroot/jrobin/src/org/jrobin/core/RrdInt.java,v retrieving revision 1.6 retrieving revision 1.7 diff -C2 -d -r1.6 -r1.7 *** RrdInt.java 20 Apr 2004 08:20:29 -0000 1.6 --- RrdInt.java 21 Apr 2004 10:56:54 -0000 1.7 *************** *** 39,45 **** void loadCache() throws IOException { - RrdFile rrdFile = getRrdFile(); if(rrdFile.getRrdMode() == RrdFile.MODE_RESTORE) { ! rrdFile.seek(getPointer()); cache = rrdFile.readInt(); cached = true; --- 39,44 ---- void loadCache() throws IOException { if(rrdFile.getRrdMode() == RrdFile.MODE_RESTORE) { ! restorePosition(); cache = rrdFile.readInt(); cached = true; *************** *** 54,59 **** void set(int value) throws IOException { if(!cached || cache != value) { ! RrdFile rrdFile = getRrdFile(); ! rrdFile.seek(getPointer()); rrdFile.writeInt(value); cache = value; --- 53,57 ---- void set(int value) throws IOException { if(!cached || cache != value) { ! restorePosition(); rrdFile.writeInt(value); cache = value; Index: RrdDoubleArray.java =================================================================== RCS file: /cvsroot/jrobin/src/org/jrobin/core/RrdDoubleArray.java,v retrieving revision 1.4 retrieving revision 1.5 diff -C2 -d -r1.4 -r1.5 *** RrdDoubleArray.java 26 Mar 2004 08:47:05 -0000 1.4 --- RrdDoubleArray.java 21 Apr 2004 10:56:54 -0000 1.5 *************** *** 50,55 **** assert index + count <= length: "Invalid robin index supplied: index=" + index + ", count=" + count + ", length=" + length; ! RrdFile rrdFile = getRrdFile(); ! rrdFile.seek(getPointer() + index * RrdDouble.SIZE); rrdFile.writeDouble(value, count); } --- 50,54 ---- assert index + count <= length: "Invalid robin index supplied: index=" + index + ", count=" + count + ", length=" + length; ! restorePosition(index, RrdDouble.SIZE); rrdFile.writeDouble(value, count); } *************** *** 57,62 **** double get(int index) throws IOException { assert index < length: "Invalid index supplied: " + index + ", length=" + length; ! RrdFile rrdFile = getRrdFile(); ! rrdFile.seek(getPointer() + index * RrdDouble.SIZE); return rrdFile.readDouble(); } --- 56,60 ---- double get(int index) throws IOException { assert index < length: "Invalid index supplied: " + index + ", length=" + length; ! restorePosition(index, RrdDouble.SIZE); return rrdFile.readDouble(); } *************** *** 65,70 **** assert index + count <= length: "Invalid index/count supplied: " + index + "/" + count + " (length=" + length + ")"; ! RrdFile rrdFile = getRrdFile(); ! rrdFile.seek(getPointer() + index * RrdDouble.SIZE); return rrdFile.readDouble(count); } --- 63,67 ---- assert index + count <= length: "Invalid index/count supplied: " + index + "/" + count + " (length=" + length + ")"; ! restorePosition(index, RrdDouble.SIZE); return rrdFile.readDouble(count); } Index: RrdFile.java =================================================================== RCS file: /cvsroot/jrobin/src/org/jrobin/core/RrdFile.java,v retrieving revision 1.7 retrieving revision 1.8 diff -C2 -d -r1.7 -r1.8 *** RrdFile.java 20 Apr 2004 08:20:29 -0000 1.7 --- RrdFile.java 21 Apr 2004 10:56:54 -0000 1.8 *************** *** 31,36 **** import java.nio.channels.FileChannel; import java.nio.channels.FileLock; - import java.util.LinkedList; - import java.util.List; /** --- 31,34 ---- *************** *** 56,61 **** private String filePath; private FileLock fileLock; ! private List primitives = new LinkedList(); private int rrdMode; RrdFile(String filePath, int rrdMode, boolean readOnly) throws IOException { --- 54,60 ---- private String filePath; private FileLock fileLock; ! private int rrdMode; + private long nextPointer = 0L; RrdFile(String filePath, int rrdMode, boolean readOnly) throws IOException { *************** *** 110,140 **** } - 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(); } --- 109,124 ---- } void allocate(RrdPrimitive primitive, int byteCount) throws IOException { ! primitive.setPointer(nextPointer); primitive.setByteCount(byteCount); ! nextPointer += byteCount; } void truncateFile() throws IOException { ! setLength(nextPointer); } boolean isEndReached() throws IOException { ! return nextPointer == length(); } Index: RrdDouble.java =================================================================== RCS file: /cvsroot/jrobin/src/org/jrobin/core/RrdDouble.java,v retrieving revision 1.6 retrieving revision 1.7 diff -C2 -d -r1.6 -r1.7 *** RrdDouble.java 20 Apr 2004 08:20:29 -0000 1.6 --- RrdDouble.java 21 Apr 2004 10:56:44 -0000 1.7 *************** *** 39,45 **** void loadCache() throws IOException { - RrdFile rrdFile = getRrdFile(); if(rrdFile.getRrdMode() == RrdFile.MODE_RESTORE) { ! rrdFile.seek(getPointer()); cache = rrdFile.readDouble(); cached = true; --- 39,44 ---- void loadCache() throws IOException { if(rrdFile.getRrdMode() == RrdFile.MODE_RESTORE) { ! restorePosition(); cache = rrdFile.readDouble(); cached = true; *************** *** 54,59 **** void set(double value) throws IOException { if(!cached || cache != value) { ! RrdFile rrdFile = getRrdFile(); ! rrdFile.seek(getPointer()); rrdFile.writeDouble(value); cache = value; --- 53,57 ---- void set(double value) throws IOException { if(!cached || cache != value) { ! restorePosition(); rrdFile.writeDouble(value); cache = value; Index: RrdDb.java =================================================================== RCS file: /cvsroot/jrobin/src/org/jrobin/core/RrdDb.java,v retrieving revision 1.14 retrieving revision 1.15 diff -C2 -d -r1.14 -r1.15 *** RrdDb.java 20 Apr 2004 08:20:29 -0000 1.14 --- RrdDb.java 21 Apr 2004 10:56:37 -0000 1.15 *************** *** 75,78 **** --- 75,80 ---- private Archive[] archives; + private boolean closed = false; + /** * <p>Constructor used to create new RRD file from the definition. New RRD file structure is specified by object of class *************** *** 251,257 **** */ public synchronized void close() throws IOException { ! if(file != null) { file.close(); ! file = null; } } --- 253,259 ---- */ public synchronized void close() throws IOException { ! if(!closed) { file.close(); ! closed = true; } } *************** *** 262,266 **** */ public boolean isClosed() { ! return file == null; } --- 264,268 ---- */ public boolean isClosed() { ! return closed; } |