org.android.activityminer.persistence
Class DBAdapter

Package class diagram package DBAdapter
java.lang.Object
  extended by org.android.activityminer.persistence.DBAdapter

public final class DBAdapter
extends Object

The database adapter class does wrap the direct Android database access.


Nested Class Summary
private static class DBAdapter.DatabaseHelper
          The internal SQLite helper class.
static class DBAdapter.RecordingStates
          Types for possible recording states.
 
Field Summary
private static String ADD_COLUMN
          Add column table statement
private static String ALTER_TABLE
          Alter table statement
private static String AND
          The and pattern.
private static String COUNT_FIELD
          The count pattern.
private static String COUNT_RECORDING_ID
          Constant for count recording id
private static String CREATE_INDEX_IF_NOT_EXISTS
          Create index if not exists statement
static String CREATE_TABLE
          Create table statement
private  SQLiteDatabase db
          The database
private static String DB_ACTIVITY_TABLE
          The table name for the activity table to store activity labels and parent relations.
private static String DB_CONGIG_TABLE
          The table name for configuration table
private static String DB_COPY_RECORDINGS_WITH_ACTIVITY_REFERENCE
          The copy statement for upgrade from version 3 to 4(5).
private static String DB_CREATE_ACITIVITY_TABLE
          The table creation statement for the activity label table.
private static String DB_CREATE_FEATURE_VECTOR_TABLE
          The table creation statement for the feature vector table.
private static String DB_CREATE_INDEX2
          The state index creation statement.
private static String DB_CREATE_INDEX3
          The sequence identifier index creation statement.
private static String DB_CREATE_RECORDING_TABLE
          The table creation statement for the recording table.
private static String DB_DROP_FEATURE_VECTOR_TABLE
          The drop statement for upgrade from version 3 to 4.
private static String DB_DROP_INDEX1
          The activity label index drop statement for upgrade to version 4.
private static String DB_DROP_OLD_RECORDING_TABLE
          The drop statement for upgrade from version 2 to 3.
private static String DB_FEATURE_VECTOR_TABLE
          The table name for the table with the feature vectors extracted from the overlapping windows for saved recordings data.
private static String DB_FILL_IN_ACTIVITIES
          The activity insertion statement for upgrade from version 3 to 4.
static String DB_INDEX1
          The activity label index name.
static String DB_INDEX2
          The processing state index name.
static String DB_INDEX3
          The sequence identifier index name.
private static String DB_RECORDING_TABLE
          The recording table name.
private static String DB_RECREATE_INDEX1
          The new activity label index creation statement.
private static String DB_RENAME_RECORDING_TABLE
          The renaming statement for upgrades.
private static String DB_SEQUENCE_NEXT_ID_TABLE
          The recording sequences last id table name.
private static int DB_VERSION
          The database version.
private  DBAdapter.DatabaseHelper dbHelper
          The SQLite helper
private  String dbName
          The database name.
private static String DROP_INDEX_IF_EXISTS
          Drop index if exists statement
private static String DROP_TABLE
          Drop table statement
private static String FIELD_EQUALS
          The equals pattern.
private static String IN
          The in pattern.
private static String INSERT_INTO
          Insert into statement
static int INVALID_ID
          Constant for invalid id's
private static String KEY_CFG_BACKUP
          Configuration backup column
private static String KEY_DURATION
          The duration column name, for the time the recording was saved.
private static String KEY_FEATURE_VECTOR
          The feature vector column.
private static String KEY_FEATURE_VECTOR_ID
          The feature vector and row identifier column name.
private static String KEY_FRAME_DATA
          The recording data column name.
private static String KEY_FVEC_COUNT
          The feature vector count column name.
private static String KEY_LABEL
          The recording activity label column name.
private static String KEY_LABEL_ID
          The recording activity label identifier column name.
private static String KEY_PARENT_ID
          The column name for a parent activity reference
private static String KEY_REC_COUNT
          The recording count column name.
private static String KEY_RECORDING_ID
          The recording and row identifier column name.
private static String KEY_SAMPLERATE
          Sample rate column
private static String KEY_SEQUENCE_ID
          The sequence identifier column name
private static String KEY_STATE
          The recording processing state flag column name.
static String KEY_TS
          The recording time stamp column name, for the recording start time.
private static String KEY_UUID
          UUID column
private static String LENGTH_RECORDING_DATA
          Constant for length of the recording data field
private static int MAX_DATA
          Equals statement pattern.
private static String ON
          On statement
private static String ORDER_BY_FEATURE_VEC_ID_ASC
          Constant for ascending order by feature vector id
private static String ORDER_BY_RECORDING_ID_ASC
          Constant for ascending order by recording id
private static String ORDER_BY_SEQUENCE_ID_ASC
          Constant for ascending order by continuous recording sequence id
private static String ORDER_BY_SEQUENCE_ID_RECORDING_ID_ASC
          Order by clause for the recording statistics functions
private static String PLACE_HOLDER
          The place holder pattern.
private static String QUERY_FEATURE_VECTOR_COUNT_FOR_LABEL_ID
          Query to count feature vectors for labels
private static String QUERY_LABEL_IDS_WITH_EXTRACTED_FEATURE_VECTORS
          Query to find activity labels with extracted feature vectors
private static String RENAME_TO
          Rename to statement
private static String SUBSTR_RECORDING_DATA_FROM_POS
          Constant for substring of the recording data field
private static String TMP_ALIAS
          temporary result filed alias
private static String TMP_TABLE_NAME
          The backup table name for upgrade from version 2 to 3.
private static String UPDATE_ACTIVITY_REC_COUNT_WHERE_LABEL_ID_EQUALS
          Update statement for record count in the activity table
private static String UPDATE_NEXT_SEQUENCE_ID
          Update statement for the configuration table ( version 8 )
