org.apache.cayenne.access
Class ObjectStore

java.lang.Object
  extended by org.apache.cayenne.access.ObjectStore
All Implemented Interfaces:
Serializable, EventListener, SnapshotEventListener, GraphChangeHandler, GraphManager

public class ObjectStore
extends Object
implements Serializable, SnapshotEventListener, GraphManager

ObjectStore stores objects using their ObjectId as a key. It works as a dedicated object cache for a DataContext. Users rarely need to access ObjectStore directly, as DataContext serves as a facade, providing cover methods for most ObjectStore operations.

Since:
1.0
See Also:
Serialized Form

Field Summary
protected  Map<Object,org.apache.cayenne.access.ObjectDiff> changes
           
protected  DataContext context
          The DataContext that owns this ObjectStore.
protected  DataRowStore dataRowCache
          Stores a reference to the DataRowStore.
protected  Map<Object,Persistent> objectMap
           
 
Constructor Summary
ObjectStore()
           
ObjectStore(DataRowStore dataRowCache)
           
ObjectStore(DataRowStore dataRowCache, Map<Object,Persistent> objectMap)
          Creates an ObjectStore with DataRowStore and a map to use for storing registered objects.
 
Method Summary
 void arcCreated(Object nodeId, Object targetNodeId, Object arcId)
          Notifies implementing object that a new arc was created between two nodes.
 void arcDeleted(Object nodeId, Object targetNodeId, Object arcId)
          Notifies implementing object that an arc between two nodes was deleted.
 int cachedQueriesCount()
          Deprecated. since 3.0. See DataContext.getQueryCache().
 void cacheQueryResult(String name, List results)
          Deprecated. since 3.0. See DataContext.getQueryCache().
 List getCachedQueryResult(String name)
          Deprecated. since 3.0. See DataContext.getQueryCache().
 DataRow getCachedSnapshot(ObjectId oid)
          Returns a snapshot for ObjectId from the underlying snapshot cache.
 DataContext getContext()
           
 DataRowStore getDataRowCache()
          Returns a DataRowStore associated with this ObjectStore.
 Object getNode(Object nodeId)
          Returns a registered DataObject or null of no object exists for the ObjectId.
 Iterator getObjectIterator()
          Returns an iterator over the registered objects.
 DataRow getSnapshot(ObjectId oid)
          Returns a snapshot for ObjectId from the underlying snapshot cache.
 boolean hasChanges()
          Returns true if there are any modified, deleted or new objects registered with this ObjectStore, false otherwise.
 void nodeCreated(Object nodeId)
          Notifies implementing object that a new node was created in the graph.
 void nodeIdChanged(Object nodeId, Object newId)
          Does nothing.
 void nodePropertyChanged(Object nodeId, String property, Object oldValue, Object newValue)
          Records dirty object snapshot.
 void nodeRemoved(Object nodeId)
          Notifies implementing object that a node was removed from the graph.
 List<Persistent> objectsInState(int state)
          Return a subset of registered objects that are in a certain persistence state.
 void objectsInvalidated(Collection objects)
          Deprecated. since 3.0, use BaseContext.invalidateObjects(Collection) or RefreshQuery.
 void objectsRolledBack()
          Reverts changes to all stored uncomitted objects.
 void objectsUnregistered(Collection objects)
          Evicts a collection of DataObjects from the ObjectStore, invalidates the underlying cache snapshots.
 Collection<Object> registeredNodes()
          Returns all registered DataObjects.
 int registeredObjectsCount()
          Returns a number of objects currently registered with this ObjectStore.
 void registerNode(Object nodeId, Object nodeObject)
          "Registers" a graph node, usually storing the node in some internal map using its id as a key.
 void resolveHollow(Persistent object)
          Deprecated. since 3.0 use ObjectContext.prepareForAccess(Persistent, String, boolean).
 void setContext(DataContext context)
           
 void setDataRowCache(DataRowStore dataRowCache)
          Sets parent DataRowStore.
 void snapshotsChanged(SnapshotEvent event)
          SnapshotEventListener implementation that processes snapshot change event, updating DataObjects that have the changes.
 void snapshotsUpdatedForObjects(List objects, List snapshots, boolean refresh)
          Deprecated. since 3.0 unused
 void startTrackingNewObjects()
          Deprecated. since 3.0 as ObjectStore holds weak reference to unmodified objects and this feature is useless.
 void unregisterNewObjects()
          Deprecated. since 3.0 as ObjectStore holds weak reference to unmodified objects and this feature is useless.
 Object unregisterNode(Object nodeId)
          "Unregisters" a graph node, forgetting any information associated with nodeId.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

