org.ov4j.hessianImpl
Class Container<T extends java.lang.Comparable<? super T> & java.lang.Cloneable & java.io.Serializable,C extends java.lang.Comparable<? super C>>

java.lang.Object
  extended by org.ov4j.hessianImpl.Container<T,C>
All Implemented Interfaces:
IContainer<T,C>

public class Container<T extends java.lang.Comparable<? super T> & java.lang.Cloneable & java.io.Serializable,C extends java.lang.Comparable<? super C>>
extends java.lang.Object
implements IContainer<T,C>

Author:
smolloy

Field Summary
protected  java.util.ArrayList<C> cachedIds
          IDs kept in cache.
protected  boolean closed
          Flag to indicate if DB is closed.
protected  boolean closing
          Flag for closing.
protected  long counter
          Next filename to assign.
protected  java.lang.String dbName
          Name of the DB.
protected static java.lang.String DELETED_FOLDER
          Folder name where to store deleted ids.
protected  java.util.TreeMap<C,java.lang.String> deletedMap
          Current live deleted ID to filename Map.
protected  java.util.TreeMap<C,java.lang.String> deletedMapWrk
          Current work deleted ID to filename Map.
protected static java.lang.Comparable<?>[] EMPTY_COMPARABLE_ARRAY
          Empty Comparable array.
protected static DirFileFilter FOLDER_FILTER
          FileFilter accepting folder files.
protected  java.lang.Thread hookThread
          Shutdown hook to make sure memory is saved to file when VM shuts down.
protected  java.util.TreeMap<C,java.lang.String> idMap
          Current live ID to filename Map.
protected  java.util.TreeMap<C,java.lang.String> idMapWrk
          Current work ID to filename Map.
protected static java.lang.String ITEM_FOLDER
          Folder name where to store items.
protected static java.lang.String LIVE_FOLDER
          Folder name where to store live data.
protected  java.io.File liveDeleted
          Live deleted ids folder.
protected  java.io.File liveItems
          Live items folder.
protected static long MAX_CACHE_TIME
          Maximum amount of time an item will stay cached before being uncached automatically.
protected static int MAX_ITEM_CACHED
          Maximum number of items to cache.
protected static int MAX_LOADTHREAD
          Maximum number of items to cache.
protected static int MAX_RETRY
          Maximum number of save retries when encountering ConcurrentModificationException.
protected  int pendingSaves
          Number of pending save or delete operations.
protected  java.util.ArrayList<java.lang.Runnable> pendingTasks
          List of pending tasks to execute.
protected  java.util.TreeMap<C,Item<T,C>> recentMap
          Recently loaded cache.
protected  java.util.TreeMap<java.lang.String,C> revIdMap
          Current live filename to ID map.
protected  java.util.TreeMap<java.lang.String,C> revIdMapWrk
          Current work filename to ID map.
protected  boolean runningPendingTasks
          Flag for performing pending tasks.
protected  java.lang.Object saveLock
          Lock used to avoid concurrent saving
protected static java.lang.String VERSION_FOLDER
          Folder name where to store versions.
protected static java.lang.String WORK_FOLDER
          Folder name where to store work data not yet committed.
protected  java.io.File workDeleted
          Work deleted ids folder.
protected  java.io.File workItems
          Work items folder.
 
Constructor Summary
Container(java.lang.String dbName, long delay, long saveInterval)
          Constructor.
 
Method Summary
protected  void backup()
          Backup the data and ID map databases.
 void batchDelete(Item<T,C>[] items, boolean keepTrack)
          Delete an array of items all at once, items which are not found will simply be ignored.
 void batchMerge(C[] ids)
          Merge an array of items all at once, items which are not found will simply be ignored.
 void batchSave(Item<T,C>[] items, boolean abortOnDuplicate)
          Save array of items all at once.
protected  void cacheItem(C id, Item<T,C> it)
          Add an item to the cache.
 void clear()
          Clear all data in the container.
 void close()
          Close the container.
 void close(boolean removeHook)
           
 void commit()
          Commit all changes.
 void delete(Item<T,C> it, boolean keepTrack)
          Delete the item.
 C[] deletedIds()
          Retrieve list of IDs that have been deleted.
 C[] deletedIdsSince(long timestamp)
          Retrieve a list of IDs that have been deleted since the given timestamp.
 void duplicate(IContainer<T,C> cont)
          Converts the given IContainer into an hessian implementation.
