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

public abstract class JdbcCacheStore<K,V> extends Object implements CacheStore<K,V>
JDBC based cache store.
  • Field Details

    • dialect

      protected JdbcDialect dialect
      Database dialect.
  • Constructor Details

    • JdbcCacheStore

      public JdbcCacheStore()
  • Method Details

    • beginSession

      public CacheStoreSession beginSession()
      Description copied from interface: CacheStore
      Begin 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:
      beginSession in interface CacheStore<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 IgniteException
      Construct 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

      protected abstract void prepareBuilders(JdbcType type) throws IgniteException
      Prepare internal store specific builders for provided types metadata.
      Parameters:
      type - The type.
      Throws:
      IgniteException - If failed to prepare internal builders for types.
    • kindForName

      protected abstract JdbcCacheStore.TypeKind kindForName(String type)
      Get type kind.
      Parameters:
      type - Type name to check.
      Returns:
      True if class not found.
    • columnIndex

      protected static Integer columnIndex(Map<String,Integer> loadColIdxs, String dbName)
      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:
      loadCacheAsync in interface CacheStore<K,V>
      Parameters:
      clo - The closure called on each loaded entry.
      args - Optional args.
      Returns:
      The future.
    • loadAsync

      public CompletableFuture<V> loadAsync(K key)
      Description copied from interface: CacheStore
      Load a single key.
      Specified by:
      loadAsync in interface CacheStore<K,V>
      Parameters:
      key - The key.
      Returns:
      The future.
    • loadAllAsync

      public CompletableFuture<Map<K,V>> loadAllAsync(Iterable<? extends K> keys)
      Description copied from interface: CacheStore
      Load a batch.
      Specified by:
      loadAllAsync in interface CacheStore<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: CacheStore
      Write a single entry to the store.
      Specified by:
      writeAsync in interface CacheStore<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: CacheStore
      Write a batch to the store.
      Specified by:
      writeAllAsync in interface CacheStore<K,V>
      Parameters:
      session - The session.
      entries - Entries.
      Returns:
      The future.
    • deleteAsync

      public CompletableFuture<Void> deleteAsync(CacheStoreSession session, K key)
      Description copied from interface: CacheStore
      Delete a key from the store.
      Specified by:
      deleteAsync in interface CacheStore<K,V>
      Parameters:
      session - The session.
      key - The key.
    • deleteAllAsync

      public CompletableFuture<Void> deleteAllAsync(CacheStoreSession session, Collection<? extends K> keys)
      Description copied from interface: CacheStore
      Delete a batch from the store.
      Specified by:
      deleteAllAsync in interface CacheStore<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

      public DataSource getDataSource()
      Get data source.
      Returns:
      Data source.
    • setDataSource

      public void setDataSource(DataSource dataSrc)
      Set data source.
      Parameters:
      dataSrc - Data source.
    • getDialect

      public JdbcDialect getDialect()
      Get database dialect.
      Returns:
      Database dialect.
    • setDialect

      public void setDialect(JdbcDialect dialect)
      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

      public JdbcType getType()
      Gets types known by store.
      Returns:
      Types known by store.
    • setType

      public void setType(JdbcType type)
      Sets store configurations.
      Parameters:
      type - The type.
    • getTransformer

      public JdbcTypesTransformer getTransformer()
      Gets types transformer.
      Returns:
      Types transformer.
    • setTransformer

      public void setTransformer(JdbcTypesTransformer transformer)
      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 0 then load sequentially.
    • setParallelLoadCacheMinimumThreshold

      public void setParallelLoadCacheMinimumThreshold(int parallelLoadCacheMinThreshold)
      Parallel load cache minimum row count threshold.
      Parameters:
      parallelLoadCacheMinThreshold - Minimum row count threshold. If 0 then load sequentially.
    • getExecutor

      public ExecutorService getExecutor()
      Get the executor for database access.
      Returns:
      The executor.
    • setExecutor

      public void setExecutor(ExecutorService executor)
      Set the executor for database access.
      Parameters:
      executor - The executor instance.
    • isSqlEscapeAll

      public boolean isSqlEscapeAll()
      If true all 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)
      If true all 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.