net.sourceforge.domian.repository
Class InMemoryAndXStreamXmlFileRepository<T extends Entity>

java.lang.Object
  extended by net.sourceforge.domian.repository.AbstractRepository<T>
      extended by net.sourceforge.domian.repository.AbstractDomianCoreRepository<T>
          extended by net.sourceforge.domian.repository.InMemoryAndXStreamXmlFileRepository<T>
All Implemented Interfaces:
HumanReadableFormatRepository<T>, PersistentRepository<T>, Repository<T>, TextualFormatRepository<T>

public class InMemoryAndXStreamXmlFileRepository<T extends Entity>
extends AbstractDomianCoreRepository<T>
implements HumanReadableFormatRepository<T>

An in-memory repository with persistence capabilities. The persistence part is backed by XStream functionality.

The only persistence definition supported by this repository is PersistenceDefinition.INMEMORY_AND_FILE. All entities in this repository are persisted into one single file. The typical disc footprint is 1KB per entity. The persistence format is human readable XStream XML. The file encoding is UTF-8.

File-based stores counts as persistent storage media, although the nature of the synchronous writings to disc obviously depends on the file system implementations involved.

Since:
0.4
Author:
Eirik Torske
See Also:
XStream, PersistenceDefinition.INMEMORY_AND_FILE

Field Summary
protected static java.lang.String DEFAULT_REPOSITORY_ROOT_DIR_NAME
           
protected static java.lang.String DEFAULT_REPOSITORY_ROOT_FILE_NAME
           
protected static java.lang.String DEFAULT_REPOSITORY_ROOT_PATH
           
protected  InMemoryRepository<T> inMemoryRepositoryDelegate
          The repository delegate dealing with the in-memory stuff.
protected  java.lang.String repositoryId
          The repository-ID the name of the repository.
protected  java.lang.String repositoryRootPath
          For file-based repositories the repositoryRootPath is the absolute path for all repositories of the same type.
protected static java.lang.String XSTREAM_XML_FILE_SUFFIX
           
 
Fields inherited from class net.sourceforge.domian.repository.AbstractDomianCoreRepository
DEFAULT_DOMIAN_ROOT_DIR_NAME, DEFAULT_DOMIAN_ROOT_PATH, log, synchronizer
 
Fields inherited from class net.sourceforge.domian.repository.AbstractRepository
supportsRecursiveIndexing, usesNativePartitioningSupport
 
Constructor Summary
InMemoryAndXStreamXmlFileRepository(java.lang.String repositoryId)
           
InMemoryAndXStreamXmlFileRepository(java.lang.String repositoryRootPath, java.lang.String repositoryId)
           
InMemoryAndXStreamXmlFileRepository(java.lang.String repositoryRootPath, java.lang.String repositoryId, Synchronizer synchronizer)
           
InMemoryAndXStreamXmlFileRepository(java.lang.String repositoryId, Synchronizer synchronizer)
           
 
Method Summary
protected  void call(java.util.concurrent.Callable<T> callable)
          Added for completeness.
protected
<T> T
callConcurrently(java.util.concurrent.Callable<T> callable)
          Convenience method for running a Callable in a concurrent manner.
protected
<T> T
callConcurrentlyInNewThread(java.util.concurrent.Callable<T> callable)
           
protected
<T> T
callConcurrentlyWithRetry(java.util.concurrent.Callable<T> callable)
          Convenience method for running a Callable in a concurrent manner.
protected
<T> T
callExclusively(java.util.concurrent.Callable<T> callable)
          Convenience method for running a Callable in an exclusively manner.
protected
<T> T
callExclusivelyInNewThread(java.util.concurrent.Callable<T> callable)
           
protected
<T> T
callExclusivelyWithRetry(java.util.concurrent.Callable<T> callable)
          Convenience method for running a Callable in an exclusively manner.
 void close()
          Closes the repository and releases all resources.
protected  void createRepositoryRootPathIfNotExist()
           
<V extends T>
java.util.Collection<V>
findAllEntitiesSpecifiedBy(Specification<V> specification)
          Finds and returns all entities approved by the given specification.
 java.lang.String getEncoding()
           
 java.lang.String getFormat()
           
 java.lang.Integer getMaxNumberOfRetries()
           
 EntityPersistenceMetaData getMetaDataFor(T notApplicableEntity)
           
 PersistenceDefinition getPersistenceDefinition()
           
 java.io.File getRepositoryDirectory()
          The unique file directory in which the repository data files resides.
 java.lang.String getRepositoryId()
          The repository-ID the name of the repository.