protected  C filenameToId(java.lang.String filename)
          Generate ID from filename.
protected  java.lang.String idToFilename(C id)
          Generate filename for given ID.
 boolean inUse(C id)
          Determine if the given id is in use.
 boolean isClosed()
          Determine if the container has already been closed.
 C[] listModifiedSince(long timestamp)
          List all items modified since the given timestamp.
 Item<T,C>[] load(C[] ids, boolean allVersions)
          Load multiple items at once, using the specified ids.
 Item<T,C> load(C id, boolean allVersions)
          Load an item using its id.
 Item<T,C>[] modifiedSince(long timestamp, boolean allVersions)
          Load all items modified since the given timestamp.
protected  void populateDeletedMap()
          Populate the deleted ID map from files.
protected  void populateIdMap()
          Populate the ID map from files.
 void release(java.lang.Object obj)
          Release the object so it can be garbage collected immediately.
 void rollback()
          Rollback and discard all changes since last commit.
 void save(Item<T,C> it)
          Save the item.
 java.lang.String toString()
          Returns a string representation of this Container.
protected  void uncacheItem(C id)
          Remove an item from cache.
 void waitForPendingSaves()
          Wait for all saves and deletes pending tasks to finish.
 void waitForPendingTasks()
          Wait for all tasks pending tasks to finish.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

EMPTY_COMPARABLE_ARRAY

protected static final java.lang.Comparable<?>[] EMPTY_COMPARABLE_ARRAY
Empty Comparable array.


ITEM_FOLDER

protected static final java.lang.String ITEM_FOLDER
Folder name where to store items.

See Also:
Constant Field Values

VERSION_FOLDER

protected static final java.lang.String VERSION_FOLDER
Folder name where to store versions.

See Also:
Constant Field Values

DELETED_FOLDER

protected static final java.lang.String DELETED_FOLDER
Folder name where to store deleted ids.

See Also:
Constant Field Values

LIVE_FOLDER

protected static final java.lang.String LIVE_FOLDER
Folder name where to store live data.

See Also:
Constant Field Values

WORK_FOLDER

protected static final java.lang.String WORK_FOLDER
Folder name where to store work data not yet committed.

See Also:
Constant Field Values

FOLDER_FILTER

protected static final DirFileFilter FOLDER_FILTER
FileFilter accepting folder files.


MAX_ITEM_CACHED

protected static final int MAX_ITEM_CACHED
Maximum number of items to cache.


MAX_LOADTHREAD

protected static final int MAX_LOADTHREAD
Maximum number of items to cache.


MAX_RETRY

protected static final int MAX_RETRY
Maximum number of save retries when encountering ConcurrentModificationException.


MAX_CACHE_TIME

protected static final long MAX_CACHE_TIME
Maximum amount of time an item will stay cached before being uncached automatically.


dbName

protected java.lang.String dbName
Name of the DB.


closed

protected boolean closed
Flag to indicate if DB is closed.


liveItems

protected java.io.File liveItems
Live items folder.


liveDeleted

protected java.io.File liveDeleted
Live deleted ids folder.


workItems

protected java.io.File workItems
Work items folder.


workDeleted

protected java.io.File workDeleted
Work deleted ids folder.


idMap

protected java.util.TreeMap<C extends java.lang.Comparable<? super C>,java.lang.String> idMap
Current live ID to filename Map.


revIdMap

protected java.util.TreeMap<java.lang.String,C extends java.lang.Comparable<? super C>> revIdMap
Current live filename to ID map.


deletedMap

protected java.util.TreeMap<C extends java.lang.Comparable<? super C>,java.lang.String> deletedMap
Current live deleted ID to filename Map.


idMapWrk

protected java.util.TreeMap<C extends java.lang.Comparable<? super C>,java.lang.String> idMapWrk
Current work ID to filename Map.


revIdMapWrk

protected java.util.TreeMap<java.lang.String,C extends java.lang.Comparable<? super C>> revIdMapWrk
Current work filename to ID map.


deletedMapWrk

protected java.util.TreeMap<C extends java.lang.Comparable<? super C>,java.lang.String> deletedMapWrk
Current work deleted ID to filename Map.