objectMap

protected Map<Object,Persistent> objectMap

changes

protected Map<Object,org.apache.cayenne.access.ObjectDiff> changes

dataRowCache

protected transient DataRowStore dataRowCache
Stores a reference to the DataRowStore.

Serialization note: It is up to the owner of this ObjectStore to initialize DataRowStore after deserialization of this object. ObjectStore will not know how to restore the DataRowStore by itself.


context

protected DataContext context
The DataContext that owns this ObjectStore.

Constructor Detail

ObjectStore

public ObjectStore()

ObjectStore

public ObjectStore(DataRowStore dataRowCache)

ObjectStore

public ObjectStore(DataRowStore dataRowCache,
                   Map<Object,Persistent> objectMap)
Creates an ObjectStore with DataRowStore and a map to use for storing registered objects. Passed map doesn't require any special synchronization behavior, as ObjectStore is synchronized itself.

Since:
3.0
Method Detail

registeredObjectsCount

public int registeredObjectsCount()
Returns a number of objects currently registered with this ObjectStore.

Since:
1.2

cachedQueriesCount

@Deprecated
public int cachedQueriesCount()
Deprecated. since 3.0. See DataContext.getQueryCache().

Returns a number of query results cached by this object store. Note that each result is a list and can possibly contain a large number of entries.

Since:
1.2

getDataRowCache

public DataRowStore getDataRowCache()
Returns a DataRowStore associated with this ObjectStore.


setDataRowCache

public void setDataRowCache(DataRowStore dataRowCache)
Sets parent DataRowStore. Registers to receive SnapshotEvents if the cache is configured to allow ObjectStores to receive such events.


objectsInvalidated

@Deprecated
public void objectsInvalidated(Collection objects)
Deprecated. since 3.0, use BaseContext.invalidateObjects(Collection) or RefreshQuery.

Invalidates a collection of DataObjects. Changes objects state to HOLLOW.


objectsUnregistered

public void objectsUnregistered(Collection objects)
Evicts a collection of DataObjects from the ObjectStore, invalidates the underlying cache snapshots. Changes objects state to TRANSIENT. This method can be used for manual cleanup of Cayenne cache.

See Also:
objectsInvalidated(Collection)

objectsRolledBack

public void objectsRolledBack()
Reverts changes to all stored uncomitted objects.

Since:
1.1

snapshotsUpdatedForObjects

@Deprecated
public void snapshotsUpdatedForObjects(List objects,
                                                  List snapshots,
                                                  boolean refresh)
Deprecated. since 3.0 unused

Updates snapshots in the underlying DataRowStore. If refresh is true, all snapshots in snapshots will be loaded into DataRowStore, regardless of the existing cache state. If refresh is false, only missing snapshots are loaded. This method is normally called internally by the DataContext owning the ObjectStore to update the caches after a select query.

Parameters:
objects - a list of object whose snapshots need to be updated.
snapshots - a list of snapshots. Must be of the same length and use the same order as objects list.
refresh - controls whether existing cached snapshots should be replaced with the new ones.
Since:
1.1

startTrackingNewObjects

@Deprecated
public void startTrackingNewObjects()
Deprecated. since 3.0 as ObjectStore holds weak reference to unmodified objects and this feature is useless.

Starts tracking the registration of new objects from this ObjectStore. Used in conjunction with unregisterNewObjects() to control garbage collection when an instance of ObjectStore is used over a longer time for batch processing.


unregisterNewObjects

@Deprecated
public void unregisterNewObjects()
Deprecated. since 3.0 as ObjectStore holds weak reference to unmodified objects and this feature is useless.

Unregisters the newly registered DataObjects from this objectStore. Used in conjunction with startTrackingNewObjects() to control garbage collection when an instance of ObjectStore is used over a longer time for batch processing.


getCachedSnapshot

public DataRow getCachedSnapshot(ObjectId oid)
Returns a snapshot for ObjectId from the underlying snapshot cache. If cache contains no snapshot, a null is returned.

Since:
1.1

getCachedQueryResult

@Deprecated
public List getCachedQueryResult(String name)
Deprecated. since 3.0. See DataContext.getQueryCache().

Returns cached query results for a given query, or null if no results are cached. Note that ObjectStore will only lookup results in its local cache, and not the shared cache associated with the underlying DataRowStore.

