Update of /cvsroot/jrobin/src/org/jrobin/core In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv11328/org/jrobin/core Modified Files: RrdBackend.java RrdDouble.java RrdInt.java RrdLong.java RrdMemoryBackend.java RrdPrimitive.java RrdString.java Util.java Log Message: Removed RrdCacher class (consumes too much memory) - caching mechanism is now implemented in subclasses of the RrdPrimitive class (RrdInt, RrdLong, RrdDouble, RrdString) Index: RrdString.java =================================================================== RCS file: /cvsroot/jrobin/src/org/jrobin/core/RrdString.java,v retrieving revision 1.9 retrieving revision 1.10 diff -C2 -d -r1.9 -r1.10 *** RrdString.java 29 Jun 2004 12:59:35 -0000 1.9 --- RrdString.java 8 Nov 2004 11:18:49 -0000 1.10 *************** *** 29,32 **** --- 29,34 ---- class RrdString extends RrdPrimitive { + private String cache; + RrdString(RrdUpdater updater) throws IOException { super(updater, RrdPrimitive.RRD_STRING); *************** *** 34,47 **** void set(String value) throws IOException { ! if(cache.setString(value)) { writeString(value); } } String get() throws IOException { ! if(cache.isEmpty()) { ! cache.setString(readString()); ! } ! return cache.getString(); } } --- 36,51 ---- void set(String value) throws IOException { ! if(!isCachingAllowed()) { writeString(value); } + // caching allowed + else if(cache == null || !cache.equals(value)) { + // update cache + writeString(cache = value); + } } String get() throws IOException { ! return (cache != null)? cache: readString(); } } Index: RrdMemoryBackend.java =================================================================== RCS file: /cvsroot/jrobin/src/org/jrobin/core/RrdMemoryBackend.java,v retrieving revision 1.4 retrieving revision 1.5 diff -C2 -d -r1.4 -r1.5 *** RrdMemoryBackend.java 29 Jun 2004 12:28:09 -0000 1.4 --- RrdMemoryBackend.java 8 Nov 2004 11:18:49 -0000 1.5 *************** *** 29,33 **** /** ! * Backend which is used to store all RRD bytes in memory.<p> */ public class RrdMemoryBackend extends RrdBackend { --- 29,33 ---- /** ! * Backend to be used to store all RRD bytes in memory.<p> */ public class RrdMemoryBackend extends RrdBackend { *************** *** 84,86 **** --- 84,95 ---- // NOP } + + /** + * This method is overriden to disable high-level caching in frontend JRobin classes. + * @return Always returns <code>false</code>. There is no need to cache anything in high-level classes + * since all RRD bytes are already in memory. + */ + protected boolean isCachingAllowed() { + return false; + } } Index: Util.java =================================================================== RCS file: /cvsroot/jrobin/src/org/jrobin/core/Util.java,v retrieving revision 1.25 retrieving revision 1.26 diff -C2 -d -r1.25 -r1.26 *** Util.java 27 Oct 2004 20:36:25 -0000 1.25 --- Util.java 8 Nov 2004 11:18:49 -0000 1.26 *************** *** 545,560 **** /** ! * Compares two doubles, but returns true if x = y = Double.NaN ! * @param x First double ! * @param y Second double ! * @return true, if doubles are equal, false otherwise. */ public static boolean equal(double x, double y) { ! if(Double.isNaN(x) && Double.isNaN(y)) { ! return true; ! } ! else { ! return x == y; ! } } --- 545,555 ---- /** ! * Compares two doubles, but returns true if x=y=Double.NaN (by default Double.NaN != Double.NaN) ! * @param x first value ! * @param y second value ! * @return <code>true</code> if x and y are both equal to Double.NaN, or if x == y. <code>false</code> otherwise */ public static boolean equal(double x, double y) { ! return (Double.isNaN(x) && Double.isNaN(y)) || (x == y); } Index: RrdLong.java =================================================================== RCS file: /cvsroot/jrobin/src/org/jrobin/core/RrdLong.java,v retrieving revision 1.9 retrieving revision 1.10 diff -C2 -d -r1.9 -r1.10 *** RrdLong.java 29 Jun 2004 12:59:35 -0000 1.9 --- RrdLong.java 8 Nov 2004 11:18:48 -0000 1.10 *************** *** 29,32 **** --- 29,35 ---- class RrdLong extends RrdPrimitive { + private long cache; + private boolean cached = false; + RrdLong(RrdUpdater updater) throws IOException { super(updater, RrdPrimitive.RRD_LONG); *************** *** 34,47 **** void set(long value) throws IOException { ! if(cache.setLong(value)) { writeLong(value); } } long get() throws IOException { ! if(cache.isEmpty()) { ! cache.setLong(readLong()); ! } ! return cache.getLong(); } } --- 37,53 ---- void set(long value) throws IOException { ! if(!isCachingAllowed()) { writeLong(value); } + // caching allowed + else if(!cached || cache != value) { + // update cache + writeLong(cache = value); + cached = true; + } } long get() throws IOException { ! return cached? cache: readLong(); } } Index: RrdPrimitive.java =================================================================== RCS file: /cvsroot/jrobin/src/org/jrobin/core/RrdPrimitive.java,v retrieving revision 1.8 retrieving revision 1.9 diff -C2 -d -r1.8 -r1.9 *** RrdPrimitive.java 29 Jun 2004 12:59:35 -0000 1.8 --- RrdPrimitive.java 8 Nov 2004 11:18:49 -0000 1.9 *************** *** 37,42 **** private final long pointer; - protected RrdCacher cache = new RrdCacher(); - RrdPrimitive(RrdUpdater updater, int type) throws IOException { this(updater, type, 1); --- 37,40 ---- *************** *** 49,53 **** } ! byte[] readBytes() throws IOException { byte[] b = new byte[byteCount]; backend.read(pointer, b); --- 47,51 ---- } ! final byte[] readBytes() throws IOException { byte[] b = new byte[byteCount]; backend.read(pointer, b); *************** *** 55,117 **** } ! void writeBytes(byte[] b) throws IOException { assert b.length == byteCount: "Invalid number of bytes supplied to RrdPrimitive.write method"; backend.write(pointer, b); } ! int readInt() throws IOException { return backend.readInt(pointer); } ! void writeInt(int value) throws IOException { backend.writeInt(pointer, value); } ! long readLong() throws IOException { return backend.readLong(pointer); } ! void writeLong(long value) throws IOException { backend.writeLong(pointer, value); } ! double readDouble() throws IOException { return backend.readDouble(pointer); } ! double readDouble(int index) throws IOException { long offset = pointer + index * RRD_PRIM_SIZES[RRD_DOUBLE]; return backend.readDouble(offset); } ! double[] readDouble(int index, int count) throws IOException { long offset = pointer + index * RRD_PRIM_SIZES[RRD_DOUBLE]; return backend.readDouble(offset, count); } ! void writeDouble(double value) throws IOException { backend.writeDouble(pointer, value); } ! void writeDouble(int index, double value, int count) throws IOException { long offset = pointer + index * RRD_PRIM_SIZES[RRD_DOUBLE]; backend.writeDouble(offset, value, count); } ! void writeDouble(int index, double[] values) throws IOException { long offset = pointer + index * RRD_PRIM_SIZES[RRD_DOUBLE]; backend.writeDouble(offset, values); } ! String readString() throws IOException { return backend.readString(pointer); } ! void writeString(String value) throws IOException { backend.writeString(pointer, value); } ! void clearCache() { ! cache.clearCache(); } } --- 53,115 ---- } ! final void writeBytes(byte[] b) throws IOException { assert b.length == byteCount: "Invalid number of bytes supplied to RrdPrimitive.write method"; backend.write(pointer, b); } ! final int readInt() throws IOException { return backend.readInt(pointer); } ! final void writeInt(int value) throws IOException { backend.writeInt(pointer, value); } ! final long readLong() throws IOException { return backend.readLong(pointer); } ! final void writeLong(long value) throws IOException { backend.writeLong(pointer, value); } ! final double readDouble() throws IOException { return backend.readDouble(pointer); } ! final double readDouble(int index) throws IOException { long offset = pointer + index * RRD_PRIM_SIZES[RRD_DOUBLE]; return backend.readDouble(offset); } ! final double[] readDouble(int index, int count) throws IOException { long offset = pointer + index * RRD_PRIM_SIZES[RRD_DOUBLE]; return backend.readDouble(offset, count); } ! final void writeDouble(double value) throws IOException { backend.writeDouble(pointer, value); } ! final void writeDouble(int index, double value, int count) throws IOException { long offset = pointer + index * RRD_PRIM_SIZES[RRD_DOUBLE]; backend.writeDouble(offset, value, count); } ! final void writeDouble(int index, double[] values) throws IOException { long offset = pointer + index * RRD_PRIM_SIZES[RRD_DOUBLE]; backend.writeDouble(offset, values); } ! final String readString() throws IOException { return backend.readString(pointer); } ! final void writeString(String value) throws IOException { backend.writeString(pointer, value); } ! final boolean isCachingAllowed() { ! return backend.isCachingAllowed(); } } Index: RrdBackend.java =================================================================== RCS file: /cvsroot/jrobin/src/org/jrobin/core/RrdBackend.java,v retrieving revision 1.9 retrieving revision 1.10 diff -C2 -d -r1.9 -r1.10 *** RrdBackend.java 19 Oct 2004 10:14:02 -0000 1.9 --- RrdBackend.java 8 Nov 2004 11:18:48 -0000 1.10 *************** *** 192,199 **** * calls {@link #sync()} internally. */ ! public void beforeClose() throws IOException { sync(); } final void writeInt(long offset, int value) throws IOException { write(offset, getIntBytes(value)); --- 192,211 ---- * calls {@link #sync()} internally. */ ! protected void beforeClose() throws IOException { sync(); } + /** + * This method suggests the caching policy to the JRobin frontend (high-level) classes. If <code>true</code> + * is returned, frontent classes will cache frequently used parts of a RRD file in memory to improve + * performance. If </code>false</code> is returned, high level classes will never cache RRD file sections + * in memory. + * @return <code>true</code> if file caching is enabled, <code>false</code> otherwise. By default, the + * method returns <code>true</code> but it can be overriden in subclasses. + */ + protected boolean isCachingAllowed() { + return true; + } + final void writeInt(long offset, int value) throws IOException { write(offset, getIntBytes(value)); Index: RrdInt.java =================================================================== RCS file: /cvsroot/jrobin/src/org/jrobin/core/RrdInt.java,v retrieving revision 1.9 retrieving revision 1.10 diff -C2 -d -r1.9 -r1.10 *** RrdInt.java 29 Jun 2004 12:59:35 -0000 1.9 --- RrdInt.java 8 Nov 2004 11:18:48 -0000 1.10 *************** *** 29,32 **** --- 29,35 ---- class RrdInt extends RrdPrimitive { + private int cache; + private boolean cached = false; + RrdInt(RrdUpdater updater) throws IOException { super(updater, RrdPrimitive.RRD_INT); *************** *** 34,47 **** void set(int value) throws IOException { ! if(cache.setInt(value)) { writeInt(value); } } int get() throws IOException { ! if(cache.isEmpty()) { ! cache.setInt(readInt()); ! } ! return cache.getInt(); } } --- 37,53 ---- void set(int value) throws IOException { ! if(!isCachingAllowed()) { writeInt(value); } + // caching allowed + else if(!cached || cache != value) { + // update cache + writeInt(cache = value); + cached = true; + } } int get() throws IOException { ! return cached? cache: readInt(); } } Index: RrdDouble.java =================================================================== RCS file: /cvsroot/jrobin/src/org/jrobin/core/RrdDouble.java,v retrieving revision 1.9 retrieving revision 1.10 diff -C2 -d -r1.9 -r1.10 *** RrdDouble.java 29 Jun 2004 12:59:35 -0000 1.9 --- RrdDouble.java 8 Nov 2004 11:18:48 -0000 1.10 *************** *** 29,32 **** --- 29,35 ---- class RrdDouble extends RrdPrimitive { + private double cache; + private boolean cached = false; + RrdDouble(RrdUpdater updater) throws IOException { super(updater, RrdDouble.RRD_DOUBLE); *************** *** 34,47 **** void set(double value) throws IOException { ! if(cache.setDouble(value)) { writeDouble(value); } } double get() throws IOException { ! if(cache.isEmpty()) { ! cache.setDouble(readDouble()); ! } ! return cache.getDouble(); } } --- 37,53 ---- void set(double value) throws IOException { ! if(!isCachingAllowed()) { writeDouble(value); } + // caching allowed + else if(!cached || !Util.equal(cache, value)) { + // update cache + writeDouble(cache = value); + cached = true; + } } double get() throws IOException { ! return cached? cache: readDouble(); } } |