com.niggle.data.inmemory
Class InMemoryIndex

java.lang.Object
  |
  +--java.rmi.server.RemoteObject
        |
        +--java.rmi.server.RemoteServer
              |
              +--java.rmi.server.UnicastRemoteObject
                    |
                    +--com.niggle.data.AbstractDataSource
                          |
                          +--com.niggle.data.inmemory.InMemoryIndex
All Implemented Interfaces:
DataListener, DataSource, java.util.EventListener, java.rmi.Remote, java.io.Serializable

public class InMemoryIndex
extends AbstractDataSource
implements DataListener

An object that listens to the insert/update/delete events of a RecordManager and maintains a lookup table based on a certain field. It works for both one-to-one and one-to-many lookups The primary record manager does lookup by the uniqueID. This object does lookups by other fields such as username. MemoryDataSource finds out about changes via the DataListener interface. There is one MemoryDataSource per field indexed.

Author:
Jonathan Revusky
See Also:
Serialized Form

Fields inherited from class java.rmi.server.RemoteObject
ref
 
Constructor Summary
InMemoryIndex()
           
InMemoryIndex(java.lang.String name, java.lang.String field, java.lang.Class cl, boolean multipleAllowed)
          A convenience constructor largely for backward compatibility.
InMemoryIndex(java.lang.String name, java.lang.String field, RecordFilter filter, boolean multipleAllowed)
          Constructor Create an MemoryDataSource that caches on the given keyField If type is non-null, it will ignore all records that are not of this type
 
Method Summary
 void attach(MutableDataSource dataSource)
          Called to run over a RecordManager's entire set of elements and index them.
protected  void cacheRecord(Record newRec)
          add a record, and index it.
 void clear()
          clear the cache -- wipe out the entire index structure.
 void close()
          A method that should be called to free any resources associated with the data source.
 Record get(java.lang.String type, java.lang.Object key)
           
 java.lang.String getName()
           
 java.util.List getRecords(java.lang.Object key)
          Use this routine to lookup if there may be multiple records with the same key.
 void init(java.util.Properties props)
          TODO
 java.util.List keys(java.lang.String type)
          Return the keys to the index.
 void recordDeleted(DataEvent event)
          Listener notified when a record has been deleted.
 void recordInserted(DataEvent event)
          Listener notified when a record has been added.
 void recordUpdated(DataEvent event)
          invoked when the RecordManager whose events we are listening to has just updated an existing record.
 
Methods inherited from class com.niggle.data.AbstractDataSource
get, keys
 
Methods inherited from class java.rmi.server.UnicastRemoteObject
clone, exportObject, exportObject, exportObject, unexportObject
 
Methods inherited from class java.rmi.server.RemoteServer
getClientHost, getLog, setLog
 
Methods inherited from class java.rmi.server.RemoteObject
equals, getRef, hashCode, toString, toStub
 
Methods inherited from class java.lang.Object
finalize, getClass, notify, notifyAll, wait, wait, wait
 

Constructor Detail

InMemoryIndex

public InMemoryIndex(java.lang.String name,
                     java.lang.String field,
                     RecordFilter filter,
                     boolean multipleAllowed)
              throws java.io.IOException
Constructor Create an MemoryDataSource that caches on the given keyField If type is non-null, it will ignore all records that are not of this type
Parameters:
field - the name of the field to use as the lookup key
type - the object type to index. If this is null, we just try to index everything.
multipleAllowed - determines whether we maintain a one-to-many or one-to-one cache.

InMemoryIndex

public InMemoryIndex(java.lang.String name,
                     java.lang.String field,
                     java.lang.Class cl,
                     boolean multipleAllowed)
              throws java.io.IOException
A convenience constructor largely for backward compatibility.

InMemoryIndex

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

recordUpdated

public void recordUpdated(DataEvent event)
                   throws java.io.IOException
Description copied from interface: DataListener
invoked when the RecordManager whose events we are listening to has just updated an existing record.
Specified by:
recordUpdated in interface DataListener

recordDeleted

public void recordDeleted(DataEvent event)
                   throws java.io.IOException
Listener notified when a record has been deleted.
Specified by:
recordDeleted in interface DataListener
Parameters:
event - describes the record deleted.

cacheRecord

protected void cacheRecord(Record newRec)
add a record, and index it.
Parameters:
newRec - the record to add.

recordInserted

public void recordInserted(DataEvent event)
                    throws java.io.IOException
Listener notified when a record has been added.
Specified by:
recordInserted in interface DataListener
Parameters:
event - describes the record added.

keys

public java.util.List keys(java.lang.String type)
                    throws java.io.IOException
Return the keys to the index.
Returns:
a copy of the keys to the index. The list can be empty, but never null.
Throws:
java.io.IOException - thrown in case of a low-level error.

get

public Record get(java.lang.String type,
                  java.lang.Object key)
           throws java.io.IOException
Following copied from interface: com.niggle.data.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.

getRecords

public java.util.List getRecords(java.lang.Object key)
                          throws java.io.IOException
Use this routine to lookup if there may be multiple records with the same key.
Parameters:
key - the key on which the record is indexed.
Returns:
a List of matches (which may be empty)

getName

public java.lang.String getName()
Overrides:
getName in class AbstractDataSource
Following copied from interface: com.niggle.data.DataSource
Returns:
a unique string by which this DataSource will be known.

attach

public void attach(MutableDataSource dataSource)
            throws java.io.IOException
Called to run over a RecordManager's entire set of elements and index them.
Parameters:
rm - primary record manager that contains the records.

clear

public void clear()
           throws java.io.IOException
clear the cache -- wipe out the entire index structure.

init

public void init(java.util.Properties props)
          throws java.io.IOException
Description copied from class: AbstractDataSource
TODO
Overrides:
init in class AbstractDataSource

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.)