Since:
1.1

cacheQueryResult

@Deprecated
public void cacheQueryResult(String name,
                                        List results)
Deprecated. since 3.0. See DataContext.getQueryCache().

Caches a list of query results.

Since:
1.1

getSnapshot

public DataRow getSnapshot(ObjectId oid)
Returns a snapshot for ObjectId from the underlying snapshot cache. If cache contains no snapshot, it will attempt fetching it using provided QueryEngine. If fetch attempt fails or inconsistent data is returned, underlying cache will throw a CayenneRuntimeException.

Since:
1.2

getObjectIterator

public Iterator getObjectIterator()
Returns an iterator over the registered objects.


hasChanges

public boolean hasChanges()
Returns true if there are any modified, deleted or new objects registered with this ObjectStore, false otherwise. This method will treat "phantom" modifications are real ones. I.e. if you "change" an object property to an equivalent value, this method will still think such object is modified. Phantom modifications are only detected and discarded during commit.


objectsInState

public List<Persistent> objectsInState(int state)
Return a subset of registered objects that are in a certain persistence state. Collection is returned by copy.


snapshotsChanged

public void snapshotsChanged(SnapshotEvent event)
SnapshotEventListener implementation that processes snapshot change event, updating DataObjects that have the changes.

Implementation note: This method should not attempt to alter the underlying DataRowStore, since it is normally invoked *AFTER* the DataRowStore was modified as a result of some external interaction.

Specified by:
snapshotsChanged in interface SnapshotEventListener
Since:
1.1

resolveHollow

@Deprecated
public void resolveHollow(Persistent object)
Deprecated. since 3.0 use ObjectContext.prepareForAccess(Persistent, String, boolean).

Initializes object with data from cache or from the database, if this object is not fully resolved.

Since:
1.1

getContext

public DataContext getContext()
Since:
1.2

setContext

public void setContext(DataContext context)
Since:
1.2

getNode

public Object getNode(Object nodeId)
Returns a registered DataObject or null of no object exists for the ObjectId.

Specified by:
getNode in interface GraphManager
Since:
1.2

registeredNodes

public Collection<Object> registeredNodes()
Returns all registered DataObjects. List is returned by copy and can be modified by the caller.

Specified by:
registeredNodes in interface GraphManager
Since:
1.2

registerNode

public void registerNode(Object nodeId,
                         Object nodeObject)
Description copied from interface: GraphManager
"Registers" a graph node, usually storing the node in some internal map using its id as a key.

Specified by:
registerNode in interface GraphManager
Since:
1.2

unregisterNode

public Object unregisterNode(Object nodeId)
Description copied from interface: GraphManager
"Unregisters" a graph node, forgetting any information associated with nodeId.

Specified by:
unregisterNode in interface GraphManager
Since:
1.2

nodeIdChanged

public void nodeIdChanged(Object nodeId,
                          Object newId)
Does nothing.

Specified by:
nodeIdChanged in interface GraphChangeHandler
Since:
1.2

nodeCreated

public void nodeCreated(Object nodeId)
Description copied from interface: GraphChangeHandler
Notifies implementing object that a new node was created in the graph.

Specified by:
nodeCreated in interface GraphChangeHandler
Since:
1.2

nodeRemoved

public void nodeRemoved(Object nodeId)
Description copied from interface: GraphChangeHandler
Notifies implementing object that a node was removed from the graph.

Specified by:
nodeRemoved in interface GraphChangeHandler
Since:
1.2

nodePropertyChanged

public void nodePropertyChanged(Object nodeId,
                                String property,
                                Object oldValue,
                                Object newValue)
Records dirty object snapshot.

Specified by:
nodePropertyChanged in interface GraphChangeHandler
Since:
1.2

arcCreated

public void arcCreated(Object nodeId,
                       Object targetNodeId,
                       Object arcId)
Description copied from interface: GraphChangeHandler
Notifies implementing object that a new arc was created between two nodes.

Specified by:
arcCreated in interface GraphChangeHandler
Since:
1.2

arcDeleted

public void arcDeleted(Object nodeId,
                       Object targetNodeId,
                       Object arcId)
Description copied from interface: GraphChangeHandler
Notifies implementing object that an arc between two nodes was deleted.

Specified by:
arcDeleted in interface GraphChangeHandler
Since:
1.2


Copyright © 2001-2011 Apache Cayenne. All Rights Reserved.