Package io.ocfl.core.db
Class BaseObjectDetailsDatabase
- java.lang.Object
-
- io.ocfl.core.db.BaseObjectDetailsDatabase
-
- All Implemented Interfaces:
ObjectDetailsDatabase
- Direct Known Subclasses:
H2ObjectDetailsDatabase
,MariaDbObjectDetailsDatabase
,PostgresObjectDetailsDatabase
public abstract class BaseObjectDetailsDatabase extends Object implements ObjectDetailsDatabase
-
-
Constructor Summary
Constructors Constructor Description BaseObjectDetailsDatabase(String tableName, DataSource dataSource, boolean storeInventory, long waitTime, TimeUnit timeUnit, String lockFailCode)
-
Method Summary
All Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description void
addObjectDetails(Inventory inventory, String inventoryDigest, byte[] inventoryBytes)
Adds ObjectDetails to the database.void
deleteAllDetails()
Removes all ObjectDetails from the databaseprotected String
deleteAllQuery(String tableName)
Constructs the query for deleting all object details entriesprotected String
deleteDetailsQuery(String tableName)
Constructs the query for deleting object details entriesvoid
deleteObjectDetails(String objectId)
Removes ObjectDetails from the database.protected String
insertDetailsQuery(String tableName)
Constructs the query for inserting object details entriesprotected abstract boolean
isConcurrentWriteException(SQLException exception)
Checks if given exception was thrown because of concurrent write issue or something else.OcflObjectDetails
retrieveObjectDetails(String objectId)
Retrieves ObjectDetails from the database.protected String
rowLockQuery(String tableName)
Constructs the query for locking object details entriesprotected String
selectDetailsQuery(String tableName)
Constructs the query for selecting object details entriesprotected String
selectDigestQuery(String tableName)
Constructs the query for selecting inventory digestsprotected abstract void
setLockWaitTimeout(Connection connection, long waitMillis)
Sets the amount of time to wait for a row lock before timing out.protected String
updateDetailsQuery(String tableName)
Constructs the query for updating object details entriesvoid
updateObjectDetails(Inventory inventory, String inventoryDigest, Path inventoryFile, Runnable runnable)
Updates existing ObjectDetails in the database.
-
-
-
Constructor Detail
-
BaseObjectDetailsDatabase
public BaseObjectDetailsDatabase(String tableName, DataSource dataSource, boolean storeInventory, long waitTime, TimeUnit timeUnit, String lockFailCode)
-
-
Method Detail
-
setLockWaitTimeout
protected abstract void setLockWaitTimeout(Connection connection, long waitMillis) throws SQLException
Sets the amount of time to wait for a row lock before timing out. Keep in mind that MariaDB actually uses seconds for timeout, make sure that you use at least 1000ms as input to the method and go up in 1 second increments.- Parameters:
connection
- db connectionwaitMillis
- time to wait for the lock in millis- Throws:
SQLException
- on sql error
-
isConcurrentWriteException
protected abstract boolean isConcurrentWriteException(SQLException exception)
Checks if given exception was thrown because of concurrent write issue or something else. Check is performed by getting the SQL State Code from exception, but since each driver and vendor uses their own codes, or even several codes this check needs to be driver specific. H2 for example throws a "duplicate key" state code, while MariaDB can throw either "deadlock" or "duplicate key" state codes.- Parameters:
exception
- instance of an SQLException class to check- Returns:
- true if exception occurred because of concurrent write, false for any other exception
-
selectDetailsQuery
protected String selectDetailsQuery(String tableName)
Constructs the query for selecting object details entries- Parameters:
tableName
- the name of the object details table- Returns:
- the query string
-
deleteDetailsQuery
protected String deleteDetailsQuery(String tableName)
Constructs the query for deleting object details entries- Parameters:
tableName
- the name of the object details table- Returns:
- the query string
-
rowLockQuery
protected String rowLockQuery(String tableName)
Constructs the query for locking object details entries- Parameters:
tableName
- the name of the object details table- Returns:
- the query string
-
updateDetailsQuery
protected String updateDetailsQuery(String tableName)
Constructs the query for updating object details entries- Parameters:
tableName
- the name of the object details table- Returns:
- the query string
-
insertDetailsQuery
protected String insertDetailsQuery(String tableName)
Constructs the query for inserting object details entries- Parameters:
tableName
- the name of the object details table- Returns:
- the query string
-
selectDigestQuery
protected String selectDigestQuery(String tableName)
Constructs the query for selecting inventory digests- Parameters:
tableName
- the name of the object details table- Returns:
- the query string
-
deleteAllQuery
protected String deleteAllQuery(String tableName)
Constructs the query for deleting all object details entries- Parameters:
tableName
- the name of the object details table- Returns:
- the query string
-
retrieveObjectDetails
public OcflObjectDetails retrieveObjectDetails(String objectId)
Retrieves ObjectDetails from the database. If no details can be found, null is returned.- Specified by:
retrieveObjectDetails
in interfaceObjectDetailsDatabase
- Parameters:
objectId
- the OCFL object id- Returns:
- ObjectDetails or null
-
addObjectDetails
public void addObjectDetails(Inventory inventory, String inventoryDigest, byte[] inventoryBytes)
Adds ObjectDetails to the database. In the case of a concurrent update, this operation will only fail if the inventory digests are different.- Specified by:
addObjectDetails
in interfaceObjectDetailsDatabase
- Parameters:
inventory
- the object's inventoryinventoryDigest
- the digest of the inventoryinventoryBytes
- the serialized inventory bytes
-
updateObjectDetails
public void updateObjectDetails(Inventory inventory, String inventoryDigest, Path inventoryFile, Runnable runnable)
Updates existing ObjectDetails in the database. The update is executed within a transaction. Before the transaction is committed, the supplied runnable is executed. The transaction is only committed if the runnable completes without exception.- Specified by:
updateObjectDetails
in interfaceObjectDetailsDatabase
- Parameters:
inventory
- the object's inventoryinventoryDigest
- the digest of the inventoryinventoryFile
- the path to the inventory on diskrunnable
- the code to execute within the update transaction
-
deleteObjectDetails
public void deleteObjectDetails(String objectId)
Removes ObjectDetails from the database.- Specified by:
deleteObjectDetails
in interfaceObjectDetailsDatabase
- Parameters:
objectId
- the OCFL object id
-
deleteAllDetails
public void deleteAllDetails()
Removes all ObjectDetails from the database- Specified by:
deleteAllDetails
in interfaceObjectDetailsDatabase
-
-