private static String UPDATE_RECORDING_FVEC_COUNT_WHERE_RECORDING_ID_EQUALS
          Update statement for feature vector count in the recording table
private static String UPDATE_RECORDING_TABLE_SET_SID_TO_RECID
          Update statement for the recording table ( version 8 )
private static String WHERE_FEATURE_VEC_ID_EQUALS
          Constant for where feature vector id equals value
private static String WHERE_LABEL_EQUALS
          Constant for where label equals value
private static String WHERE_LABEL_ID_EQUALS
          Constant for where label id equals value
private static String WHERE_RECORDING_ID_EQUALS
          Constant for where recording id equals value
private static String WHERE_SEQUENCE_ID_EQUALS
          Constant for where sequence id equals value
private static String WHERE_STATE_EQUALS
          Constant for where state equals value
 
Constructor Summary
DBAdapter(String dbName, Context context)
          Constructor
DBAdapter(String dbName, long maxDBSize, Context context)
          Constructor
 
Method Summary
 void close()
          Method to close the database
private  String createInStatement(String column, int cntValues)
          Does create an in statement for a given value list
private  String[] createStringArray(List<? extends Object> list)
          Does create a string array representation of a list of objects
 void deleteActivityLabelId(long activityLabelId)
          Does delete the activity table entry for a given activity label
 boolean deleteAllFeatureVectors()
          Method to delete all stored feature vectors at once.
protected static boolean deleteAllFeatureVectors(SQLiteDatabase db)
          Method to delete all stored feature vectors at once.
 boolean deleteAllRecordings()
          Method to delete all stored recordings at once, together with related feature vectors.
 int deleteFeatureVectors(Long recordingId)
          Method to delete all stored feature vectors for a given recording
 void deleteFeatureVectorsForLabels(List<Long> activityLabelIds)
          Method to delete all stored feature vectors for a set of labels
 int deleteRecording(long recordingId)
          Does delete the recording with the given identifier and all related features.
 int deleteRecordings(List<Long> recordingIds)
          Does delete the recordings with the given identifiers and all related features.
 int deleteRecordingsWithActivityLabel(ActivityLabel activityLabel)
          Does delete the recording with the given label.
 int deleteSequence(long sId)
          Does delete all recordings with the given sequence identifier
private  DBFeatureVector featureVectorFromCursor(Cursor cursor)
          Does create a recording from the current cursor position.
 long getActivityCount()
          Method to get the current activity count.
private  Long getActivityIdForLabelName(String activityLabelName)
          Does get the activity label identifier for a given name
private  long getActivityLabelIdForRecordingId(long recordingId)
          Getter for the activity label identifier of a recording.
private  long getActivityLabelIdForSequenceId(long sid)
          Getter for the activity label identifier of a sequence.
 List<Long> getActivityLabelIdsWithSamplesIgnoreDependecies()
          Getter for all activity labels with extracted training samples.
 String getActivityLabelNameForId(long activityLabelId)
          Does get the activity label name for a given identifier
private  Cursor getActivityLabels()
          Method to get the current activity label count from the activity table.
private static Set<Long> getAffectedActivityLabelNames(ActivityLabelMap mapActivityLabels, long activityLabelId)
          Method to extract successor activities for a given activity label identifier
 ActivityLabelMap getAvailableActivityLabels()
          Getter for a map with all available activity labels.
 List<Long> getAvailableFeatureVectorIds()
          Getter for all available feature vector identifiers.
 List<Long> getAvailableFeatureVectorIdsForActivityLabelId(long activityId)
          Getter for all available feature vector identifiers.
 List<Long> getAvailableFeatureVectorIdsForRecording(long recordingId)
          Getter for all available feature vector identifiers for a given recoding identifier.
 List<Long> getAvailableRecordingIds()
          Getter for all available recording identifiers.
 List<Long> getAvailableRecordingIds(DBAdapter.RecordingStates state)
          Getter for all available recording identifiers for a given processing state.
private  List<Long> getAvailableRecordingIds(DBAdapter.RecordingStates state, List<Long> activityLabelIds)
          Getter for all available recording identifiers for a given processing state an optional subset of labels.
 List<Long> getAvailableRecordingIdsForActivityLabelId(long activityLabelId)
          Getter for all available recording identifiers for a given activity label
private  List<Long> getAvailableRecordingIdsForActivityNames(Set<Long> activityLabelIds)
          Getter for all available recording identifiers for a given set of activity identifiers
 List<Long> getAvailableSequenceIds()
          Getter for all available recording identifiers.
 String getConfigurationFromDB()
          Does get the sample rate from an sqlite database
private  Cursor getCursorForAvailableFeatureVectorIds()
          Getter for all available feature vector identifiers
private  Cursor getCursorForAvailableFeatureVectorIds(long recordingId)
          Getter for a cursor for available feature vector identifiers for a given recording identifier.
 String getDatabaseName()
          Getter for the database name.
 DBStatistic getDBStatistic(boolean recordingsWithSamplesOnly, boolean skipSampleCounts)
          Getter for statistics about the stored feature vectors per activity label.
private  DBStatistic getDBStatistics(boolean activitiesWithSamplesOnly, boolean skipSampleCounts, ActivityLabelMap mapActivityLabels)
          Getter for statistics about the stored feature vectors and recordings per activity label.
 DBFeatureVector getFeatureVector(Long id)
          Method to retrieve a feature vector by its id.
private  Cursor getFeatureVectorById(Long id)
          Getter for a feature vector by its unique identifier.
 long getFeatureVectorCount()
          Method to get the current count of stored feature vector.
private  long getFeatureVectorCountForActivityLabelId(long activityLabelId)
          Method to get the current feature vector count for given activity label.
 long getFeatureVectorCountForRecoding(long recordingId)
          Method to get the feature vector count for a given recording identifier.