protected  java.lang.String getRepositoryPath(java.lang.String repositoryRootPath, PersistentRepository<T> repository)
           
 java.lang.String getRepositoryPathString()
           
 java.lang.String getRepositoryRootPath()
           
protected  java.lang.String getRepositoryTypeName()
           
 java.lang.Integer getRetryTimeoutInMilliseconds()
           
<V extends T>
java.util.Iterator<V>
iterateAllEntitiesSpecifiedBy(Specification<V> specification)
          Finds and returns all entities approved by the given specification.
 void load()
          The semantics of this operation depends on the Repository's PersistenceDefinition.
 void persist()
          The semantics of this operation depends on the Repository's PersistenceDefinition.
protected  void purgeRepositoryEntitiesFileOnly_IfExist()
           
protected  void purgeRepositoryFilesIfExist()
           
protected  void purgeRepositoryFilesIfExist(int filesToBePurgedLimit, java.lang.Boolean purgeRepositoryFolder, java.lang.Boolean purgeMetadata)
           
<V extends T>
void
put(V entity)
          Puts the given entity into this repository.
<V extends T>
java.lang.Boolean
remove(V entity)
          Removes the given entity from this repository.
<V extends T>
java.lang.Long
removeAllEntitiesSpecifiedBy(Specification<V> specification)
          Removes all entities approved by the given specification.
protected  void run(java.lang.Runnable runnable)
          Added for completeness.
protected  void runAsynchronously(java.lang.Runnable runnable)
          Executes the given Runnable in a fresh and independent thread.
protected  void runConcurrently(java.lang.Runnable runnable)
          Convenience method for running a Runnable in a concurrent manner.
 void setMaxNumberOfRetries(java.lang.Integer maxNumberOfRetries)
           
 void setRetryTimeoutInMilliseconds(java.lang.Integer retryTimeoutInMilliseconds)
           
<V extends T>
void
update(V entity)
          Updates an entity.
 
Methods inherited from class net.sourceforge.domian.repository.AbstractDomianCoreRepository
createUniqueSpecificationFor, getSynchronizer, makePartition, onMakePartition, setSynchronizer
 
Methods inherited from class net.sourceforge.domian.repository.AbstractRepository
contains, count, countAll, countAllEntitiesSpecifiedBy, find, findAll, findSingle, findSingleEntitySpecifiedBy, isIndexingEntitiesRecursively, isPartitioningNatively, iterate, iterateAll, putAll, remove, removeAll, update
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 
Methods inherited from interface net.sourceforge.domian.repository.TextualFormatRepository
getEncoding
 
Methods inherited from interface net.sourceforge.domian.repository.PersistentRepository
getMetaDataFor, getRepositoryDirectory, getRepositoryId
 
Methods inherited from interface net.sourceforge.domian.repository.Repository
count, countAll, countAllEntitiesSpecifiedBy, find, findAll, findSingle, findSingleEntitySpecifiedBy, isIndexingEntitiesRecursively, isPartitioningNatively, iterate, iterateAll, makePartition, putAll, remove, removeAll, update
 

Field Detail

DEFAULT_REPOSITORY_ROOT_FILE_NAME

protected static final java.lang.String DEFAULT_REPOSITORY_ROOT_FILE_NAME
See Also:
Constant Field Values

DEFAULT_REPOSITORY_ROOT_DIR_NAME

protected static final java.lang.String DEFAULT_REPOSITORY_ROOT_DIR_NAME
See Also:
Constant Field Values

DEFAULT_REPOSITORY_ROOT_PATH

protected static final java.lang.String DEFAULT_REPOSITORY_ROOT_PATH

inMemoryRepositoryDelegate

protected InMemoryRepository<T extends Entity> inMemoryRepositoryDelegate
The repository delegate dealing with the in-memory stuff.


XSTREAM_XML_FILE_SUFFIX

protected static final java.lang.String XSTREAM_XML_FILE_SUFFIX
See Also:
Constant Field Values

repositoryRootPath

protected java.lang.String repositoryRootPath
For file-based repositories the repositoryRootPath is the absolute path for all repositories of the same type.


repositoryId

protected java.lang.String repositoryId
The repository-ID the name of the repository. It should be unique within a running system.

For file-based repositories the ${repositoryRootPath}/${repositoryId} forms the absolute folder path in which the repository data resides.

Constructor Detail

InMemoryAndXStreamXmlFileRepository

public InMemoryAndXStreamXmlFileRepository(java.lang.String repositoryId)