recentMap

protected java.util.TreeMap<C extends java.lang.Comparable<? super C>,Item<T extends java.lang.Comparable<? super T> & java.lang.Cloneable & java.io.Serializable,C extends java.lang.Comparable<? super C>>> recentMap
Recently loaded cache.


cachedIds

protected java.util.ArrayList<C extends java.lang.Comparable<? super C>> cachedIds
IDs kept in cache.


counter

protected long counter
Next filename to assign.


saveLock

protected final java.lang.Object saveLock
Lock used to avoid concurrent saving


pendingTasks

protected java.util.ArrayList<java.lang.Runnable> pendingTasks
List of pending tasks to execute.


closing

protected boolean closing
Flag for closing.


runningPendingTasks

protected boolean runningPendingTasks
Flag for performing pending tasks.


pendingSaves

protected int pendingSaves
Number of pending save or delete operations.


hookThread

protected transient java.lang.Thread hookThread
Shutdown hook to make sure memory is saved to file when VM shuts down.

Constructor Detail

Container

public Container(java.lang.String dbName,
                 long delay,
                 long saveInterval)
          throws java.io.IOException
Constructor.

Parameters:
dbName - Name of the database.
delay - Delay before the first save.
saveInterval - Interval (in ms) between periodic replication to backup database.
Throws:
java.io.IOException
Method Detail

backup

protected void backup()
Backup the data and ID map databases.


batchDelete

public void batchDelete(Item<T,C>[] items,
                        boolean keepTrack)
                 throws java.io.IOException
Description copied from interface: IContainer
Delete an array of items all at once, items which are not found will simply be ignored.

Specified by:
batchDelete in interface IContainer<T extends java.lang.Comparable<? super T> & java.lang.Cloneable & java.io.Serializable,C extends java.lang.Comparable<? super C>>
Parameters:
items - Items to be deleted.
keepTrack - Whether or not the ID should be kept in history.
Throws:
java.io.IOException
See Also:
org.ov4j.IContainer#batchDelete(org.ov4j.data.Item[], boolean)

batchMerge

public void batchMerge(C[] ids)
                throws java.io.IOException
Description copied from interface: IContainer
Merge an array of items all at once, items which are not found will simply be ignored.

Specified by:
batchMerge in interface IContainer<T extends java.lang.Comparable<? super T> & java.lang.Cloneable & java.io.Serializable,C extends java.lang.Comparable<? super C>>
Parameters:
ids - IDs to be merged.
Throws:
java.io.IOException
See Also:
IContainer.batchMerge(java.lang.Comparable[])

batchSave

public void batchSave(Item<T,C>[] items,
                      boolean abortOnDuplicate)
               throws java.io.IOException
Description copied from interface: IContainer
Save array of items all at once. If abortOnDuplicate is set to true, data will not be saved if 1 or more item ids are in use.

Specified by:
batchSave in interface IContainer<T extends java.lang.Comparable<? super T> & java.lang.Cloneable & java.io.Serializable,C extends java.lang.Comparable<? super C>>
Parameters:
items - Array of items to be saved.
abortOnDuplicate - Whether or not saving should abort when an item has an ID already in use.
Throws:
java.io.IOException
See Also:
org.ov4j.IContainer#batchSave(org.ov4j.data.Item[], boolean)

cacheItem

protected void cacheItem(C id,
                         Item<T,C> it)
Add an item to the cache.

Parameters:
id - ID of the item.
it - Item to cache.

clear

public void clear()
Description copied from interface: IContainer
Clear all data in the container.

Specified by:
clear in interface IContainer<T extends java.lang.Comparable<? super T> & java.lang.Cloneable & java.io.Serializable,C extends java.lang.Comparable<? super C>>
See Also:
IContainer.clear()

close

public void close()
           throws java.io.IOException
Description copied from interface: IContainer
Close the container.

Specified by:
close in interface IContainer<T extends java.lang.Comparable<? super T> & java.lang.Cloneable & java.io.Serializable,C extends java.lang.Comparable<? super C>>
Throws:
java.io.IOException
See Also:
IContainer.close()

close

public void close(boolean removeHook)
           throws java.io.IOException
Throws:
java.io.IOException
See Also:
IContainer.close()

commit