private  Cursor getLabelSequenceStateAndTimesForRecording(Long recordingId)
          Getter for a recording by its unique identifier.
 long getMaximumDatabaseSize()
          Getter for the maximum database size
 List<Long> getNewSequenceIdentifiers()
          Getter for all available new continuous recording sequence identifiers.
 long getNextRecordingSequenceId()
          Does determine the next unique sequence identifier
 long getPageSize()
          Getter for the current database page size in bytes
protected static Cursor getPartialRecordingData(SQLiteDatabase db, Long recordingId, int pos, int cnt)
          Access to substrings from recording data
 DBRecording getRecording(Long recordingId)
          Method to retrieve a recordings by its id.
 long getRecordingCount()
          Method to get the current recording count.
 long getRecordingCountForActivityLabelId(long activityId)
          Method to get the current recording count for given activity id.
 long getRecordingCountForState(DBAdapter.RecordingStates state)
          Method to get the current recording count for a given state.
private  long getRecordingCountForStateAndActivityLabelIds(DBAdapter.RecordingStates state, List<Long> listActivityLabelIds)
          Method to get the current recording count for a given state and an optional subset of label identifiers.
private static String getRecordingData(SQLiteDatabase db, Long recordingId)
          Getter for the recording data by its unique identifier.
private  Cursor getRecordingIdsForActivityLabel(Long activityId)
          Getter for available recording identifiers for a given activity label
private  Cursor getRecordingIdsForForStateAndActivityLabelIds(DBAdapter.RecordingStates state, List<Long> activityLabelIds)
          Getter for available recording identifiers for a given state.
 List<Long> getRecordingIdsForSequenceId(long sid)
          Does look up the recording identifiers of a given sequence identifier
protected static int getRecordingSize(SQLiteDatabase db, Long recordingId)
          Getter for the size of the recording data field
 DBRecordingStatistic getRecordingStatistics()
          Getter for statistics about the stored recordings.
 long getSampleRate()
          Does get the sample rate
static long getSampleRateFromDB(SQLiteDatabase db)
          Does get the sample rate from an sqlite database
 List<DBSampleStatistic> getSampleStatistics(long[] activityLabelIds)
          Method to get sample and recording statistics for the given activity labels
private  Cursor getSequenceIdentifiersForNewRecordings()
          Getter for new continuous recording sequence identifiers (by searching for unprocessed recordings).
 String getUuid()
          Does get the sample rate from an sqlite database
static int getVersion()
          Getter for the supported database version
 boolean insertActivityLabelByName(String newActivityLabel)
          Does insert an activity table entry for a new activity label
private  Long insertActivityLabelIfNotExists(ActivityLabel activitylabel)
          Method to insert a new activity label into the database.
 long insertFeatureVector(DBFeatureVector vector, Long idRecording)
          Method to insert an extracted feature vector for a given recording.
 Long insertRecording(DBRecording recording)
          Method to insert a labeled time series frame into the database.
 DBAdapter open()
          Method to open the database for write access
 DBAdapter openForRead()
          Method to open the database for read access
 boolean renameSequence(long sid, Long activityLabelId)
          Does update the activity assignment for all recordings with the given sequence identifier
 boolean requiresRawDataUpgrade()
          Test method for the requirement of an upgrade of the raw time series data (for compatibility of older DB versions)
 long setMaximumDatabaseSize(long size)
          Setter for the maximum database size
protected  void storeConfigurationToDB(String config)
          Method to update the stored configuration
 boolean updateActivityLabel(ActivityLabel activityLabel)
          Does update the activity table entry for an existing activity label.
 boolean updateAllRecordingStates(DBAdapter.RecordingStates state)
          Method to update the processing state for all existing recordings.
protected  void updateFeatureVectorCountInRecordingTable(SQLiteDatabase db, Long recordingId, int featureVecCountUpdateValue)
          Does update the feature vector count for a given recording identifier
protected  void updateRecordingCountInActivityTable(SQLiteDatabase db, long activityId, int recCountUpdateValue)
          Does update the recording count for a given activity identifier
 boolean updateRecordingData(DBRecording recording)
          Method to update the recording data.
 boolean updateRecordingState(Long recordingId, DBAdapter.RecordingStates state)
          Method to update the processing state for an existing recoding.
 void updateSampleRate(long sampleRate)
          Method to update the sample rate
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

INVALID_ID

public static final int INVALID_ID
Constant for invalid id's

See Also:
Constant Field Values

TMP_ALIAS

private static final String TMP_ALIAS
temporary result filed alias

See Also:
Constant Field Values

RENAME_TO

private static final String RENAME_TO
Rename to statement

See Also:
Constant Field Values

ALTER_TABLE

private static final String ALTER_TABLE
Alter table statement

See Also:
Constant Field Values

ADD_COLUMN

private static final String ADD_COLUMN
Add column table statement

See Also:
Constant Field Values

INSERT_INTO

private static final String INSERT_INTO
Insert into statement

See Also:
Constant Field Values

DROP_TABLE

private static final String DROP_TABLE
Drop table statement

See Also:
Constant Field Values

CREATE_TABLE

public static final String CREATE_TABLE
Create table statement

See Also:
Constant Field Values

ON

private static final String ON
On statement

See Also:
Constant Field Values

CREATE_INDEX_IF_NOT_EXISTS

private static final String CREATE_INDEX_IF_NOT_EXISTS
Create index if not exists statement

See Also:
Constant Field Values

DROP_INDEX_IF_EXISTS

private static final String DROP_INDEX_IF_EXISTS
Drop index if exists statement

See Also:
Constant Field Values

AND

private static final String AND
The and pattern.

See Also:
Constant Field Values

COUNT_FIELD

private static final String COUNT_FIELD
The count pattern.

See Also:
Constant Field Values

FIELD_EQUALS

private static final String FIELD_EQUALS
The equals pattern.

See Also:
Constant Field Values

IN

private static final String IN
The in pattern.

