Package org.dlese.dpc.datamgr
Class SerializedDataManager
java.lang.Object
org.dlese.dpc.datamgr.DataManager
org.dlese.dpc.datamgr.SerializedDataManager
- All Implemented Interfaces:
Serializable
- Direct Known Subclasses:
SimpleDataStore
A Hashtable-like interface for reading and writing persistent Java objects from and to
disc. Objects are stored in serialized form on disc - any object that implements
Serializable can be stored in this DataManager for persistent
retrieval. The objects can also be held in a RAM cache to provide faster retrieval.
Serialized objects are stored in a directory specified at the time of construction.
Objects stored in a SerializedDataManager can be of mixed type. This class is safe to
use with multiple process and/or JVMs that access the same dataPath concurrently.- Author:
- John Weatherley
- See Also:
-
Field Summary
FieldsModifier and TypeFieldDescriptionprotected static HashtableConverts a string that contains escaped hex encoding of the form %HEX back to plain text.Provides the inverse operation of the method encodeFileName()protected StringThe path to the data storeprotected static booleanDESCRIPTIONprotected HashtableDESCRIPTIONprotected longA unique id for locking purposesprotected longDESCRIPTIONprotected HashtableDESCRIPTIONprotected booleanDESCRIPTIONprotected final booleanDESCRIPTION -
Constructor Summary
ConstructorsConstructorDescriptionConstructor for the SerializedDataManager object, restores a serialized SerializedDataManager.SerializedDataManager(String dataPath, boolean useCache) Constructs a new SerializedDataManager that reads and writes data to the given dataPath directory. -
Method Summary
Modifier and TypeMethodDescriptionprotected StringdecodeFileName(String name) Decodes a String that was encoded using the methodencodeFileName(String name).booleanDeletes an existing data object iff the object is not locked by another user.booleanDeletes an existing data object, assuming the caller had requested and recieved the necessary lock.protected StringencodeFileName(String name) Encodes a String so that it is suitable for use as a file name by encoding all non letter or digit chars such as "/" and ":" into escaped hex values of the form _HEX.Retrieves a single data object.Retrieves aListof data objects.Retrieves a copy of a single data object, sutable for modifying.Retrieves aListof copied data objects, suitable for modifying.String[]getIDs()Retrieves aString []of all IDs that exist in this SerializedDataManager.String[]Retrieves aString []of all IDs that exists in this SerializedDataManager sorted lexagraphically.longReturns the time that the object denoted by this oid was last modified.protected booleanGets an internal read/write lock for a given object in the this SerializedDataManager.protected StringGets the nextLockKey attribute of the SerializedDataManager objectprotected intReturn the number of files in this DataManager as reported by the file system.longGet the number of records in this SerializedDataManager.booleanDetermine whether a given object is locked by another user.booleanisValidLock(String oid, String lockKey) Determine whether a given object is locked with the given key.Get a user-requested lock for a data object, preventing it from being written by other threads that are using this SerializedDataManager.booleanDetermines whether an object with the given oid exists in the DataManager.protected voidPrint a line to standard output.Adds a new object of data.protected voidreleaseLock(String objectPath) Release the read/write lock.Removes an existing data object iff the object is not locked by another user.Removes an existing data object, assuming the caller had requested and recieved the necessary lock for the object.protected ObjectrestoreSerializedObject(String oid, boolean fromCache) Restores a Serialized object from the given file path, pulling if from a RAM cache if caching indicated at construction time.protected voidserailizeObject(String path, Object obj) Serializes an object to the given file path, placing it in a RAM cache if caching indicated at construction time.static voidsetDebug(boolean isDebugOutput) Sets the debug attribute of the SerializedDataManager classbooleanRemove the lock on a data object, if it exists.Updates a new object of data iff the object is not locked by another user.Updates a new object of data, assuming the caller had requested and recieved the necessary lock.
-
Field Details
-
dataPath
The path to the data store -
debug
protected static boolean debugDESCRIPTION -
locks
DESCRIPTION -
objectCache
DESCRIPTION -
num_records
protected long num_recordsDESCRIPTION -
useFileLocks
protected final boolean useFileLocksDESCRIPTION- See Also:
-
useCache
protected boolean useCacheDESCRIPTION -
codes
Converts a string that contains escaped hex encoding of the form %HEX back to plain text.Provides the inverse operation of the method encodeFileName() -
nextLockKey
protected long nextLockKeyA unique id for locking purposes
-
-
Constructor Details
-
SerializedDataManager
Constructs a new SerializedDataManager that reads and writes data to the given dataPath directory. Allows control over whether or not to hold a cache of the objects in RAM memory for faster retrieval. Note that if caching is turned on in this SerializedDataManager, the objects that are returned from theget(String)andget(List)methods are not safe to modify. Specifically, any changes made to the objects will be reflected in subsequent calls to those methods. To get a copy of the data items that are safe to modify the caller should use thegetCopy(String)orgetCopy(List)methods instead. If caching is not turned on, the effect of the get methods are the same as the getCopy methods.This class is safe to use with multiple process and/or JVMs that access the same dataPath.
- Parameters:
dataPath- The directory where the serialzed objects are stored.useCache- Indicates whether to cache the objects in RAM memory for faster retrievel.- Throws:
Exception- If error.
-
SerializedDataManager
public SerializedDataManager()Constructor for the SerializedDataManager object, restores a serialized SerializedDataManager.
-
-
Method Details
-
get
Retrieves a single data object. Note that if caching is turned on in this SerializedDataManager, the object that is returned is not safe to modify. Specifically, if caching is enabled, any changes made to that object will be reflected in subsequent calls to this method and theget(List)method. To get a copy of a data item that is safe to modify use thegetCopy(String)method instead. If caching is not enabled, the effect of this method is the same asgetCopy(String).- Specified by:
getin classDataManager- Parameters:
oid- The data identifier- Returns:
Objectof data- Throws:
OIDDoesNotExistException- If no object exists for the given uid.- See Also:
-
getCopy
Retrieves a copy of a single data object, sutable for modifying. The object that is returned is safe for modifying without affecting the data that is in this SerializedDataManager.- Parameters:
oid- The data identifier.- Returns:
- A copy of an
Objectof data that is in this SerializedDataManager. - Throws:
OIDDoesNotExistException- If no object exists for the given uid.- See Also:
-
get
Retrieves aListof data objects. Note that if caching is turned on in this SerializedDataManager, the objects that are returned are not safe to modify. Specifically, if caching is enabled, any changes made to the objects will be reflected in subsequent calls to this method and theget(String)method. To get a copy of a data items that are safe to modify use thegetCopy(List)method instead. If caching is not ebabled, the effect of this method is the same asgetCopy(List).- Specified by:
getin classDataManager- Parameters:
oids-Listof data identifiers- Returns:
Listof corresponding data objects- Throws:
OIDDoesNotExistException- If no object exists for the given uid.- See Also:
-
getCopy
Retrieves aListof copied data objects, suitable for modifying. The objects that are returned are safe for modifying without affecting the data that is in this SerializedDataManager.- Parameters:
oids-Listof data identifiers.- Returns:
Listof corresponding data objects copies.- Throws:
OIDDoesNotExistException- If no object exists for the given uid.- See Also:
-
getIDs
Retrieves aString []of all IDs that exist in this SerializedDataManager. Results are not guaranteed to be in lexagraphical order, however the order is guaranteed to be in the same order from one invocation to the next.- Returns:
String []of the IDs in this SerializedDataManager
-
getIDsSorted
Retrieves aString []of all IDs that exists in this SerializedDataManager sorted lexagraphically.- Returns:
- A lexagraphically sorted
String []of the IDs in this SerializedDataManager
-
getNumRecords
public long getNumRecords()Get the number of records in this SerializedDataManager.- Returns:
- The number of records in this SerializedDataManager.
-
put
public Object put(String oid, Object obj) throws OIDAlreadyExistsException, ObjectNotSupportedException Adds a new object of data. This method is safe for concurrent use among multiple processes and JVMs accessing the same SerializedDataManager.- Specified by:
putin classDataManager- Parameters:
oid- The unique identifier that references the new data objectobj- The new data object- Returns:
- The added data object iff successful, otherwise null
- Throws:
OIDAlreadyExistsException- If an object with given oid already exists in the data store.ObjectNotSupportedException- If the object type is not suppored.
-
getLock
Gets an internal read/write lock for a given object in the this SerializedDataManager. This method, together withreleaseLock(String objectPath), provide reliable locking method for use across multiple processes or JVMs that may access the SDM data concurrently.- Parameters:
objectPath- The absolute path to the data object being stored.- Returns:
- True if the lock was obtained.
-
releaseLock
Release the read/write lock.- Parameters:
objectPath- The absolute path to the data object being stored.
-
remove
public Object remove(String oid, String lockKey) throws OIDDoesNotExistException, InvalidLockException Removes an existing data object, assuming the caller had requested and recieved the necessary lock for the object. After successful completion the object is removed from the DataManager and returned to the caller.- Specified by:
removein classDataManager- Parameters:
oid- The unique identifier that references the data object to be removedlockKey- The key corresponding to the lock on this data object- Returns:
- The removed data object iff successful, otherwise null
- Throws:
OIDDoesNotExistException- If no object exists for the given uid.InvalidLockException- If the lock provided for the object is not valid.
-
remove
Removes an existing data object iff the object is not locked by another user. If the object is locked a LockNotAvailableException is thrown. After successful completion the object is removed from the DataManager and returned to the caller.- Specified by:
removein classDataManager- Parameters:
oid- The unique identifier that references the data object to be removed- Returns:
- The Object that was removed.
- Throws:
OIDDoesNotExistException- If no object exists for the given uid.LockNotAvailableException- If another user currently has a lock on this object.
-
delete
public boolean delete(String oid, String lockKey) throws OIDDoesNotExistException, InvalidLockException Deletes an existing data object, assuming the caller had requested and recieved the necessary lock. Similar toremove()except no object is returned and thus is more efficient if an object is not required.- Specified by:
deletein classDataManager- Parameters:
oid- The unique identifier that references the data object to be deletedlockKey- The key corresponding to the lock on this data object- Returns:
- DESCRIPTION
- Throws:
OIDDoesNotExistException- If no object exists for the given uid.InvalidLockException- If the lock provided for the object is not valid.
-
delete
Deletes an existing data object iff the object is not locked by another user. If the object is locked a LockNotAvailableException is thrown. Similar toremove()except no object is returned and thus is more efficient. After successful completion the object is deleted from the DataManager.- Specified by:
deletein classDataManager- Parameters:
oid- The unique identifier that references the data object to be deleted- Returns:
- DESCRIPTION
- Throws:
OIDDoesNotExistException- If no object exists for the given uid.LockNotAvailableException- If another user currently has a lock on this object.
-
update
public Object update(String oid, Object obj, String lockKey) throws OIDDoesNotExistException, ObjectNotSupportedException, InvalidLockException Updates a new object of data, assuming the caller had requested and recieved the necessary lock. The caller retains the lock after this method returns.- Specified by:
updatein classDataManager- Parameters:
oid- The unique identifier that references the data object to be updatedobj- The new updated data objectlockKey- The key corresponding to the lock on this data object- Returns:
- The updated data object, if successful otherwise null
- Throws:
OIDDoesNotExistException- If no object exists for the given uid.ObjectNotSupportedException- If the object type is not suppored.InvalidLockException- If the lock provided for the object is not valid.
-
update
public Object update(String oid, Object obj) throws OIDDoesNotExistException, ObjectNotSupportedException, LockNotAvailableException Updates a new object of data iff the object is not locked by another user. If the object is locked a LockNotAvailableException is thrown.- Specified by:
updatein classDataManager- Parameters:
oid- The unique identifier that references the data object to be updatedobj- The new updated data object- Returns:
- The updated data object, if successful otherwise null
- Throws:
OIDDoesNotExistException- If no object exists for the given uid.ObjectNotSupportedException- If the object type is not suppored.LockNotAvailableException- If another user currently has a lock on this object.
-
lock
Get a user-requested lock for a data object, preventing it from being written by other threads that are using this SerializedDataManager. Note that this type of locking only works within a single instance of the SerializedDataManager. Other instances of SerializedDataManager will not know about this lock.- Specified by:
lockin classDataManager- Parameters:
oid- The unique identifier that references the data object to be locked- Returns:
- The key used to unlock this object.
- Throws:
OIDDoesNotExistException- If no object exists for the given uid.LockNotAvailableException- If another user currently has a lock on this object.
-
getLastModifiedDate
Returns the time that the object denoted by this oid was last modified.- Parameters:
oid- The unique identifier that references the data object- Returns:
- A long value representing the time the file was last modified, measured in milliseconds since the epoch (00:00:00 GMT, January 1, 1970)
- Throws:
OIDDoesNotExistException- If no object exists for the given uid.
-
oidExists
Determines whether an object with the given oid exists in the DataManager.- Specified by:
oidExistsin classDataManager- Parameters:
oid- The unique identifier that references the data object- Returns:
- True iff the given object exists in this DataManager
-
isLocked
Determine whether a given object is locked by another user.- Specified by:
isLockedin classDataManager- Parameters:
oid- The unique identifier that references the data object- Returns:
- True iff the object referred to by this oid is locked
-
isValidLock
Determine whether a given object is locked with the given key.- Specified by:
isValidLockin classDataManager- Parameters:
oid- The unique identifier that references the data objectlockKey- The lock key- Returns:
- True iff the object referred to by this oid is locked with the given key
-
unlock
public boolean unlock(String oid, String lockKey) throws OIDDoesNotExistException, InvalidLockException Remove the lock on a data object, if it exists.- Specified by:
unlockin classDataManager- Parameters:
oid- The unique identifier that references the locked data objectlockKey- The key corresponding to the lock on this data object- Returns:
- true iff the lock has been removed for the given object regardless of whether it had been locked prior to calling this method.
- Throws:
OIDDoesNotExistException- If no object exists for the given uid.InvalidLockException- If the lock provided for the object is not valid.
-
serailizeObject
protected void serailizeObject(String path, Object obj) throws IOException, InvalidClassException, NotSerializableException Serializes an object to the given file path, placing it in a RAM cache if caching indicated at construction time.- Parameters:
path- DESCRIPTIONobj- DESCRIPTION- Throws:
IOException- DESCRIPTIONInvalidClassException- DESCRIPTIONNotSerializableException- DESCRIPTION
-
restoreSerializedObject
protected Object restoreSerializedObject(String oid, boolean fromCache) throws IOException, ClassNotFoundException Restores a Serialized object from the given file path, pulling if from a RAM cache if caching indicated at construction time. Returns null if no file was found for the given path.- Parameters:
oid- DESCRIPTIONfromCache- DESCRIPTION- Returns:
- DESCRIPTION
- Throws:
IOException- DESCRIPTIONClassNotFoundException- DESCRIPTION
-
encodeFileName
Encodes a String so that it is suitable for use as a file name by encoding all non letter or digit chars such as "/" and ":" into escaped hex values of the form _HEX. Note: Unix commands cannot include the following chars: * ? ! | \ / ' " { } invalid input: '<' > ; , ^ ( ) $ ~ Windows file names may not contain: \ / : * ? " invalid input: '<' > | nor does Win 2000 like it when a . is at the end of the filename (it removes it when returning the file name).- Parameters:
name- The String to encode.- Returns:
- An encoded String.
-
decodeFileName
Decodes a String that was encoded using the methodencodeFileName(String name).- Parameters:
name- The string to decods.- Returns:
- A decoded String.
-
getNumFiles
protected int getNumFiles()Return the number of files in this DataManager as reported by the file system.- Returns:
- int The number of files currently in the DataManager
-
getNextLockKey
Gets the nextLockKey attribute of the SerializedDataManager object- Returns:
- The nextLockKey value
-
setDebug
public static void setDebug(boolean isDebugOutput) Sets the debug attribute of the SerializedDataManager class- Parameters:
isDebugOutput- The new debug value
-
prtln
Print a line to standard output.- Parameters:
s- DESCRIPTION
-