public void commit()
Description copied from interface: IContainer
Commit all changes.

Specified by:
commit in interface IContainer<T extends java.lang.Comparable<? super T> & java.lang.Cloneable & java.io.Serializable,C extends java.lang.Comparable<? super C>>
See Also:
IContainer.commit()

delete

public void delete(Item<T,C> it,
                   boolean keepTrack)
            throws java.io.IOException
Description copied from interface: IContainer
Delete the item.

Specified by:
delete in interface IContainer<T extends java.lang.Comparable<? super T> & java.lang.Cloneable & java.io.Serializable,C extends java.lang.Comparable<? super C>>
Parameters:
it - Item to be deleted.
keepTrack - Whether or not the ID should be kept in history.
Throws:
java.io.IOException
See Also:
IContainer.delete(org.ov4j.data.Item, boolean)

deletedIds

public C[] deletedIds()
Description copied from interface: IContainer
Retrieve list of IDs that have been deleted.

Specified by:
deletedIds in interface IContainer<T extends java.lang.Comparable<? super T> & java.lang.Cloneable & java.io.Serializable,C extends java.lang.Comparable<? super C>>
Returns:
All IDs deleted.
See Also:
IContainer.deletedIds()

deletedIdsSince

public C[] deletedIdsSince(long timestamp)
                                                            throws java.io.IOException
Description copied from interface: IContainer
Retrieve a list of IDs that have been deleted since the given timestamp.

Specified by:
deletedIdsSince in interface IContainer<T extends java.lang.Comparable<? super T> & java.lang.Cloneable & java.io.Serializable,C extends java.lang.Comparable<? super C>>
Parameters:
timestamp - Timestamp to use.
Returns:
All IDS deleted since the given timestamp.
Throws:
java.io.IOException
See Also:
IContainer.deletedIdsSince(long)

duplicate

public void duplicate(IContainer<T,C> cont)
               throws java.io.IOException
Converts the given IContainer into an hessian implementation.

Specified by:
duplicate in interface IContainer<T extends java.lang.Comparable<? super T> & java.lang.Cloneable & java.io.Serializable,C extends java.lang.Comparable<? super C>>
Parameters:
cont - IContainer to convert.
Throws:
java.io.IOException

filenameToId

protected C filenameToId(java.lang.String filename)
                                                          throws java.io.IOException
Generate ID from filename.

Parameters:
filename - Filename from which to generate ID.
Returns:
ID generated
Throws:
java.io.IOException

idToFilename

protected java.lang.String idToFilename(C id)
                                 throws java.io.IOException
Generate filename for given ID.

Parameters:
id - ID for which to generate a filename.
Returns:
Filename generated.
Throws:
java.io.IOException

inUse

public boolean inUse(C id)
Description copied from interface: IContainer
Determine if the given id is in use.

Specified by:
inUse in interface IContainer<T extends java.lang.Comparable<? super T> & java.lang.Cloneable & java.io.Serializable,C extends java.lang.Comparable<? super C>>
Parameters:
id - ID to check.
Returns:
True if the ID is already used in the system.
See Also:
IContainer.inUse(java.lang.Comparable)

isClosed

public boolean isClosed()
Description copied from interface: IContainer
Determine if the container has already been closed.

Specified by:
isClosed in interface IContainer<T extends java.lang.Comparable<? super T> & java.lang.Cloneable & java.io.Serializable,C extends java.lang.Comparable<? super C>>
Returns:
True if the container has been closed.
See Also:
IContainer.isClosed()

listModifiedSince

public C[] listModifiedSince(long timestamp)
                                                              throws java.io.IOException
Description copied from interface: IContainer
List all items modified since the given timestamp.

Specified by:
listModifiedSince in interface IContainer<T extends java.lang.Comparable<? super T> & java.lang.Cloneable & java.io.Serializable,C extends java.lang.Comparable<? super C>>
Parameters:
timestamp - Timestamp to use.
Returns:
An array of IDs for items modified since specified timestamp.
Throws:
java.io.IOException
See Also:
IContainer.listModifiedSince(long)

load

public Item<T,C> load(C id,
                      boolean allVersions)
                                                                                                                                          throws java.io.IOException
Description copied from interface: IContainer
Load an item using its id.