See Also:
Constant Field Values

PLACE_HOLDER

private static final String PLACE_HOLDER
The place holder pattern.

See Also:
Constant Field Values

MAX_DATA

private static final int MAX_DATA
Equals statement pattern.

See Also:
Constant Field Values

KEY_RECORDING_ID

private static final String KEY_RECORDING_ID
The recording and row identifier column name.

See Also:
Constant Field Values

KEY_SEQUENCE_ID

private static final String KEY_SEQUENCE_ID
The sequence identifier column name

See Also:
Constant Field Values

KEY_FRAME_DATA

private static final String KEY_FRAME_DATA
The recording data column name.

See Also:
Constant Field Values

KEY_LABEL_ID

private static final String KEY_LABEL_ID
The recording activity label identifier column name.

See Also:
Constant Field Values

KEY_LABEL

private static final String KEY_LABEL
The recording activity label column name.

See Also:
Constant Field Values

KEY_REC_COUNT

private static final String KEY_REC_COUNT
The recording count column name.

See Also:
Constant Field Values

KEY_FVEC_COUNT

private static final String KEY_FVEC_COUNT
The feature vector count column name.

See Also:
Constant Field Values

KEY_TS

public static final String KEY_TS
The recording time stamp column name, for the recording start time.

See Also:
Constant Field Values

KEY_DURATION

private static final String KEY_DURATION
The duration column name, for the time the recording was saved.

See Also:
Constant Field Values

KEY_STATE

private static final String KEY_STATE
The recording processing state flag column name.

See Also:
Constant Field Values

KEY_FEATURE_VECTOR_ID

private static final String KEY_FEATURE_VECTOR_ID
The feature vector and row identifier column name.

See Also:
Constant Field Values

KEY_PARENT_ID

private static final String KEY_PARENT_ID
The column name for a parent activity reference

See Also:
Constant Field Values

KEY_FEATURE_VECTOR

private static final String KEY_FEATURE_VECTOR
The feature vector column.

See Also:
Constant Field Values

KEY_SAMPLERATE

private static final String KEY_SAMPLERATE
Sample rate column

See Also:
Constant Field Values

KEY_UUID

private static final String KEY_UUID
UUID column

See Also:
Constant Field Values

KEY_CFG_BACKUP

private static final String KEY_CFG_BACKUP
Configuration backup column

See Also:
Constant Field Values

ORDER_BY_SEQUENCE_ID_RECORDING_ID_ASC

private static final String ORDER_BY_SEQUENCE_ID_RECORDING_ID_ASC
Order by clause for the recording statistics functions


ORDER_BY_RECORDING_ID_ASC

private static final String ORDER_BY_RECORDING_ID_ASC
Constant for ascending order by recording id


ORDER_BY_SEQUENCE_ID_ASC

private static final String ORDER_BY_SEQUENCE_ID_ASC
Constant for ascending order by continuous recording sequence id


ORDER_BY_FEATURE_VEC_ID_ASC

private static final String ORDER_BY_FEATURE_VEC_ID_ASC
Constant for ascending order by feature vector id


WHERE_LABEL_ID_EQUALS

private static final String WHERE_LABEL_ID_EQUALS
Constant for where label id equals value


WHERE_LABEL_EQUALS

private static final String WHERE_LABEL_EQUALS
Constant for where label equals value


WHERE_STATE_EQUALS

private static final String WHERE_STATE_EQUALS
Constant for where state equals value


LENGTH_RECORDING_DATA

private static final String LENGTH_RECORDING_DATA
Constant for length of the recording data field


SUBSTR_RECORDING_DATA_FROM_POS

private static final String SUBSTR_RECORDING_DATA_FROM_POS
Constant for substring of the recording data field

See Also:
Constant Field Values

WHERE_RECORDING_ID_EQUALS

private static final String WHERE_RECORDING_ID_EQUALS
Constant for where recording id equals value


WHERE_SEQUENCE_ID_EQUALS

private static final String WHERE_SEQUENCE_ID_EQUALS
Constant for where sequence id equals value


COUNT_RECORDING_ID

private static final String COUNT_RECORDING_ID
Constant for count recording id


WHERE_FEATURE_VEC_ID_EQUALS

private static final String WHERE_FEATURE_VEC_ID_EQUALS
Constant for where feature vector id equals value


DB_RECORDING_TABLE

private static final String DB_RECORDING_TABLE
The recording table name.

See Also:
Constant Field Values

DB_SEQUENCE_NEXT_ID_TABLE

private static final String DB_SEQUENCE_NEXT_ID_TABLE
The recording sequences last id table name.

See Also:
Constant Field Values

DB_FEATURE_VECTOR_TABLE

private static final String DB_FEATURE_VECTOR_TABLE
The table name for the table with the feature vectors extracted from the overlapping windows for saved recordings data.

See Also:
Constant Field Values

DB_ACTIVITY_TABLE

private static final String DB_ACTIVITY_TABLE
The table name for the activity table to store activity labels and parent relations.

See Also:
Constant Field Values

DB_CONGIG_TABLE

private static final String DB_CONGIG_TABLE
The table name for configuration table

See Also:
Constant Field Values

QUERY_FEATURE_VECTOR_COUNT_FOR_LABEL_ID

private static final String QUERY_FEATURE_VECTOR_COUNT_FOR_LABEL_ID
Query to count feature vectors for labels


QUERY_LABEL_IDS_WITH_EXTRACTED_FEATURE_VECTORS

private static final String QUERY_LABEL_IDS_WITH_EXTRACTED_FEATURE_VECTORS
Query to find activity labels with extracted feature vectors

See Also:
Constant Field Values

UPDATE_ACTIVITY_REC_COUNT_WHERE_LABEL_ID_EQUALS

private static final String UPDATE_ACTIVITY_REC_COUNT_WHERE_LABEL_ID_EQUALS
Update statement for record count in the activity table


