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

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

public final class AndroidStorage
extends SerializedContextStorage
implements DBConstants

Context store storage implementation based on the Android SQLite API.

The table layout is according to DBConstants. The table is stored in the database file OSAMI.

An example query on the context store table 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 LIMIT 1";
        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.

Be aware, that storing of context events happens in a background thread, therefore it is not guaranteed that a context event is immediately stored after calling SerializedContextStorage.store(ContextEvent).

See Also:
DBConstants

Field Summary
 
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
AndroidStorage(android.content.Context context)
           
 
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.
protected  void finalize()
           
 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, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

AndroidStorage

public AndroidStorage(android.content.Context context)
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...)

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

queryLazy

public java.lang.Iterable<ContextEvent> queryLazy(boolean deserialize,
                                                  java.lang.String query,
                                                  java.lang.Object... params)
                                           throws java.lang.UnsupportedOperationException
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
Throws:
java.lang.UnsupportedOperationException - if the underlying storage implementation does not support lazy querying
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)
                                          throws java.lang.UnsupportedOperationException
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
Throws:
java.lang.UnsupportedOperationException - if the underlying storage implementation does not support lazy querying
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)

finalize

protected void finalize()
                 throws java.lang.Throwable
Overrides:
finalize in class java.lang.Object
Throws:
java.lang.Throwable