Class JdbcStore

java.lang.Object
me.ahoo.cosid.store.jdbc.JdbcStore
All Implemented Interfaces:
AutoCloseable, me.ahoo.cosid.store.Store

public final class JdbcStore extends Object implements me.ahoo.cosid.store.Store
The JDBC store (06-stores.md ยง2.1): ranges and slot leases over a DataSource, which should be pooled. Every statement carries the store timeout as its queryTimeout. Closing the store does not close the data source.

The tables are those of the dialect's schema-*.sql; initSchema() creates them when missing, and adds cosid_namespace.slot_offset to a table of 5.0.0-alpha.1. Without it, such a table still serves namespaces of slot offset 0.

  • Constructor Details

    • JdbcStore

      public JdbcStore(DataSource dataSource, JdbcDialect dialect, JdbcTables tables, Duration timeout)
      A store on a database of the given dialect.
      Parameters:
      dataSource - the data source
      dialect - the dialect of its database
      tables - the table names
      timeout - the timeout of each statement, rounded up to seconds; positive
    • JdbcStore

      public JdbcStore(DataSource dataSource, JdbcDialect dialect, JdbcTables tables, Duration timeout, FetchPlan fetchPlan)
      A store on a database of the given dialect, reserving ranges with the given plan.
      Parameters:
      dataSource - the data source
      dialect - the dialect of its database
      tables - the table names
      timeout - the timeout of each statement, rounded up to seconds; positive
      fetchPlan - how a reserve reads the value it wrote (options.fetchPlan, R1-jdbc-06)
  • Method Details

    • of

      public static JdbcStore of(DataSource dataSource, Duration timeout)
      A store on the default tables, with the dialect detected from the database (JdbcDialects.detect(java.sql.Connection, java.lang.ClassLoader)).
      Parameters:
      dataSource - the data source
      timeout - the timeout of each statement; positive
      Returns:
      the store
      Throws:
      JdbcStoreException - if the database cannot be reached
      IllegalArgumentException - if no dialect supports the database
    • dialect

      public JdbcDialect dialect()
      The dialect of the database.
      Returns:
      the dialect
    • tables

      public JdbcTables tables()
      The table names.
      Returns:
      the table names
    • initSchema

      public void initSchema()
      Creates the tables that do not exist (options.initSchema); existing tables are left as they are, except that a cosid_namespace of 5.0.0-alpha.1 gets the slot_offset column it lacks (JdbcDialect.addColumn(java.lang.String, java.lang.String, java.lang.String); its rows read 0, the column default).
      Throws:
      JdbcStoreException - if a statement fails
    • addMissingIndex

      public static void addMissingIndex(Connection connection, JdbcDialect dialect, JdbcTables tables) throws SQLException
      Adds the slot table's index on (namespace, last_owner_key) (R1-jdbc-01) when the table lacks it, as a table of 5.0.0-alpha.1 does; initSchema runs it, and so does the migration tool on its target. Idempotent: when a concurrent caller created the index first, the failure is ignored.
      Parameters:
      connection - a connection that may create indexes
      dialect - the dialect
      tables - the tables
      Throws:
      SQLException - if creating the index fails and it still does not exist
    • ranges

      public me.ahoo.cosid.segment.RangeStore ranges()
      Specified by:
      ranges in interface me.ahoo.cosid.store.Store
    • slots

      public me.ahoo.cosid.lease.SlotLeaseStore slots()
      Specified by:
      slots in interface me.ahoo.cosid.store.Store
    • close

      public void close()
      Specified by:
      close in interface AutoCloseable
      Specified by:
      close in interface me.ahoo.cosid.store.Store