UPDATE_RECORDING_FVEC_COUNT_WHERE_RECORDING_ID_EQUALS

private static final String UPDATE_RECORDING_FVEC_COUNT_WHERE_RECORDING_ID_EQUALS
Update statement for feature vector count in the recording table


UPDATE_RECORDING_TABLE_SET_SID_TO_RECID

private static final String UPDATE_RECORDING_TABLE_SET_SID_TO_RECID
Update statement for the recording table ( version 8 )

See Also:
Constant Field Values

UPDATE_NEXT_SEQUENCE_ID

private static final String UPDATE_NEXT_SEQUENCE_ID
Update statement for the configuration table ( version 8 )

See Also:
Constant Field Values

DB_INDEX1

public static final String DB_INDEX1
The activity label index name.

See Also:
Constant Field Values

DB_INDEX2

public static final String DB_INDEX2
The processing state index name.

See Also:
Constant Field Values

DB_INDEX3

public static final String DB_INDEX3
The sequence identifier index name.

See Also:
Constant Field Values

DB_VERSION

private static final int DB_VERSION
The database version.

See Also:
Constant Field Values

DB_CREATE_ACITIVITY_TABLE

private static final String DB_CREATE_ACITIVITY_TABLE
The table creation statement for the activity label table.

See Also:
Constant Field Values

DB_CREATE_RECORDING_TABLE

private static final String DB_CREATE_RECORDING_TABLE
The table creation statement for the recording table.

See Also:
Constant Field Values

DB_FILL_IN_ACTIVITIES

private static final String DB_FILL_IN_ACTIVITIES
The activity insertion statement for upgrade from version 3 to 4.


TMP_TABLE_NAME

private static final String TMP_TABLE_NAME
The backup table name for upgrade from version 2 to 3.

See Also:
Constant Field Values

DB_RENAME_RECORDING_TABLE

private static final String DB_RENAME_RECORDING_TABLE
The renaming statement for upgrades.

See Also:
Constant Field Values

DB_COPY_RECORDINGS_WITH_ACTIVITY_REFERENCE

private static final String DB_COPY_RECORDINGS_WITH_ACTIVITY_REFERENCE
The copy statement for upgrade from version 3 to 4(5).


DB_DROP_OLD_RECORDING_TABLE

private static final String DB_DROP_OLD_RECORDING_TABLE
The drop statement for upgrade from version 2 to 3.

See Also:
Constant Field Values

DB_DROP_FEATURE_VECTOR_TABLE

private static final String DB_DROP_FEATURE_VECTOR_TABLE
The drop statement for upgrade from version 3 to 4.

See Also:
Constant Field Values

DB_CREATE_FEATURE_VECTOR_TABLE

private static final String DB_CREATE_FEATURE_VECTOR_TABLE
The table creation statement for the feature vector table.

See Also:
Constant Field Values

DB_DROP_INDEX1

private static final String DB_DROP_INDEX1
The activity label index drop statement for upgrade to version 4.

See Also:
Constant Field Values

DB_RECREATE_INDEX1

private static final String DB_RECREATE_INDEX1
The new activity label index creation statement.

See Also:
Constant Field Values

DB_CREATE_INDEX2

private static final String DB_CREATE_INDEX2
The state index creation statement.

See Also:
Constant Field Values

DB_CREATE_INDEX3

private static final String DB_CREATE_INDEX3
The sequence identifier index creation statement.

See Also:
Constant Field Values

dbHelper

private final DBAdapter.DatabaseHelper dbHelper
The SQLite helper


db

private SQLiteDatabase db
The database


dbName

private String dbName
The database name.

Constructor Detail

DBAdapter

public DBAdapter(String dbName,
                 Context context)
Constructor

Parameters:
dbName - the database name
context - the application context

DBAdapter

public DBAdapter(String dbName,
                 long maxDBSize,
                 Context context)
Constructor

Parameters:
dbName - the database name
maxDBSize - the maximum database size
context - the application context
Method Detail

open

public final DBAdapter open()
                     throws SQLiteException
Method to open the database for write access

Returns:
this database adapter
Throws:
SQLiteException - if unable to open the database

openForRead

public final DBAdapter openForRead()
                            throws SQLiteException
Method to open the database for read access

Returns:
this database adapter
Throws:
SQLiteException - if unable to open the database

close

public final void close()
Method to close the database


setMaximumDatabaseSize

public final long setMaximumDatabaseSize(long size)
Setter for the maximum database size

Parameters:
size - the maximum database size
Returns:
the new maximum database size

getMaximumDatabaseSize

public final long getMaximumDatabaseSize()
Getter for the maximum database size

Returns:
the maximum database size

getPageSize

public final long getPageSize()
Getter for the current database page size in bytes

Returns:
the page size in bytes

getSampleRateFromDB

public static final long getSampleRateFromDB(SQLiteDatabase db)
Does get the sample rate from an sqlite database

Parameters:
db - the database to query
Returns:
the sample rate

updateSampleRate

public final void updateSampleRate(long sampleRate)
Method to update the sample rate

Parameters:
sampleRate - the sample rate to update

getSampleRate

public final long getSampleRate()
Does get the sample rate

Returns:
the sample rate

getConfigurationFromDB

public final String getConfigurationFromDB()
Does get the sample rate from an sqlite database

Returns:
the sample rate

getUuid

public final String getUuid()
Does get the sample rate from an sqlite database

Returns:
the sample rate

storeConfigurationToDB

protected final void storeConfigurationToDB(String config)
Method to update the stored configuration

Parameters:
config - the XML representation of the configuration to store

getNextRecordingSequenceId

public final long getNextRecordingSequenceId()
Does determine the next unique sequence identifier

Returns:
the next sequence identifier

deleteActivityLabelId

