org.hackystat.sensorbase.db
Class DbManager

java.lang.Object
  extended by org.hackystat.sensorbase.db.DbManager

public class DbManager
extends java.lang.Object

Provides an interface to storage for the resources managed by the SensorBase. Currently we have one storage mechanisms: a persistent store which is implemented by an embedded Derby database.

Author:
Philip Johnson

Field Summary
static java.lang.String sensorDataIndexCloseTag
          The SensorDataIndex close tag.
static java.lang.String sensorDataIndexOpenTag
          The SensorDataIndex open tag.
 
Constructor Summary
DbManager(Server server)
          Creates a new DbManager which manages access to the underlying persistency layer(s).
 
Method Summary
 boolean compressTables()
          Databases like Derby require an explicit compress command for releasing disk space after a large number of rows have been deleted.
 void deleteProject(User user, java.lang.String projectName)
          Ensures that the Project with the given user and name is no longer present in this db.
 void deleteSensorData(User user)
          Ensures that sensor data with the given user no longer exists.
 void deleteSensorData(User user, javax.xml.datatype.XMLGregorianCalendar timestamp)
          Ensures that sensor data with the given user and timestamp no longer exists.
 void deleteSensorDataType(java.lang.String sdtName)
          Ensures that the SensorDataType with the given name no longer exists.
 void deleteUser(java.lang.String email)
          Ensures that the User with the given email address is no longer present in this db.
 java.lang.String getProject(User user, java.lang.String projectName)
          Returns the Project instance as an XML string, or null.
 java.lang.String getProjectIndex()
          Returns the XML Project Index for all Projects.
 java.lang.String getProjectSensorDataSnapshot(java.util.List<User> users, javax.xml.datatype.XMLGregorianCalendar startTime, javax.xml.datatype.XMLGregorianCalendar endTime, java.util.List<java.lang.String> uriPatterns, java.lang.String sdt, java.lang.String tool)
          Returns a SensorDataIndex representing the "snapshot" of sensor data in the given time interval for the given sdt and tool (if tool is not null).
 ProjectSummary getProjectSummary(java.util.List<User> users, javax.xml.datatype.XMLGregorianCalendar startTime, javax.xml.datatype.XMLGregorianCalendar endTime, java.util.List<java.lang.String> uriPatterns, java.lang.String href)
          Returns a ProjectSummary instance constructed for the given Project between the startTime and endTime.
 int getRowCount(java.lang.String table)
          Returns the current number of rows in the specified table.
 java.lang.String getSensorData(User user, javax.xml.datatype.XMLGregorianCalendar timestamp)
          Returns the SensorData instance as an XML string, or null.
 java.lang.String getSensorDataIndex()
          Returns the XML SensorDataIndex for all sensor data.
 java.lang.String getSensorDataIndex(java.util.List<User> users, javax.xml.datatype.XMLGregorianCalendar startTime, javax.xml.datatype.XMLGregorianCalendar endTime, java.util.List<java.lang.String> uriPatterns, int startIndex, int maxInstances)
          Returns the XML SensorDataIndex for all sensor data matching these users, start/end time, and whose resource string matches at least one in the list of UriPatterns.
 java.lang.String getSensorDataIndex(java.util.List<User> users, javax.xml.datatype.XMLGregorianCalendar startTime, javax.xml.datatype.XMLGregorianCalendar endTime, java.util.List<java.lang.String> uriPatterns, java.lang.String sdt)
          Returns the XML SensorDataIndex for all sensor data matching this user, start/end time, and whose resource string matches at least one in the list of UriPatterns.
 java.lang.String getSensorDataIndex(java.util.List<User> users, javax.xml.datatype.XMLGregorianCalendar startTime, javax.xml.datatype.XMLGregorianCalendar endTime, java.util.List<java.lang.String> uriPatterns, java.lang.String sdt, java.lang.String tool)
          Returns the XML SensorDataIndex for all sensor data matching this user, start/end time, and whose resource string matches at least one in the list of UriPatterns.
 java.lang.String getSensorDataIndex(User user)
          Returns the XML SensorDataIndex for all sensor data for this user.
 java.lang.String getSensorDataIndex(User user, java.lang.String sdtName)
          Returns the XML SensorDataIndex for all sensor data for this user and sensor data type.
 java.lang.String getSensorDataIndexLastMod(User user, javax.xml.datatype.XMLGregorianCalendar lastModStartTime, javax.xml.datatype.XMLGregorianCalendar lastModEndTime)
          Returns the XML SensorDataIndex for all sensor data for the given user that arrived at the server between the two timestamps.
 java.lang.String getSensorDataType(java.lang.String sdtName)
          Returns the SensorDataType instance as an XML string, or null.
 java.lang.String getSensorDataTypeIndex()
          Returns the XML SensorDataTypeIndex for all sensor data.
 java.util.Set<java.lang.String> getTableNames()
          Returns a set containing the names of all tables in this database.
 java.lang.String getUser(java.lang.String email)
          Returns the User instance as an XML string, or null.
 java.lang.String getUserIndex()
          Returns the XML UserIndex for all Users..
 boolean hasSensorData(User user, javax.xml.datatype.XMLGregorianCalendar timestamp)
          Returns true if the passed [user, timestamp] has sensor data defined for it.
 boolean indexTables()
          The most appropriate set of indexes for the database has been evolving over time as we develop new queries.
 void storeProject(Project project, java.lang.String xmlProject, java.lang.String xmlProjectRef)
          Persists a Project instance.
 void storeSensorData(SensorData data, java.lang.String xmlSensorData, java.lang.String xmlSensorDataRef)
          Persists a SensorData instance.
 void storeSensorDataType(SensorDataType sdt, java.lang.String xmlSensorDataType, java.lang.String xmlSensorDataTypeRef)
          Persists a SensorDataType instance.
 void storeUser(User user, java.lang.String xmlUser, java.lang.String xmlUserRef)
          Persists a User instance.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

