org.netbeans.mdr.persistence.btreeimpl.btreestorage
Class BtreeDatabase

java.lang.Object
  extended by org.netbeans.mdr.persistence.btreeimpl.btreestorage.BtreeDatabase
All Implemented Interfaces:
MDRCache.OverflowHandler, Index, SinglevaluedIndex

public class BtreeDatabase
extends java.lang.Object
implements SinglevaluedIndex, MDRCache.OverflowHandler

This is the primary index for the btree implementation of Storage. It consists of three files:

The data file, with an extension of ".btd", which contains the serialized versions of the Streamable objects. This is accessed via the BtreeDatafile class.

The index file, with an extension of ".btx", which translates MOFIDs to the corresponding offset in the data file. This is accessed via the SinglevaluedBtree class.

The transactional log file has the extnsion ".btb". It exists only between the time a trasaction starts to be comitted and the time it is fully comitted or rolled back.

The data file contains enough information to regenerate the index file if the index file is lost or damaged. If the index file is missing when the btree database is opened, it is silently regenerated.

A Btree storage is transactional. This is implemented using the logging feature of the FileCache. Objects stored in a Btree storage have identity in memory. That is, fetching the same object multiple times will result in multiple references to the same object, not mutilple copies. This is implemented by MDRCache.

Performance of the Btree can be tuned by adjusting these parameters:

PAGE_SIZE is the size of the pages in the file cache.

FILE_CACHE_SIZE in the number of pages cached in memory. Increasing this will minimize the amount of reading done from the btree files, though at the cost of decreased memory for other purposes.

MDR_CACHE_SIZE is the number of currently unreferenced persistent objects kept in memory. Increasing this will reduce the number of reads done from the btree storage, though again at the cost of decreased memory for other purposes.

MDR_CACHE_THRESHHOLD is the number of changed objects to keep in memory before saving them to disk. Increasing this will reduce the amount of disk I/O done, though yet again at the cost of decreased memory for other purposes.


Method Summary
 void add(MOFID mKey, java.lang.Object value)
          Adds a repository record, throwing an exception if it already exists
 void add(java.lang.Object key, java.lang.Object value)
          Adds a repository record, throwing an exception if it already exists
 void cacheThreshholdReached(MDRCache cach, int size)
          cache has reached threshhold
 int checkConsistency(java.io.PrintWriter strm)
          Check consistency of btree database
 void commitChanges()
          Commits changes to transaction cache, if cache treshold is reached, flushes cache to disk.
 void compress()
          Compress repository.
 java.lang.Object get(MOFID mKey)
          Gets a record from the repository.
 java.lang.Object get(java.lang.Object key)
          Gets a record from the repository.
 java.lang.Object getIfExists(MOFID mKey)
          Gets a record from the repository.
 java.lang.Object getIfExists(java.lang.Object key)
          Gets a record from the repository.
 Storage.EntryType getKeyType()
          Returns the type of keys in index.
 MofidGenerator getMofidGenerator()
          Return the MOFID generator for this repository
 java.util.Map getMofidMap()
          Return the map of MOFID UUIDs we know about
 java.lang.String getName()
          Returns the unique name of the index in the Storage.
 java.lang.Object getObject(java.lang.Object key, SinglevaluedIndex dummy)
          Like get, since we don't return keys
 java.lang.Object getObjectIfExists(java.lang.Object key, SinglevaluedIndex dummy)
          Like getIfExists, since we don't return keys
 Storage.EntryType getValueType()
          Returns the type of values indexed by this index.
 java.util.Set keySet()
          Returns a set view of the keys contained in this index.
 java.lang.String[] listIndexes()
          List all index names
 void objectStateChanged(MOFID mKey)
          Mark that the object has changed, and so needs to be saved on commit
 void objectStateChanged(java.lang.Object key)
          Mark that the object has changed, and so needs to be saved on commit
 boolean put(java.lang.Object key, java.lang.Object value)
          Adds or replaces a repository record
 java.util.Collection queryByKeyPrefix(java.lang.Object prefix, SinglevaluedIndex repos)
          Returns a collection view of Map.Entry key-value pairs in the index, where key matches the queried prefix.
 boolean remove(MOFID mKey)
          Deletes a repository record based on a MOFID
 boolean remove(java.lang.Object key)
          Deletes a repository record
 void replace(MOFID mKey, java.lang.Object value)
          Replaces the original value associated with the specified key in this index with new value.
 void replace(java.lang.Object key, java.lang.Object value)
          Replaces the original value associated with the specified key in this index with new value.
 void rollbackChanges()
          roll back all changes
 void saveChanges()
          save all changes to disk without comitting
 void setLoggingStream(java.io.PrintStream strm)
          Set our logging stream
 void shutDown()
          Called on exit, commited data cached in transaction cache need to be written to disk.
 int size()
          returns the number of objects in the repository
 java.util.Collection values()
          Returns a collection view of the values contained in the repository.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