public final void deleteActivityLabelId(long activityLabelId)
Does delete the activity table entry for a given activity label

Parameters:
activityLabelId - the activity label identifier to delete activity for

deleteSequence

public final int deleteSequence(long sId)
Does delete all recordings with the given sequence identifier

Parameters:
sId - the unique identifier of the recording to delete
Returns:
the count of deleted rows

renameSequence

public final boolean renameSequence(long sid,
                                    Long activityLabelId)
Does update the activity assignment for all recordings with the given sequence identifier

Parameters:
sid - the unique identifier of the recording to delete
activityLabelId - the new activity label id
Returns:
true if successful, false otherwise

deleteAllRecordings

public final boolean deleteAllRecordings()
Method to delete all stored recordings at once, together with related feature vectors.

Returns:
true if successful, false otherwise.

deleteRecording

public final int deleteRecording(long recordingId)
Does delete the recording with the given identifier and all related features.

Parameters:
recordingId - the unique identifier of the recording to delete
Returns:
the count of deleted rows

deleteRecordings

public final int deleteRecordings(List<Long> recordingIds)
Does delete the recordings with the given identifiers and all related features.

Parameters:
recordingIds - s list with unique identifiers of the recordings to delete
Returns:
the count of deleted rows

updateRecordingCountInActivityTable

protected void updateRecordingCountInActivityTable(SQLiteDatabase db,
                                                   long activityId,
                                                   int recCountUpdateValue)
Does update the recording count for a given activity identifier

Parameters:
db - the database
recCountUpdateValue - the value to update recording count with, can be negative for deleted recordings
activityId - the activity identifier

deleteRecordingsWithActivityLabel

public final int deleteRecordingsWithActivityLabel(ActivityLabel activityLabel)
Does delete the recording with the given label.

Parameters:
activityLabel - the activity label
Returns:
the count of deleted rows

deleteAllFeatureVectors

protected static final boolean deleteAllFeatureVectors(SQLiteDatabase db)
Method to delete all stored feature vectors at once.

Parameters:
db - the database to use
Returns:
true if successful, false otherwise.

deleteAllFeatureVectors

public final boolean deleteAllFeatureVectors()
Method to delete all stored feature vectors at once.

Returns:
true if successful, false otherwise.

deleteFeatureVectorsForLabels

public final void deleteFeatureVectorsForLabels(List<Long> activityLabelIds)
Method to delete all stored feature vectors for a set of labels

Parameters:
activityLabelIds - the list with the activity label identifiers

deleteFeatureVectors

public final int deleteFeatureVectors(Long recordingId)
Method to delete all stored feature vectors for a given recording

Parameters:
recordingId - the recording identifier
Returns:
the count of deleted feature vectors

updateFeatureVectorCountInRecordingTable

protected void updateFeatureVectorCountInRecordingTable(SQLiteDatabase db,
                                                        Long recordingId,
                                                        int featureVecCountUpdateValue)
Does update the feature vector count for a given recording identifier

Parameters:
db - the database
recordingId - the recording identifier
featureVecCountUpdateValue - the value to update feature vector count with, can be negative for deleted samples

insertFeatureVector

public final long insertFeatureVector(DBFeatureVector vector,
                                      Long idRecording)
                               throws Exception
Method to insert an extracted feature vector for a given recording.

Parameters:
vector - the feature vector to insert
idRecording - the unique identifier of the recording, this feature vector is associated with
Returns:
the row id of the inserted feature vector.
Throws:
Exception - in case of SQL or serialization errors

insertActivityLabelIfNotExists

private final Long insertActivityLabelIfNotExists(ActivityLabel activitylabel)
                                           throws Exception
Method to insert a new activity label into the database.

Parameters:
activitylabel - the activity label to insert.
Returns:
the unique identifier of the existing or just inserted activity label
Throws:
Exception - in case of SQL or serialization errors

insertActivityLabelByName

public final boolean insertActivityLabelByName(String newActivityLabel)
Does insert an activity table entry for a new activity label

Parameters:
newActivityLabel - the new activity label to insert
Returns:
true if successful, false if label already exists

updateActivityLabel

public final boolean updateActivityLabel(ActivityLabel activityLabel)
Does update the activity table entry for an existing activity label. Just the columns for parent reference and label name are updated!

Parameters:
activityLabel - the activity label to update
Returns:
true if successful, false otherwise

insertRecording

public final Long insertRecording(DBRecording recording)
                           throws Exception
Method to insert a labeled time series frame into the database.

Parameters:
recording - the recording to insert.
Returns:
the row id of the inserted recording.
Throws:
Exception - in case of SQL or serialization errors

getFeatureVectorCount

public final long getFeatureVectorCount()
Method to get the current count of stored feature vector.

Returns:
the current count of stored feature vectors.

getFeatureVectorCountForRecoding

public final long getFeatureVectorCountForRecoding(long recordingId)
Method to get the feature vector count for a given recording identifier.

Parameters:
recordingId - the feature vector count for a given recording identifier
Returns:
the current count of stored feature vectors for the given recording identifier

getCursorForAvailableFeatureVectorIds

private final Cursor getCursorForAvailableFeatureVectorIds(long recordingId)
Getter for a cursor for available feature vector identifiers for a given recording identifier.

Parameters:
recordingId - the recording identifier
Returns:
the cursor for all feature vector identifiers sorted in ascending order.

getCursorForAvailableFeatureVectorIds

private final Cursor getCursorForAvailableFeatureVectorIds()
Getter for all available feature vector identifiers

Returns:
the cursor for all feature vector identifiers sorted in ascending order.

getAvailableFeatureVectorIds

public final List<Long> getAvailableFeatureVectorIds()
Getter for all available feature vector identifiers.

Returns:
the available feature vector identifiers as list

getAvailableFeatureVectorIdsForRecording

public final List<Long> getAvailableFeatureVectorIdsForRecording(long recordingId)
Getter for all available feature vector identifiers for a given recoding identifier.

