com.sag.osami.contextstore.storage.impl
Class JDBCStorage

java.lang.Object
  extended by com.sag.osami.contextstore.storage.SerializedContextStorage
      extended by com.sag.osami.contextstore.storage.impl.JDBCStorage
All Implemented Interfaces:
IContextStorage, DBConstants, ISerializedContextStorage

public final class JDBCStorage
extends SerializedContextStorage
implements DBConstants

Context storage implementation based on JDBC. It is database-agnostic by using a IJDBCDialect interface which provides all database-specific SQL statements. By default, DerbyDialect is used.

It is configurable using the ContextStoreBuilder. Following properties can be configured:

If a JDBC URL is configured, this class will try to derive the used dialect from this URL. For the JDBC URL jdbc:mydb://..., this dialect class will be used:
 com.sag.osami.contextstore.storage.jdbc.MydbDialect
 
The table layout is according to DBConstants.

An example query on the context store table, when using Derby, would look like this (example fetches last context event with given origin and type):

 public static ContextEvent queryLast(String type, String originID, IContextStore contextStore) {
        String queryGetLastItem = "SELECT * FROM contextstore WHERE type = ? AND originID = ? ORDER BY timestamp FETCH ONLY LAST ROW";
        IContextEvent[] tmp = contextStore.query(queryGetLastItem, type, originID);
        if (tmp.length != 0)
                return tmp[0];
        else
                return null;
 }
 

Please note, that all column names (see table layout in DBConstants) of the ContextStore table must be in the result. While it is most convenient to use "SELECT * FROM contextstore ..." to ensure this, it is not mandatory. The only requirement is, that the result contains at least all column names, therefore allowing complex queries (JOINs etc.) to be performed.

OSGi notice: To conveniently use other dialects than Derby in an OSGi environment, deploy the according fragment bundle (MySQL, PostgreSQL and SQLite are available) and further deploy a configuration file in another fragment bundle (with the ContextStore bundle as its host). Another way is to register your own IJDBCDialect implementation as an OSGi service before the ContextStore initialization.

See Also:
IJDBCDialect, ContextStoreBuilder, DBConstants

Field Summary
static java.lang.String CONFIG_AUTODROP
           
static java.lang.String CONFIG_PASSWORD
           
static java.lang.String CONFIG_URL
           
static java.lang.String CONFIG_USERNAME
           
 
Fields inherited from class com.sag.osami.contextstore.storage.SerializedContextStorage
serializer
 
Fields inherited from interface com.sag.osami.contextstore.storage.DBConstants
COL_CTXSTORE_ID, COL_DATA, COL_DATA_CLASS, COL_ID, COL_ORIGINID, COL_TIMESTAMP, COL_TYPE, COLS, TABLE_AND_COLS, TABLE_NAME
 
Constructor Summary
JDBCStorage(IJDBCDialect dialect, java.util.Properties jdbcProps)
          Creates a JDBC-based storage with the given dialect, taking the JDBC URL etc. from the given properties
JDBCStorage(IJDBCDialect dialect, java.lang.String jdbcUrl, boolean autodrop)
          Creates a JDBC-based storage with the given dialect
JDBCStorage(IJDBCDialect dialect, java.lang.String jdbcUrl, java.lang.String jdbcUsername, java.lang.String jdbcPassword, boolean autodrop)
          Creates a JDBC-based storage with the given dialect
JDBCStorage(java.util.Properties jdbcProps)
          Creates a JDBC-based storage, taking the JDBC URL etc. from the given properties, auto-detecting the dialect based on the JDBC URL
JDBCStorage(java.lang.String jdbcUrl, boolean autodrop)
          Creates a JDBC-based storage, auto-detecting the dialect based on the JDBC URL
JDBCStorage(java.lang.String jdbcUrl, java.lang.String jdbcUsername, java.lang.String jdbcPassword, boolean autodrop)
          Creates a JDBC-based storage, auto-detecting the dialect based on the JDBC URL
 
