org.springframework.datastore.keyvalue.engine
Class AbstractKeyValueEntityPesister<T,K>

java.lang.Object
  extended by org.springframework.datastore.engine.EntityPersister
      extended by org.springframework.datastore.engine.LockableEntityPersister
          extended by org.springframework.datastore.keyvalue.engine.AbstractKeyValueEntityPesister<T,K>
All Implemented Interfaces:
EntityInterceptorAware, Persister
Direct Known Subclasses:
RedisEntityPersister

public abstract class AbstractKeyValueEntityPesister<T,K>
extends LockableEntityPersister

Abstract implementation of the EntityPersister abstract class for key/value style stores

Since:
1.0

Field Summary
protected  ClassMapping classMapping
           
protected  String entityFamily
           
 
Fields inherited from class org.springframework.datastore.engine.LockableEntityPersister
DEFAULT_TIMEOUT
 
Fields inherited from class org.springframework.datastore.engine.EntityPersister
interceptors, proxyFactory, session
 
Constructor Summary
AbstractKeyValueEntityPesister(MappingContext context, PersistentEntity entity, Session session)
           
 
Method Summary
protected  Serializable convertToNativeKey(Serializable nativeKey)
           
protected  EntityAccess createEntityAccess(PersistentEntity persistentEntity, Object obj)
           
protected  EntityAccess createEntityAccess(PersistentEntity persistentEntity, Object obj, T nativeEntry)
           
protected abstract  T createNewEntry(String family)
          Creates a new entry for the given family.
protected  Object createObjectFromNativeEntry(PersistentEntity persistentEntity, Serializable nativeKey, T nativeEntry)
           
protected  void deleteEntities(PersistentEntity persistentEntity, Iterable objects)
           
protected  void deleteEntity(PersistentEntity persistentEntity, Object obj)
           
protected abstract  void deleteEntries(String family, List<K> keys)
          Deletes one or many entries for the given list of Keys
protected abstract  void deleteEntry(String family, K key)
          Deletes a single entry
protected  PersistentEntity discriminatePersistentEntity(PersistentEntity persistentEntity, T nativeEntry)
          Subclasses should override to customize how entities in hierarchies are discriminated
protected abstract  K generateIdentifier(PersistentEntity persistentEntity, T id)
           
abstract  AssociationIndexer getAssociationIndexer(Association association)
          Obtains an indexer for the given association
 ClassMapping getClassMapping()
           
 String getEntityFamily()
           
protected abstract  Object getEntryValue(T nativeEntry, String property)
          Reads a value for the given key from the native entry
protected  String getFamily(PersistentEntity persistentEntity, ClassMapping<Family> cm)
           
protected  String getIdentifierName(ClassMapping cm)
           
protected  String getKeyspace(ClassMapping<Family> cm, String defaultValue)
           
abstract  PropertyValueIndexer getPropertyIndexer(PersistentProperty property)
          Obtains an indexer for a particular property
protected  K inferNativeKey(String family, Object identifier)
          Used to establish the native key to use from the identifier defined by the object
 boolean isLocked(Object o)
          Subclasses can override to provide locking semantics
 Object lock(Serializable id)
          Locks an object for the given identifier returning the locked instance
 Object lock(Serializable id, int timeout)
          Acquire a lock using the given identifier and timeout delay
protected  void lockEntry(PersistentEntity persistentEntity, String entityFamily, Serializable id, int timeout)
          Subclasses can override to provide locking semantics
protected  List<Serializable> persistEntities(PersistentEntity persistentEntity, Iterable objs)
          This is a rather simplistic and unoptimized implementation.
protected  Serializable persistEntity(PersistentEntity persistentEntity, Object obj)
          Persist the given persistent entity
 Object proxy(Serializable key)
          Creates a proxy for the given key
protected  K readObjectIdentifier(EntityAccess entityAccess, ClassMapping<Family> cm)
           
 Serializable refresh(Object o)
          Refreshes the given objects state
protected  void refreshObjectStateFromNativeEntry(PersistentEntity persistentEntity, Object obj, Serializable nativeKey, T nativeEntry)
           
protected  List<Object> retrieveAllEntities(PersistentEntity persistentEntity, Iterable<Serializable> keys)
          Simplistic default implementation of retrieveAllEntities that iterates over each key and retrieves the entities one-by-one.
protected  List<Object> retrieveAllEntities(PersistentEntity persistentEntity, Serializable[] keys)
          Simplistic default implementation of retrieveAllEntities that iterates over each key and retrieves the entities one-by-one.
protected  Object retrieveEntity(PersistentEntity persistentEntity, Serializable nativeKey)
          Retrieve a PersistentEntity for the given mappingContext and key
protected abstract  T retrieveEntry(PersistentEntity persistentEntity, String family, Serializable key)
          Reads the native form of a Key/value datastore entry.
protected abstract  void setEntryValue(T nativeEntry, String key, Object value)
          Sets a value on an entry
