Class DBCachedObjectIndex<K,T extends DBAnnotatedObject>
- Type Parameters:
K- the type of keys in the index, i.e., the indexed field's typeT- the type of objects in the store
DBCachedObjectStore
This provides access to a table index backing the store, allowing clients to retrieve objects
having specified field values. Its methods are inspired by NavigableMap; however, its
semantics permit duplicate keys, so this cannot implement it in the manner desired.
- Implementation Notes:
- This seems rife for implementing a collection interface, but each defies implementation on our DB framework. Probably because it's better understood as a multimap, which is not a standard Java collection. Guava's proved too burdensome to implement. We never tried Apache's.
-
Field Summary
FieldsModifier and TypeFieldDescriptionprotected final DBCachedObjectStoreFactory.DBFieldCodec<K, T, ?> protected final intprotected final DirectedIterator.Directionprotected final ErrorHandlerprotected final FieldSpanprotected final DBCachedObjectStore<T> -
Constructor Summary
ConstructorsModifierConstructorDescriptionprotectedDBCachedObjectIndex(DBCachedObjectStore<T> store, ErrorHandler errHandler, DBCachedObjectStoreFactory.DBFieldCodec<K, T, ?> codec, int columnIndex, FieldSpan fieldSpan, DirectedIterator.Direction direction) Construct an index -
Method Summary
Modifier and TypeMethodDescriptionceilingEntry(K key) Get the entry at or after the given keyceilingKey(K key) Get the key at or after the given keyceilingValue(K key) Get the value at or after the given keyprotected booleancontainsKey(Field encoded) booleancontainsKey(K key) Check if there is any object having the given value for its indexed fieldbooleancontainsValue(T value) Check if the given object is in the indexprotected intintCount the number of objects whose indexed field has the given valueGet a reversed view of this indexentries()Iterate over the entries as ordered by the indexGet the first entry in the indexfirstKey()Get the first key in the indexprotected static <T> TGet the first object in the indexfloorEntry(K key) Get the entry at or before the given keyGet the key at or before the given keyfloorValue(K key) Get the value at or before the given keyprotected Collection<T> Get the objects having the given value in the indexed fieldGet the objects having the given value in the index fieldGet a unique object having the given value in the index fieldGet a sub-ranged view of this index, limited to entries whose keys occur before the given keyhigherEntry(K key) Get the entry after the given keyGet the key after the given keyhigherValue(K key) Get the value after the given keybooleanisEmpty()Check if this index is emptykeys()Iterate over the values of the indexed column, in orderGet the last entry in the indexlastKey()Get the last key in the indexGet the last object in the indexlowerEntry(K key) Get the entry before the given keyGet the key before the given keylowerValue(K key) Get the value before the given keyGet a sub-ranged view of this indexGet a sub-ranged view of this index, limited to entries whose keys occur after the given keyvalues()Iterate over the objects as ordered by the index
-
Field Details
-
store
-
errHandler
-
codec
-
columnIndex
protected final int columnIndex -
fieldSpan
-
direction
-
-
Constructor Details
-
DBCachedObjectIndex
protected DBCachedObjectIndex(DBCachedObjectStore<T> store, ErrorHandler errHandler, DBCachedObjectStoreFactory.DBFieldCodec<K, T, ?> codec, int columnIndex, FieldSpan fieldSpan, DirectedIterator.Direction direction) Construct an indexClients should use
DBCachedObjectStore.getIndex(Class, DBObjectColumn).- Parameters:
store- the store containing the indexed objectserrHandler- an error handlercodec- the codec for the indexed field/columncolumnIndex- the column numberfieldSpan- required: the restricted range, can beFieldSpan.ALLdirection- the sort order / direction of iteration
-
-
Method Details
-
get
-
get
Get the objects having the given value in the indexed fieldNOTE: The objects' primary keys are retrieved immediately, but the returned collection loads each requested object lazily. This may have timing implications. If the returned collection is used at a later time, the keys found may no longer be valid, and even if they are, the indexed field may no longer have the requested value when retrieved. See
getLazily(Object).- Parameters:
key- the value- Returns:
- the collection of objects
-
getLazily
Get the objects having the given value in the index fieldThis differs from
get(Object)in that the keys are retrieved each time the collection is iterated. The returned collection can be saved and used later. The iterator itself still has a fixed set of keys, though, so clients should use it and discard it in a timely fashion, and/or while holding the domain object's lock.- Parameters:
key- the value- Returns:
- the lazy collection of objects
-
getOne
Get a unique object having the given value in the index fieldClients should use this method when the index behaves like a map, rather than a multimap. It is the client's responsibility to ensure that duplicate values do not exist in the indexed column.
- Parameters:
value- the value- Returns:
- the object, if found, or null
- Throws:
IllegalStateException- if the object is not unique
-
keys
Iterate over the values of the indexed column, in orderDespite being called keys, the values may not be unique
- Returns:
- the iterator
-
values
Iterate over the objects as ordered by the index- Returns:
- the iterator
-
entries
Iterate over the entries as ordered by the indexEach entry is a key-value value where the "key" is the value of the indexed field, and the "value" is the object.
- Returns:
- the iterator
-
firstOf
-
isEmpty
public boolean isEmpty()Check if this index is emptyExcept for sub-ranged indexes, this is equivalent to checking if the object store is empty. For sub-ranged indexes, this checks if the store contains any object whose value for the indexed field falls within the restricted range.
- Returns:
- true if empty
-
containsKey
-
containsKey
Check if there is any object having the given value for its indexed fieldThis method is more efficient than using
get(key).isEmpty(), since it need only find one match, whereasget(Object)will retrieve every match. Granted, it doesn't make sense to immediately callget(Object)aftercontainsKey(Object)returns true. -
containsValue
Check if the given object is in the indexExcept for sub-ranged indexes, this is equivalent to checking if the object is in the store. For a sub-ranged index, the value of its indexed field must fall within the restricted range.
- Parameters:
value- the object- Returns:
- true if it appears in this (sub-ranged) index.
-
countKey
-
countKey
Count the number of objects whose indexed field has the given value- Parameters:
key- the value- Returns:
- the count
-
firstKey
Get the first key in the index- Returns:
- the first key, or null
- See Also:
-
firstValue
Get the first object in the index- Returns:
- the first object, or null
- See Also:
-
firstEntry
Get the first entry in the index- Returns:
- the first key, or null
- See Also:
-
lastKey
Get the last key in the index- Returns:
- the first key, or null
- See Also:
-
lastValue
Get the last object in the index- Returns:
- the first object, or null
- See Also:
-
lastEntry
Get the last entry in the index- Returns:
- the first key, or null
- See Also:
-
lowerKey
Get the key before the given key- Parameters:
key- the key- Returns:
- the previous key, or null
- See Also:
-
lowerValue
Get the value before the given key- Parameters:
key- the key- Returns:
- the value of the previous key, or null
- See Also:
-
lowerEntry
Get the entry before the given key- Parameters:
key- the key- Returns:
- the entry of the previous key, or null
- See Also:
-
floorKey
Get the key at or before the given key- Parameters:
key- the key- Returns:
- the same or previous key, or null
- See Also:
-
floorValue
Get the value at or before the given key- Parameters:
key- the key- Returns:
- the value of the same or previous key, or null
- See Also:
-
floorEntry
Get the entry at or before the given key- Parameters:
key- the key- Returns:
- the entry of the same or previous key, or null
- See Also:
-
ceilingKey
Get the key at or after the given key- Parameters:
key- the key- Returns:
- the same or next key, or null
- See Also:
-
ceilingValue
Get the value at or after the given key- Parameters:
key- the key- Returns:
- the value of the same or next key, or null
- See Also:
-
ceilingEntry
Get the entry at or after the given key- Parameters:
key- the key- Returns:
- the entry of the same or next key, or null
- See Also:
-
higherKey
Get the key after the given key- Parameters:
key- the key- Returns:
- the same or next key, or null
- See Also:
-
higherValue
Get the value after the given key- Parameters:
key- the key- Returns:
- the value of the next key, or null
- See Also:
-
higherEntry
Get the entry after the given key- Parameters:
key- the key- Returns:
- the entry of the next key, or null
- See Also:
-
head
Get a sub-ranged view of this index, limited to entries whose keys occur before the given key- Parameters:
to- the upper boundtoInclusive- whether the upper bound is included in the restricted view- Returns:
- the restricted view
- See Also:
-
tail
Get a sub-ranged view of this index, limited to entries whose keys occur after the given key- Parameters:
from- the lower boundfromInclusive- whether the lower bound is included in the restricted view- Returns:
- the restricted view
- See Also:
-
sub
Get a sub-ranged view of this index- Parameters:
from- the lower boundfromInclusive- whether the lower bound is included in the restricted viewto- the upper boundtoInclusive- whether the upper bound is included in the restricted view- Returns:
- the restricted view
- See Also:
-
descending
Get a reversed view of this indexThis affects iteration as well as all the navigation and sub-ranging methods. E.g.,
lowerKey(Object)in the reversed view will behave likehigherKey(Object)in the original. In other words, the returned index is equivalent to the original, but with a negated comparator. Callingdescending()on the returned view will return a view equivalent to the original.- Returns:
- the reversed view
-