public class JdbcStringBasedStore<K,V> extends Object implements AdvancedLoadWriteStore<K,V>, TransactionalCacheWriter<K,V>
AdvancedCacheLoader
implementation that stores the entries in a database. In contrast to the
org.infinispan.persistence.jdbc.binary.JdbcBinaryStore
, this cache store will store each entry within a row
in the table (rather than grouping multiple entries into an row). This assures a finer grained granularity for all
operation, and better performance. In order to be able to store non-string keys, it relies on an Key2StringMapper
.
Note that only the keys are stored as strings, the values are still saved as binary data. Using a character
data type for the value column will result in unmarshalling errors.
The actual storage table is defined through configuration JdbcStringBasedStoreConfiguration
. The table can
be
created/dropped on-the-fly, at deployment time. For more details consult javadoc for JdbcStringBasedStoreConfiguration
.
It is recommended to use JdbcStringBasedStore
} over
org.infinispan.persistence.jdbc.binary.JdbcBinaryStore
} whenever it is possible, as is has a better performance.
One scenario in which this is not possible to use it though, is when you can't write an Key2StringMapper
} to map the
keys to to string objects (e.g. when you don't have control over the types of the keys, for whatever reason).
Preload.In order to support preload functionality the store needs to read the string keys from the database and transform them
into the corresponding key objects. Key2StringMapper
only supports
key to string transformation(one way); in order to be able to use preload one needs to specify an
TwoWayKey2StringMapper
, which extends Key2StringMapper
and
allows bidirectional transformation.
Rehashing. When a node leaves/joins, Infinispan moves around persistent state as part of rehashing process.
For this it needs access to the underlaying key objects, so if distribution is used, the mapper needs to be an
TwoWayKey2StringMapper
otherwise the cache won't start (same constraint as with preloading).Key2StringMapper
,
DefaultTwoWayKey2StringMapper
AdvancedCacheLoader.CacheLoaderTask<K,V>, AdvancedCacheLoader.TaskContext
AdvancedCacheWriter.PurgeListener<K>
Constructor and Description |
---|
JdbcStringBasedStore() |
Modifier and Type | Method and Description |
---|---|
void |
clear()
Removes all the data from the storage.
|
void |
commit(Transaction tx)
Commit the provided transaction's changes to the underlying store.
|
boolean |
contains(Object key)
Returns true if the storage contains an entry associated with the given key.
|
boolean |
delete(Object key) |
void |
deleteBatch(Iterable<Object> keys)
Remove all provided keys from the store in a single batch operation.
|
ConnectionFactory |
getConnectionFactory() |
TableManager |
getTableManager() |
void |
init(InitializationContext ctx)
Used to initialize a cache loader.
|
MarshalledEntry |
load(Object key)
Fetches an entry from the storage.
|
void |
prepareWithModifications(Transaction transaction,
BatchModification batchModification)
Write modifications to the store in the prepare phase, as this is the only way we know the FINAL values of the entries.
|
void |
process(KeyFilter filter,
AdvancedCacheLoader.CacheLoaderTask task,
Executor executor,
boolean fetchValue,
boolean fetchMetadata)
Iterates in parallel over the entries in the storage using the threads from the executor pool.
|
void |
purge(Executor executor,
AdvancedCacheWriter.PurgeListener purgeListener)
Using the thread in the pool, removed all the expired data from the persistence storage.
|
void |
rollback(Transaction tx)
Rollback the provided transaction's changes to the underlying store.
|
int |
size()
Returns the number of elements in the store.
|
void |
start()
Invoked on component start
|
void |
stop()
Invoked on component stop
|
void |
write(MarshalledEntry entry)
Persists the entry to the storage.
|
void |
writeBatch(Iterable<MarshalledEntry<? extends K,? extends V>> marshalledEntries)
Persist all provided entries to the store in a single batch update.
|
public void init(InitializationContext ctx)
CacheLoader
PersistenceManager
when setting up cache loaders.init
in interface CacheLoader<K,V>
init
in interface CacheWriter<K,V>
public void start()
Lifecycle
public void stop()
Lifecycle
public ConnectionFactory getConnectionFactory()
public void write(MarshalledEntry entry)
CacheWriter
write
in interface CacheWriter<K,V>
MarshalledEntry
public void writeBatch(Iterable<MarshalledEntry<? extends K,? extends V>> marshalledEntries)
CacheWriter
CacheWriter.write(MarshalledEntry)
.writeBatch
in interface CacheWriter<K,V>
marshalledEntries
- an Iterable of MarshalledEntry to be written to the store. Implementations
should take into account that the passed iterable may contain no entries.public void deleteBatch(Iterable<Object> keys)
CacheWriter
CacheWriter.delete(Object)
.deleteBatch
in interface CacheWriter<K,V>
keys
- an Iterable of entry Keys to be removed from the store. Implementations
should take into account that the passed iterable may contain no keys.public MarshalledEntry load(Object key)
CacheLoader
MarshalledEntry
needs to be created here, InitializationContext.getMarshalledEntryFactory()
and InitializationContext.getByteBufferFactory()
should be used.load
in interface CacheLoader<K,V>
public void clear()
AdvancedCacheWriter
clear
in interface AdvancedCacheWriter<K,V>
public boolean delete(Object key)
delete
in interface CacheWriter<K,V>
public void purge(Executor executor, AdvancedCacheWriter.PurgeListener purgeListener)
AdvancedCacheWriter
When this method returns all entries will be purged and no tasks will be running due to this loader in the provided executor. If however an exception is thrown there could be tasks still pending or running in the executor.
purge
in interface AdvancedCacheWriter<K,V>
public boolean contains(Object key)
CacheLoader
contains
in interface CacheLoader<K,V>
public void process(KeyFilter filter, AdvancedCacheLoader.CacheLoaderTask task, Executor executor, boolean fetchValue, boolean fetchMetadata)
AdvancedCacheLoader
CacheLoaderTask#processEntry(MarshalledEntry, TaskContext)
is
invoked. Before passing an entry to the callback task, the entry should be validated against the filter.
Implementors should build an AdvancedCacheLoader.TaskContext
instance (implementation) that is fed to the AdvancedCacheLoader.CacheLoaderTask
on every invocation. The AdvancedCacheLoader.CacheLoaderTask
might invoke AdvancedCacheLoader.TaskContext.stop()
at any time, so implementors of this method
should verify TaskContext's state for early termination of iteration. The method should only return once the
iteration is complete or as soon as possible in the case TaskContext.stop() is invoked.process
in interface AdvancedCacheLoader<K,V>
filter
- to validate which entries should be feed into the task. Might be null.task
- callback to be invoked in parallel for each stored entry that passes the filter checkexecutor
- an external thread pool to be used for parallel iterationfetchValue
- whether or not to fetch the value from the persistent store. E.g. if the iteration is
intended only over the key set, no point fetching the values from the persistent store as
wellfetchMetadata
- whether or not to fetch the metadata from the persistent store. E.g. if the iteration is
intended only ove the key set, then no pint fetching the metadata from the persistent store
as wellpublic void prepareWithModifications(Transaction transaction, BatchModification batchModification) throws PersistenceException
TransactionalCacheWriter
prepareWithModifications
in interface TransactionalCacheWriter<K,V>
transaction
- the current transactional context.batchModification
- an object containing the write/remove operations required for this transaction.PersistenceException
- if an error occurs when communicating/performing writes on the underlying store.public void commit(Transaction tx)
TransactionalCacheWriter
commit
in interface TransactionalCacheWriter<K,V>
tx
- the current transactional context.public void rollback(Transaction tx)
TransactionalCacheWriter
rollback
in interface TransactionalCacheWriter<K,V>
tx
- the current transactional context.public int size()
AdvancedCacheLoader
size
in interface AdvancedCacheLoader<K,V>
public TableManager getTableManager()
Copyright © 2018 JBoss, a division of Red Hat. All rights reserved.