Method Summary
 void delete(java.util.List<java.lang.Long> contextIds)
          Deletes several context events with given IDs.
 void delete(java.lang.Long contextId)
          Deletes a context event with given ID.
 void delete(java.lang.String query, java.lang.Object... params)
          Deletes several context events defined by the query.
 ContextEvent[] load(boolean deserialize, java.util.List<java.lang.Long> contextIds)
          Loads several context events with the given IDs.
 ContextEvent load(boolean deserialize, java.lang.Long contextId)
          Loads a context event with the given ID.
 java.lang.Iterable<ContextEvent> loadLazy(boolean deserialize, java.util.List<java.lang.Long> contextIds)
          Lazy variant of ISerializedContextStorage.load(boolean, List), i.e. it loads one ContextEvent at a time during iteration.
 ContextEvent[] query(boolean deserialize, java.lang.String query, java.lang.Object... params)
          Queries several context events.
 java.lang.Iterable<ContextEvent> queryLazy(boolean deserialize, java.lang.String query, java.lang.Object... params)
          Lazy variant of ISerializedContextStorage.query(boolean, String, Object...), i.e. it loads one ContextEvent at a time during iteration.
 java.lang.Long retrieveLastID()
          ContextStore uses the result of this method as the offset for its auto-incremented context event IDs.
 void store(boolean serialize, ContextEvent context)
          Stores a context event object persistently.
 
Methods inherited from class com.sag.osami.contextstore.storage.SerializedContextStorage
load, load, loadLazy, query, queryLazy, setSerializer, store
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

CONFIG_URL

public static final java.lang.String CONFIG_URL
See Also:
Constant Field Values

CONFIG_AUTODROP

public static final java.lang.String CONFIG_AUTODROP
See Also:
Constant Field Values

CONFIG_USERNAME

public static final java.lang.String CONFIG_USERNAME
See Also:
Constant Field Values

CONFIG_PASSWORD

public static final java.lang.String CONFIG_PASSWORD
See Also:
Constant Field Values
Constructor Detail

JDBCStorage

public JDBCStorage(IJDBCDialect dialect,
                   java.util.Properties jdbcProps)
Creates a JDBC-based storage with the given dialect, taking the JDBC URL etc. from the given properties

Parameters:
dialect - the JDBC dialect to be used
jdbcProps - the configuration properties

JDBCStorage

public JDBCStorage(java.util.Properties jdbcProps)
Creates a JDBC-based storage, taking the JDBC URL etc. from the given properties, auto-detecting the dialect based on the JDBC URL

Parameters:
jdbcProps - the configuration properties

JDBCStorage

public JDBCStorage(java.lang.String jdbcUrl,
                   boolean autodrop)
Creates a JDBC-based storage, auto-detecting the dialect based on the JDBC URL

Parameters:
jdbcUrl - the JDBC URL
autodrop - whether the context store table should be auto-dropped

JDBCStorage

public JDBCStorage(java.lang.String jdbcUrl,
                   java.lang.String jdbcUsername,
                   java.lang.String jdbcPassword,
                   boolean autodrop)
Creates a JDBC-based storage, auto-detecting the dialect based on the JDBC URL

Parameters:
jdbcUrl - the JDBC URL
jdbcUsername - the JDBC username
jdbcPassword - the JDBC password
autodrop - whether the context store table should be auto-dropped

JDBCStorage

public JDBCStorage(IJDBCDialect dialect,
                   java.lang.String jdbcUrl,
                   boolean autodrop)
Creates a JDBC-based storage with the given dialect

Parameters:
dialect - the JDBC dialect to be used
jdbcUrl - the JDBC URL
autodrop - whether the context store table should be auto-dropped

JDBCStorage

public JDBCStorage(IJDBCDialect dialect,
                   java.lang.String jdbcUrl,
                   java.lang.String jdbcUsername,
                   java.lang.String jdbcPassword,
                   boolean autodrop)
Creates a JDBC-based storage with the given dialect

Parameters:
dialect - the JDBC dialect to be used
jdbcUrl - the JDBC URL
jdbcUsername - the JDBC username
jdbcPassword - the JDBC password
autodrop - whether the context store table should be auto-dropped
Method Detail