Parameters:
recordingId - the recording identifier
Returns:
the available feature vector identifiers as list

getAvailableFeatureVectorIdsForActivityLabelId

public final List<Long> getAvailableFeatureVectorIdsForActivityLabelId(long activityId)
Getter for all available feature vector identifiers.

Parameters:
activityId - the activity label identifier to get feature vector identifiers for
Returns:
the available feature vector identifiers as list

getFeatureVectorById

private final Cursor getFeatureVectorById(Long id)
Getter for a feature vector by its unique identifier.

Parameters:
id - the recording identifier
Returns:
the cursor for the request.

featureVectorFromCursor

private final DBFeatureVector featureVectorFromCursor(Cursor cursor)
                                               throws Exception
Does create a recording from the current cursor position.

Parameters:
cursor - the database cursor
Returns:
the recording created from cursor position
Throws:
Exception

getFeatureVector

public final DBFeatureVector getFeatureVector(Long id)
                                       throws Exception
Method to retrieve a feature vector by its id.

Parameters:
id - the feature vector identifier
Returns:
the feature vector with the given id, or null if none exists
Throws:
Exception

getActivityLabelIdsWithSamplesIgnoreDependecies

public final List<Long> getActivityLabelIdsWithSamplesIgnoreDependecies()
Getter for all activity labels with extracted training samples.

Returns:
a list with activity labels with associated training data

getRecordingStatistics

public final DBRecordingStatistic getRecordingStatistics()
Getter for statistics about the stored recordings.

Returns:
a list with the recording statistics

getDBStatistics

private final DBStatistic getDBStatistics(boolean activitiesWithSamplesOnly,
                                          boolean skipSampleCounts,
                                          ActivityLabelMap mapActivityLabels)
Getter for statistics about the stored feature vectors and recordings per activity label.

Parameters:
activitiesWithSamplesOnly - if true, activities without samples will be ignored
skipSampleCounts - flag if sample count calculation shall be skipped
mapActivityLabels - the activity label map to use
Returns:
a list with the recording statistics

getDBStatistic

public final DBStatistic getDBStatistic(boolean recordingsWithSamplesOnly,
                                        boolean skipSampleCounts)
Getter for statistics about the stored feature vectors per activity label.

Parameters:
recordingsWithSamplesOnly - if true, recordings without samples will be ignored
skipSampleCounts - flag if sample count calculation shall be skipped
Returns:
a list with the recording statistics

getSampleStatistics

public List<DBSampleStatistic> getSampleStatistics(long[] activityLabelIds)
Method to get sample and recording statistics for the given activity labels

Parameters:
activityLabelIds - the activity labels identifier to get sample statistics for
Returns:
the recording and sample count statistics

getActivityCount

public final long getActivityCount()
Method to get the current activity count.

Returns:
the current count of stored activities

getRecordingCount

public final long getRecordingCount()
Method to get the current recording count.

Returns:
the current count of stored recordings

getRecordingCountForState

public final long getRecordingCountForState(DBAdapter.RecordingStates state)
Method to get the current recording count for a given state.

Parameters:
state - the processing state
Returns:
the current count of stored recordings with the given activity label

getRecordingCountForStateAndActivityLabelIds

private final long getRecordingCountForStateAndActivityLabelIds(DBAdapter.RecordingStates state,
                                                                List<Long> listActivityLabelIds)
Method to get the current recording count for a given state and an optional subset of label identifiers.

Parameters:
state - the processing state
listActivityLabelIds - a list with the activity label identifiers to get recordings for, null or an empty list will give records for any activity label
Returns:
the current count of stored recordings with the given activity labels and the given state

getRecordingCountForActivityLabelId

public final long getRecordingCountForActivityLabelId(long activityId)
Method to get the current recording count for given activity id. Recordings of successor labels will not be counted!

Parameters:
activityId - the unique activity id
Returns:
the current count of stored recordings with the given activity label

getFeatureVectorCountForActivityLabelId

private final long getFeatureVectorCountForActivityLabelId(long activityLabelId)
Method to get the current feature vector count for given activity label. Feature vectors of successor labels will not be counted!

Parameters:
activityLabelId - the activity label
Returns:
the current count of stored feature vectors for the given activity label

getActivityLabelIdForSequenceId

private final long getActivityLabelIdForSequenceId(long sid)
Getter for the activity label identifier of a sequence.

Parameters:
sid - the sequence identifier
Returns:
the activity label identifier of the sequence if exists, -1 otherwise

getActivityLabelIdForRecordingId

private final long getActivityLabelIdForRecordingId(long recordingId)
Getter for the activity label identifier of a recording.

Parameters:
recordingId - the recoding identifier
Returns:
the activity label identifier of the recording if exists, -1 otherwise

getActivityLabels

private final Cursor getActivityLabels()
Method to get the current activity label count from the activity table.

Returns:
the current activity label count in the activity table

getAvailableActivityLabels

public final ActivityLabelMap getAvailableActivityLabels()
Getter for a map with all available activity labels.

Returns:
a map with all available activity labels

getRecordingIdsForActivityLabel

private final Cursor getRecordingIdsForActivityLabel(Long activityId)
Getter for available recording identifiers for a given activity label

Parameters:
activityId - the activity identifier
Returns:
the cursor for all recordings with the given sorted by recording IDs in ascending order.

getRecordingIdsForForStateAndActivityLabelIds

private final Cursor getRecordingIdsForForStateAndActivityLabelIds(DBAdapter.RecordingStates state,
                                                                   List<Long> activityLabelIds)
Getter for available recording identifiers for a given state.

Parameters:
state - the recording state
activityLabelIds - a set with the activity label identifiers to get recordings for, null or an empty set will return records for any activity label
Returns:
the cursor for all recordings sorted by recording IDs in ascending order.