compress

public void compress()
              throws StorageException
Compress repository. We do this by copying to a temporary repository, deleting the current repository, and renaming the temporary repository to our name.

Throws:
StorageException

size

public int size()
returns the number of objects in the repository


getName

public java.lang.String getName()
Returns the unique name of the index in the Storage.

Specified by:
getName in interface Index
Returns:
base file name for repository

getValueType

public Storage.EntryType getValueType()
Returns the type of values indexed by this index.

Specified by:
getValueType in interface Index
Returns:
EntryType.STREAMABLE

getKeyType

public Storage.EntryType getKeyType()
Returns the type of keys in index.

Specified by:
getKeyType in interface Index
Returns:
EntryType.MOFID

cacheThreshholdReached

public void cacheThreshholdReached(MDRCache cach,
                                   int size)
                            throws StorageException
cache has reached threshhold

Specified by:
cacheThreshholdReached in interface MDRCache.OverflowHandler
Parameters:
cach - cache which reached threshhold
size - number of changed objects currently in cache
Throws:
StorageException

commitChanges

public void commitChanges()
                   throws StorageException
Commits changes to transaction cache, if cache treshold is reached, flushes cache to disk.

Throws:
StorageException

shutDown

public void shutDown()
              throws StorageException
Called on exit, commited data cached in transaction cache need to be written to disk.

Throws:
StorageException

saveChanges

public void saveChanges()
                 throws StorageException
save all changes to disk without comitting

Throws:
StorageException

rollbackChanges

public void rollbackChanges()
                     throws StorageException
roll back all changes

Throws:
StorageException

remove

public boolean remove(java.lang.Object key)
               throws StorageException
Deletes a repository record

Specified by:
remove in interface Index
Parameters:
key - -- a String
Returns:
true if record exists
Throws:
StorageException

remove

public boolean remove(MOFID mKey)
               throws StorageException
Deletes a repository record based on a MOFID

Parameters:
mKey -
Returns:
true if record exists
Throws:
StorageException

add

public void add(java.lang.Object key,
                java.lang.Object value)
         throws StorageException
Adds a repository record, throwing an exception if it already exists

Specified by:
add in interface Index
Parameters:
key - -- a String
value -
Throws:
StorageException

add

public void add(MOFID mKey,
                java.lang.Object value)
         throws StorageException
Adds a repository record, throwing an exception if it already exists

Parameters:
mKey -
value -
Throws:
StorageException

replace

public void replace(java.lang.Object key,
                    java.lang.Object value)
             throws StorageException
Replaces the original value associated with the specified key in this index with new value. If no value was associated with this key prior to this call StorageBadRequestException is thrown.

Specified by:
replace in interface SinglevaluedIndex
Parameters:
key -
value -
Throws:
StorageException
StorageBadRequestException

replace

public void replace(MOFID mKey,
                    java.lang.Object value)
             throws StorageException
Replaces the original value associated with the specified key in this index with new value. If no value was associated with this key prior to this call StorageBadRequestException is thrown.

Parameters:
mKey -
value -
Throws:
StorageException

put

public boolean put(java.lang.Object key,
                   java.lang.Object value)
            throws StorageException
Adds or replaces a repository record