protected abstract  K storeEntry(PersistentEntity persistentEntity, K storeId, T nativeEntry)
          Stores the native form of a Key/value datastore to the actual data store
 void unlock(Object o)
          Unlocks a locked object
protected  void unlockEntry(PersistentEntity persistentEntity, String entityFamily, Serializable id)
          Subclasses to override to provide locking semantics
protected abstract  void updateEntry(PersistentEntity persistentEntity, K key, T entry)
          Updates an existing entry to the actual datastore
 
Methods inherited from class org.springframework.datastore.engine.EntityPersister
addEntityInterceptor, delete, delete, getMappingContext, getObjectIdentifier, getPersistentEntity, getProxyFactory, getType, persist, persist, retrieve, retrieveAll, retrieveAll, setEntityInterceptors
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 
Methods inherited from interface org.springframework.datastore.engine.Persister
createQuery
 

Field Detail

entityFamily

protected String entityFamily

classMapping

protected ClassMapping classMapping
Constructor Detail

AbstractKeyValueEntityPesister

public AbstractKeyValueEntityPesister(MappingContext context,
                                      PersistentEntity entity,
                                      Session session)
Method Detail

getEntityFamily

public String getEntityFamily()

getClassMapping

public ClassMapping getClassMapping()

getFamily

protected String getFamily(PersistentEntity persistentEntity,
                           ClassMapping<Family> cm)

getKeyspace

protected String getKeyspace(ClassMapping<Family> cm,
                             String defaultValue)

deleteEntity

protected void deleteEntity(PersistentEntity persistentEntity,
                            Object obj)
Specified by:
deleteEntity in class EntityPersister

createEntityAccess

protected EntityAccess createEntityAccess(PersistentEntity persistentEntity,
                                          Object obj)
Overrides:
createEntityAccess in class EntityPersister

createEntityAccess

protected EntityAccess createEntityAccess(PersistentEntity persistentEntity,
                                          Object obj,
                                          T nativeEntry)

deleteEntry

protected abstract void deleteEntry(String family,
                                    K key)
Deletes a single entry

Parameters:
family - The family
key - The key

deleteEntities

protected final void deleteEntities(PersistentEntity persistentEntity,
                                    Iterable objects)
Specified by:
deleteEntities in class EntityPersister

lock

public final Object lock(Serializable id)
                  throws org.springframework.dao.CannotAcquireLockException
Description copied from class: LockableEntityPersister
Locks an object for the given identifier returning the locked instance

Specified by:
lock in class LockableEntityPersister
Parameters:
id - The identifier
Returns:
The locked object
Throws:
org.springframework.dao.CannotAcquireLockException - Thrown if a lock couldn't be acquired before the default timeout elapsed

lock

public final Object lock(Serializable id,
                         int timeout)
                  throws org.springframework.dao.CannotAcquireLockException
Description copied from class: LockableEntityPersister
Acquire a lock using the given identifier and timeout delay

Specified by:
lock in class LockableEntityPersister
Parameters:
id - the identifier
timeout - the amount of time to wait before giving up
Returns:
The locked object
Throws:
org.springframework.dao.CannotAcquireLockException

lockEntry

protected void lockEntry(PersistentEntity persistentEntity,
                         String entityFamily,
                         Serializable id,
                         int timeout)
Subclasses can override to provide locking semantics

Parameters:
persistentEntity - The PesistentEntity instnace
entityFamily - The family
id - The identifer
timeout - The lock timeout in seconds

isLocked

public boolean isLocked(Object o)
Subclasses can override to provide locking semantics

Specified by:
isLocked in class LockableEntityPersister
Parameters:
o - The object
Returns:
True if the object is locked

unlock

public void unlock(Object o)
Description copied from class: LockableEntityPersister
Unlocks a locked object

Specified by:
unlock in class LockableEntityPersister
Parameters:
o - The object to unlock

unlockEntry

protected void unlockEntry(PersistentEntity persistentEntity,
                           String entityFamily,
                           Serializable id)
Subclasses to override to provide locking semantics

Parameters:
persistentEntity - The persistent entity
entityFamily - The entity family
id - The identifer

retrieveEntity

protected final Object retrieveEntity(PersistentEntity persistentEntity,
                                      Serializable nativeKey)
Description copied from class: EntityPersister
Retrieve a PersistentEntity for the given mappingContext and key

Specified by:
retrieveEntity in class EntityPersister
Parameters:
persistentEntity - The entity
nativeKey - The key
Returns:
The object or null if it doesn't exist

convertToNativeKey

protected Serializable convertToNativeKey(Serializable nativeKey)

proxy

public Object proxy(Serializable key)
Description copied from interface: Persister
Creates a proxy for the given key

Parameters:
key - The key
Returns:
The proxy

refresh

public Serializable refresh(Object o)
Description copied from interface: Persister
Refreshes the given objects state

Parameters:
o - The object to refresh
Returns:
The objects id

createObjectFromNativeEntry

protected Object createObjectFromNativeEntry(PersistentEntity persistentEntity,
                                             Serializable nativeKey,
                                             T nativeEntry)

refreshObjectStateFromNativeEntry

