ch.javasoft.jbase
Class BufferedRandomAccessPersister

java.lang.Object
  extended by ch.javasoft.jbase.BufferedRandomAccessPersister
All Implemented Interfaces:
Stateful, RandomAccessPersister

public class BufferedRandomAccessPersister
extends Object
implements RandomAccessPersister

The BufferedRandomAccessPersister caches data in a hash table, and delegates the real i/o operations to an underlying delegate RandomAccessPersister.

Note that all cache-access is thread safe. If the caller guarantees that atomic read and write operations do not overlap, the class is thread-safe, even if concurrent read operations are overlapping.


Constructor Summary
BufferedRandomAccessPersister(File file, int tableSize, int entrySize)
          Constructor using a RandomAccessFilePersistor as delegate, and a cache acording to the specified specifications.
BufferedRandomAccessPersister(RandomAccessPersister delegate, int tableSize, int entrySize)
          Constructor with delegate persistor and cache specifications.
 
Method Summary
 void close(boolean erase)
          Closes this persistor, subsequent read or write calls will cause an exception.
 RandomAccessPersister createReadCopy(ReadWriteLock lock)
          Override to specialize return type
 void flush()
          Ensures that any possibly cached data is written to the underlying store
protected  void flushDirtyEntry(RandomAccessPersister delegate, ch.javasoft.jbase.BufferedRandomAccessPersister.TableEntry entry)
          PRECONDITION: read or write lock held
 DataInput getInput()
          Returns a data input object to read from.
 DataOutput getOutput()
          Returns a data input object to write to.
 long getPosition()
          Returns the current byte position.
 void setLength(long byteLength)
          Sets the length of the storage.
 void setPosition(long bytePos)
          Sets the byte offset position.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

BufferedRandomAccessPersister

public BufferedRandomAccessPersister(File file,
                                     int tableSize,
                                     int entrySize)
                              throws FileNotFoundException
Constructor using a RandomAccessFilePersistor as delegate, and a cache acording to the specified specifications.

Notes:

Recommended values:

Parameters:
file - file to create a RandomAccessFilePersistor
tableSize - number of entries in the hash table
entrySize - buffer byte length of a single hash table entry
Throws:
FileNotFoundException - if the specified file is not found

BufferedRandomAccessPersister

public BufferedRandomAccessPersister(RandomAccessPersister delegate,
                                     int tableSize,
                                     int entrySize)
Constructor with delegate persistor and cache specifications.

Notes:

Recommended values:

Parameters:
delegate - delegate persistor, does the real i/o operations
tableSize - number of entries in the hash table
entrySize - buffer byte length of a single hash table entry
Method Detail

getInput

public DataInput getInput()
                   throws IOException
Description copied from interface: RandomAccessPersister
Returns a data input object to read from. Read operations increment the current byte position

Specified by:
getInput in interface RandomAccessPersister
Throws:
IOException

getOutput

public DataOutput getOutput()
                     throws IOException
Description copied from interface: RandomAccessPersister
Returns a data input object to write to. Write operations increment the current byte position

Specified by:
getOutput in interface RandomAccessPersister
Throws:
IOException

getPosition

public long getPosition()
                 throws IOException
Description copied from interface: RandomAccessPersister
Returns the current byte position. Note that any read or write operation increments the position.

Specified by:
getPosition in interface RandomAccessPersister
Throws:
IOException

setPosition

public void setPosition(long bytePos)
                 throws IOException
Description copied from interface: RandomAccessPersister
Sets the byte offset position. Subsequent read or write operations start at the specified position, even if it is after the actual length of the file or store. However, the length does not change before actually writing at this position.

Specified by:
setPosition in interface RandomAccessPersister
Throws:
IOException

setLength

public void setLength(long byteLength)
               throws IOException
Description copied from interface: RandomAccessPersister
Sets the length of the storage. If this is shorter than the current length, the data will be truncated. If it is after the current length, additional data is appended, but its content is undefined.

Specified by:
setLength in interface RandomAccessPersister
Throws:
IOException

flush

public void flush()
           throws IOException
Description copied from interface: RandomAccessPersister
Ensures that any possibly cached data is written to the underlying store

Specified by:
flush in interface RandomAccessPersister
Throws:
IOException

flushDirtyEntry

protected void flushDirtyEntry(RandomAccessPersister delegate,
                               ch.javasoft.jbase.BufferedRandomAccessPersister.TableEntry entry)
                        throws IOException
PRECONDITION: read or write lock held

Throws:
IOException

close

public void close(boolean erase)
           throws IOException
Description copied from interface: RandomAccessPersister
Closes this persistor, subsequent read or write calls will cause an exception. Multiple close calls, however, do not cause any exceptions.

Specified by:
close in interface RandomAccessPersister
Parameters:
erase - if true, underlying files are deleted upon close
Throws:
IOException

createReadCopy

public RandomAccessPersister createReadCopy(ReadWriteLock lock)
                                     throws IOException
Description copied from interface: RandomAccessPersister
Override to specialize return type

Specified by:
createReadCopy in interface Stateful
Specified by:
createReadCopy in interface RandomAccessPersister
Parameters:
lock - A read/write lock, the write lock is held. The lock, however, might also be used for other functionality of the returned read copy, for instance, if the read copy has to be put in sync with the main object.
Returns:
a new read copy instance, with copied state compared to other read copy instances
Throws:
IOException
See Also:
Stateful.createReadCopy(ReadWriteLock)