sensorDataIndexOpenTag

public static final java.lang.String sensorDataIndexOpenTag
The SensorDataIndex open tag.

See Also:
Constant Field Values

sensorDataIndexCloseTag

public static final java.lang.String sensorDataIndexCloseTag
The SensorDataIndex close tag.

See Also:
Constant Field Values
Constructor Detail

DbManager

public DbManager(Server server)
Creates a new DbManager which manages access to the underlying persistency layer(s). Instantiates the underlying storage system to use.

Parameters:
server - The Restlet server instance.
Method Detail

storeSensorData

public void storeSensorData(SensorData data,
                            java.lang.String xmlSensorData,
                            java.lang.String xmlSensorDataRef)
Persists a SensorData instance. If the Owner/Timestamp already exists in the table, it is overwritten.

Parameters:
data - The sensor data.
xmlSensorData - The sensor data resource as an XML String.
xmlSensorDataRef - The sensor data resource as an XML resource reference

storeSensorDataType

public void storeSensorDataType(SensorDataType sdt,
                                java.lang.String xmlSensorDataType,
                                java.lang.String xmlSensorDataTypeRef)
Persists a SensorDataType instance. If the SDT name already exists in the table, it is overwritten.

Parameters:
sdt - The sensor data.
xmlSensorDataType - The SDT resource as an XML String.
xmlSensorDataTypeRef - The SDT as an XML resource reference

storeUser

public void storeUser(User user,
                      java.lang.String xmlUser,
                      java.lang.String xmlUserRef)
Persists a User instance. If the User email already exists in the table, it is overwritten.

Parameters:
user - The user instance.
xmlUser - The User resource as an XML String.
xmlUserRef - The User as an XML resource reference

storeProject

public void storeProject(Project project,
                         java.lang.String xmlProject,
                         java.lang.String xmlProjectRef)
Persists a Project instance. If the Project already exists in the db, it is overwritten.

Parameters:
project - The project instance.
xmlProject - The Project resource as an XML String.
xmlProjectRef - The Project as an XML resource reference

getSensorDataIndex

public java.lang.String getSensorDataIndex()
Returns the XML SensorDataIndex for all sensor data.

Returns:
An XML String providing an index of all sensor data resources.

getSensorDataIndex

public java.lang.String getSensorDataIndex(User user)
Returns the XML SensorDataIndex for all sensor data for this user.

Parameters:
user - The User whose sensor data is to be returned.
Returns:
The XML String providing an index of all relevent sensor data resources.

getSensorDataIndex

public java.lang.String getSensorDataIndex(User user,
                                           java.lang.String sdtName)
Returns the XML SensorDataIndex for all sensor data for this user and sensor data type.

