Class DBCachedObjectStore<T extends DBAnnotatedObject>
- Type Parameters:
T- the type of objects stored
- All Implemented Interfaces:
ErrorHandler
Table
Essentially, this provides object-based accessed to records in the table via DAOs. See
DBAnnotatedObject for further documentation including an example object definition. The
store keeps a cache of objects using DBObjectCache. See
DBCachedObjectStoreFactory for documentation describing how to create a store, including
for the example object definition.
The store provides views for locating, iterating, and retrieving its objects in a variety of
fashions. This includes the primary key (object id), or any indexed column (see
DBAnnotatedField.indexed()). These views generally implement an interface from Java's
Collections API, providing for familiar semantics. A notable exception is that none of the
interfaces support mutation, aside from deletion. The store is populated only via the
create() methods.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionprotected classAbstractions for navigating within a given viewprotected static interfaceA variation ofSupplierthat allowsIOExceptionto pass through -
Field Summary
FieldsModifier and TypeFieldDescriptionprotected final DBCachedObjectStoreEntrySet<T> protected final DBCachedObjectStoreKeySetprotected final DBCachedObjectStoreMap<T> protected final DBCachedObjectStoreValueCollection<T> protected final List<DBCachedObjectStoreFactory.DBFieldCodec<?, T, ?>> protected final DBCachedObjectStore<T>.BoundedStuff<Map.Entry<Long, T>, DBRecord> The implementation ofDBCachedObjectStore.BoundedStuffto facilitate the implementation ofMap.entrySet().protected final DBCachedObjectStore<T>.BoundedStuff<Long, Long> The implementation ofDBCachedObjectStore.BoundedStuffto facilitate the implementation ofMap.keySet().protected final DBCachedObjectStore<T>.BoundedStuff<T, DBRecord> The implementation ofDBCachedObjectStore.BoundedStuffto facilitate the implementation ofMap.values(). -
Constructor Summary
ConstructorsModifierConstructorDescriptionprotectedDBCachedObjectStore(DBCachedDomainObjectAdapter adapter, Class<T> objectType, DBAnnotatedObjectFactory<T> factory, Table table) Construct a store -
Method Summary
Modifier and TypeMethodDescriptionasMap()Provides access to the store as aNavigableMap.booleanCheck if the given object exists in the storebooleancontainsKey(long key) Check if an object with the given key exists in the storecreate()Create a new object with the next available key.create(long key) Create a new object with the given key.voidNotification that an IO exception occurred.booleanDelete the given objectvoidClear the entire tabledeleteKey(long key) Delete the object with the given keyprotected voiddeleteKeys(KeySpan keySpan) protected TdoCreate(long key) protected DBCachedObjectStoreFoundKeysValueCollection<T> findObjects(int columnIndex, Field field) Search a column index for all objects having the given valueprotected TfindOneObject(int columnIndex, Field field) Search a column index for a single object having the given valueprotected intgetColumnByName(String name) Get the column number given a column nameprotected <K> DBCachedObjectIndex<K, T> Get the table index for the given column number<K> DBCachedObjectIndex<K, T> getIndex(Class<K> fieldClass, DBObjectColumn column) Get the index for a given column<K> DBCachedObjectIndex<K, T> Get the index for a given column by nameprotected intgetKeyCount(KeySpan keySpan) Count the number of keys in a given range.protected booleangetKeysExist(KeySpan keySpan) Check if any keys exist within the given range.getLock()Get the read-write lockGet the maximum key which has ever existed in this storegetObjectAt(long key) Get the object having the given keyintGet the number of objects (records) in this storeGet the name of the table backing this storevoidInvalidate this store's cacheiterator(int columnIndex, FieldSpan fieldSpan, DirectedIterator.Direction direction) Search a column index and iterate over objects having the given valueprotected Comparator<? super Long> Get the key comparatorreadLock()Get the read lockprotected <U> Usafe(Lock l, DBCachedObjectStore.SupplierAllowsIOException<U> supplier) Invoke the given supplier with a lock, directingIOExceptions to the domain object adaptertoString()Display useful information about this cached storeGet the write lock
-
Field Details
-
keys
The implementation ofDBCachedObjectStore.BoundedStuffto facilitate the implementation ofMap.keySet().Because tables let us navigate keys directly, we use the key as the raw component here instead of the full record.
-
objects
protected final DBCachedObjectStore<T extends DBAnnotatedObject>.BoundedStuff<T extends DBAnnotatedObject,DBRecord> objectsThe implementation ofDBCachedObjectStore.BoundedStuffto facilitate the implementation ofMap.values(). -
entries
protected final DBCachedObjectStore<T extends DBAnnotatedObject>.BoundedStuff<Map.Entry<Long,T extends DBAnnotatedObject>, entriesDBRecord> The implementation ofDBCachedObjectStore.BoundedStuffto facilitate the implementation ofMap.entrySet(). -
asForwardMap
-
asForwardKeySet
-
asForwardValueCollection
protected final DBCachedObjectStoreValueCollection<T extends DBAnnotatedObject> asForwardValueCollection -
asForwardEntrySet
-
codecs
protected final List<DBCachedObjectStoreFactory.DBFieldCodec<?,T extends DBAnnotatedObject, codecs?>>
-
-
Constructor Details
-
DBCachedObjectStore
protected DBCachedObjectStore(DBCachedDomainObjectAdapter adapter, Class<T> objectType, DBAnnotatedObjectFactory<T> factory, Table table) Construct a storeUsers should instead construct stores using
DBCachedObjectStoreFactory.getOrCreateCachedStore(String, Class, DBAnnotatedObjectFactory, boolean).- Parameters:
adapter- the domain object backed by the same database as this storeobjectType- the type of objects storedfactory- the factory creating this storetable- the table backing this store
-
-
Method Details
-
getRecordCount
public int getRecordCount()Get the number of objects (records) in this store- Returns:
- the record count
-
getMaxKey
Get the maximum key which has ever existed in this storeNote, the returned key may not actually be present
- Returns:
- the maximum, or null if the store is unused
-
getKeyCount
Count the number of keys in a given range.This implementation is not very efficient. It must visit at least every record in the range.
- Parameters:
keySpan- the range of keys- Returns:
- the count of records whose keys fall within the range
-
getKeysExist
Check if any keys exist within the given range.This implementation is more efficient than using
getKeyCount(KeySpan)and comparing to 0, since there's no need to visit more than one record in the range.- Parameters:
keySpan- the range of keys- Returns:
- true if at least one record has a key within the range
-
containsKey
public boolean containsKey(long key) Check if an object with the given key exists in the storeUsing this is preferred to
getObjectAt(long)and checking for null, if that object does not actually need to be retrieved.- Parameters:
key- the key- Returns:
- true if it exists
-
contains
Check if the given object exists in the storeNo matter the definition of
Object.equals(Object), this requires the identical object to be present.- Parameters:
obj- the object- Returns:
-
doCreate
- Throws:
IOException
-
create
Create a new object with the given key.If the key already exists in the table, the existing record is overwritten.
- Parameters:
key- the key for the new object- Returns:
- the new object
-
create
Create a new object with the next available key.- Returns:
- the new object
-
getColumnByName
Get the column number given a column name- Parameters:
name- the name- Returns:
- the number (0-up index) for the column
- Throws:
NoSuchElementException- if no column with the given name exists
-
getIndex
Get the table index for the given column number- Type Parameters:
K- the type of the object field for the indexed column- Parameters:
fieldClass- the class specifyingDBCachedObjectStorecolumnIndex- the column number- Returns:
- the index
- Throws:
IllegalArgumentException- if the column has a different type thanDBCachedObjectStore
-
getIndex
Get the index for a given columnSee
DBCachedObjectStoreFactoryfor an example that includes use of an index- Type Parameters:
K- the type of the object field for the indexed column- Parameters:
fieldClass- the class specifyingDBCachedObjectStorecolumn- the indexed column- Returns:
- the index
- Throws:
IllegalArgumentException- if the column has a different type thanDBCachedObjectStore
-
getIndex
Get the index for a given column by nameSee
DBCachedObjectStoreFactoryfor an example that includes use of an index- Type Parameters:
K- the type of the object field for the indexed column- Parameters:
fieldClass- the class specifyingDBCachedObjectStorecolumnName- the name of the indexed column- Returns:
- the index
- Throws:
IllegalArgumentException- if the given column is not indexed
-
delete
Delete the given object- Parameters:
obj- the object- Returns:
- true if the object was removed, false for no effect
-
deleteKey
Delete the object with the given key- Parameters:
key- the key- Returns:
- true if the key was removed, false for no effect
-
deleteAll
public void deleteAll()Clear the entire table -
deleteKeys
-
safe
Invoke the given supplier with a lock, directingIOExceptions to the domain object adapter- Type Parameters:
U- the type of the result- Parameters:
l- the lock to hold during invocationsupplier- the supplier to invoke- Returns:
- the result
-
getObjectAt
Get the object having the given key- Parameters:
key- the key- Returns:
- the object, or null
-
keyComparator
Get the key comparator- Returns:
- the comparator
- Implementation Notes:
- this is probably vestigial, left from when we attempted to allow customization of the primary key. This currently just gives the natural ordering of longs.
-
asMap
Provides access to the store as aNavigableMap.- Returns:
- the map
-
findOneObject
Search a column index for a single object having the given value- Parameters:
columnIndex- the indexed column's numberfield- a field holding the value to seek- Returns:
- the object, if found, or null
- Throws:
IOException- if there's an issue reading the tableIllegalStateException- if the object is not unique
-
findObjects
protected DBCachedObjectStoreFoundKeysValueCollection<T> findObjects(int columnIndex, Field field) throws IOException Search a column index for all objects having the given value- Parameters:
columnIndex- the indexed column's numberfield- a field holding the value to seek- Returns:
- the collection of objects found, possibly empty but never null
- Throws:
IOException- if there's an issue reading the table
-
iterator
protected Iterator<T> iterator(int columnIndex, FieldSpan fieldSpan, DirectedIterator.Direction direction) throws IOException Search a column index and iterate over objects having the given value- Parameters:
columnIndex- the indexed column's numberfieldSpan- required: the range to considerdirection- the direction of iteration- Returns:
- the iterator, possibly empty but never null
- Throws:
IOException- if there's an issue reading the table
-
readLock
Get the read lock- Returns:
- the lock
-
writeLock
Get the write lock- Returns:
- the lock
-
getLock
Get the read-write lock- Returns:
- the lock
-
dbError
Description copied from interface:ErrorHandlerNotification that an IO exception occurred.- Specified by:
dbErrorin interfaceErrorHandler- Parameters:
e-IOExceptionwhich was cause of error
-
toString
Display useful information about this cached storePlease avoid calling this except for debugging.
-
getTableName
Get the name of the table backing this store- Returns:
- the name
-
invalidateCache
public void invalidateCache()Invalidate this store's cacheThis should be called whenever the table may have changed in a way not caused by the store itself, e.g., whenever
DBHandle.undo()is called.
-