InMemoryAndXStreamXmlFileRepository

public InMemoryAndXStreamXmlFileRepository(java.lang.String repositoryRootPath,
                                           java.lang.String repositoryId)

InMemoryAndXStreamXmlFileRepository

public InMemoryAndXStreamXmlFileRepository(java.lang.String repositoryId,
                                           Synchronizer synchronizer)

InMemoryAndXStreamXmlFileRepository

public InMemoryAndXStreamXmlFileRepository(java.lang.String repositoryRootPath,
                                           java.lang.String repositoryId,
                                           Synchronizer synchronizer)
Method Detail

getRepositoryTypeName

protected java.lang.String getRepositoryTypeName()
Returns:
the repository type name; the name of the repository implementation (e.g. Unix-name-variant of the class simple name)

iterateAllEntitiesSpecifiedBy

public <V extends T> java.util.Iterator<V> iterateAllEntitiesSpecifiedBy(Specification<V> specification)
Description copied from interface: Repository
Finds and returns all entities approved by the given specification. An iterator is returned for lazy retrieval of the entities.

Specified by:
iterateAllEntitiesSpecifiedBy in interface Repository<T extends Entity>
Parameters:
specification - the entity specification
Returns:
an iterator with all entities approved by the specification argument

findAllEntitiesSpecifiedBy

public <V extends T> java.util.Collection<V> findAllEntitiesSpecifiedBy(Specification<V> specification)
Description copied from interface: Repository
Finds and returns all entities approved by the given specification.

Specified by:
findAllEntitiesSpecifiedBy in interface Repository<T extends Entity>
Parameters:
specification - the entity specification
Returns:
a collection of all entities approved by the specification argument, or an empty set if no entities are found and approved

put

public <V extends T> void put(V entity)
Description copied from interface: Repository
Puts the given entity into this repository.

Specified by:
put in interface Repository<T extends Entity>
Parameters:
entity - the entity to be stored

update

public <V extends T> void update(V entity)
Description copied from interface: Repository
Updates an entity. When an entity is (re)-retrieved, all state changes will be visible, no matter what kinds of repository implementations being involved. Do notice that for all memory-based repositories (and possibly some delegated ones), entity updates are of course immediately visible.