Parameters:
user - The User whose sensor data is to be returned.
sdtName - The sensor data type name.
Returns:
The XML Document instance providing an index of all relevent sensor data resources.

getSensorDataIndex

public java.lang.String getSensorDataIndex(java.util.List<User> users,
                                           javax.xml.datatype.XMLGregorianCalendar startTime,
                                           javax.xml.datatype.XMLGregorianCalendar endTime,
                                           java.util.List<java.lang.String> uriPatterns,
                                           java.lang.String sdt)
Returns the XML SensorDataIndex for all sensor data matching this user, start/end time, and whose resource string matches at least one in the list of UriPatterns.

Parameters:
users - The list of users.
startTime - The start time.
endTime - The end time.
uriPatterns - A list of UriPatterns.
sdt - The SensorDataType of interest, or null if all data of all SDTs should be retrieved.
Returns:
The XML SensorDataIndex string corresponding to the matching sensor data.

getSensorDataIndex

public java.lang.String getSensorDataIndex(java.util.List<User> users,
                                           javax.xml.datatype.XMLGregorianCalendar startTime,
                                           javax.xml.datatype.XMLGregorianCalendar endTime,
                                           java.util.List<java.lang.String> uriPatterns,
                                           java.lang.String sdt,
                                           java.lang.String tool)
Returns the XML SensorDataIndex for all sensor data matching this user, start/end time, and whose resource string matches at least one in the list of UriPatterns.

Parameters:
users - The list of users.
startTime - The start time.
endTime - The end time.
uriPatterns - A list of UriPatterns.
sdt - The SensorDataType of interest, or null if all data of all SDTs should be retrieved.
tool - The tool of interest.
Returns:
The XML SensorDataIndex string corresponding to the matching sensor data.

getSensorDataIndex

public java.lang.String getSensorDataIndex(java.util.List<User> users,
                                           javax.xml.datatype.XMLGregorianCalendar startTime,
                                           javax.xml.datatype.XMLGregorianCalendar endTime,
                                           java.util.List<java.lang.String> uriPatterns,
                                           int startIndex,
                                           int maxInstances)
Returns the XML SensorDataIndex for all sensor data matching these users, start/end time, and whose resource string matches at least one in the list of UriPatterns. Client must guarantee that startTime and endTime are within Project dates, and that startIndex and maxInstances are non-negative.

Parameters:
users - The users.
startTime - The start time.
endTime - The end time.
uriPatterns - A list of UriPatterns.
startIndex - The starting index.
maxInstances - The maximum number of instances to return.
Returns:
The XML SensorDataIndex string corresponding to the matching sensor data.

getSensorDataIndexLastMod

public java.lang.String getSensorDataIndexLastMod(User user,
                                                  javax.xml.datatype.XMLGregorianCalendar lastModStartTime,
                                                  javax.xml.datatype.XMLGregorianCalendar lastModEndTime)
Returns the XML SensorDataIndex for all sensor data for the given user that arrived at the server between the two timestamps. This method uses the LastMod timestamp rather than the "regular" timestamp, and is used for real-time monitoring of data arriving at the server.

Parameters:
user - The user whose data is being monitored.
lastModStartTime - The lastMod startTime of interest.
lastModEndTime - The lastMod endTime of interest.
Returns:
The XML SensorDataIndex for the data that arrived between the two timestamps.

getSensorDataTypeIndex

public java.lang.String getSensorDataTypeIndex()
Returns the XML SensorDataTypeIndex for all sensor data.

Returns:
An XML String providing an index of all SDT resources.

getUserIndex

public java.lang.String getUserIndex()
Returns the XML UserIndex for all Users..

Returns:
An XML String providing an index of all User resources.

getProjectIndex

public java.lang.String getProjectIndex()
Returns the XML Project Index for all Projects.

Returns:
An XML String providing an index of all Project resources.

getProjectSummary

public ProjectSummary getProjectSummary(java.util.List<User> users,
                                        javax.xml.datatype.XMLGregorianCalendar startTime,
                                        javax.xml.datatype.XMLGregorianCalendar endTime,
                                        java.util.List<java.lang.String> uriPatterns,
                                        java.lang.String href)
Returns a ProjectSummary instance constructed for the given Project between the startTime and endTime. This summary provides a breakdown of the number of sensor data instances found of the given type during the given time period.