Specified by:
load in interface IContainer<T extends java.lang.Comparable<? super T> & java.lang.Cloneable & java.io.Serializable,C extends java.lang.Comparable<? super C>>
Parameters:
id - ID to look for.
allVersions - Whether or not all versions should be loaded.
Returns:
The item with specified ID.
Throws:
java.io.IOException
See Also:
IContainer.load(java.lang.Comparable, boolean)

load

public Item<T,C>[] load(C[] ids,
                        boolean allVersions)
                                                                                                                                            throws java.io.IOException
Description copied from interface: IContainer
Load multiple items at once, using the specified ids.

Specified by:
load in interface IContainer<T extends java.lang.Comparable<? super T> & java.lang.Cloneable & java.io.Serializable,C extends java.lang.Comparable<? super C>>
Parameters:
ids - Array of IDs to look for.
allVersions - Whether or not all versions should be loaded.
Returns:
An array of items with specified IDs, not necessarily in the same order.
Throws:
java.io.IOException
See Also:
IContainer.load(java.lang.Comparable[], boolean)

modifiedSince

public Item<T,C>[] modifiedSince(long timestamp,
                                 boolean allVersions)
                                                                                                                                                     throws java.io.IOException
Description copied from interface: IContainer
Load all items modified since the given timestamp.

Specified by:
modifiedSince in interface IContainer<T extends java.lang.Comparable<? super T> & java.lang.Cloneable & java.io.Serializable,C extends java.lang.Comparable<? super C>>
Parameters:
timestamp - Timestamp to use.
allVersions - Whether or not all versions should be loaded.
Returns:
An array of items modified since specified timestamp.
Throws:
java.io.IOException
See Also:
IContainer.modifiedSince(long, boolean)

populateDeletedMap

protected void populateDeletedMap()
                           throws java.io.IOException
Populate the deleted ID map from files.

Throws:
java.io.IOException

populateIdMap

protected void populateIdMap()
                      throws java.io.IOException
Populate the ID map from files.

Throws:
java.io.IOException

release

public void release(java.lang.Object obj)
Description copied from interface: IContainer
Release the object so it can be garbage collected immediately.

Specified by:
release in interface IContainer<T extends java.lang.Comparable<? super T> & java.lang.Cloneable & java.io.Serializable,C extends java.lang.Comparable<? super C>>
Parameters:
obj - Object to release.
See Also:
IContainer.release(java.lang.Object)

rollback

public void rollback()
Description copied from interface: IContainer
Rollback and discard all changes since last commit.

Specified by:
rollback in interface IContainer<T extends java.lang.Comparable<? super T> & java.lang.Cloneable & java.io.Serializable,C extends java.lang.Comparable<? super C>>
See Also:
IContainer.rollback()

save

public void save(Item<T,C> it)
          throws java.io.IOException
Description copied from interface: IContainer
Save the item.

Specified by:
save in interface IContainer<T extends java.lang.Comparable<? super T> & java.lang.Cloneable & java.io.Serializable,C extends java.lang.Comparable<? super C>>
Parameters:
it - Item to be saved.
Throws:
java.io.IOException
See Also:
IContainer.save(org.ov4j.data.Item)

toString

public java.lang.String toString()
Returns a string representation of this Container.

Overrides:
toString in class java.lang.Object
Returns:
String representation of the Container.

uncacheItem

protected void uncacheItem(C id)
Remove an item from cache.

Parameters:
id - ID of the item.

waitForPendingSaves

public void waitForPendingSaves()
Description copied from interface: IContainer
Wait for all saves and deletes pending tasks to finish.

Specified by:
waitForPendingSaves in interface IContainer<T extends java.lang.Comparable<? super T> & java.lang.Cloneable & java.io.Serializable,C extends java.lang.Comparable<? super C>>
See Also:
IContainer.waitForPendingSaves()

waitForPendingTasks

public void waitForPendingTasks()
Description copied from interface: IContainer
Wait for all tasks pending tasks to finish.

Specified by:
waitForPendingTasks in interface IContainer<T extends java.lang.Comparable<? super T> & java.lang.Cloneable & java.io.Serializable,C extends java.lang.Comparable<? super C>>
See Also:
IContainer.waitForPendingTasks()


Copyright © 2004-2007 Steve Molloy. All Rights Reserved.