jdbm.recman
Class BaseRecordManager

java.lang.Object
  extended by jdbm.recman.BaseRecordManager
All Implemented Interfaces:
RecordManager

public final class BaseRecordManager
extends java.lang.Object
implements RecordManager

This class manages records, which are uninterpreted blobs of data. The set of operations is simple and straightforward: you communicate with the class using long "rowids" and byte[] data blocks. Rowids are returned on inserts and you can stash them away someplace safe to be able to get back to them. Data blocks can be as long as you wish, and may have lengths different from the original when updating.

Operations are synchronized, so that only one of them will happen concurrently even if you hammer away from multiple threads. Operations are made atomic by keeping a transaction log which is recovered after a crash, so the operations specified by this interface all have ACID properties.

You identify a file by just the name. The package attaches .db for the database file, and .lg for the transaction log. The transaction log is synchronized regularly and then restarted, so don't worry if you see the size going up and down.

Version:
$Id: BaseRecordManager.java,v 1.8 2005/06/25 23:12:32 doomdark Exp $
Author:
Alex Boisvert, Cees de Groot

Field Summary
static boolean DEBUG
          Static debugging flag
static int NAME_DIRECTORY_ROOT
          Reserved slot for name directory.
 
Constructor Summary
BaseRecordManager(java.lang.String filename)
          Creates a record manager for the indicated file
 
Method Summary
 void close()
          Closes the record manager.
 void commit()
          Commit (make persistent) all changes since beginning of transaction.
 void delete(long recid)
          Deletes a record.
 void disableTransactions()
          Switches off transactioning for the record manager.
 java.lang.Object fetch(long recid)
          Fetches a record using standard java object serialization.
 java.lang.Object fetch(long recid, Serializer serializer)
          Fetches a record using a custom serializer.
 long getNamedObject(java.lang.String name)
          Obtain the record id of a named object.
 long getRoot(int id)
          Returns the indicated root rowid.
 int getRootCount()
          Returns the number of slots available for "root" rowids.
 TransactionManager getTransactionManager()
          Get the underlying Transaction Manager
 long insert(java.lang.Object obj)
          Inserts a new record using standard java object serialization.
 long insert(java.lang.Object obj, Serializer serializer)
          Inserts a new record using a custom serializer.
 void rollback()
          Rollback (cancel) all changes since beginning of transaction.
 void setNamedObject(java.lang.String name, long recid)
          Set the record id of a named object.
 void setRoot(int id, long rowid)
          Sets the indicated root rowid.
 void update(long recid, java.lang.Object obj)
          Updates a record using standard java object serialization.
 void update(long recid, java.lang.Object obj, Serializer serializer)
          Updates a record using a custom serializer.
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

NAME_DIRECTORY_ROOT

public static final int NAME_DIRECTORY_ROOT
Reserved slot for name directory.

See Also:
Constant Field Values

DEBUG

public static final boolean DEBUG
Static debugging flag

See Also:
Constant Field Values
Constructor Detail

BaseRecordManager

public BaseRecordManager(java.lang.String filename)
                  throws java.io.IOException
Creates a record manager for the indicated file

Throws:
java.io.IOException - when the file cannot be opened or is not a valid file content-wise.
Method Detail

getTransactionManager

public TransactionManager getTransactionManager()
Get the underlying Transaction Manager


disableTransactions

public void disableTransactions()
Switches off transactioning for the record manager. This means that a) a transaction log is not kept, and b) writes aren't synch'ed after every update. This is useful when batch inserting into a new database.

Only call this method directly after opening the file, otherwise the results will be undefined.


close

public void close()
           throws java.io.IOException
Closes the record manager.

Specified by:
close in interface RecordManager
Throws:
java.io.IOException - when one of the underlying I/O operations fails.

insert

public long insert(java.lang.Object obj)
            throws java.io.IOException
Inserts a new record using standard java object serialization.