Parameters:
users - The list of users in this project.
startTime - The startTime
endTime - The endTime.
uriPatterns - The uriPatterns for this project.
href - The URL naming this resource.
Returns:
The ProjectSummary instance.

getProjectSensorDataSnapshot

public java.lang.String getProjectSensorDataSnapshot(java.util.List<User> users,
                                                     javax.xml.datatype.XMLGregorianCalendar startTime,
                                                     javax.xml.datatype.XMLGregorianCalendar endTime,
                                                     java.util.List<java.lang.String> uriPatterns,
                                                     java.lang.String sdt,
                                                     java.lang.String tool)
Returns a SensorDataIndex representing the "snapshot" of sensor data in the given time interval for the given sdt and tool (if tool is not null). The "snapshot" is the set of sensor data with the most recent runtime value during the interval.

Parameters:
users - The list of users in this project.
startTime - The startTime
endTime - The endTime.
uriPatterns - The uriPatterns for this project.
sdt - The sensor data type of the sensor data of interest.
tool - The tool associated with this snapshot, or null if any tool will do.
Returns:
The SensorDataIndex with the latest runtime.

getSensorData

public java.lang.String getSensorData(User user,
                                      javax.xml.datatype.XMLGregorianCalendar timestamp)
Returns the SensorData instance as an XML string, or null.

Parameters:
user - The user.
timestamp - The timestamp associated with this sensor data.
Returns:
The SensorData instance as an XML string, or null.

getSensorDataType

public java.lang.String getSensorDataType(java.lang.String sdtName)
Returns the SensorDataType instance as an XML string, or null.

Parameters:
sdtName - The name of the SDT to retrieve.
Returns:
The SensorDataType instance as an XML string, or null.

getUser

public java.lang.String getUser(java.lang.String email)
Returns the User instance as an XML string, or null.

Parameters:
email - The email address of the User to retrieve.
Returns:
The User instance as an XML string, or null.

getProject

public java.lang.String getProject(User user,
                                   java.lang.String projectName)
Returns the Project instance as an XML string, or null.

Parameters:
user - The User that owns the Project to retrieve.
projectName - The name of the Project to retrieve.
Returns:
The Project instance as an XML string, or null.

hasSensorData

public boolean hasSensorData(User user,
                             javax.xml.datatype.XMLGregorianCalendar timestamp)
Returns true if the passed [user, timestamp] has sensor data defined for it.

Parameters:
user - The user.
timestamp - The timestamp
Returns:
True if there is any sensor data for this [user, timestamp].

deleteSensorData

public void deleteSensorData(User user,
                             javax.xml.datatype.XMLGregorianCalendar timestamp)
Ensures that sensor data with the given user and timestamp no longer exists.

Parameters:
user - The user.
timestamp - The timestamp associated with this sensor data.

deleteSensorData

public void deleteSensorData(User user)
Ensures that sensor data with the given user no longer exists.

Parameters:
user - The user.

deleteSensorDataType

public void deleteSensorDataType(java.lang.String sdtName)
Ensures that the SensorDataType with the given name no longer exists.

Parameters:
sdtName - The SDT name.

deleteUser

public void deleteUser(java.lang.String email)
Ensures that the User with the given email address is no longer present in this db.

Parameters:
email - The user email.

deleteProject

public void deleteProject(User user,
                          java.lang.String projectName)
Ensures that the Project with the given user and name is no longer present in this db.

Parameters:
user - The User who owns this Project.
projectName - The name of the Project to delete.

compressTables

public boolean compressTables()
Databases like Derby require an explicit compress command for releasing disk space after a large number of rows have been deleted. This operation invokes the compress command on all tables in the database. If a database implementation does not support explicit compression, then this command should do nothing but return true.

Returns:
True if the compress command succeeded or if the database does not support compression.

indexTables

public boolean indexTables()
The most appropriate set of indexes for the database has been evolving over time as we develop new queries. This command sets up the appropriate set of indexes. It should be able to be called repeatedly without error.

Returns:
True if the index commands succeeded.

getRowCount

public int getRowCount(java.lang.String table)
Returns the current number of rows in the specified table.

Parameters:
table - The table whose rows are to be counted.
Returns:
The number of rows in the table, or -1 if the table does not exist or an error occurs.

getTableNames

public java.util.Set<java.lang.String> getTableNames()
Returns a set containing the names of all tables in this database. Used by clients to invoke getRowCount with a legal table name.

Returns:
A set of table names.