com.hammurapi.store

## Class AbstractStore<T,PK,S extends Store<T,PK,S>>

- java.lang.Object
- - com.hammurapi.store.AbstractStore<T,PK,S>

- Type Parameters:
    `T` -
    `S` - Self-type - sub-type of store. Generic parameter for predicates, extractors, update tasks and views. S should be an interface for master store, deputy store and views to implement.
  All Implemented Interfaces:
    Store<T,PK,S>, Iterable<T>, ReadWriteLock
  Direct Known Subclasses:
    DeputyStore, LocalStoreBase

      public abstract class AbstractStore<T,PK,S extends Store<T,PK,S>>
      extends Object
      implements Store<T,PK,S>

  Abstract base class for object stores.

- - ### Nested Class Summary

    Nested Classes
    Modifier and Type | Class and Description
    --- | ---
    `protected class` | `AbstractStore.ExtractorTask<V>`
    `protected class` | `AbstractStore.IndexizationTask<V>`
    `protected class` | `AbstractStore.QueryCallable<V>`
    `protected class` | `AbstractStore.QueryItemCallable<V>`
    `protected class` | `AbstractStore.QueryMultipleCallable<V>`
    `protected class` | `AbstractStore.RemoveTask`
    `protected class` | `AbstractStore.SelectorTask`
    `protected class` | `AbstractStore.UpdateCallable`
    `protected class` | `AbstractStore.ValueSelectorTask<V>`

    - ### Nested classes/interfaces inherited from interface com.hammurapi.store.Store

      `Store.Handle<T,PK,S extends Store<T,PK,S>>, Store.QueryTask<T,PK,S extends Store<?,PK,S>>, Store.StoreIterator<T>, Store.UpdateTask<T,PK,S extends Store<T,PK,S>>, Store.ViewType`

  - ### Constructor Summary

    Constructors
    Constructor and Description
    `AbstractStore()`

  - ### Method Summary

    Methods
    Modifier and Type | Method and Description
    --- | ---
    `<V,ST extends T> Index<T,ST,PK,V,S>` | `addIndex(Predicate<T,S> predicate, Extractor<ST,V,S> extractor, Index.Type type, boolean ordered, Comparator<V> comparator)` Adds index to the store to speed-up extraction operations.
    `void` | `clear()` Removes all entries from the store.
    `protected abstract Map<S,Map<Extractor<T,? super PK,S>,? super PK>>` | `createCache()`
    `protected abstract S` | `createDeputy()` Creates a store which encapsulates current call state (e.g.
    `protected abstract Store.Handle<T,PK,S>` | `createHandle(T obj, PK primaryKey, Map<S,Map<Extractor<T,? super PK,S>,? super PK>> cache, Predicate<T,S>[] validators)` Creates handle.
    `protected Store.Handle<T,PK,S>` | `createHandle(T obj, Predicate<T,S>[] validators)`
    `protected abstract <V,ST extends T> AbstractIndex<T,ST,PK,V,S>` | `createIndex(Predicate<T,S> predicate, Extractor<ST,V,S> extractor, Index.Type type, boolean ordered, Comparator<V> comparator)`
    `protected abstract Collection<Store.Handle<T,PK,S>>` | `createIndexCollection(boolean unique, boolean ordered, Comparator<T> comparator)` Creates collection for index entries
    `protected abstract S` | `createLiveView(Predicate<T,S> selector)`
    `protected abstract ReadWriteLock` | `createMasterLock()`
    `S` | `createView(Predicate<T,S> selector, Store.ViewType viewType)` Creates a view - a store which sees only objects matching the given selector.
    `Iterable<T>` | `get(Predicate<T,S> selector)` Retrieves objects matching the predicate.
    `<V> Iterable<V>` | `get(Predicate<T,S> selector, Extractor<T,V,S> extractor, boolean ordered, Comparator<V> comparator)` Retrieves values from objects matching the predicate.
    `Iterable<T>` | `getAll()` Retrieves all objects from the store.
    `T` | `getByPrimaryKey(PK primaryKey)` Retrieves object with given primary key.
    `protected abstract ExecutorService` | `getExecutorService()`
    `protected Iterable<Store.Handle<T,PK,S>>` | `getHandles(Predicate<T,S> selector)`
    `protected abstract Collection<AbstractIndex<T,? extends T,PK,?,S>>` | `getIndices()`
    `<V> Iterable<V>` | `getMultiple(Predicate<T,S> selector, Extractor<T,Iterable<V>,S> extractor, Predicate<V,S> valueSelector, boolean ordered, Comparator<V> comparator)` Retrieves values from objects matching the predicate.
    `protected abstract Collection<Store.Handle<T,PK,S>>` | `getNoPkStore()` Store for the mode without primary key or pkStore values collection.
    `protected abstract Map<PK,Store.Handle<T,PK,S>>` | `getPkStore()`
    `Iterator<T>` | `iterator()`
    `protected abstract void` | `onRemoved(int removed)` Invoked when some handles have been removed.
    `Store.Handle<T,PK,S>` | `put(T obj, Predicate<T,S>... validators)` Puts object to the store.
    `<V> int` | `query(Predicate<T,S> selector, Extractor<T,V,S> extractor, Store.QueryTask<V,PK,S> query)` Processes values extracted from objects matching the predicate.
    `int` | `query(Predicate<T,S> selector, Store.QueryTask<T,PK,S> query)` Processes objects matching the predicate.
    `int` | `queryAll(Store.QueryTask<T,PK,S> query)` Processes all objects in the store.
    `<V> int` | `queryMultiple(Predicate<T,S> selector, Extractor<T,Iterable<V>,S> extractor, Predicate<V,S> valueSelector, Store.QueryTask<V,PK,S> query)` Processes multiple values extracted from objects matching the predicate.
    `int` | `remove(Predicate<T,S> selector)` Removes objects matching the predicate.
    `boolean` | `remove(T obj)` Removes specified object from the store.
    `boolean` | `removeByPrimaryKey(PK primaryKey)` Removes object by primary key.
    `int` | `update(Predicate<T,S> selector, Store.UpdateTask<T,PK,S> updater)` Updates objects matching the predicate.
    `protected void` | `updateIndices(Store.Handle<T,PK,S> handle, boolean isNew)` This method is invoked by handle implementations.

    - ### Methods inherited from class java.lang.Object

      `clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

    - ### Methods inherited from interface com.hammurapi.store.Store

      `createUnmodifiableFacade, getPrimaryKeyExtractor`

    - ### Methods inherited from interface java.util.concurrent.locks.ReadWriteLock

      `readLock, writeLock`

- - ### Constructor Detail

    - #### AbstractStore

          public AbstractStore()

  - ### Method Detail

    - #### getPkStore

          protected abstract Map<PK,Store.Handle<T,PK,S>> getPkStore()

      Returns:
        Store for the mode with primary key.

    - #### getNoPkStore

          protected abstract Collection<Store.Handle<T,PK,S>> getNoPkStore()

      Store for the mode without primary key or pkStore values collection.

    - #### getExecutorService

          protected abstract ExecutorService getExecutorService()

      Returns:
        Executor service to parallel processing.

    - #### createHandle

          protected abstract Store.Handle<T,PK,S> createHandle(T obj,
                                          PK primaryKey,
                                          Map<S,Map<Extractor<T,? super PK,S>,? super PK>> cache,
                                          Predicate<T,S>[] validators)

      Creates handle.

    - #### createIndexCollection

          protected abstract Collection<Store.Handle<T,PK,S>> createIndexCollection(boolean unique,
                                                               boolean ordered,
                                                               Comparator<T> comparator)

      Creates collection for index entries
      Parameters:
        `unique` - Collection should be unique (set)
        `ordered` - Collection should be ordered by the comparator or by natural order.
        `comparator` -
      Returns:

    - #### createHandle

          protected Store.Handle<T,PK,S> createHandle(T obj,
                                          Predicate<T,S>[] validators)

    - #### createCache

          protected abstract Map<S,Map<Extractor<T,? super PK,S>,? super PK>> createCache()

      Returns:
        Cache for extractors.

    - #### createDeputy

          protected abstract S createDeputy()

      Creates a store which encapsulates current call state (e.g. locks currently held) and can be passed to executor for asynchronous execution. Delegates operate on behalf of the master store. They don't acquire locks if the master store already holds them. If the master store holds read lock then attempt of a deputy to acquire write lock will result in StoreException.
      Returns:

    - #### createMasterLock

          protected abstract ReadWriteLock createMasterLock()

    - #### onRemoved

          protected abstract void onRemoved(int removed)

      Invoked when some handles have been removed. This operation shall remove invalidated handles from store and index collections. This method is invoked outside of the write lock.
      Parameters:
        `removed` - Number of removed handles.

    - #### put

          public Store.Handle<T,PK,S> put(T obj,
                                 Predicate<T,S>... validators)

      Description copied from interface: `Store`
      Puts object to the store. If object is already present in the store, it gets updated in the store.
      Specified by:
        `put` in interface `Store<T,PK,S extends Store<T,PK,S>>`
      Parameters:
        `obj` - Object to put to the store.
        `validators` - Predicate(s) which are checked when object is retrieved from the store. If there is more than one predicate, predicates are connected with AND. If validator(s) evaluate to false, the object handle is considered invalid, i.e. object is considered removed from the store. Once validator(s) evaluate to false, they shall always evaluate to false. One of use of validators can be to expire object in the store, i.e. after some time validator returns false and object is cleared from the database. If validators evaluate to false at put time, object is not put to the store and put() returns null. Validators evaluation result is not cached.
      Returns:
        Object handle or null if validator(s) evaluate to false.

    - #### getAll

          public Iterable<T> getAll()

      Description copied from interface: `Store`
      Retrieves all objects from the store.
      Specified by:
        `getAll` in interface `Store<T,PK,S extends Store<T,PK,S>>`
      Returns:

    - #### getByPrimaryKey

          public T getByPrimaryKey(PK primaryKey)

      Description copied from interface: `Store`
      Retrieves object with given primary key.
      Specified by:
        `getByPrimaryKey` in interface `Store<T,PK,S extends Store<T,PK,S>>`
      Returns:

    - #### get

          public Iterable<T> get(Predicate<T,S> selector)

      Description copied from interface: `Store`
      Retrieves objects matching the predicate.
      Specified by:
        `get` in interface `Store<T,PK,S extends Store<T,PK,S>>`
      Returns:

    - #### getHandles

          protected Iterable<Store.Handle<T,PK,S>> getHandles(Predicate<T,S> selector)

      Parameters:
        `selector` -
      Returns:

    - #### get

          public <V> Iterable<V> get(Predicate<T,S> selector,
                            Extractor<T,V,S> extractor,
                            boolean ordered,
                            Comparator<V> comparator)

      Description copied from interface: `Store`
      Retrieves values from objects matching the predicate.
      Specified by:
        `get` in interface `Store<T,PK,S extends Store<T,PK,S>>`
      Parameters:
        `selector` - Object selector.
        `extractor` - Value extractor. If null, then object itself is returned.
        `ordered` - If true, then returned result is ordered by comparator or by natural order if comparator is null.
        `comparator` - Comparator to use for ordering.
      Returns:

    - #### getMultiple

          public <V> Iterable<V> getMultiple(Predicate<T,S> selector,
                                    Extractor<T,Iterable<V>,S> extractor,
                                    Predicate<V,S> valueSelector,
                                    boolean ordered,
                                    Comparator<V> comparator)

      Description copied from interface: `Store`
      Retrieves values from objects matching the predicate. Multiple values are retrieved from each object.
      Specified by:
        `getMultiple` in interface `Store<T,PK,S extends Store<T,PK,S>>`
      Parameters:
        `selector` - Objects shall match this predicate. If null, all objects match.
        `extractor` - Value extractor.
        `valueSelector` - Values from matched objects shall match this predicate. If null, all values match. Value selectors are executed in separate tasks from extractor. Separation of value selection logic from value extraction logic makes sense if value selection logic is relatively heavy and parallelization of its execution will reduce overall query execution time.
        `ordered` - If true, then returned result is ordered by comparator or by natural order if comparator is null.
        `comparator` - Comparator to use for ordering.
      Returns:

    - #### clear

          public void clear()

      Description copied from interface: `Store`
      Removes all entries from the store.
      Specified by:
        `clear` in interface `Store<T,PK,S extends Store<T,PK,S>>`

    - #### remove

          public boolean remove(T obj)

      Description copied from interface: `Store`
      Removes specified object from the store.
      Specified by:
        `remove` in interface `Store<T,PK,S extends Store<T,PK,S>>`
      Parameters:
        `obj` - Object to remove.
      Returns:
        true if object was removed.

    - #### removeByPrimaryKey

          public boolean removeByPrimaryKey(PK primaryKey)

      Description copied from interface: `Store`
      Removes object by primary key.
      Specified by:
        `removeByPrimaryKey` in interface `Store<T,PK,S extends Store<T,PK,S>>`
      Returns:

    - #### remove

          public int remove(Predicate<T,S> selector)

      Description copied from interface: `Store`
      Removes objects matching the predicate.
      Specified by:
        `remove` in interface `Store<T,PK,S extends Store<T,PK,S>>`
      Returns:
        number of returned objects or -1 if number is unknown.

    - #### update

          public int update(Predicate<T,S> selector,
                   Store.UpdateTask<T,PK,S> updater)

      Description copied from interface: `Store`
      Updates objects matching the predicate.
      Specified by:
        `update` in interface `Store<T,PK,S extends Store<T,PK,S>>`
      Returns:

    - #### getIndices

          protected abstract Collection<AbstractIndex<T,? extends T,PK,?,S>> getIndices()

    - #### createIndex

          protected abstract <V,ST extends T> AbstractIndex<T,ST,PK,V,S> createIndex(Predicate<T,S> predicate,
                                                                Extractor<ST,V,S> extractor,
                                                                Index.Type type,
                                                                boolean ordered,
                                                                Comparator<V> comparator)

    - #### addIndex

          public <V,ST extends T> Index<T,ST,PK,V,S> addIndex(Predicate<T,S> predicate,
                                                     Extractor<ST,V,S> extractor,
                                                     Index.Type type,
                                                     boolean ordered,
                                                     Comparator<V> comparator)

      Description copied from interface: `Store`
      Adds index to the store to speed-up extraction operations.
      Specified by:
        `addIndex` in interface `Store<T,PK,S extends Store<T,PK,S>>`
      Type Parameters:
        `V` - Value type extracted by extractor for ordering.
        `ST` - Type of index elements, which can be a sub-type of store element type. index creation parameters.
      Parameters:
        `predicate` - Only store items matching the predicate are indexed.
        `extractor` - Extractor of the index key.
        `ordered` - If true index is ordered.
        `comparator` - Comparator to use for ordering. If null, then natural order is used.
      Returns:
        A "mix" of Index sub-interfaces corresponding to the

    - #### queryAll

          public int queryAll(Store.QueryTask<T,PK,S> query)

      Description copied from interface: `Store`
      Processes all objects in the store.
      Specified by:
        `queryAll` in interface `Store<T,PK,S extends Store<T,PK,S>>`
      Returns:
        number of object queried.

    - #### query

          public int query(Predicate<T,S> selector,
                  Store.QueryTask<T,PK,S> query)

      Description copied from interface: `Store`
      Processes objects matching the predicate.
      Specified by:
        `query` in interface `Store<T,PK,S extends Store<T,PK,S>>`
      Returns:
        Number of objects queried.

    - #### query

          public <V> int query(Predicate<T,S> selector,
                      Extractor<T,V,S> extractor,
                      Store.QueryTask<V,PK,S> query)

      Description copied from interface: `Store`
      Processes values extracted from objects matching the predicate.
      Specified by:
        `query` in interface `Store<T,PK,S extends Store<T,PK,S>>`
        `extractor` - Extractor.
        `query` - Processor of query results.
      Returns:
        Number of objects queried.

    - #### queryMultiple

          public <V> int queryMultiple(Predicate<T,S> selector,
                              Extractor<T,Iterable<V>,S> extractor,
                              Predicate<V,S> valueSelector,
                              Store.QueryTask<V,PK,S> query)

      Description copied from interface: `Store`
      Processes multiple values extracted from objects matching the predicate.
      Specified by:
        `queryMultiple` in interface `Store<T,PK,S extends Store<T,PK,S>>`
      Parameters:
        `selector` - Objects shall match this predicate. If null, all objects match.
        `extractor` - Extractor
        `valueSelector` - Values from matched objects shall match this predicate. If null, all values match. Value selectors are executed in separate tasks from extractor. Separation of value selection logic from value extraction logic makes sense if value selection logic is relatively heavy and parallelization of its execution will reduce overall query execution time.
        `query` - Processor of query results.
      Returns:
        Number of objects queried.

    - #### updateIndices

          protected void updateIndices(Store.Handle<T,PK,S> handle,
                           boolean isNew)

      This method is invoked by handle implementations. It shall not be invoked by client code.
      Parameters:
        `handle` -
        `isNew` -

    - #### iterator

          public Iterator<T> iterator()

      Specified by:
        `iterator` in interface `Iterable<T>`

    - #### createView

          public S createView(Predicate<T,S> selector,
                     Store.ViewType viewType)

      Description copied from interface: `Store`
      Creates a view - a store which sees only objects matching the given selector.
      Specified by:
        `createView` in interface `Store<T,PK,S extends Store<T,PK,S>>`
      Parameters:
        `selector` - View selector.
      Returns:
        Store which sees only objects matching give selector. The returned store is backed by the master store.

    - #### createLiveView

          protected abstract S createLiveView(Predicate<T,S> selector)