Specified by:
insert in interface RecordManager
Parameters:
obj - the object for the new record.
Returns:
the rowid for the new record.
Throws:
java.io.IOException - when one of the underlying I/O operations fails.

insert

public long insert(java.lang.Object obj,
                   Serializer serializer)
            throws java.io.IOException
Inserts a new record using a custom serializer.

Specified by:
insert in interface RecordManager
Parameters:
obj - the object for the new record.
serializer - a custom serializer
Returns:
the rowid for the new record.
Throws:
java.io.IOException - when one of the underlying I/O operations fails.

delete

public void delete(long recid)
            throws java.io.IOException
Deletes a record.

Specified by:
delete in interface RecordManager
Parameters:
recid - the rowid for the record that should be deleted.
Throws:
java.io.IOException - when one of the underlying I/O operations fails.

update

public void update(long recid,
                   java.lang.Object obj)
            throws java.io.IOException
Updates a record using standard java object serialization.

Specified by:
update in interface RecordManager
Parameters:
recid - the recid for the record that is to be updated.
obj - the new object for the record.
Throws:
java.io.IOException - when one of the underlying I/O operations fails.

update

public void update(long recid,
                   java.lang.Object obj,
                   Serializer serializer)
            throws java.io.IOException
Updates a record using a custom serializer.

Specified by:
update in interface RecordManager
Parameters:
recid - the recid for the record that is to be updated.
obj - the new object for the record.
serializer - a custom serializer
Throws:
java.io.IOException - when one of the underlying I/O operations fails.

fetch

public java.lang.Object fetch(long recid)
                       throws java.io.IOException
Fetches a record using standard java object serialization.

Specified by:
fetch in interface RecordManager
Parameters:
recid - the recid for the record that must be fetched.
Returns:
the object contained in the record.
Throws:
java.io.IOException - when one of the underlying I/O operations fails.

fetch

public java.lang.Object fetch(long recid,
                              Serializer serializer)
                       throws java.io.IOException
Fetches a record using a custom serializer.

Specified by:
fetch in interface RecordManager
Parameters:
recid - the recid for the record that must be fetched.
serializer - a custom serializer
Returns:
the object contained in the record.
Throws:
java.io.IOException - when one of the underlying I/O operations fails.

getRootCount

public int getRootCount()
Returns the number of slots available for "root" rowids. These slots can be used to store special rowids, like rowids that point to other rowids. Root rowids are useful for bootstrapping access to a set of data.

Specified by:
getRootCount in interface RecordManager

getRoot

public long getRoot(int id)
             throws java.io.IOException
Returns the indicated root rowid.

Specified by:
getRoot in interface RecordManager
Throws:
java.io.IOException
See Also:
getRootCount()

setRoot

public void setRoot(int id,
                    long rowid)
             throws java.io.IOException
Sets the indicated root rowid.

Specified by:
setRoot in interface RecordManager
Throws:
java.io.IOException
See Also:
getRootCount()

getNamedObject

public long getNamedObject(java.lang.String name)
                    throws java.io.IOException
Obtain the record id of a named object. Returns 0 if named object doesn't exist.

Specified by:
getNamedObject in interface RecordManager
Throws:
java.io.IOException

setNamedObject

public void setNamedObject(java.lang.String name,
                           long recid)
                    throws java.io.IOException
Set the record id of a named object.

Specified by:
setNamedObject in interface RecordManager
Throws:
java.io.IOException

commit

public void commit()
            throws java.io.IOException
Commit (make persistent) all changes since beginning of transaction.

Specified by:
commit in interface RecordManager
Throws:
java.io.IOException

rollback

public void rollback()
              throws java.io.IOException
Rollback (cancel) all changes since beginning of transaction.

Specified by:
rollback in interface RecordManager
Throws:
java.io.IOException


Cees de Groot (C) 2000. All rights reserved http://jdbm.sourceforge.net