|
From: <sa...@us...> - 2004-03-01 12:21:40
|
Update of /cvsroot/jrobin/src/org/jrobin/core In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv20110/org/jrobin/core Modified Files: RrdDbPool.java Log Message: Improved RrdDbPool, removed bug in Grapher (RRD files were never closed or returned to the pool) Index: RrdDbPool.java =================================================================== RCS file: /cvsroot/jrobin/src/org/jrobin/core/RrdDbPool.java,v retrieving revision 1.3 retrieving revision 1.4 diff -C2 -d -r1.3 -r1.4 *** RrdDbPool.java 12 Nov 2003 10:22:09 -0000 1.3 --- RrdDbPool.java 1 Mar 2004 12:02:46 -0000 1.4 *************** *** 90,95 **** * threads and many simultaneously open RRD files.<p> * ! * The pool is thread-safe. * */ public class RrdDbPool { --- 90,99 ---- * threads and many simultaneously open RRD files.<p> * ! * RrdDbPool can be forced to block (force wait on) all threads requesting access ! * to a single RRD file which is already in use (not yet returned to the pool). ! * If you want such feature, call ! * {@link #setExclusiveMode setExclusiveMode()} method with a <code>true</code> argument.<p> * + * The pool is thread-safe.<p> */ public class RrdDbPool { *************** *** 103,106 **** --- 107,112 ---- private static int capacity = INITIAL_CAPACITY; + private boolean exclusiveMode = false; + private HashMap rrdMap = new HashMap(); *************** *** 131,146 **** public synchronized RrdDb requestRrdDb(String path) throws IOException, RrdException { String keypath = getCanonicalPath(path); ! if(rrdMap.containsKey(keypath)) { ! // already open ! RrdEntry rrdEntry = (RrdEntry) rrdMap.get(keypath); ! rrdEntry.reportUsage(); ! debug("EXISTING: " + rrdEntry.dump()); ! return rrdEntry.getRrdDb(); ! } ! else { ! // not found, open it ! RrdDb rrdDb = new RrdDb(path); ! put(keypath, rrdDb); ! return rrdDb; } } --- 137,162 ---- public synchronized RrdDb requestRrdDb(String path) throws IOException, RrdException { String keypath = getCanonicalPath(path); ! for (;;) { // ugly, but it works ! if (rrdMap.containsKey(keypath)) { ! // already open ! RrdEntry rrdEntry = (RrdEntry) rrdMap.get(keypath); ! if(exclusiveMode && rrdEntry.getUsageCount() > 0) { ! // reference already in use, wait until the reference ! // is returned to the pool ! try { ! wait(); ! } catch (InterruptedException e) { } ! // try to obtain the same reference once again ! continue; ! } ! rrdEntry.reportUsage(); ! debug("EXISTING: " + rrdEntry.dump()); ! return rrdEntry.getRrdDb(); ! } else { ! // not found, open it ! RrdDb rrdDb = new RrdDb(path); ! put(keypath, rrdDb); ! return rrdDb; ! } } } *************** *** 230,237 **** RrdEntry rrdEntry = (RrdEntry) rrdMap.get(keypath); rrdEntry.release(); debug("RELEASED: " + rrdEntry.dump()); } else { ! throw new RrdException("RrdDb with path " + keypath + " not in pool"); } gc(); --- 246,256 ---- RrdEntry rrdEntry = (RrdEntry) rrdMap.get(keypath); rrdEntry.release(); + if(exclusiveMode) { + notifyAll(); + } debug("RELEASED: " + rrdEntry.dump()); } else { ! throw new RrdException("RrdDb with path " + keypath + " not in the pool"); } gc(); *************** *** 342,345 **** --- 361,385 ---- } + /** + * Checks if RrdDbPool will return a reference to an already open RRD file if it is still + * in use (not released, not returned to the pool). If true, simultaneous access + * to the same RRD file will not be possible. + * @return Current working mode of RrdDbPool (initial working mode is <code>false</code>) + */ + public boolean isExclusiveMode() { + return exclusiveMode; + } + + /** + * Method used to set the current working mode of RrdDbPool. If set to true, simultaneous + * access to the same RRD file will not be possible. In case of several threads requesting + * a reference to the same RRD file, one thread will get the reference, other threads will + * block until the reference is returned to the pool. + * @param exclusiveMode Working mode of RrdDbPool + */ + public void setExclusiveMode(boolean exclusiveMode) { + this.exclusiveMode = exclusiveMode; + } + private class RrdEntry { private RrdDb rrdDb; |