From: Sasa M. <sa...@us...> - 2004-07-16 14:16:48
|
Update of /cvsroot/jrobin/src/org/jrobin/core In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv20180/org/jrobin/core Modified Files: RrdNioBackend.java Log Message: Workaround for strange NIO exceptions (finally). Index: RrdNioBackend.java =================================================================== RCS file: /cvsroot/jrobin/src/org/jrobin/core/RrdNioBackend.java,v retrieving revision 1.14 retrieving revision 1.15 diff -C2 -d -r1.14 -r1.15 *** RrdNioBackend.java 16 Jul 2004 08:01:14 -0000 1.14 --- RrdNioBackend.java 16 Jul 2004 14:16:38 -0000 1.15 *************** *** 33,49 **** /** ! * JRobin backend which is used to store RRD data to ordinary files on the disk ! * by using java.nio.* package. This is the default backend engine since JRobin 1.4.0. */ public class RrdNioBackend extends RrdFileBackend { /** ! * Defines if the <code>System.gc()</code> method should be executed when necessary. ! * NIO backend uses large in-memory buffer to cache file data. The buffer remains 'active' ! * (by prohibiting file re-creation, for example) as long as it is not garbage collected. ! * By forcing <code>gc()</code> where appropriate, memory gets freed sooner and file ! * re-creation won't fail.<p> * ! * The constant is set to true initially and currently there is no API to change it ! * during runtime.<p> */ public static final boolean SHOULD_GC = true; --- 33,53 ---- /** ! * JRobin backend which is used to store RRD data to ordinary disk files ! * by using fast java.nio.* package. This is the default backend engine since JRobin 1.4.0. */ public class RrdNioBackend extends RrdFileBackend { /** ! * Defines <code>System.gc()</code> usage policy for this backend.<p> * ! * NIO backend uses potentially large in-memory buffer to cache file data. ! * The buffer remains 'active' (by prohibiting file re-creation with the smaller file size) ! * as long as it is not garbage-collected. By forcing <code>System.gc()</code> call where ! * appropriate, this backend will free in-memory buffers sooner and file re-creation won't fail.<p> ! * ! * The constant is set to <b><code>true</code></b> initially and currently there is no ! * API to change it during runtime. ! * ! * Garbage collection will be forced only in some special circumstances. ! * It should not affect the speed of your application significantly.<p> */ public static final boolean SHOULD_GC = true; *************** *** 51,57 **** static { if(SHOULD_GC) { ! Runtime.getRuntime().addShutdownHook(new Thread() { public void run() { ! System.gc(); } }); --- 55,63 ---- static { if(SHOULD_GC) { ! final Runtime runtime = Runtime.getRuntime(); ! runtime.addShutdownHook(new Thread() { public void run() { ! runtime.runFinalization(); ! runtime.gc(); } }); *************** *** 68,72 **** throws IOException { super(path, readOnly, lockMode); ! map(false, readOnly); this.syncMode = syncMode; if(syncMode == RrdNioBackendFactory.SYNC_BACKGROUND && !readOnly) { --- 74,78 ---- throws IOException { super(path, readOnly, lockMode); ! map(readOnly); this.syncMode = syncMode; if(syncMode == RrdNioBackendFactory.SYNC_BACKGROUND && !readOnly) { *************** *** 75,90 **** } ! private void map(boolean isShorter, boolean readOnly) throws IOException { ! if(isShorter) { ! byteBuffer = null; ! if(SHOULD_GC) { ! System.gc(); ! } ! } ! long newLength = getLength(); ! if(newLength > 0) { FileChannel.MapMode mapMode = readOnly? FileChannel.MapMode.READ_ONLY: FileChannel.MapMode.READ_WRITE; ! byteBuffer = channel.map(mapMode, 0, newLength); } } --- 81,93 ---- } ! private void map(boolean readOnly) throws IOException { ! long length = getLength(); ! if(length > 0) { FileChannel.MapMode mapMode = readOnly? FileChannel.MapMode.READ_ONLY: FileChannel.MapMode.READ_WRITE; ! byteBuffer = channel.map(mapMode, 0, length); ! } ! else { ! byteBuffer = null; } } *************** *** 106,112 **** */ protected void setLength(long newLength) throws IOException { ! boolean isShorter = newLength < getLength(); super.setLength(newLength); ! map(isShorter, false); } --- 109,121 ---- */ protected void setLength(long newLength) throws IOException { ! if(newLength < getLength()) { ! // the file will be truncated ! if(SHOULD_GC) { ! byteBuffer = null; ! System.gc(); ! } ! } super.setLength(newLength); ! map(false); } |