K - Key type.V - Value type.I - Input type.public abstract class GridCacheLoadOnlyStoreAdapter<K,V,I> extends Object implements GridCacheStore<K,V>
This class processes input data in the following way:
inputIterator(Object...).
batchSize.
threadsCnt working threads.
parse(Object, Object...) method
and result is stored into cache.
Two methods should be implemented by inheritants:
inputIterator(Object...). It should open underlying data source
and iterate all record available in it. Individual records could be in very raw form,
like text lines for CSV files.
parse(Object, Object...). This method should process input records
and transform them into key-value pairs for cache.
| Modifier and Type | Field and Description |
|---|---|
static int |
DFLT_BATCH_QUEUE_SIZE
Default batch queue size (max batches count to limit memory usage).
|
static int |
DFLT_BATCH_SIZE
Default batch size (number of records read with
inputIterator(Object...)
and then submitted to internal pool at a time). |
static int |
DFLT_THREADS_COUNT
Default number of working threads (equal to the number of available processors).
|
| Constructor and Description |
|---|
GridCacheLoadOnlyStoreAdapter() |
| Modifier and Type | Method and Description |
|---|---|
int |
getBatchQueueSize()
Returns batch queue size.
|
int |
getBatchSize()
Returns batch size.
|
int |
getThreadsCount()
Returns number of worker threads.
|
protected abstract Iterator<I> |
inputIterator(Object... args)
Returns iterator of input records.
|
V |
load(GridCacheTx tx,
K key)
Loads value for the key from underlying persistent storage.
|
void |
loadAll(GridCacheTx tx,
Collection<? extends K> keys,
GridBiInClosure<K,V> c)
Loads all values for given keys and passes every value to the provided closure.
|
void |
loadCache(GridBiInClosure<K,V> c,
Object... args)
Loads all values from underlying persistent storage.
|
protected abstract GridBiTuple<K,V> |
parse(I rec,
Object... args)
This method should transform raw data records into valid key-value pairs
to be stored into cache.
|
void |
put(GridCacheTx tx,
K key,
V val)
Stores a given value in persistent storage.
|
void |
putAll(GridCacheTx tx,
Map<? extends K,? extends V> map)
Stores given key value pairs in persistent storage.
|
void |
remove(GridCacheTx tx,
K key)
Removes the value identified by given key from persistent storage.
|
void |
removeAll(GridCacheTx tx,
Collection<? extends K> keys)
Removes all vales identified by given keys from persistent storage.
|
void |
setBatchQueueSize(int batchQueueSize)
Sets batch queue size.
|
void |
setBatchSize(int batchSize)
Sets batch size.
|
void |
setThreadsCount(int threadsCnt)
Sets number of worker threads.
|
void |
txEnd(GridCacheTx tx,
boolean commit)
Tells store to commit or rollback a transaction depending on the value of the
'commit'
parameter. |
public static final int DFLT_BATCH_SIZE
inputIterator(Object...)
and then submitted to internal pool at a time).public static final int DFLT_BATCH_QUEUE_SIZE
public static final int DFLT_THREADS_COUNT
protected abstract Iterator<I> inputIterator(@Nullable Object... args) throws GridException
Note that returned iterator doesn't have to be thread-safe. Thus it could operate on raw streams, DB connections, etc. without additional synchronization.
args - Arguments passes into GridCache.loadCache(GridBiPredicate, long, Object...) method.GridException - If iterator can't be created with the given arguments.@Nullable protected abstract GridBiTuple<K,V> parse(I rec, @Nullable Object... args)
If null is returned then this record will be just skipped.
rec - A raw data record.args - Arguments passed into GridCache.loadCache(GridBiPredicate, long, Object...) method.null if no entry could be produced from this record.public void loadCache(GridBiInClosure<K,V> c, @Nullable Object... args) throws GridException
GridCache.loadCache(GridBiPredicate, long, Object...)
method is invoked which is usually to preload the cache from persistent storage.
This method is optional, and cache implementation does not depend on this
method to do anything. Default implementation of this method in
GridCacheStoreAdapter does nothing.
For every loaded value method GridBiInClosure.apply(Object, Object)
should be called on the passed in closure. The closure will then make sure
that the loaded value is stored in cache.
loadCache in interface GridCacheStore<K,V>c - Closure for loaded values.args - Arguments passes into
GridCache.loadCache(GridBiPredicate, long, Object...) method.GridException - If loading failed.public int getBatchSize()
public void setBatchSize(int batchSize)
batchSize - Batch size.public int getBatchQueueSize()
public void setBatchQueueSize(int batchQueueSize)
batchQueueSize - Batch queue size.public int getThreadsCount()
public void setThreadsCount(int threadsCnt)
threadsCnt - Number of worker threads.public V load(@Nullable GridCacheTx tx, K key) throws GridException
load in interface GridCacheStore<K,V>tx - Cache transaction.key - Key to load.null if value was not found.GridException - If load failed.public void loadAll(@Nullable GridCacheTx tx, @Nullable Collection<? extends K> keys, GridBiInClosure<K,V> c) throws GridException
For every loaded value method GridInClosure.apply(Object) should be called on
the passed in closure. The closure will then make sure that the loaded value is stored
in cache.
loadAll in interface GridCacheStore<K,V>tx - Cache transaction.keys - Collection of keys to load.c - Closure to call for every loaded element.GridException - If load failed.public void put(@Nullable GridCacheTx tx, K key, @Nullable V val) throws GridException
null.put in interface GridCacheStore<K,V>tx - Cache transaction, if write-behind is not enabled, null otherwise.key - Key to put.val - Value to put.GridException - If put failed.public void putAll(@Nullable GridCacheTx tx, @Nullable Map<? extends K,? extends V> map) throws GridException
null.putAll in interface GridCacheStore<K,V>tx - Cache transaction, if write-behind is not enabled, null otherwise.map - Values to store.GridException - If store failed.public void remove(@Nullable GridCacheTx tx, K key) throws GridException
null.remove in interface GridCacheStore<K,V>tx - Cache transaction, if write-behind is not enabled, null otherwise.key - Key to remove.GridException - If remove failed.public void removeAll(@Nullable GridCacheTx tx, @Nullable Collection<? extends K> keys) throws GridException
null.removeAll in interface GridCacheStore<K,V>tx - Cache transaction, if write-behind is not enabled, null otherwise.keys - Keys to remove.GridException - If remove failed.public void txEnd(GridCacheTx tx, boolean commit) throws GridException
'commit'
parameter.
Note that if explicit transactions are not used in code, then it is possible
to commit or rollback transactions directly in 'put(..)', or 'remove(..)'
methods. In that case, this method should be left empty (GridCacheStoreAdapter provides
empty implementation of this method).
txEnd in interface GridCacheStore<K,V>tx - Cache transaction being ended.commit - True if transaction should commit, false for rollback.GridException - If commit or rollback failed. Note that commit failure in some cases
may bring cache transaction into GridCacheTxState.UNKNOWN which will
consequently cause all transacted entries to be invalidated.Copyright © 2014. All rights reserved.