|
Side of Software Persistence Library 2.0 |
||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object sos.db.AbstractDatabase sos.db.AbstractSerializationDatabase
public abstract class AbstractSerializationDatabase
A partial implementation of a serialization-based database.
This abstract class saves objects by serializing them and
assigning them a unique id. The root
object and all objects passed to addObject
must be serializable using Java's standard serialization framework.
Once this class serializes an object to an array of bytes, it leaves it
up to the subclass as to where these bytes should be stored. The subclass
must define:
Subclasses need not worry about proxy objects nor the transaction policy.
These are handled by
AbstractSerializationDatabase
.
Database.logger
.
It logs messages for the following activities:
Activity | Severity |
---|---|
The database is created | INFO |
The database is closed | INFO |
The database is deleted | INFO |
The database is opened | INFO |
A transaction is started | FINE |
A transaction is committed | FINE |
A transaction is aborted | FINE |
An object is added to the database | FINER |
An object is resaved in the database | FINER |
An object is removed from the database | FINER |
Subclasses may change the log messages or severity by overriding
the logXxx
methods.
The transaction policy may log additional activity, such as lock acquisition and release.
Warning: Externally serializing a database object does not prevent the object from being automatically removed from the database when it is no longer reachable from database objects. Therefore, clients should ensure that externally serialized database objects are also reachable from the root database object.
Database
,
TransactionPolicy
Field Summary |
---|
Fields inherited from interface sos.db.Database |
---|
logger |
Constructor Summary | |
---|---|
protected |
AbstractSerializationDatabase(java.lang.String databaseName,
TransactionPolicy policy)
Creates an AbstractSerializationDatabase with the specified
database name and transaction policy. |
Method Summary | |
---|---|
protected TransactionPolicy |
createDefaultTransactionPolicy()
Creates and returns a default transaction policy. |
protected void |
doAbortTransaction()
Performs the action of abortTransaction . |
protected java.lang.Object |
doAddObject(java.lang.Object object)
Performs the action of addObject . |
protected void |
doClose()
Performs the action of close . |
protected void |
doCommitTransaction(Progress progress)
Performs the action of commitTransaction . |
protected boolean |
doContainsObject(java.lang.Object object)
Performs the action of containsObject . |
protected boolean |
doCreate(java.lang.Object root)
Performs the action of create . |
protected boolean |
doDelete()
Performs the action of delete . |
protected abstract byte[] |
doFetchObject(long id)
Returns the serialized object associated with id . |
protected abstract 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 java.lang.Object |
doGetRoot()
Performs the action of getRoot . |
protected abstract long |
doNextKey(long fromKey)
Returns the next key greater than or equal to fromKey that
corresponds to an object in this database. |
protected void |
doOpen(boolean readOnly)
Performs the action of open . |
protected abstract void |
doRemoveObject(long oldKey)
Removes the specified object from this database. |
protected void |
doStartTransaction()
Performs the action of startTransaction . |
protected int |
doTransactionDepth()
Performs the action of transactionDepth . |
protected boolean |
enableReplaceObject(boolean enable)
Enables the substitution of objects during serialization. |
java.lang.String |
getName()
Returns the name of this database. |
TransactionPolicy |
getTransactionPolicy()
Returns the transaction policy governing the transactions. |
protected void |
logDatabaseClosed()
Records that this database has been closed. |
protected void |
logDatabaseCreated()
Records that this database has been created. |
protected void |
logDatabaseDeleted()
Records that this database has been deleted. |
protected void |
logDatabaseOpened()
Records that this database has been opened. |
protected void |
logObjectAdded(long id,
java.lang.Class objectClass)
Records that this database added an object with the specified id. |
protected void |
logObjectRemoved(long id)
Records that this database has removed the object with the specified id. |
protected void |
logObjectResaved(long id)
Records that this database has resaved the object with the specified id. |
protected void |
logTransactionAborted()
Records that the transaction for the current thread has aborted. |
protected void |
logTransactionCommitted()
Records that the transaction for the current thread has committed. |
protected void |
logTransactionStarted()
Records that a transaction for the current thread has started. |
protected java.lang.Object |
readResolve()
Returns a deserialized version of this database. |
protected java.lang.Object |
replaceObject(java.lang.Object obj)
Allows subclasses to substitute one object for another during serialization. |
protected java.lang.Object |
resolveObject(java.lang.Object obj)
Allows subclasses to substitute one object for another during deserialization. |
void |
setAutoCreateTransactions(boolean autoCreateTransactions)
Sets whether or not this database allows a method invocation on a database object when there is no current transaction. |
Methods inherited from class sos.db.AbstractDatabase |
---|
abortTransaction, addObject, close, commitTransaction, containsObject, create, delete, doExists, exists, getRoot, isOpen, open, startTransaction, transactionDepth |
Methods inherited from class java.lang.Object |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Constructor Detail |
---|
protected AbstractSerializationDatabase(java.lang.String databaseName, TransactionPolicy policy)
AbstractSerializationDatabase
with the specified
database name and transaction policy. If
policy
is null
, this methods invokes
createDefaultTransactionPolicy
to create a default transaction policy.
databaseName
- name of the databasepolicy
- policy to use to control transactions (may be null
)Method Detail |
---|
protected TransactionPolicy createDefaultTransactionPolicy()
protected boolean doContainsObject(java.lang.Object object)
AbstractDatabase
containsObject
. This method is called
inside containsObject
if this database is in
an acceptable state. It has the same semantics as
containsObject
.
doContainsObject
in class AbstractDatabase
object
- the object to check for containment
true
if this database contains object
protected void doAbortTransaction()
AbstractDatabase
abortTransaction
. This method is called
inside abortTransaction
if this database is in
an acceptable state. It has the same semantics as
abortTransaction
.
doAbortTransaction
in class AbstractDatabase
AbstractDatabase.abortTransaction()
protected java.lang.Object doAddObject(java.lang.Object object) throws TransactionAbortedException
AbstractDatabase
addObject
. This method is called
inside addObject
if this database is in
an acceptable state. It has the same semantics as
addObject
.
doAddObject
in class AbstractDatabase
object
- object to add to the database
object
TransactionAbortedException
- if this database cannot add object
and the transaction has been aborted as a resultAbstractDatabase.addObject(java.lang.Object)
protected void doClose() throws java.io.IOException
AbstractDatabase
close
. This method is called
inside close
if this database is in
an acceptable state. It has the same semantics as
close
.
doClose
in class AbstractDatabase
java.io.IOException
- if an I/O error occursAbstractDatabase.close()
protected void doCommitTransaction(Progress progress) throws TransactionAbortedException
AbstractDatabase
commitTransaction
. This method is called
inside commitTransaction
if this database is in
an acceptable state. It has the same semantics as
commitTransaction
.
doCommitTransaction
in class AbstractDatabase
progress
- an object to be updated and checked while the
commit is in progress (may be null
to indicate that no updating/checking
is necessary)
TransactionAbortedException
- if this database cannot commit
the transaction or if the progress is canceledAbstractDatabase.commitTransaction(sos.db.Progress)
protected boolean doCreate(java.lang.Object root) throws java.io.IOException
AbstractDatabase
create
. This method is called
inside create
if this database is in
an acceptable state. It has the same semantics as
create
.
doCreate
in class AbstractDatabase
root
- the root of this database
true
if this database does not exist and was successfully created;
false
if this database already exists
java.io.IOException
- if an I/O error occursAbstractDatabase.create(java.lang.Object)
protected boolean doDelete() throws java.io.IOException
AbstractDatabase
delete
. This method is called
inside delete
if this database is in
an acceptable state. It has the same semantics as
delete
.
doDelete
in class AbstractDatabase
true
if this database is successfully deleted
java.io.IOException
- if an I/O error occursAbstractDatabase.delete()
protected abstract byte[] doFetchObject(long id) throws java.io.IOException
id
.This method is called while holding this database's monitor.
id
- id of object to fetch
id
java.io.IOException
- if an I/O error occursprotected abstract boolean doFlush(LongMap<byte[]> newObjects, LongMap<byte[]> modifiedObjects, Progress progress) throws java.io.IOException
This method is called when a top-level transaction commits. It is called while holding this database's monitor.
newObjects
- a mapping of new ids (as Long
s) to
new database objects (as byte[]
s)modifiedObjects
- a mapping of ids (as Long
s) to
modified database objects (as byte[]
s)progress
- an object to be updated and checked as the flush is occurring
(may be null
)
false
if it is interrupted because the progress is canceled;
true
otherwise
java.io.IOException
- if an I/O error occursprotected java.lang.Object doGetRoot() throws TransactionAbortedException
AbstractDatabase
getRoot
. This method is called
inside getRoot
if this database is in
an acceptable state. It has the same semantics as
getRoot
.
doGetRoot
in class AbstractDatabase
null
)
TransactionAbortedException
- if the root object cannot
be retrieved and the transaction has been aborted as a result.AbstractDatabase.getRoot()
protected void doOpen(boolean readOnly) throws java.io.IOException
AbstractDatabase
open
. This method is called
inside open
if this database is in
an acceptable state. It has the same semantics as
open
.
doOpen
in class AbstractDatabase
readOnly
- true
if database updates are not allowed
java.io.IOException
- if an I/O error occursAbstractDatabase.open
protected abstract long doNextKey(long fromKey) throws java.io.IOException
fromKey
that
corresponds to an object in this database.
This method is called while holding this database's monitor.
fromKey
- key from which to start the search
java.io.IOException
- if an I/O error occursprotected abstract void doRemoveObject(long oldKey) throws java.io.IOException
This method is called when a garbage collection finds the object unreachable. It is called while holding this database's monitor.
oldKey
- keys of the object to remove from this database
java.io.IOException
- if an I/O error occursprotected void doStartTransaction() throws TransactionAbortedException
AbstractDatabase
startTransaction
. This method is called
inside startTransaction
if this database is in
an acceptable state. It has the same semantics as
startTransaction
.
doStartTransaction
in class AbstractDatabase
TransactionAbortedException
- if the transaction cannot
be startedAbstractDatabase.startTransaction()
protected int doTransactionDepth()
AbstractDatabase
transactionDepth
. This method is called
inside transactionDepth
if this database is in
an acceptable state. It has the same semantics as
transactionDepth
.
doTransactionDepth
in class AbstractDatabase
AbstractDatabase.transactionDepth()
protected boolean enableReplaceObject(boolean enable)
true
instructs this database to invoke
replaceObject
during serialization, thereby allowing
subclasses to replace objects before they are written to an output stream.
This method is similar to java.io.ObjectOutputStream.enableReplaceObject
.
Subclasses that call this method with true
will typically
override replaceObject
.
enable
- a flag indicating whether or not replaceObject
should be invoked
replaceObject(java.lang.Object)
public java.lang.String getName()
Database
getName
in interface Database
public TransactionPolicy getTransactionPolicy()
Database
getTransactionPolicy
in interface Database
protected void logDatabaseClosed()
This implementation uses the localized message for key "logDatabaseClosed". The message takes one parameter: the name of this database.
Database.logger
protected void logDatabaseCreated()
This implementation uses the localized message for key "logDatabaseCreated". The message takes one parameter: the name of this database.
Database.logger
protected void logDatabaseDeleted()
This implementation uses the localized message for key "logDatabaseDeleted". The message takes one parameter: the name of this database.
Database.logger
protected void logDatabaseOpened()
This implementation uses the localized message for key "logDatabaseOpened". The message takes one parameter: the name of this database.
Database.logger
protected void logObjectAdded(long id, java.lang.Class objectClass)
This implementation uses the localized message for key "logObjectAdded". The message takes four parameters: the name of this database, the transaction thread, the id, and the class of the new object.
id
- id of object being addedobjectClass
- class of the object being addedDatabase.logger
protected void logObjectRemoved(long id)
This implementation uses the localized message for key "logObjectRemoved". The message takes three parameters: the name of this database, the transaction thread, and the id.
id
- id of the object being removedDatabase.logger
protected void logObjectResaved(long id)
This implementation uses the localized message for key "logObjectResaved". The message takes three parameters: the name of this database, the transaction thread, and the id.
id
- id of the object being resavedDatabase.logger
protected void logTransactionAborted()
This implementation uses the localized message for key "logTransactionAborted". The message takes two parameters: the name of this database and the transaction thread.
Database.logger
protected void logTransactionCommitted()
This implementation uses the localized message for key "logTransactionCommitted". The message takes two parameters: the name of this database and the transaction thread.
Database.logger
protected void logTransactionStarted()
This implementation uses the localized message for key "logTransactionStarted". The message takes two parameters: the name of this database and the transaction thread.
Database.logger
protected java.lang.Object replaceObject(java.lang.Object obj) throws java.io.IOException
enableReplaceObject
has been
called with true
.
This method is similar to java.io.ObjectOutputStream.replaceObject
.
The default implementation simply returns the object parameter. Subclasses can override this method to provide a different implementation.
obj
- object to be replaced
obj
java.io.IOException
- if this database cannot replace the specified objectenableReplaceObject(boolean)
,
resolveObject(java.lang.Object)
protected java.lang.Object readResolve() throws java.io.ObjectStreamException
java.io.ObjectStreamException
- if an error occursprotected java.lang.Object resolveObject(java.lang.Object obj)
This method is similar to java.io.ObjectInputStream.resolveObject
.
The default implementation simply returns the object parameter.
Subclasses can override this method to provide a different
implementation. This method can be used to
limit the number of objects resulting from deserialization.
For examples, subclasses can intern java.lang.String
objects
(or other objects) as they are deserialized.
obj
- object to be replaced
obj
java.io.IOException
- if this database cannot replace the specified objectreplaceObject(java.lang.Object)
public void setAutoCreateTransactions(boolean autoCreateTransactions)
autoCreateTransactions
- if true, this database automatically
starts and ends a transaction when a method invocation is not wrapped
in an explicit transaction
|
Side of Software Persistence Library 2.0 |
||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |