com.hammurapi.store

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

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

- Type Parameters:
    `T` -
    `PK` -
  All Implemented Interfaces:
    Store<T,PK,S>, Iterable<T>, ReadWriteLock
  Direct Known Subclasses:
    LocalStoreImpl.LocalDeputyStore

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

  This class operates on master's data. It uses master's locks and storage, and is aware if the master already holds a certain lock. In this case this implementation doesn't acquire its own lock but operates within master's lock because it acts on behalf of the master. Attempt to acquire write lock if the master holds read lock results in exception. Deputy stores are used by the AbstractStore implementations to operate in asynchronous tasks.

- - ### Nested Class Summary

    - ### Nested classes/interfaces inherited from class com.hammurapi.store.AbstractStore

      `AbstractStore.ExtractorTask<V>, AbstractStore.IndexizationTask<V>, AbstractStore.QueryCallable<V>, AbstractStore.QueryItemCallable<V>, AbstractStore.QueryMultipleCallable<V>, AbstractStore.RemoveTask, AbstractStore.SelectorTask, AbstractStore.UpdateCallable, 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`

  - ### Field Summary

    Fields
    Modifier and Type | Field and Description
    --- | ---
    `protected AbstractStore<T,PK,S>` | `master`

  - ### Constructor Summary

    Constructors
    Constructor and Description
    `DeputyStore(AbstractStore<T,PK,S> master, ReadWriteLock masterLock)` Creates deputy store.

  - ### 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 Map<S,Map<Extractor<T,? super PK,S>,? super PK>>` | `createCache()`
    `protected 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 <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 Collection<Store.Handle<T,PK,S>>` | `createIndexCollection(boolean unique, boolean ordered, Comparator<T> comparator)` Creates collection for index entries
    `protected ReadWriteLock` | `createMasterLock()`
    `S` | `createView(Predicate<T,S> selector, Store.ViewType viewType)` Creates a view - a store which sees only objects matching the given selector.
    `boolean` | `equals(Object obj)`
    `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 ExecutorService` | `getExecutorService()`
    `protected Collection<AbstractIndex<T,? extends T,PK,?,S>>` | `getIndices()`
    `protected AbstractStore<T,PK,S>` | `getMaster()`
    `protected Collection<Store.Handle<T,PK,S>>` | `getNoPkStore()` Store for the mode without primary key or pkStore values collection.
    `protected Map<PK,Store.Handle<T,PK,S>>` | `getPkStore()`
    `Extractor<T,PK,S>` | `getPrimaryKeyExtractor()` If primary key is set, then the store shall use equals on the value returned from the primaryKeyExtractor instead of equals on the object to compare objects for removal and update (put).
    `int` | `hashCode()`
    `Iterator<T>` | `iterator()`
    `protected void` | `onRemoved(int removed)` Invoked when some handles have been removed.
    `Store.Handle<T,PK,S>` | `put(T obj)`
    `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.
    `Lock` | `readLock()`
    `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.
    `String` | `toString()`
    `int` | `update(Predicate<T,S> selector, Store.UpdateTask<T,PK,S> updater)` Updates objects matching the predicate.
    `Lock` | `writeLock()`

    - ### Methods inherited from class com.hammurapi.store.AbstractStore

      `createDeputy, createHandle, createLiveView, getHandles, getMultiple, put, query, queryMultiple, updateIndices`

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

      `clone, finalize, getClass, notify, notifyAll, wait, wait, wait`

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

      `createUnmodifiableFacade`