(Only applicable for persistent repositories doing explicit data writes, like PersistenceDefinition.FILE, and possibly PersistenceDefinition.DELEGATED... For all other persistence definitions, this method is redundant/not applicable. Anyway, by applying this method in a consistent manner, an eventual repository migration will not need any code changes.

Specified by:
update in interface Repository<T extends Entity>

removeAllEntitiesSpecifiedBy

public <V extends T> java.lang.Long removeAllEntitiesSpecifiedBy(Specification<V> specification)
Description copied from interface: Repository
Removes all entities approved by the given specification.

Specified by:
removeAllEntitiesSpecifiedBy in interface Repository<T extends Entity>
Parameters:
specification - the entity specification
Returns:
the number of removed entities

remove

public <V extends T> java.lang.Boolean remove(V entity)
Description copied from interface: Repository
Removes the given entity from this repository.

Specified by:
remove in interface Repository<T extends Entity>
Parameters:
entity - the entity to be removed
Returns:
true if the entity is found and removed, otherwise false

getPersistenceDefinition

public PersistenceDefinition getPersistenceDefinition()
Specified by:
getPersistenceDefinition in interface PersistentRepository<T extends Entity>

getFormat

public java.lang.String getFormat()
Specified by:
getFormat in interface PersistentRepository<T extends Entity>

load

public void load()
Description copied from interface: PersistentRepository
The semantics of this operation depends on the Repository's PersistenceDefinition.

Specified by:
load in interface PersistentRepository<T extends Entity>

persist

public void persist()
Description copied from interface: PersistentRepository
The semantics of this operation depends on the Repository's PersistenceDefinition.

Specified by:
persist in interface PersistentRepository<T extends Entity>

close

public void close()
Description copied from interface: PersistentRepository
Closes the repository and releases all resources.

This method is redundant for certain repository implementations, but by using this method in a consistent manner, an eventual repository migration will not need any code changes.

Specified by:
close in interface PersistentRepository<T extends Entity>

getMetaDataFor

public EntityPersistenceMetaData getMetaDataFor(T notApplicableEntity)
Returns:
persistence meta data for given entity

createRepositoryRootPathIfNotExist

protected void createRepositoryRootPathIfNotExist()

purgeRepositoryFilesIfExist

protected void purgeRepositoryFilesIfExist()

purgeRepositoryEntitiesFileOnly_IfExist

protected void purgeRepositoryEntitiesFileOnly_IfExist()

purgeRepositoryFilesIfExist

protected void purgeRepositoryFilesIfExist(int filesToBePurgedLimit,
                                           java.lang.Boolean purgeRepositoryFolder,
                                           java.lang.Boolean purgeMetadata)

getRepositoryRootPath

public java.lang.String getRepositoryRootPath()

getRepositoryId

public java.lang.String getRepositoryId()
Description copied from interface: PersistentRepository
The repository-ID the name of the repository. It should be unique within a running system.

For file-based repositories, the repository-ID should constitute the last directory element of the repository directory.

Specified by:
getRepositoryId in interface PersistentRepository<T extends Entity>
Returns:
the repository id

getRepositoryPathString

public java.lang.String getRepositoryPathString()

getRepositoryDirectory

public java.io.File getRepositoryDirectory()
Description copied from interface: PersistentRepository
The unique file directory in which the repository data files resides.

Specified by:
getRepositoryDirectory in interface PersistentRepository<T extends Entity>
Returns:
the repository directory, if applicable

getMaxNumberOfRetries

public java.lang.Integer getMaxNumberOfRetries()

setMaxNumberOfRetries

public void setMaxNumberOfRetries(java.lang.Integer maxNumberOfRetries)

getRetryTimeoutInMilliseconds

public java.lang.Integer getRetryTimeoutInMilliseconds()

setRetryTimeoutInMilliseconds

public void setRetryTimeoutInMilliseconds(java.lang.Integer retryTimeoutInMilliseconds)

getEncoding

public java.lang.String getEncoding()
Specified by:
getEncoding in interface TextualFormatRepository<T extends Entity>

getRepositoryPath

protected java.lang.String getRepositoryPath(java.lang.String repositoryRootPath,
                                             PersistentRepository<T> repository)
Returns:
the absolute path to the repository location based on root path and the repository ID

run

protected void run(java.lang.Runnable runnable)
Added for completeness.


call

protected void call(java.util.concurrent.Callable<T> callable)
             throws java.lang.Exception
Added for completeness.

Throws:
java.lang.Exception

runConcurrently

protected void runConcurrently(java.lang.Runnable runnable)
Convenience method for running a Runnable in a concurrent manner.

See Also:
Synchronizer

callConcurrently

protected <T> T callConcurrently(java.util.concurrent.Callable<T> callable)
Convenience method for running a Callable in a concurrent manner.

See Also:
Synchronizer

callExclusively

protected <T> T callExclusively(java.util.concurrent.Callable<T> callable)
Convenience method for running a Callable in an exclusively manner.

See Also:
Synchronizer

callConcurrentlyWithRetry

protected <T> T callConcurrentlyWithRetry(java.util.concurrent.Callable<T> callable)
Convenience method for running a Callable in a concurrent manner. If an exception is thrown, the Callable will be immediately retried. The nature of the retries are further defined by the maxNumberOfRetries and retryTimeoutInMilliseconds member values.


callExclusivelyWithRetry

protected <T> T callExclusivelyWithRetry(java.util.concurrent.Callable<T> callable)
Convenience method for running a Callable in an exclusively manner. If an exception is thrown, the Callable will be immediately queued for retrial. The nature of the retries are further defined by the maxNumberOfRetries and retryTimeoutInMilliseconds member values.


callConcurrentlyInNewThread

protected <T> T callConcurrentlyInNewThread(java.util.concurrent.Callable<T> callable)
                                 throws java.util.concurrent.ExecutionException,
                                        java.util.concurrent.TimeoutException,
                                        java.lang.InterruptedException
Throws:
java.util.concurrent.ExecutionException
java.util.concurrent.TimeoutException
java.lang.InterruptedException

callExclusivelyInNewThread

protected <T> T callExclusivelyInNewThread(java.util.concurrent.Callable<T> callable)
                                throws java.util.concurrent.ExecutionException,
                                       java.util.concurrent.TimeoutException,
                                       java.lang.InterruptedException
Throws:
java.util.concurrent.ExecutionException
java.util.concurrent.TimeoutException
java.lang.InterruptedException

runAsynchronously

protected void runAsynchronously(java.lang.Runnable runnable)
Executes the given Runnable in a fresh and independent thread. This method does not block the original thread. The Synchronizer mode is concurrent. (An asynchronous version of Synchronizer in exclusive mode has no meaning, as it is a "stop-the-world" kind of mode.)



Copyright © 2006-2010. All Rights Reserved.