Side of Software
Persistence Library 2.0

sos.db
Class InMemoryDatabase

java.lang.Object
  extended by sos.db.AbstractDatabase
      extended by sos.db.AbstractSerializationDatabase
          extended by sos.db.InMemoryDatabase
All Implemented Interfaces:
java.io.Serializable, Database

public class InMemoryDatabase
extends AbstractSerializationDatabase
implements Database

A non-persistent database that stores objects in memory. Since the database is entirely in memory, database objects cannot persist across multiple executions. Each time the application starts, it will have an empty database, which must be initialized with create. Most applications will use FileDatabase rather than this class.

This class is useful for applications wishing to use a temporary database, such as a cache. Also, applets can use this database without requiring access to the host's file system.

Logging

Instances of this database log database activity. See AbstractSerializationDatabase for more information.

External Serialization

Proxy objects can be serialized to a stream outside of the database. See AbstractSerializationDatabase for more information.

Since:
1.0
See Also:
Database, AbstractSerializationDatabase, TransactionPolicy, FileDatabase

Field Summary
 
Fields inherited from interface sos.db.Database
logger
 
Constructor Summary
InMemoryDatabase(java.lang.String databaseName)
          Creates an instance of InMemoryDatabase with the specified database name.
InMemoryDatabase(java.lang.String databaseName, TransactionPolicy policy)
          Creates a InMemoryDatabase with the specified database name and transaction policy.
 
Method Summary
protected  boolean doDelete()
          Performs the action of delete.
protected  boolean doExists()
          Performs the action of exists.
protected  byte[] doFetchObject(long id)
          Returns the serialized object associated with id.
protected  boolean doFlush(LongMap<byte[]> newObjects, LongMap<byte[]> modifiedObjects, Progress progress)
          Adds new objects, re-saves modified objects, and removes old objects from this database in a single, atomic step.
protected  long doNextKey(long fromKey)
          Returns the next key greater than or equal to fromKey that corresponds to an object in this database.
protected  void doRemoveObject(long oldKey)
          Removes the specified object from this database.
 
Methods inherited from class sos.db.AbstractSerializationDatabase
createDefaultTransactionPolicy, doAbortTransaction, doAddObject, doClose, doCommitTransaction, doContainsObject, doCreate, doGetRoot, doOpen, doStartTransaction, doTransactionDepth, enableReplaceObject, getName, getTransactionPolicy, logDatabaseClosed, logDatabaseCreated, logDatabaseDeleted, logDatabaseOpened, logObjectAdded, logObjectRemoved, logObjectResaved, logTransactionAborted, logTransactionCommitted, logTransactionStarted, readResolve, replaceObject, resolveObject, setAutoCreateTransactions
 
Methods inherited from class sos.db.AbstractDatabase
abortTransaction, addObject, close, commitTransaction, containsObject, create, delete, exists, getRoot, isOpen, open, startTransaction, transactionDepth
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 
Methods inherited from interface sos.db.Database
abortTransaction, addObject, close, commitTransaction, containsObject, create, delete, exists, getName, getRoot, getTransactionPolicy, isOpen, open, startTransaction, transactionDepth
 

Constructor Detail

InMemoryDatabase

public InMemoryDatabase(java.lang.String databaseName)
Creates an instance of InMemoryDatabase with the specified database name. The name is used only in the logging of messages.

Parameters:
databaseName - name of this database
See Also:
InMemoryDatabase(String,TransactionPolicy)

InMemoryDatabase

public InMemoryDatabase(java.lang.String databaseName,
                        TransactionPolicy policy)
Creates a InMemoryDatabase with the specified database name and transaction policy.

Parameters:
databaseName - name of the database
policy - policy to use to control transactions (may be null)
Method Detail

doDelete

protected boolean doDelete()
                    throws java.io.IOException
Description copied from class: AbstractDatabase
Performs the action of delete. This method is called inside delete if this database is in an acceptable state. It has the same semantics as delete.

Overrides:
doDelete in class AbstractSerializationDatabase
Returns:
true if this database is successfully deleted
Throws:
java.io.IOException - if an I/O error occurs
See Also:
AbstractDatabase.delete()

doExists

protected boolean doExists()
Description copied from class: AbstractDatabase
Performs the action of exists. This method is called inside exists if this database is in an acceptable state. It has the same semantics as exists.

Specified by:
doExists in class AbstractDatabase
Returns:
true if this database exists
See Also:
AbstractDatabase.exists()

doFetchObject

protected byte[] doFetchObject(long id)
                        throws java.io.IOException
Description copied from class: AbstractSerializationDatabase
Returns the serialized object associated with id.

This method is called while holding this database's monitor.

Specified by:
doFetchObject in class AbstractSerializationDatabase
Parameters:
id - id of object to fetch
Returns:
the serialized object associated with id
Throws:
java.io.IOException - if an I/O error occurs

doFlush

protected boolean doFlush(LongMap<byte[]> newObjects,
                          LongMap<byte[]> modifiedObjects,
                          Progress progress)
                   throws java.io.IOException
Description copied from class: AbstractSerializationDatabase
Adds new objects, re-saves modified objects, and removes old objects from this database in a single, atomic step. If any of these steps should fail, the database should roll back to its state prior to the call.

This method is called when a top-level transaction commits. It is called while holding this database's monitor.

Specified by:
doFlush in class AbstractSerializationDatabase
Parameters:
newObjects - a mapping of new ids (as Longs) to new database objects (as byte[]s)
modifiedObjects - a mapping of ids (as Longs) to modified database objects (as byte[]s)
progress - an object to be updated and checked as the flush is occurring (may be null)
Returns:
false if it is interrupted because the progress is canceled; true otherwise
Throws:
java.io.IOException - if an I/O error occurs

doRemoveObject

protected void doRemoveObject(long oldKey)
                       throws java.io.IOException
Description copied from class: AbstractSerializationDatabase
Removes the specified object from this database.

This method is called when a garbage collection finds the object unreachable. It is called while holding this database's monitor.

Specified by:
doRemoveObject in class AbstractSerializationDatabase
Parameters:
oldKey - keys of the object to remove from this database
Throws:
java.io.IOException - if an I/O error occurs

doNextKey

protected long doNextKey(long fromKey)
                  throws java.io.IOException
Description copied from class: AbstractSerializationDatabase
Returns the next key greater than or equal to fromKey that corresponds to an object in this database. This method is called while holding this database's monitor.

Specified by:
doNextKey in class AbstractSerializationDatabase
Parameters:
fromKey - key from which to start the search
Returns:
the key of the next database object or -1, if no more database objects
Throws:
java.io.IOException - if an I/O error occurs

Side of Software
Persistence Library 2.0

Copyright 2004-08 Side of Software (SOS). All rights reserved.