From: Sasa M. <sa...@us...> - 2004-10-18 13:09:48
|
Update of /cvsroot/jrobin/src/org/jrobin/core In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv5788/org/jrobin/core Modified Files: RrdBackend.java RrdFileBackend.java RrdNioBackend.java Log Message: Minor bug removed Index: RrdNioBackend.java =================================================================== RCS file: /cvsroot/jrobin/src/org/jrobin/core/RrdNioBackend.java,v retrieving revision 1.17 retrieving revision 1.18 diff -C2 -d -r1.17 -r1.18 *** RrdNioBackend.java 28 Sep 2004 09:01:31 -0000 1.17 --- RrdNioBackend.java 18 Oct 2004 13:09:33 -0000 1.18 *************** *** 141,147 **** syncTask.cancel(); } ! super.close(); // calls sync() eventually // release the buffer, make it eligible for GC as soon as possible byteBuffer = null; } --- 141,150 ---- syncTask.cancel(); } ! // synchronize with the disk for the last time ! sync(); // release the buffer, make it eligible for GC as soon as possible byteBuffer = null; + // close the underlying file + super.close(); } Index: RrdFileBackend.java =================================================================== RCS file: /cvsroot/jrobin/src/org/jrobin/core/RrdFileBackend.java,v retrieving revision 1.9 retrieving revision 1.10 diff -C2 -d -r1.9 -r1.10 *** RrdFileBackend.java 27 Sep 2004 14:11:38 -0000 1.9 --- RrdFileBackend.java 18 Oct 2004 13:09:33 -0000 1.10 *************** *** 36,40 **** * JRobin backend which is used to store RRD data to ordinary files on the disk. This was the * default factory before 1.4.0 version<p> ! * * This backend is based on the RandomAccessFile class (java.io.* package). */ --- 36,40 ---- * JRobin backend which is used to store RRD data to ordinary files on the disk. This was the * default factory before 1.4.0 version<p> ! * <p/> * This backend is based on the RandomAccessFile class (java.io.* package). */ *************** *** 44,47 **** --- 44,51 ---- private static HashSet openFiles = new HashSet(); + private boolean readOnly; + private int lockMode; + private boolean closed = false; + protected RandomAccessFile file; protected FileChannel channel; *************** *** 50,93 **** protected RrdFileBackend(String path, boolean readOnly, int lockMode) throws IOException { super(path); ! file = new RandomAccessFile(path, readOnly? "r": "rw"); channel = file.getChannel(); ! if(!readOnly) { ! // We'll try to lock the file only in "rw" mode ! lockFile(lockMode); ! registerWriter(path); ! } ! } ! ! private static synchronized void registerWriter(String path) throws IOException { ! String canonicalPath = getCanonicalPath(path); ! if(openFiles.contains(canonicalPath)) { ! throw new IOException("File \"" + path + "\" already open for R/W access. " + ! "You cannot open the same file for R/W access twice"); ! } ! else { ! openFiles.add(canonicalPath); ! } } ! private void lockFile(int lockMode) throws IOException { ! if(lockMode == RrdDb.WAIT_IF_LOCKED || lockMode == RrdDb.EXCEPTION_IF_LOCKED) { ! do { fileLock = channel.tryLock(); ! if(fileLock == null) { // could not obtain lock ! if(lockMode == RrdDb.WAIT_IF_LOCKED) { ! // wait a little, than try again try { Thread.sleep(LOCK_DELAY); ! } catch (InterruptedException e) { // NOP } } - else { - throw new IOException("Access denied. " + - "File [" + getPath() + "] already locked"); - } } ! } while(fileLock == null); } } --- 54,106 ---- protected RrdFileBackend(String path, boolean readOnly, int lockMode) throws IOException { super(path); ! this.readOnly = readOnly; ! this.lockMode = lockMode; ! file = new RandomAccessFile(path, readOnly ? "r" : "rw"); channel = file.getChannel(); ! lockFile(); ! registerWriter(); } ! private void lockFile() throws IOException { ! switch (lockMode) { ! case RrdDb.EXCEPTION_IF_LOCKED: fileLock = channel.tryLock(); ! if (fileLock == null) { // could not obtain lock ! throw new IOException("Access denied. " + "File [" + getPath() + "] already locked"); ! } ! break; ! case RrdDb.WAIT_IF_LOCKED: ! while (fileLock == null) { ! fileLock = channel.tryLock(); ! if (fileLock == null) { ! // could not obtain lock, wait a little, than try again try { Thread.sleep(LOCK_DELAY); ! } ! catch (InterruptedException e) { // NOP } } } ! break; ! case RrdDb.NO_LOCKS: ! break; ! } ! } ! ! private void registerWriter() throws IOException { ! if (!readOnly) { ! String path = getPath(); ! String canonicalPath = getCanonicalPath(path); ! synchronized (openFiles) { ! if (openFiles.contains(canonicalPath)) { ! throw new IOException("File \"" + path + "\" already open for R/W access. " + ! "You cannot open the same file for R/W access twice"); ! } ! else { ! openFiles.add(canonicalPath); ! } ! } } } *************** *** 95,117 **** /** * Closes the underlying RRD file. * @throws IOException Thrown in case of I/O error */ public void close() throws IOException { ! super.close(); // calls sync() ! unregisterWriter(getPath()); ! unlockFile(); ! channel.close(); ! file.close(); ! } ! ! private static synchronized void unregisterWriter(String path) throws IOException { ! String canonicalPath = getCanonicalPath(path); ! openFiles.remove(canonicalPath); } private void unlockFile() throws IOException { ! if(fileLock != null) { fileLock.release(); ! fileLock = null; } } --- 108,141 ---- /** * Closes the underlying RRD file. + * * @throws IOException Thrown in case of I/O error */ public void close() throws IOException { ! if (!closed) { ! unregisterWriter(); ! unlockFile(); ! channel.close(); ! file.close(); ! closed = true; ! } } private void unlockFile() throws IOException { ! if (fileLock != null) { fileLock.release(); ! } ! } ! ! private void unregisterWriter() throws IOException { ! if (!readOnly) { ! String path = getPath(); ! String canonicalPath = getCanonicalPath(path); ! synchronized (openFiles) { ! boolean removed = openFiles.remove(canonicalPath); ! if (!removed) { ! throw new IOException("File [" + file + "] could not be removed from the list of files " + ! "open for R/W access"); ! } ! } } } *************** *** 119,122 **** --- 143,147 ---- /** * Closes the underlying RRD file if not already closed + * * @throws IOException Thrown in case of I/O error */ *************** *** 127,130 **** --- 152,156 ---- /** * Returns canonical path to the file on the disk. + * * @param path File path * @return Canonical file path *************** *** 137,140 **** --- 163,167 ---- /** * Returns canonical path to the file on the disk. + * * @return Canonical file path * @throws IOException Thrown in case of I/O error *************** *** 146,151 **** /** * Writes bytes to the underlying RRD file on the disk * @param offset Starting file offset ! * @param b Bytes to be written. * @throws IOException Thrown in case of I/O error */ --- 173,179 ---- /** * Writes bytes to the underlying RRD file on the disk + * * @param offset Starting file offset ! * @param b Bytes to be written. * @throws IOException Thrown in case of I/O error */ *************** *** 157,167 **** /** * Reads a number of bytes from the RRD file on the disk * @param offset Starting file offset ! * @param b Buffer which receives bytes read from the file. * @throws IOException Thrown in case of I/O error. */ protected void read(long offset, byte[] b) throws IOException { file.seek(offset); ! if(file.read(b) != b.length) { throw new IOException("Not enough bytes available in file " + getPath()); } --- 185,196 ---- /** * Reads a number of bytes from the RRD file on the disk + * * @param offset Starting file offset ! * @param b Buffer which receives bytes read from the file. * @throws IOException Thrown in case of I/O error. */ protected void read(long offset, byte[] b) throws IOException { file.seek(offset); ! if (file.read(b) != b.length) { throw new IOException("Not enough bytes available in file " + getPath()); } *************** *** 170,173 **** --- 199,203 ---- /** * Returns RRD file length. + * * @return File length. * @throws IOException Thrown in case of I/O error. *************** *** 180,183 **** --- 210,214 ---- * Sets length of the underlying RRD file. This method is called only once, immediately * after a new RRD file gets created. + * * @param length Length of the RRD file * @throws IOException Thrown in case of I/O error. |