com.revusky.oreo.inmemory
Class InMemoryRecordSet

java.lang.Object
  |
  +--com.revusky.oreo.AbstractDataSource
        |
        +--com.revusky.oreo.AbstractMutableDataSource
              |
              +--com.revusky.oreo.inmemory.InMemoryRecordSet
All Implemented Interfaces:
DataSource, MutableDataSource, java.rmi.Remote

public class InMemoryRecordSet
extends AbstractMutableDataSource

A simple implementation of the com.revusky.oreo.MutableDataSource interface. All records are kept in an flat array all in RAM and persisted to a flat-file. In configuring this file in XML, you only have to specify 2 properties: STORE, which indicates a file location to persist to. You can optionally indicate COMPACT_FREQUENCY, which says how frequently to rewrite the entire store in a compact form.

Author:
Jonathan Revusky

Constructor Summary
InMemoryRecordSet()
           
 
Method Summary
 void close()
          A method that should be called to free any resources associated with the data source.
 void delete(Record record)
          Delete a record
 Record get(java.lang.String type, java.lang.Object key)
           
 void init(java.util.Properties props)
          A method that initializes a DataSource with a set of initialization properties.
 void insert(Record record)
          Adds a new record to the managed pool.
 java.util.List keys(java.lang.String type)
           
 void loadRecords(java.io.ObjectInput input)
          Slurp the records into this InMemoryRecordSet object with a stream-based idiom.
protected  void loadRecordsFromFlatFile(java.lang.String filename)
          Wrapper around loadRecords() to read records from a flat file.
protected  java.lang.Object nextKey()
          Returns the next available primary key, assuming that it is an integer.
 void playEvent(DataEvent event)
          "play" an event This will be mostly used in restarting an app and reconstructing the state of the data from logs.
 java.util.List select(RecordFilter filter)
          Fetches a list of records matching filter.
 void update(Record oldRec, Record newRec)
          Replaces an existing version of a record with a new updated version.
 
Methods inherited from class com.revusky.oreo.AbstractMutableDataSource
addDataListener, fireDataEvent, freeze, freeze, getRecords, markStale, normalizeKey, removeDataListener
 
Methods inherited from class com.revusky.oreo.AbstractDataSource
get, getName, keys, wipeCache
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 
Methods inherited from interface com.revusky.oreo.DataSource
get, getName, keys, wipeCache
 

Constructor Detail

InMemoryRecordSet

public InMemoryRecordSet()
                  throws java.io.IOException
Method Detail

loadRecords

public void loadRecords(java.io.ObjectInput input)
                 throws java.io.IOException
Slurp the records into this InMemoryRecordSet object with a stream-based idiom. Subclasses of DataException will be caught and logged. Core Java IOExceptions will bubble up. NB: This method is meant to be called at initialization. The loading of the records does not go through the DataListener mechanism.

update

public void update(Record oldRec,
                   Record newRec)
            throws java.io.IOException
Description copied from interface: MutableDataSource
Replaces an existing version of a record with a new updated version. Some implementations may use the oldRec parameter to guarantee that there are no concurrency issues. In such case, it would throw a ConcurrentModificationException
Following copied from interface: com.revusky.oreo.MutableDataSource
Parameters:
oldRec - the record to replace.
newRec - the new record.
Throws:
java.io.IOException - thrown in case of any other database or communication error.

insert

public void insert(Record record)
            throws java.io.IOException
Description copied from interface: MutableDataSource
Adds a new record to the managed pool.
Following copied from interface: com.revusky.oreo.MutableDataSource
Parameters:
rec - the record to add
Throws:
DuplicateRecordException - if another record already exists with same primary key as 'rec'.
java.io.IOException - if the record cannot be initialized (i.e. has missing fields or invalid field values, etc.), or in case of a low-level error.

select

public java.util.List select(RecordFilter filter)
                      throws java.io.IOException
Description copied from interface: MutableDataSource
Fetches a list of records matching filter. If filter is null, returns all the records in the container.
Following copied from interface: com.revusky.oreo.MutableDataSource
Parameters:
filter - the record filter, or null.
Returns:
a List of all the records that are an instance of a given class. If no records match the filter, an empty List.

get

public Record get(java.lang.String type,
                  java.lang.Object key)
           throws java.io.IOException
Following copied from interface: com.revusky.oreo.DataSource
Parameters:
type - the type of the record, if this is null, then any type will do.
key - the lookup key
Returns:
a Record of the given type, with the given lookup key. If the data source allows multiple records, only returns the first one found. Use getRecords() in that situation. returns null if none is found.

init

public void init(java.util.Properties props)
          throws java.io.IOException
Description copied from interface: DataSource
A method that initializes a DataSource with a set of initialization properties. This is called internally within Oreo and should not be called by application code. As an implementor, you should make sure that this method is only called once, for example, throw a RuntimeException if an attempt is made to call this on a DataSource a second time.
Overrides:
init in class AbstractDataSource

keys

public java.util.List keys(java.lang.String type)
                    throws java.io.IOException
Following copied from interface: com.revusky.oreo.DataSource
Parameters:
type - the record type we are interested, under some circumstances, this may be null.
Returns:
a list of all valid lookup keys that correspond to a given record type.

delete

public void delete(Record record)
            throws java.io.IOException
Description copied from interface: MutableDataSource
Delete a record
Following copied from interface: com.revusky.oreo.MutableDataSource
Parameters:
the - record to delete.

close

public void close()
           throws java.io.IOException
Description copied from interface: DataSource
A method that should be called to free any resources associated with the data source. Note that no other methods should be called on this object after this one. (In many implementations, this method will likely do nothing.)
Overrides:
close in class AbstractDataSource

nextKey

protected java.lang.Object nextKey()
Returns the next available primary key, assuming that it is an integer.

playEvent

public void playEvent(DataEvent event)
               throws java.io.IOException
Description copied from interface: MutableDataSource
"play" an event This will be mostly used in restarting an app and reconstructing the state of the data from logs. Note that data listeners are not notified.

loadRecordsFromFlatFile

protected void loadRecordsFromFlatFile(java.lang.String filename)
                                throws java.io.IOException
Wrapper around loadRecords() to read records from a flat file. Creates a DefaultRecordInputStream and invokes the record manager's loadRecords() method. Will try to pick it up from the .### temp file if that is available and filename is not. NB: This method is meant to be called at initialization. The loading of the records does not go through the DataListener mechanism.