store

public void store(boolean serialize,
                  ContextEvent context)
Description copied from interface: ISerializedContextStorage
Stores a context event object persistently.

Specified by:
store in interface ISerializedContextStorage
Parameters:
serialize - whether the payload data should be serialized or not (because it is already serialized)
context - context event object to be stored

delete

public void delete(java.lang.Long contextId)
Description copied from interface: IContextStorage
Deletes a context event with given ID.

Specified by:
delete in interface IContextStorage
Parameters:
contextId - ID of context event to be deleted.

delete

public void delete(java.util.List<java.lang.Long> contextIds)
Description copied from interface: IContextStorage
Deletes several context events with given IDs.

Specified by:
delete in interface IContextStorage
Parameters:
contextIds - IDs of context events to be deleted.

delete

public void delete(java.lang.String query,
                   java.lang.Object... params)
Description copied from interface: IContextStorage
Deletes several context events defined by the query.

Specified by:
delete in interface IContextStorage
Parameters:
query - Defines the context events to be deleted.
params - Parameters for the query.
See Also:
IContextStorage.query(String, Object...)

queryLazy

public java.lang.Iterable<ContextEvent> queryLazy(boolean deserialize,
                                                  java.lang.String query,
                                                  java.lang.Object... params)
Description copied from interface: ISerializedContextStorage
Lazy variant of ISerializedContextStorage.query(boolean, String, Object...), i.e. it loads one ContextEvent at a time during iteration. This operation is optional.

Specified by:
queryLazy in interface ISerializedContextStorage
See Also:
ISerializedContextStorage.query(boolean, String, Object...)

query

public ContextEvent[] query(boolean deserialize,
                            java.lang.String query,
                            java.lang.Object... params)
Description copied from interface: ISerializedContextStorage
Queries several context events.

Specified by:
query in interface ISerializedContextStorage
Parameters:
deserialize - whether the payload data should remain serialized or should be deserialized
query - its language/format depends on the actual storage management class.
params - Should be used for parameter binding à la JDBC: query("SELECT * FROM CONTEXTSTORE WHERE TYPE = ? AND TIMESTAMP >= ?", "someType", someDate.getTime());
Returns:
queried context events (or a zero-length array, if there are no according context events)

loadLazy

public java.lang.Iterable<ContextEvent> loadLazy(boolean deserialize,
                                                 java.util.List<java.lang.Long> contextIds)
Description copied from interface: ISerializedContextStorage
Lazy variant of ISerializedContextStorage.load(boolean, List), i.e. it loads one ContextEvent at a time during iteration. This operation is optional.

Specified by:
loadLazy in interface ISerializedContextStorage
See Also:
ISerializedContextStorage.load(boolean, List)

load

public ContextEvent[] load(boolean deserialize,
                           java.util.List<java.lang.Long> contextIds)
Description copied from interface: ISerializedContextStorage
Loads several context events with the given IDs. The order within the result is unspecified.

Specified by:
load in interface ISerializedContextStorage
Parameters:
deserialize - whether the payload data should remain serialized or should be deserialized
contextIds - IDs of the context events
Returns:
loaded context events (or a zero-length array, if there are no context events with given IDs)

load

public ContextEvent load(boolean deserialize,
                         java.lang.Long contextId)
Description copied from interface: ISerializedContextStorage
Loads a context event with the given ID.

Specified by:
load in interface ISerializedContextStorage
Parameters:
deserialize - whether the payload data should remain serialized or should be deserialized
contextId - ID of the context event
Returns:
loaded context event (or null, if there is no context event with given ID)

retrieveLastID

public java.lang.Long retrieveLastID()
Description copied from interface: ISerializedContextStorage
ContextStore uses the result of this method as the offset for its auto-incremented context event IDs. This method is used only during the initialization of the ContextStore, it doesn't have to return the current last given ID during the whole lifetime.

Specified by:
retrieveLastID in interface ISerializedContextStorage
Returns:
the so-far highest already assigned ID of a persisted context event