Package org.gridgain.cache.store.jdbc
Class JdbcCacheStore<K,V>
java.lang.Object
org.gridgain.cache.store.jdbc.JdbcCacheStore<K,V>
- All Implemented Interfaces:
CacheStore<K,V>
- Direct Known Subclasses:
JdbcPojoCacheStore,JdbcTupleCacheStore
JDBC based cache store.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionprotected static classEntry mapping description.protected static enumType kind. -
Field Summary
Fields -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionBegin a store session.protected abstract <R> RbuildObject(String typeName, JdbcCacheStore.TypeKind typeKind, JdbcTypeField[] fields, Map<String, Integer> loadColIndexes, ResultSet rs) Construct object from query result.protected static IntegercolumnIndex(Map<String, Integer> loadColIdxs, String dbName) Find column index by database name.deleteAllAsync(CacheStoreSession session, Collection<? extends K> keys) Delete a batch from the store.deleteAsync(CacheStoreSession session, K key) Delete a key from the store.protected abstract @Nullable ObjectextractParameter(String typeName, JdbcCacheStore.TypeKind typeKind, String fieldName, Object obj) Get field value from object for use as query parameter.protected intfillKeyParameters(PreparedStatement stmt, int idx, JdbcCacheStore.EntryMapping em, Object key) Fill key parameters.protected intfillKeyParameters(PreparedStatement stmt, JdbcCacheStore.EntryMapping m, Object key) Fill key parameters.protected static voidfillParameter(PreparedStatement stmt, int idx, JdbcTypeField field, @Nullable Object fieldVal) Sets the value of the designated parameter using the given object.protected intfillValueParameters(PreparedStatement stmt, int idx, JdbcCacheStore.EntryMapping em, Object val) Fill value parameters.intGet maximum batch size for delete and delete operations.Get data source.Get database dialect.Get the executor for database access.intGets maximum number of write attempts in case of database error.intParallel load cache minimum row count threshold.Gets types transformer.getType()Gets types known by store.booleanIftrueall the SQL table and field names will be escaped with double quotes like ("tableName"."fieldsName").protected abstract JdbcCacheStore.TypeKindkindForName(String type) Get type kind.loadAllAsync(Iterable<? extends K> keys) Load a batch.Load a single key.loadCacheAsync(BiConsumer<K, V> clo, @Nullable Object... args) Load a cache.protected abstract voidprepareBuilders(JdbcType type) Prepare internal store specific builders for provided types metadata.voidsetBatchSize(int batchSize) Set maximum batch size for write and delete operations.voidsetDataSource(DataSource dataSrc) Set data source.voidsetDialect(JdbcDialect dialect) Set database dialect.voidsetExecutor(ExecutorService executor) Set the executor for database access.voidsetMaximumWriteAttempts(int maxWrtAttempts) Sets maximum number of write attempts in case of database error.voidsetParallelLoadCacheMinimumThreshold(int parallelLoadCacheMinThreshold) Parallel load cache minimum row count threshold.voidsetSqlEscapeAll(boolean sqlEscapeAll) Iftrueall the SQL table and field names will be escaped with double quotes like ("tableName"."fieldsName").voidsetTransformer(JdbcTypesTransformer transformer) Sets types transformer.voidSets store configurations.writeAllAsync(CacheStoreSession session, Collection<Map.Entry<? extends K, ? extends V>> entries) Write a batch to the store.writeAsync(CacheStoreSession session, Map.Entry<? extends K, ? extends V> entry) Write a single entry to the store.
-
Field Details
-
dialect
Database dialect.
-
-
Constructor Details
-
JdbcCacheStore
public JdbcCacheStore()
-
-
Method Details
-
beginSession
Description copied from interface:CacheStoreBegin a store session.The session is the equivalent of a transaction for a cache.
The same session can be reused between multiple compatible stores, enlisting them in a cross-store transaction.
Some implementations might choose not to support sessions. In this case there is no consistency guarantee between cache and store on write failure.
- Specified by:
beginSessionin interfaceCacheStore<K,V> - Returns:
- The session.
- See Also:
-
extractParameter
@Nullable protected abstract @Nullable Object extractParameter(String typeName, JdbcCacheStore.TypeKind typeKind, String fieldName, Object obj) throws IgniteException Get field value from object for use as query parameter.- Parameters:
typeName- Type name.typeKind- Type kind.fieldName- Field name.obj- Cache object.- Returns:
- Field value from object.
- Throws:
IgniteException- in case of error.
-
buildObject
protected abstract <R> R buildObject(String typeName, JdbcCacheStore.TypeKind typeKind, JdbcTypeField[] fields, Map<String, Integer> loadColIndexes, ResultSet rs) throws IgniteExceptionConstruct object from query result.- Type Parameters:
R- Type of result object.- Parameters:
typeName- Type name.typeKind- Type kind.fields- Fields descriptors.loadColIndexes- Select query columns index.rs- ResultSet.- Returns:
- Constructed object.
- Throws:
IgniteException- If failed to construct cache object.
-
prepareBuilders
Prepare internal store specific builders for provided types metadata.- Parameters:
type- The type.- Throws:
IgniteException- If failed to prepare internal builders for types.
-
kindForName
Get type kind.- Parameters:
type- Type name to check.- Returns:
Trueif class not found.
-
columnIndex
Find column index by database name.- Parameters:
loadColIdxs- Select query columns indexes.dbName- Column name in database.- Returns:
- Column index.
- Throws:
IllegalStateException- if column not found.
-
loadCacheAsync
public CompletableFuture<Void> loadCacheAsync(BiConsumer<K, V> clo, @Nullable @Nullable Object... args) Load a cache.- Specified by:
loadCacheAsyncin interfaceCacheStore<K,V> - Parameters:
clo- The closure called on each loaded entry.args- Optional args.- Returns:
- The future.
-
loadAsync
Description copied from interface:CacheStoreLoad a single key.- Specified by:
loadAsyncin interfaceCacheStore<K,V> - Parameters:
key- The key.- Returns:
- The future.
-
loadAllAsync
Description copied from interface:CacheStoreLoad a batch.- Specified by:
loadAllAsyncin interfaceCacheStore<K,V> - Parameters:
keys- Keys.- Returns:
- The future.
-
writeAsync
public CompletableFuture<Void> writeAsync(CacheStoreSession session, Map.Entry<? extends K, ? extends V> entry) Description copied from interface:CacheStoreWrite a single entry to the store.- Specified by:
writeAsyncin interfaceCacheStore<K,V> - Parameters:
session- The session.entry- The entry.- Returns:
- The future.
-
writeAllAsync
public CompletableFuture<Void> writeAllAsync(CacheStoreSession session, Collection<Map.Entry<? extends K, ? extends V>> entries) Description copied from interface:CacheStoreWrite a batch to the store.- Specified by:
writeAllAsyncin interfaceCacheStore<K,V> - Parameters:
session- The session.entries- Entries.- Returns:
- The future.
-
deleteAsync
Description copied from interface:CacheStoreDelete a key from the store.- Specified by:
deleteAsyncin interfaceCacheStore<K,V> - Parameters:
session- The session.key- The key.
-
deleteAllAsync
public CompletableFuture<Void> deleteAllAsync(CacheStoreSession session, Collection<? extends K> keys) Description copied from interface:CacheStoreDelete a batch from the store.- Specified by:
deleteAllAsyncin interfaceCacheStore<K,V> - Parameters:
session- The session.keys- Keys.- Returns:
- The future.
-
fillParameter
protected static void fillParameter(PreparedStatement stmt, int idx, JdbcTypeField field, @Nullable @Nullable Object fieldVal) Sets the value of the designated parameter using the given object.- Parameters:
stmt- Prepare statement.idx- Index for parameters.field- Field descriptor.fieldVal- Field value.- Throws:
IgniteException- If failed to set statement parameter.
-
fillKeyParameters
protected int fillKeyParameters(PreparedStatement stmt, int idx, JdbcCacheStore.EntryMapping em, Object key) throws IgniteException Fill key parameters.- Parameters:
stmt- Prepare statement.idx- Start index for parameters.em- Entry mapping.key- Key object.- Returns:
- Next index for parameters.
- Throws:
IgniteException- If failed to set statement parameters.
-
fillKeyParameters
protected int fillKeyParameters(PreparedStatement stmt, JdbcCacheStore.EntryMapping m, Object key) throws IgniteException Fill key parameters.- Parameters:
stmt- Prepare statement.m- Type mapping description.key- Key object.- Returns:
- Next index for parameters.
- Throws:
IgniteException- If failed to set statement parameters.
-
fillValueParameters
protected int fillValueParameters(PreparedStatement stmt, int idx, JdbcCacheStore.EntryMapping em, Object val) throws IgniteException Fill value parameters.- Parameters:
stmt- Prepare statement.idx- Start index for parameters.em- Type mapping description.val- Value object.- Returns:
- Next index for parameters.
- Throws:
IgniteException- If failed to set statement parameters.
-
getDataSource
Get data source.- Returns:
- Data source.
-
setDataSource
Set data source.- Parameters:
dataSrc- Data source.
-
getDialect
Get database dialect.- Returns:
- Database dialect.
-
setDialect
Set database dialect.- Parameters:
dialect- Database dialect.
-
getMaximumWriteAttempts
public int getMaximumWriteAttempts()Gets maximum number of write attempts in case of database error.- Returns:
- Maximum number of write attempts.
-
setMaximumWriteAttempts
public void setMaximumWriteAttempts(int maxWrtAttempts) Sets maximum number of write attempts in case of database error.- Parameters:
maxWrtAttempts- Number of write attempts.
-
getType
Gets types known by store.- Returns:
- Types known by store.
-
setType
Sets store configurations.- Parameters:
type- The type.
-
getTransformer
Gets types transformer.- Returns:
- Types transformer.
-
setTransformer
Sets types transformer.- Parameters:
transformer- Types transformer.
-
getBatchSize
public int getBatchSize()Get maximum batch size for delete and delete operations.- Returns:
- Maximum batch size.
-
setBatchSize
public void setBatchSize(int batchSize) Set maximum batch size for write and delete operations.- Parameters:
batchSize- Maximum batch size.
-
getParallelLoadCacheMinimumThreshold
public int getParallelLoadCacheMinimumThreshold()Parallel load cache minimum row count threshold.- Returns:
- If
0then load sequentially.
-
setParallelLoadCacheMinimumThreshold
public void setParallelLoadCacheMinimumThreshold(int parallelLoadCacheMinThreshold) Parallel load cache minimum row count threshold.- Parameters:
parallelLoadCacheMinThreshold- Minimum row count threshold. If0then load sequentially.
-
getExecutor
Get the executor for database access.- Returns:
- The executor.
-
setExecutor
Set the executor for database access.- Parameters:
executor- The executor instance.
-
isSqlEscapeAll
public boolean isSqlEscapeAll()Iftrueall the SQL table and field names will be escaped with double quotes like ("tableName"."fieldsName"). This enforces case sensitivity for field names and also allows having special characters in table and field names.- Returns:
- Flag value.
-
setSqlEscapeAll
public void setSqlEscapeAll(boolean sqlEscapeAll) Iftrueall the SQL table and field names will be escaped with double quotes like ("tableName"."fieldsName"). This enforces case sensitivity for field names and also allows having special characters in table and field names.- Parameters:
sqlEscapeAll- Flag value.
-