T
- the class of objects stored in this storagepublic class DatabaseStorage<T> extends ExtendedDatabaseConnection implements IntStorageIndexed<T>, java.io.Serializable
A database table that will hold the stored data must be created. The primary key of the table must be integer compatible and must be automatically generated by the database. The data column types must match the given column convertors.
Note that some database drivers (e.g. MySQL) will load the whole result set into memory which might prove problematic for large tables and full-scan search. This default behavior might be changed by providing additional parameters to the connection URL or connection properties, e.g. useCursorFetch=true&defaultFetchSize=1000 can be used for MySQL driver.
Modifier and Type | Class and Description |
---|---|
static class |
DatabaseStorage.BeanPropertyColumnConvertor<T>
Column convertor that uses a bean property to decompose/restore
a given object to/from a database storage.
|
static class |
DatabaseStorage.BinarySerializableColumnConvertor<T>
Column convertor that uses a database BLOB into which instances of
T are
binary serialized/deserialized . |
static interface |
DatabaseStorage.ColumnConvertor<T>
Provides a conversion between database column data and object instances.
|
static class |
DatabaseStorage.FixedValueColumnConvertor<T>
Column convertor that uses a given fixed value for the respective database attribute.
|
static class |
DatabaseStorage.LocalAbstractObjectTextStreamColumnConvertor<T extends LocalAbstractObject>
Column convertor that uses a database String into which instances of
T are
serialized using write
method and deserialized using LocalAbstractObject.TextStreamFactory . |
static class |
DatabaseStorage.MetaObjectTextStreamColumnConvertor<T extends MetaObject>
Column convertor that uses a database String into which instances of
an
LocalAbstractObject encapsulated in T are
serialized using write
method and deserialized using LocalAbstractObject.TextStreamFactory . |
static class |
DatabaseStorage.ParametricColumnConvertor
Column convertor that uses a
ModifiableParametric value to decompose/restore
a given object to/from a database storage. |
static interface |
DatabaseStorage.SearchableColumnConvertor<K,T>
Extension of the
DatabaseStorage.ColumnConvertor that allows a direct searching
by the column value in the database. |
ExtendedDatabaseConnection.ExtendedDatabaseConnectionPublic
Modifier and Type | Field and Description |
---|---|
static DatabaseStorage.ColumnConvertor<LocalAbstractObject> |
locatorColumnConvertor
Column convertor that stores/restores a
locator URI
of LocalAbstractObject s stored in the database. |
static DatabaseStorage.ColumnConvertor<java.lang.Object> |
trivialColumnConvertor
Column convertor that stores/restores instances supported directly by the database.
|
Constructor and Description |
---|
DatabaseStorage(java.lang.Class<? extends T> storedObjectsClass,
ExtendedDatabaseConnection dbConnection,
java.lang.String tableName,
java.lang.String primaryKeyColumn,
java.lang.String[] columnNames,
DatabaseStorage.ColumnConvertor<T>[] columnConvertors)
Constructs an empty database storage.
|
DatabaseStorage(java.lang.Class<? extends T> storedObjectsClass,
java.lang.String dbConnUrl,
java.util.Properties dbConnInfo,
java.lang.String tableName,
java.lang.String primaryKeyColumn,
java.lang.String[] columnNames,
DatabaseStorage.ColumnConvertor<T>[] columnConvertors)
Constructs an empty database storage.
|
DatabaseStorage(java.lang.Class<? extends T> storedObjectsClass,
java.lang.String dbConnUrl,
java.util.Properties dbConnInfo,
java.lang.String dbDriverClass,
java.lang.String tableName,
java.lang.String primaryKeyColumn,
java.util.Map<java.lang.String,DatabaseStorage.ColumnConvertor<T>> columns)
Constructs an empty database storage.
|
DatabaseStorage(java.lang.Class<? extends T> storedObjectsClass,
java.lang.String dbConnUrl,
java.util.Properties dbConnInfo,
java.lang.String dbDriverClass,
java.lang.String tableName,
java.lang.String primaryKeyColumn,
java.lang.String[] columnNames,
DatabaseStorage.ColumnConvertor<T>[] columnConvertors)
Constructs an empty database storage.
|
Modifier and Type | Method and Description |
---|---|
boolean |
add(T object)
Adds the specified object to this instance.
|
protected void |
closeConnection()
Closes connection to the database.
|
static <T> DatabaseStorage<T> |
create(java.lang.Class<T> storedObjectsClass,
java.util.Map<java.lang.String,java.lang.Object> parameters)
Creates a new database storage.
|
void |
destroy()
Destroy this index.
|
protected java.sql.PreparedStatement |
execute(java.sql.PreparedStatement statement,
java.lang.String sql,
java.lang.Integer primaryKey,
T object,
boolean returnGeneratedKeys)
Prepares and executes an SQL command.
|
DatabaseStorage.ColumnConvertor<T> |
getColumnConvertor(java.lang.String colName)
Retrieve a column convertor assigned for the passed column name
|
static <T extends LocalAbstractObject> |
getLocatorColumnConvertor(boolean convertFromColumn,
boolean forceReplaceLocator,
boolean convertToColumn)
Returns a column convertor that stores/restores a
locator URI
of LocalAbstractObject s stored in the database. |
java.lang.Class<? extends T> |
getStoredObjectsClass()
Returns the class of objects that the this storage works with.
|
boolean |
isEmpty()
Returns true if this storage contains no elements.
|
T |
read(int address)
Reads the object stored at the specified address in this storage.
|
void |
remove(int address)
Removes the object stored at the specified address in this storage.
|
IntStorageSearch<T> |
search()
Returns a search for all objects in this index.
|
<C> IntStorageSearch<T> |
search(IndexComparator<? super C,? super T> comparator,
C key)
Returns a search for objects in this index that have any of the specified keys.
|
<C> IntStorageSearch<T> |
search(IndexComparator<? super C,? super T> comparator,
C from,
C to)
Returns a search for objects in this index that are within the specified key-range.
|
<C> IntStorageSearch<T> |
search(IndexComparator<? super C,? super T> comparator,
java.util.Collection<? extends C> keys)
Returns a search for objects in this index that have any of the specified keys.
|
int |
size()
Returns the number of elements in this storage.
|
IntAddress<T> |
store(T object)
Stores an object in this storage.
|
void |
update(int address,
T object)
Updates the object on address
address with data from object . |
static <T> DatabaseStorage.ColumnConvertor<T> |
wrapConvertor(DatabaseStorage.ColumnConvertor<T> convertor,
boolean usedToRead,
boolean usedToWrite,
boolean skipReadIfNotNull)
Wraps a column convertor while changing its read/write capabilities.
|
createConnection, createDriver, executeDataManipulation, executeSingleValue, finalize, getConnection, prepareAndExecute, prepareAndExecute, resultSetToMap, toString
clone, equals, getClass, hashCode, notify, notifyAll, wait, wait, wait
finalize
public static final DatabaseStorage.ColumnConvertor<LocalAbstractObject> locatorColumnConvertor
locator URI
of LocalAbstractObject
s stored in the database. Note that the associated column must
be String
-compatible. When restoring an object, its key is set only if it has no key yet.public static final DatabaseStorage.ColumnConvertor<java.lang.Object> trivialColumnConvertor
Integer
s or Double
s, BLOBs as byte[] arrays, etc.
Note that this convertor states a compatibility with LocalAbstractObjectOrder.trivialObjectComparator
since practically all database types are inherently Comparable
.public DatabaseStorage(java.lang.Class<? extends T> storedObjectsClass, ExtendedDatabaseConnection dbConnection, java.lang.String tableName, java.lang.String primaryKeyColumn, java.lang.String[] columnNames, DatabaseStorage.ColumnConvertor<T>[] columnConvertors) throws java.lang.IllegalArgumentException, java.sql.SQLException
storedObjectsClass
- the class of objects that the storage will work withdbConnection
- tableName
- the name of the table in the databaseprimaryKeyColumn
- the name of the column that is the primary key of the tablecolumnNames
- the names of columns where the data are storedcolumnConvertors
- the convertors that convert between the storage instances and database column;
there must be one convertor for every data column name from columnNames
java.lang.IllegalArgumentException
- if the column names and column convertors do not matchjava.sql.SQLException
- if there was a problem connecting to the databasepublic DatabaseStorage(java.lang.Class<? extends T> storedObjectsClass, java.lang.String dbConnUrl, java.util.Properties dbConnInfo, java.lang.String dbDriverClass, java.lang.String tableName, java.lang.String primaryKeyColumn, java.lang.String[] columnNames, DatabaseStorage.ColumnConvertor<T>[] columnConvertors) throws java.lang.IllegalArgumentException, java.sql.SQLException
storedObjectsClass
- the class of objects that the storage will work withdbConnUrl
- the database connection URL (e.g. "jdbc:mysql://localhost/somedb")dbConnInfo
- additional parameters of the connection (e.g. "user" and "password")dbDriverClass
- class of the database driver to use (can be null if the driver is already registered)tableName
- the name of the table in the databaseprimaryKeyColumn
- the name of the column that is the primary key of the tablecolumnNames
- the names of columns where the data are storedcolumnConvertors
- the convertors that convert between the storage instances and database column;
there must be one convertor for every data column name from columnNames
java.lang.IllegalArgumentException
- if the column names and column convertors do not matchjava.sql.SQLException
- if there was a problem connecting to the databasepublic DatabaseStorage(java.lang.Class<? extends T> storedObjectsClass, java.lang.String dbConnUrl, java.util.Properties dbConnInfo, java.lang.String tableName, java.lang.String primaryKeyColumn, java.lang.String[] columnNames, DatabaseStorage.ColumnConvertor<T>[] columnConvertors) throws java.lang.IllegalArgumentException, java.sql.SQLException
storedObjectsClass
- the class of objects that the storage will work withdbConnUrl
- the database connection URL (e.g. "jdbc:mysql://localhost/somedb")dbConnInfo
- additional parameters of the connection (e.g. "user" and "password")tableName
- the name of the table in the databaseprimaryKeyColumn
- the name of the column that is the primary key of the tablecolumnNames
- the names of columns where the data are storedcolumnConvertors
- the convertors that convert between the storage instances and database column;
there must be one convertor for every data column name from columnNames
java.lang.IllegalArgumentException
- if the column names and column convertors do not matchjava.sql.SQLException
- if there was a problem connecting to the databasepublic DatabaseStorage(java.lang.Class<? extends T> storedObjectsClass, java.lang.String dbConnUrl, java.util.Properties dbConnInfo, java.lang.String dbDriverClass, java.lang.String tableName, java.lang.String primaryKeyColumn, java.util.Map<java.lang.String,DatabaseStorage.ColumnConvertor<T>> columns) throws java.lang.IllegalArgumentException, java.sql.SQLException
storedObjectsClass
- the class of objects that the storage will work withdbConnUrl
- the database connection URL (e.g. "jdbc:mysql://localhost/somedb")dbConnInfo
- additional parameters of the connection (e.g. "user" and "password")dbDriverClass
- class of the database driver to use (can be null if the driver is already registered)tableName
- the name of the table in the databaseprimaryKeyColumn
- the name of the column that is the primary key of the tablecolumns
- a map with the names of columns in keys and the convertors that convert
between the storage instances and database column in values (note
that the map's keyset and values collection should return the data ordered)java.lang.IllegalArgumentException
- if the column names and column convertors do not matchjava.sql.SQLException
- if there was a problem connecting to the databasepublic void destroy() throws java.lang.Throwable
ModifiableIndex
public DatabaseStorage.ColumnConvertor<T> getColumnConvertor(java.lang.String colName)
colName
- name of the columnnull
if not found.protected void closeConnection() throws java.sql.SQLException
closeConnection
in class ExtendedDatabaseConnection
java.sql.SQLException
- if there was an error while closing the connectionprotected final java.sql.PreparedStatement execute(java.sql.PreparedStatement statement, java.lang.String sql, java.lang.Integer primaryKey, T object, boolean returnGeneratedKeys) throws java.sql.SQLFeatureNotSupportedException, java.sql.SQLException, BucketStorageException
ResultSet
returned by the execution can be retrieved by Statement.getResultSet()
.
Note that if a SQLRecoverableException
is thrown while executing,
the current connection is closed
and the command
retried.statement
- the previous cached statement that matches the given sql
(can be null)sql
- the SQL command to prepare and execute; one parameter (using "?") for the primary key or n parameters for data can be specifiedprimaryKey
- the value for the one SQL parameter of the primary key (if null the parameter is not set)object
- the instance that is converted to database columns using columnConvertors
(if null the parameters are not set)returnGeneratedKeys
- flag whether to set the Statement.RETURN_GENERATED_KEYS
on the prepared statementjava.sql.SQLFeatureNotSupportedException
- if the Statement.RETURN_GENERATED_KEYS
is not supported by the driverjava.sql.SQLException
- if there was an unrecoverable error when parsing or executing the SQL commandBucketStorageException
- if there was an error converting a column valuepublic static <T> DatabaseStorage<T> create(java.lang.Class<T> storedObjectsClass, java.util.Map<java.lang.String,java.lang.Object> parameters) throws java.lang.IllegalArgumentException, java.sql.SQLException
T
- the class of objects that the new storage will work withstoredObjectsClass
- the class of objects that the new storage will work withparameters
- list of named parameters (see above)java.lang.IllegalArgumentException
- if the parameters specified are invalid (null values, etc.)java.sql.SQLException
- if there was a problem connecting to the databasepublic java.lang.Class<? extends T> getStoredObjectsClass()
public boolean isEmpty()
public int size()
public IntAddress<T> store(T object) throws BucketStorageException
Storage
store
in interface IntStorage<T>
store
in interface Storage<T>
object
- the object to storeBucketStorageException
- if there was an error writing the datapublic T read(int address) throws BucketStorageException
IntStorage
read
in interface IntStorage<T>
address
- the address of the object to readBucketStorageException
- if there was an error reading the datapublic void remove(int address) throws BucketStorageException, java.lang.UnsupportedOperationException
IntStorage
remove
in interface IntStorage<T>
address
- the address of the object to removeBucketStorageException
- if there was an error deleting an objectjava.lang.UnsupportedOperationException
- if this storage does not support removal of objectspublic void update(int address, T object) throws BucketStorageException, java.lang.UnsupportedOperationException
address
with data from object
.
Note that some storages may not support in-place update - the
UnsupportedOperationException
is thrown when calling this method.address
- the address of the object in the storage that is to be updatedobject
- the new object dataBucketStorageException
- if there was a problem storing the objectjava.lang.UnsupportedOperationException
- if this operation is not supported by this storagepublic boolean add(T object) throws BucketStorageException
Addible
add
in interface Addible<T>
object
- the object to be addedBucketStorageException
- if there was an error adding the objectpublic IntStorageSearch<T> search() throws java.lang.IllegalStateException
Index
search
in interface Index<T>
search
in interface ModifiableIndex<T>
search
in interface IntStorageIndexed<T>
search
in interface StorageIndexed<T>
java.lang.IllegalStateException
- if there was an error initializing the search on this indexpublic <C> IntStorageSearch<T> search(IndexComparator<? super C,? super T> comparator, C key) throws java.lang.IllegalStateException
Index
key
need not necessarily be of the same class as the objects stored
in this index and also consistency with equals
is not required.
Note that objects are not necessarily returned in the order defined by the comparator
search
in interface Index<T>
search
in interface ModifiableIndex<T>
search
in interface IntStorageIndexed<T>
search
in interface StorageIndexed<T>
C
- the type of the key used by the searchcomparator
- compares the key
with the stored objectskey
- the key to search forjava.lang.IllegalStateException
- if there was an error initializing the search on this indexpublic <C> IntStorageSearch<T> search(IndexComparator<? super C,? super T> comparator, java.util.Collection<? extends C> keys) throws java.lang.IllegalStateException
Index
key
need not necessarily be of the same class as the objects stored
in this index and also consistency with equals
is not required.
Note that objects are not necessarily returned in the order defined by the comparator
search
in interface Index<T>
search
in interface ModifiableIndex<T>
search
in interface IntStorageIndexed<T>
search
in interface StorageIndexed<T>
C
- the type of the keys used by the searchcomparator
- compares the keys
with the stored objectskeys
- the keys to search for (at least one key must be given)java.lang.IllegalStateException
- if there was an error initializing the search on this indexpublic <C> IntStorageSearch<T> search(IndexComparator<? super C,? super T> comparator, C from, C to) throws java.lang.IllegalStateException
Index
[from, to]
need not necessarily be of the same
class as the objects stored in this index, however, the comparator must be
able to compare the boundaries and the internal objects.
Note that objects are not returned in the order defined by the comparator
search
in interface Index<T>
search
in interface ModifiableIndex<T>
search
in interface IntStorageIndexed<T>
search
in interface StorageIndexed<T>
C
- the type the boundaries used by the searchcomparator
- compares the boundaries [from, to]
with the stored objectsfrom
- the lower bound on the searched objects, i.e. objects greater or equal are returnedto
- the upper bound on the searched objects, i.e. objects smaller or equal are returnedjava.lang.IllegalStateException
- if there was an error initializing the search on this indexpublic static <T extends LocalAbstractObject> DatabaseStorage.SearchableColumnConvertor<java.lang.String,T> getLocatorColumnConvertor(boolean convertFromColumn, boolean forceReplaceLocator, boolean convertToColumn)
locator URI
of LocalAbstractObject
s stored in the database. In fact, this method
just returns the generic-typed locatorColumnConvertor
.T
- the class of object for which to get the locator column convertorconvertFromColumn
- flag whether this column convertor is used (true)
or should be skipped (false) when the object is retrieved from the storageforceReplaceLocator
- flag whether to replace the object key by this convertor even if the key is already setconvertToColumn
- flag whether this column convertor is used (true)
or should be skipped (false) when the object is stored into the storagepublic static <T> DatabaseStorage.ColumnConvertor<T> wrapConvertor(DatabaseStorage.ColumnConvertor<T> convertor, boolean usedToRead, boolean usedToWrite, boolean skipReadIfNotNull) throws java.lang.IllegalArgumentException
T
- the column convertor classconvertor
- the convertor to wrapusedToRead
- a flag whether this column convertor is used (true) or should be skipped (false) when the object is retrieved from the storageusedToWrite
- a flag whether this column convertor is used (true) or should be skipped (false) when the object is stored into the storageskipReadIfNotNull
- a flag whether this column convertor is used to convert the value from database even if there was a value already provided (true) or not (false)java.lang.IllegalArgumentException
- if a read/write flag was set for a convertor that does not support reading/writing