- - ### Field Detail

    - #### master

          protected AbstractStore<T,PK,S extends Store<T,PK,S>> master

  - ### Constructor Detail

    - #### DeputyStore

          public DeputyStore(AbstractStore<T,PK,S> master,
                     ReadWriteLock masterLock)

      Creates deputy store.
      Parameters:
        `master` - Master store.
        `masterLock` - Lock which delegates to master lock and can be used from multiple threads/processes, i.e. the lock owner is not the current thread but the master store.

  - ### Method Detail

    - #### hashCode

          public int hashCode()

      Overrides:
        `hashCode` in class `Object`

    - #### put

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

    - #### equals

          public boolean equals(Object obj)

      Overrides:
        `equals` in class `Object`

    - #### 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>>`
      Overrides:
        `getAll` in class `AbstractStore<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>>`
      Overrides:
        `getByPrimaryKey` in class `AbstractStore<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>>`
      Overrides:
        `get` in class `AbstractStore<T,PK,S extends Store<T,PK,S>>`
      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>>`
      Overrides:
        `get` in class `AbstractStore<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:

    - #### toString

          public String toString()

      Overrides:
        `toString` in class `Object`

    - #### 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>>`
      Overrides:
        `clear` in class `AbstractStore<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>>`
      Overrides:
        `remove` in class `AbstractStore<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>>`
      Overrides:
        `removeByPrimaryKey` in class `AbstractStore<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>>`
      Overrides:
        `remove` in class `AbstractStore<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>>`
      Overrides:
        `update` in class `AbstractStore<T,PK,S extends Store<T,PK,S>>`
      Returns:

    - #### 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>>`
      Overrides:
        `addIndex` in class `AbstractStore<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

    - #### getPrimaryKeyExtractor

          public Extractor<T,PK,S> getPrimaryKeyExtractor()

      Description copied from interface: `Store`
      If primary key is set, then the store shall use equals on the value returned from the primaryKeyExtractor instead of equals on the object to compare objects for removal and update (put).

    - #### 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>>`
      Overrides:
        `queryAll` in class `AbstractStore<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>>`
      Overrides:
        `query` in class `AbstractStore<T,PK,S extends Store<T,PK,S>>`
      Returns:
        Number of objects queried.

    - #### iterator

          public Iterator<T> iterator()

      Specified by:
        `iterator` in interface `Iterable<T>`
      Overrides:
        `iterator` in class `AbstractStore<T,PK,S extends Store<T,PK,S>>`

    - #### 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>>`
      Overrides:
        `createView` in class `AbstractStore<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.

    - #### readLock

          public Lock readLock()

    - #### writeLock

          public Lock writeLock()

    - #### getPkStore

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

      Specified by:
        `getPkStore` in class `AbstractStore<T,PK,S extends Store<T,PK,S>>`
      Returns:
        Store for the mode with primary key.

    - #### getNoPkStore

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

      Description copied from class: `AbstractStore`
      Store for the mode without primary key or pkStore values collection.
      Specified by:
        `getNoPkStore` in class `AbstractStore<T,PK,S extends Store<T,PK,S>>`

    - #### getExecutorService

          protected ExecutorService getExecutorService()

      Specified by:
        `getExecutorService` in class `AbstractStore<T,PK,S extends Store<T,PK,S>>`
      Returns:
        Executor service to parallel processing.

    - #### createHandle

          protected 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)

      Description copied from class: `AbstractStore`
      Creates handle.
      Specified by:
        `createHandle` in class `AbstractStore<T,PK,S extends Store<T,PK,S>>`

    - #### createIndexCollection

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

      Description copied from class: `AbstractStore`
      Creates collection for index entries
      Specified by:
        `createIndexCollection` in class `AbstractStore<T,PK,S extends Store<T,PK,S>>`
      Parameters:
        `unique` - Collection should be unique (set)
        `ordered` - Collection should be ordered by the comparator or by natural order.
      Returns:

    - #### createCache

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

      Specified by:
        `createCache` in class `AbstractStore<T,PK,S extends Store<T,PK,S>>`
      Returns:
        Cache for extractors.

    - #### createMasterLock

          protected ReadWriteLock createMasterLock()

      Specified by:
        `createMasterLock` in class `AbstractStore<T,PK,S extends Store<T,PK,S>>`

    - #### onRemoved

          protected void onRemoved(int removed)

      Description copied from class: `AbstractStore`
      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.
      Specified by:
        `onRemoved` in class `AbstractStore<T,PK,S extends Store<T,PK,S>>`
      Parameters:
        `removed` - Number of removed handles.

    - #### getIndices

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

      Specified by:
        `getIndices` in class `AbstractStore<T,PK,S extends Store<T,PK,S>>`

    - #### createIndex

          protected <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)

      Specified by:
        `createIndex` in class `AbstractStore<T,PK,S extends Store<T,PK,S>>`

    - #### getMaster

          protected AbstractStore<T,PK,S> getMaster()