Specified by:
put in interface SinglevaluedIndex
Parameters:
key - -- a String
value -
Returns:
always null
Throws:
StorageException

getIfExists

public java.lang.Object getIfExists(java.lang.Object key)
                             throws StorageException
Gets a record from the repository. Returns null if none exists.

Specified by:
getIfExists in interface SinglevaluedIndex
Parameters:
key - -- a String
Returns:
value associated with specified key, or null if there was no mapping for key
Throws:
StorageException

getObjectIfExists

public java.lang.Object getObjectIfExists(java.lang.Object key,
                                          SinglevaluedIndex dummy)
                                   throws StorageException
Like getIfExists, since we don't return keys

Specified by:
getObjectIfExists in interface SinglevaluedIndex
Returns:
Throws:
StorageException

getIfExists

public java.lang.Object getIfExists(MOFID mKey)
                             throws StorageException
Gets a record from the repository. Returns null if none exists.

Parameters:
key -
Returns:
value associated with specified key, or null if there was no mapping for key
Throws:
StorageException

get

public java.lang.Object get(java.lang.Object key)
                     throws StorageException
Gets a record from the repository. Throws an exception if none exists.

Specified by:
get in interface SinglevaluedIndex
Parameters:
key -
Returns:
value associated with specified key, or null if there was no mapping for key
Throws:
StorageException
StorageBadRequestException

getObject

public java.lang.Object getObject(java.lang.Object key,
                                  SinglevaluedIndex dummy)
                           throws StorageException
Like get, since we don't return keys

Specified by:
getObject in interface SinglevaluedIndex
Returns:
Throws:
StorageException

get

public java.lang.Object get(MOFID mKey)
                     throws StorageException
Gets a record from the repository. Throws an exception if none exists.

Parameters:
mKey -
Returns:
value associated with specified key, or null if there was no mapping for key
Throws:
StorageException

queryByKeyPrefix

public java.util.Collection queryByKeyPrefix(java.lang.Object prefix,
                                             SinglevaluedIndex repos)
Description copied from interface: SinglevaluedIndex
Returns a collection view of Map.Entry key-value pairs in the index, where key matches the queried prefix. Values are objects (as returned by getObject method).

Specified by:
queryByKeyPrefix in interface SinglevaluedIndex
Parameters:
prefix - queried prefix
repos - primary index
Returns:

objectStateChanged

public void objectStateChanged(java.lang.Object key)
                        throws StorageException
Mark that the object has changed, and so needs to be saved on commit

Parameters:
key - key of object whch changed (a String)
Throws:
StorageException

objectStateChanged

public void objectStateChanged(MOFID mKey)
                        throws StorageException
Mark that the object has changed, and so needs to be saved on commit

Parameters:
mKey - key of object whch changed
Throws:
StorageException

listIndexes

public java.lang.String[] listIndexes()
                               throws StorageException
List all index names

Returns:
an array of all the index names in alphabetical order
Throws:
StorageException

keySet

public java.util.Set keySet()
                     throws StorageException
Returns a set view of the keys contained in this index. Returned collection is read only and may not be modified.

Specified by:
keySet in interface Index
Returns:
the set of all MOFIDs which are keys for the repository
Throws:
StorageException

values

public java.util.Collection values()
                            throws StorageException
Returns a collection view of the values contained in the repository. Returned collection is read only and may not be modified.

Specified by:
values in interface SinglevaluedIndex
Returns:
all objects stored in the repository
Throws:
StorageException

setLoggingStream

public void setLoggingStream(java.io.PrintStream strm)
Set our logging stream


getMofidGenerator

public MofidGenerator getMofidGenerator()
Return the MOFID generator for this repository


getMofidMap

public java.util.Map getMofidMap()
Return the map of MOFID UUIDs we know about


checkConsistency

public int checkConsistency(java.io.PrintWriter strm)
                     throws StorageException
Check consistency of btree database

Parameters:
strm - where to write inconsistencies
Returns:
number of errors encountered
Throws:
StorageException


Copyright © 2005-2008 Apache Software Foundation. All Rights Reserved.