Class DataContextObjectStore
java.lang.Object
org.apache.cayenne.access.DataContextObjectStore
- All Implemented Interfaces:
ObjectStore
The default
ObjectStore implementation. Exposes a few locks for the callers to provide concurrency:
per-object object lock and broader changes lock used by commits, etc. To prevent
deadlocks, the changes lock is always taken before any object lock.- Since:
- 5.0
-
Field Summary
FieldsModifier and TypeFieldDescriptionprotected Map<ObjectId, ObjectDiff> protected final DataContextprotected final DataRowStoreprotected final Map<ObjectId, Persistent> protected final Map<ObjectId, Map<CayennePath, ObjectId>> Presence of path in this map is used to separate insert from update case of flattened records. -
Constructor Summary
ConstructorsConstructorDescriptionDataContextObjectStore(DataContext context, DataRowStore dataRowCache, Map<ObjectId, Persistent> objectMap, boolean syncWithSnapshotCache) -
Method Summary
Modifier and TypeMethodDescriptionReturns a snapshot for ObjectId from the underlying snapshot cache, or null if the cache contains no snapshot.Returns a DataRowStore associated with this ObjectStore.getFlattenedId(ObjectId objectId, CayennePath path) getFlattenedIds(ObjectId objectId) getFlattenedPathIdMap(ObjectId objectId) Returns a registered object for the id, or null if no such object is registered.Returns an iterator over the registered objects.getSnapshot(ObjectId oid) Returns a snapshot for ObjectId from the underlying snapshot cache.booleanReturnstrueif there are any modified, deleted or new objects registered with this ObjectStore,falseotherwise.voidmarkFlattenedPath(ObjectId objectId, CayennePath path, ObjectId id) Mark that flattened path for object has data row in DB.objectsInState(int state) Return a subset of registered objects that are in a certain persistence state.voidReverts changes to all stored uncomitted objects.voidobjectsUnregistered(Collection<? extends Persistent> objects) Evicts a collection of Persistent objects from the ObjectStore, invalidates the underlying cache snapshots.voidpostprocessAfterCommit(GraphDiff parentChanges) Processes objects state after commit.Returns a copy of the collection of all registered objects.intReturns a number of objects currently registered with this ObjectStore.voidregisterObject(ObjectId id, Persistent nodeObject) Registers an object under the id.Unregisters an object with the id, forgetting any information associated with it.<T> TwithChangesLock(Supplier<T> action) Runs the action under the lock guarding the uncommitted changes, returning its result.<T> TwithObjectLock(ObjectId id, Supplier<T> action) Returns the monitor guarding writes to the state of the object with the id: attribute and relationship values, persistence state and snapshot version.
-
Field Details
-
context
-
dataRowCache
-
objectMap
-
changes
-
trackedFlattenedPaths
Presence of path in this map is used to separate insert from update case of flattened records.
-
-
Constructor Details
-
DataContextObjectStore
public DataContextObjectStore(DataContext context, DataRowStore dataRowCache, Map<ObjectId, Persistent> objectMap, boolean syncWithSnapshotCache)
-
-
Method Details
-
withChangesLock
Runs the action under the lock guarding the uncommitted changes, returning its result. A commit holds the lock for its whole duration, so that no changes are recorded while the ones being committed are read. The lock is reentrant. It is always taken before an object lock, never while holding one. -
withObjectLock
Returns the monitor guarding writes to the state of the object with the id: attribute and relationship values, persistence state and snapshot version. Every writer of object state synchronizes on it for the duration of a write, so that concurrent writes do not interleave and other writers never see an object half-written. Readers do not synchronize. A monitor may be shared by several ids, so a writer must not hold more than one at a time, and must not perform blocking operations, such as queries, while holding it. -
registeredObjectsCount
public int registeredObjectsCount()Returns a number of objects currently registered with this ObjectStore. -
getDataRowCache
Returns a DataRowStore associated with this ObjectStore. -
objectsUnregistered
Evicts a collection of Persistent objects from the ObjectStore, invalidates the underlying cache snapshots. Changes objects state to TRANSIENT. This method can be used for manual cleanup of Cayenne cache. -
objectsRolledBack
public void objectsRolledBack()Reverts changes to all stored uncomitted objects. -
postprocessAfterCommit
Processes objects state after commit. Normally runs on the committing thread, that already holds the changes lock for the duration of the commit. -
getCachedSnapshot
Returns a snapshot for ObjectId from the underlying snapshot cache, or null if the cache contains no snapshot. Snapshots are keyed by the concrete entity of an object, so an id of a superentity is matched against the ids of its subentities as well. A nested context has no snapshot cache of its own, so it takes the current state of the object in the parent context as the snapshot.- Specified by:
getCachedSnapshotin interfaceObjectStore
-
getSnapshot
Returns a snapshot for ObjectId from the underlying snapshot cache. If the cache contains no snapshot, it is fetched from the database. Returns null if no matching row exists.- Specified by:
getSnapshotin interfaceObjectStore- Since:
- 1.2
-
getObjectIterator
Returns an iterator over the registered objects. -
hasChanges
public boolean hasChanges()Returnstrueif there are any modified, deleted or new objects registered with this ObjectStore,falseotherwise. 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.- Specified by:
hasChangesin interfaceObjectStore
-
objectsInState
Return a subset of registered objects that are in a certain persistence state. Collection is returned by copy.- Specified by:
objectsInStatein interfaceObjectStore
-
getObject
Description copied from interface:ObjectStoreReturns a registered object for the id, or null if no such object is registered.- Specified by:
getObjectin interfaceObjectStore
-
registeredObjects
Description copied from interface:ObjectStoreReturns a copy of the collection of all registered objects.- Specified by:
registeredObjectsin interfaceObjectStore
-
registerObject
Description copied from interface:ObjectStoreRegisters an object under the id.- Specified by:
registerObjectin interfaceObjectStore
-
unregisterObject
Description copied from interface:ObjectStoreUnregisters an object with the id, forgetting any information associated with it. Returns the unregistered object, or null if none was registered.- Specified by:
unregisterObjectin interfaceObjectStore
-
getFlattenedId
-
getFlattenedIds
-
getFlattenedPathIdMap
-
markFlattenedPath
Mark that flattened path for object has data row in DB.- Since:
- 4.1
-