getSequenceIdentifiersForNewRecordings

private final Cursor getSequenceIdentifiersForNewRecordings()
Getter for new continuous recording sequence identifiers (by searching for unprocessed recordings).

Returns:
the cursor for all sequences identifiers sorted in ascending order.

createInStatement

private final String createInStatement(String column,
                                       int cntValues)
Does create an in statement for a given value list

Parameters:
column - the column to refer to
cntValues - the count of values
Returns:
the in statement for the given value list

getAvailableSequenceIds

public final List<Long> getAvailableSequenceIds()
Getter for all available recording identifiers.

Returns:
the available recording identifiers as list

getAvailableRecordingIds

public final List<Long> getAvailableRecordingIds()
Getter for all available recording identifiers.

Returns:
the available recording identifiers as list

getAvailableRecordingIds

public final List<Long> getAvailableRecordingIds(DBAdapter.RecordingStates state)
Getter for all available recording identifiers for a given processing state.

Parameters:
state - the state of the recordings
Returns:
the available recording identifiers as list

getNewSequenceIdentifiers

public final List<Long> getNewSequenceIdentifiers()
Getter for all available new continuous recording sequence identifiers.

Returns:
the available sequence identifiers as list

getAffectedActivityLabelNames

private static final Set<Long> getAffectedActivityLabelNames(ActivityLabelMap mapActivityLabels,
                                                             long activityLabelId)
Method to extract successor activities for a given activity label identifier

Parameters:
mapActivityLabels - an actual map with all activities
activityLabelId - a list with activity label identifiers, to determine all successors for
Returns:
a list with activity label identifiers of all successors and the label itself

getAvailableRecordingIds

private final List<Long> getAvailableRecordingIds(DBAdapter.RecordingStates state,
                                                  List<Long> activityLabelIds)
Getter for all available recording identifiers for a given processing state an optional subset of labels.

Parameters:
state - the state of the recordings
activityLabelIds - the list with the activity label identifiers, can be null to get records for any activity label
Returns:
the available recording identifiers as list

getAvailableRecordingIdsForActivityLabelId

public final List<Long> getAvailableRecordingIdsForActivityLabelId(long activityLabelId)
Getter for all available recording identifiers for a given activity label

Parameters:
activityLabelId - the activity label identifier
Returns:
the available recording identifiers for the given activity label as list

getAvailableRecordingIdsForActivityNames

private final List<Long> getAvailableRecordingIdsForActivityNames(Set<Long> activityLabelIds)
Getter for all available recording identifiers for a given set of activity identifiers

Parameters:
activityLabelIds - the activity identifier set
Returns:
a list with the available recording identifiers for the given activity name set

getLabelSequenceStateAndTimesForRecording

private final Cursor getLabelSequenceStateAndTimesForRecording(Long recordingId)
Getter for a recording by its unique identifier.

Parameters:
recordingId - the recording identifier
Returns:
the cursor for the request.

getPartialRecordingData

protected static final Cursor getPartialRecordingData(SQLiteDatabase db,
                                                      Long recordingId,
                                                      int pos,
                                                      int cnt)
Access to substrings from recording data

Parameters:
db - the database
recordingId - the recording identifier
pos - the data start position
cnt - the data end position
Returns:
the cursor to the substring of the data from pos to end

getRecordingSize

protected static final int getRecordingSize(SQLiteDatabase db,
                                            Long recordingId)
Getter for the size of the recording data field

Parameters:
db - the database
recordingId - the recording identifier
Returns:
the recording data size

getRecordingData

private static final String getRecordingData(SQLiteDatabase db,
                                             Long recordingId)
Getter for the recording data by its unique identifier.

Parameters:
db - the database
recordingId - the recording identifier
Returns:
the cursor for the request.

getRecordingIdsForSequenceId

public final List<Long> getRecordingIdsForSequenceId(long sid)
Does look up the recording identifiers of a given sequence identifier

Parameters:
sid - the sequence identifier
Returns:
a list with all recordings of the given sequence id

getRecording

public final DBRecording getRecording(Long recordingId)
Method to retrieve a recordings by its id.

Parameters:
recordingId - the recording identifier
Returns:
the recording

getActivityLabelNameForId

public final String getActivityLabelNameForId(long activityLabelId)
Does get the activity label name for a given identifier

Parameters:
activityLabelId - the activity label identifier
Returns:
the label name for the given id

getActivityIdForLabelName

private final Long getActivityIdForLabelName(String activityLabelName)
Does get the activity label identifier for a given name

Parameters:
activityLabelName - the activity label name
Returns:
the identifier for the given label name for the given id

updateRecordingState

public final boolean updateRecordingState(Long recordingId,
                                          DBAdapter.RecordingStates state)
Method to update the processing state for an existing recoding.

Parameters:
recordingId - the unique recording identifier
state - the new processing state
Returns:
true if successful, false otherwise

updateRecordingData

public final boolean updateRecordingData(DBRecording recording)
Method to update the recording data.

Parameters:
recording - the recording data
Returns:
true if successful, false otherwise

createStringArray

private String[] createStringArray(List<? extends Object> list)
Does create a string array representation of a list of objects

Parameters:
list - the list to create a string array from
Returns:
a string array representation

updateAllRecordingStates

public final boolean updateAllRecordingStates(DBAdapter.RecordingStates state)
Method to update the processing state for all existing recordings.

Parameters:
state - the new processing state
Returns:
true if successful, false otherwise

getDatabaseName

public final String getDatabaseName()
Getter for the database name.

Returns:
the database name.

getVersion

public static final int getVersion()
Getter for the supported database version

Returns:
the database version supported by this adapter

requiresRawDataUpgrade

public boolean requiresRawDataUpgrade()
Test method for the requirement of an upgrade of the raw time series data (for compatibility of older DB versions)

Returns:
true if required, false otherwise.