protected void refreshObjectStateFromNativeEntry(PersistentEntity persistentEntity,
                                                 Object obj,
                                                 Serializable nativeKey,
                                                 T nativeEntry)

discriminatePersistentEntity

protected PersistentEntity discriminatePersistentEntity(PersistentEntity persistentEntity,
                                                        T nativeEntry)
Subclasses should override to customize how entities in hierarchies are discriminated

Parameters:
persistentEntity - The PersistentEntity
nativeEntry - The native entry
Returns:
The discriminated entity

persistEntity

protected final Serializable persistEntity(PersistentEntity persistentEntity,
                                           Object obj)
Description copied from class: EntityPersister
Persist the given persistent entity

Specified by:
persistEntity in class EntityPersister
Parameters:
persistentEntity - The PersistentEntity
Returns:
The generated key

generateIdentifier

protected abstract K generateIdentifier(PersistentEntity persistentEntity,
                                        T id)

getPropertyIndexer

public abstract PropertyValueIndexer getPropertyIndexer(PersistentProperty property)
Obtains an indexer for a particular property

Parameters:
property - The property to index
Returns:
The indexer

getAssociationIndexer

public abstract AssociationIndexer getAssociationIndexer(Association association)
Obtains an indexer for the given association

Parameters:
association - The association
Returns:
An indexer

readObjectIdentifier

protected K readObjectIdentifier(EntityAccess entityAccess,
                                 ClassMapping<Family> cm)

getIdentifierName

protected String getIdentifierName(ClassMapping cm)

persistEntities

protected List<Serializable> persistEntities(PersistentEntity persistentEntity,
                                             Iterable objs)
This is a rather simplistic and unoptimized implementation. Subclasses can override to provide batch insert capabilities to optimize the insertion of multiple entities in one go

Specified by:
persistEntities in class EntityPersister
Parameters:
persistentEntity - The persistent entity
objs - The objext to persist
Returns:
A list of keys

retrieveAllEntities

protected List<Object> retrieveAllEntities(PersistentEntity persistentEntity,
                                           Iterable<Serializable> keys)
Simplistic default implementation of retrieveAllEntities that iterates over each key and retrieves the entities one-by-one. Data stores that support batch retrieval can optimize this to retrieve all entities in one go.

Specified by:
retrieveAllEntities in class EntityPersister
Parameters:
persistentEntity - The persist entity
keys - The keys
Returns:
A list of entities

retrieveAllEntities

protected List<Object> retrieveAllEntities(PersistentEntity persistentEntity,
                                           Serializable[] keys)
Simplistic default implementation of retrieveAllEntities that iterates over each key and retrieves the entities one-by-one. Data stores that support batch retrieval can optimize this to retrieve all entities in one go.

Specified by:
retrieveAllEntities in class EntityPersister
Parameters:
persistentEntity - The persist entity
keys - The keys
Returns:
A list of entities

inferNativeKey

protected K inferNativeKey(String family,
                           Object identifier)
Used to establish the native key to use from the identifier defined by the object

Parameters:
family - The family
identifier - The identifier specified by the object
Returns:
The native key which may just be a cast from the identifier parameter to K

createNewEntry

protected abstract T createNewEntry(String family)
Creates a new entry for the given family.

Parameters:
family - The family
Returns:
An entry such as a BigTable Entity, ColumnFamily etc.

getEntryValue

protected abstract Object getEntryValue(T nativeEntry,
                                        String property)
Reads a value for the given key from the native entry

Parameters:
nativeEntry - The native entry. Could be a ColumnFamily, a BigTable entity, a Map etc.
property - The property key
Returns:
The value

setEntryValue

protected abstract void setEntryValue(T nativeEntry,
                                      String key,
                                      Object value)
Sets a value on an entry

Parameters:
nativeEntry - The native entry such as a BigTable Entity, ColumnFamily etc.
key - The key
value - The value

retrieveEntry

protected abstract T retrieveEntry(PersistentEntity persistentEntity,
                                   String family,
                                   Serializable key)
Reads the native form of a Key/value datastore entry. This could be a ColumnFamily, a BigTable Entity, a Map etc.

Parameters:
persistentEntity - The persistent entity
family - The family
key - The key
Returns:
The native form

storeEntry

protected abstract K storeEntry(PersistentEntity persistentEntity,
                                K storeId,
                                T nativeEntry)
Stores the native form of a Key/value datastore to the actual data store

Parameters:
persistentEntity - The persistent entity
storeId -
nativeEntry - The native form. Could be a a ColumnFamily, BigTable Entity etc.
Returns:
The native key

updateEntry

protected abstract void updateEntry(PersistentEntity persistentEntity,
                                    K key,
                                    T entry)
Updates an existing entry to the actual datastore

Parameters:
persistentEntity - The PersistentEntity
key - The key of the object to update
entry - The entry

deleteEntries

protected abstract void deleteEntries(String family,
                                      List<K> keys)
Deletes one or many entries for the given list of Keys

Parameters:
family - The family
keys - The keys