org.hackystat.sensorbase.db.derby
Class DerbyImplementation

java.lang.Object
  extended by org.hackystat.sensorbase.db.DbImplementation
      extended by org.hackystat.sensorbase.db.derby.DerbyImplementation

public class DerbyImplementation
extends DbImplementation

Provides a implementation of DbImplementation using Derby in embedded mode. Note: If you are using this implementation as a guide for implementing an alternative database, you should be aware that this implementation does not do connection pooling. It turns out that embedded Derby does not require connection pooling, so it is not present in this code. You will probably want it for your version, of course.

Author:
Philip Johnson

Field Summary
 
Fields inherited from class org.hackystat.sensorbase.db.DbImplementation
logger, server
 
Constructor Summary
DerbyImplementation(Server server)
          Instantiates the Derby implementation.
 
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.
(package private) static java.lang.String constructLikeClauses(java.util.List<java.lang.String> uriPatterns)
          Constructs a set of LIKE clauses corresponding to the passed set of UriPatterns.
 void deleteProject(User owner, java.lang.String projectName)
          Ensures that the Project with the given owner and projectName is no longer present in the db.
 void deleteSensorData(User user)
          Ensures that sensor data with the given user is no longer present in this manager.
 void deleteSensorData(User user, javax.xml.datatype.XMLGregorianCalendar timestamp)
          Ensures that sensor data with the given user and timestamp is no longer present in this manager.
 void deleteSensorDataType(java.lang.String sdtName)
          Ensures that the SensorDataType with the given name is no longer present in this manager.
 void deleteUser(java.lang.String email)
          Ensures that the User with the given email is no longer present in this manager.
 java.lang.String getProject(User owner, java.lang.String projectName)
          Returns the Project instance as XML string, or null if not found.
 java.lang.String getProjectIndex()
          Returns the XML ProjectIndex for all Projects in this server.
 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 an XML SensorDataIndex to sensor data for the given time interval and sdt with the most recent runtime value.
 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 XML string, or null if not found.
 java.lang.String getSensorDataIndex()
          Returns the XML SensorDataIndex for all sensor data in this server.
 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 an XML SensorDataIndex representing the SensorData for the given user between start and end time whose resource string matches at least one of the 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 an XML SensorDataIndex representing the SensorData for the given user between start and end time whose resource string matches at least one of the 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 since the given start and end timestamps.
 java.lang.String getSensorDataType(java.lang.String sdtName)
          Returns the SensorDataType instance as XML string, or null if not found.
 java.lang.String getSensorDataTypeIndex()
          Returns the XML SensorDataTypeIndex for all SDTs in this server.
 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 XML string, or null if not found.
 java.lang.String getUserIndex()
          Returns the XML UserIndex for all Users in this server.
 boolean hasSensorData(User user, javax.xml.datatype.XMLGregorianCalendar timestamp)
          Returns true if the passed [key, 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 initialize()
          To be called as part of the startup process for a storage system.
 boolean isFreshlyCreated()
          Returns true if the initialize() method did indeed create a fresh storage system.
 boolean storeProject(Project project, java.lang.String xmlProject, java.lang.String xmlProjectRef)
          Persists a Project instance.
 boolean storeSensorData(SensorData data, java.lang.String xmlSensorData, java.lang.String xmlSensorDataRef)
          Persists a SensorData instance.
 boolean storeSensorDataType(SensorDataType sdt, java.lang.String xmlSensorDataType, java.lang.String xmlSensorDataTypeRef)
          Persists a SensorDataType instance.
 boolean storeUser(User user, java.lang.String xmlUser, java.lang.String xmlUserRef)
          Persists a User instance.
 
Methods inherited from class org.hackystat.sensorbase.db.DbImplementation
getProjectManager, getSdtManager, getSensorDataManager, getUserManager
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

DerbyImplementation

public DerbyImplementation(Server server)
Instantiates the Derby implementation. Throws a Runtime exception if the Derby jar file cannot be found on the classpath.

Parameters:
server - The SensorBase server instance.
Method Detail

initialize

public void initialize()
To be called as part of the startup process for a storage system. This method should:

Specified by:
initialize in class DbImplementation

storeSensorData

public boolean storeSensorData(SensorData data,
                               java.lang.String xmlSensorData,
                               java.lang.String xmlSensorDataRef)
Persists a SensorData instance. If SensorData with this [email, timestamp] already exists in the storage system, it should be overwritten.

Specified by:
storeSensorData in class DbImplementation
Parameters:
data - The sensor data.
xmlSensorData - The SensorData marshalled into an XML String.
xmlSensorDataRef - The corresponding SensorDataRef marshalled into an XML String
Returns:
True if the sensor data was successfully inserted.

isFreshlyCreated

public boolean isFreshlyCreated()
Returns true if the initialize() method did indeed create a fresh storage system. This is used by the ResourceManagers to determine if they should read in default data or not.

Specified by:
isFreshlyCreated in class DbImplementation
Returns:
True if the storage system is freshly created.

getSensorDataIndex

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

Specified by:
getSensorDataIndex in class DbImplementation
Returns:
The XML String containing an index to all Sensor Data.

getSensorDataIndex

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

Specified by:
getSensorDataIndex in class DbImplementation
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.

Specified by:
getSensorDataIndex in class DbImplementation
Parameters:
user - The User whose sensor data is to be returned.
sdtName - The sensor data type name.
Returns:
The XML String 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 an XML SensorDataIndex representing the SensorData for the given user between start and end time whose resource string matches at least one of the UriPatterns.

Specified by:
getSensorDataIndex in class DbImplementation
Parameters:
users - The list of users whose SensorData will be returned.
startTime - The earliest Sensor Data to be returned.
endTime - The latest SensorData to be returned.
uriPatterns - At least one UriPattern must match the SensorData resource field.
sdt - The SDT of interest, or null if data from all SDTs should be retrieved.
Returns:
An XML String containing a SensorDataIndex of matching SensorData.

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 an XML SensorDataIndex representing the SensorData for the given user between start and end time whose resource string matches at least one of the UriPatterns.

Specified by:
getSensorDataIndex in class DbImplementation
Parameters:
users - The list of users whose SensorData will be returned.
startTime - The earliest Sensor Data to be returned.
endTime - The latest SensorData to be returned.
uriPatterns - At least one UriPattern must match the SensorData resource field.
sdt - The SDT of interest. Should not be null.
tool - The tool of interest. Should not be null.
Returns:
An XML String containing a SensorDataIndex of matching SensorData.

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 an XML SensorDataIndex to sensor data for the given time interval and sdt with the most recent runtime value. This constitutes the "snapshot" of the data with the given sdt. If the tool parameter is null, then the last runtime snapshot is returned without regard to the tool.

Specified by:
getProjectSensorDataSnapshot in class DbImplementation
Parameters:
users - The list of users whose sensor data will be checked.
startTime - The start time.
endTime - The end time.
uriPatterns - The uripatterns that must match the resource string.
sdt - The sensor data type of interest.
tool - The tool of interest, or null if any tool is OK.
Returns:
A string containing the XML representation of the SensorDataIndex for this snapshot.

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.

Specified by:
getSensorDataIndex in class DbImplementation
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.

constructLikeClauses

static java.lang.String constructLikeClauses(java.util.List<java.lang.String> uriPatterns)
Constructs a set of LIKE clauses corresponding to the passed set of UriPatterns.

Each UriPattern is translated in the following way:

The new set of 'translated' UriPatterns are now used to generate a set of LIKE clauses with the following form:
 (RESOURCE like 'translatedUriPattern1' escape 'escapeChar1') OR
 (RESOURCE like 'translatedUriPattern2' escape 'escapeChar2') ..
 

There is one special case. If the List(UriPattern) is null, empty, or consists of exactly one UriPattern which is "**" or "*", then the empty string is returned. This is an optimization for the common case where all resources should be matched and so we don't need any LIKE clauses.

We return either the empty string (""), or else a string of the form: " AND ([like clause] AND [like clause] ... )" This enables the return value to be appended to the SELECT statement.

This method is static and package private to support testing. See the class TestConstructUriPattern for example invocations and expected return values.

Parameters:
uriPatterns - The list of uriPatterns.
Returns:
The String to be used in the where clause to check for resource correctness.

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 since the given start and end 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.

Specified by:
getSensorDataIndexLastMod in class DbImplementation
Parameters:
user - The user whose data is being monitored.
lastModStartTime - The beginning lastMod timestamp of interest.
lastModEndTime - The ending lastMod timestamp of interest.
Returns:
The XML SensorDataIndex for the recently arrived data based upon the lastMod tstamps.

hasSensorData

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

Specified by:
hasSensorData in class DbImplementation
Parameters:
user - The user.
timestamp - The timestamp
Returns:
True if there is any sensor data for this [key, sdtName, timestamp].

deleteSensorData

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

Specified by:
deleteSensorData in class DbImplementation
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 is no longer present in this manager.

Specified by:
deleteSensorData in class DbImplementation
Parameters:
user - The user.

getSensorData

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

Specified by:
getSensorData in class DbImplementation
Parameters:
user - The user.
timestamp - The timestamp associated with this sensor data.
Returns:
The SensorData XML string, or null.

storeSensorDataType

public boolean storeSensorDataType(SensorDataType sdt,
                                   java.lang.String xmlSensorDataType,
                                   java.lang.String xmlSensorDataTypeRef)
Persists a SensorDataType instance. If a SensorDataType with this name already exists in the storage system, it will be overwritten.

Specified by:
storeSensorDataType in class DbImplementation
Parameters:
sdt - The sensor data type
xmlSensorDataType - The SensorDataType marshalled into an XML String.
xmlSensorDataTypeRef - The corresponding SensorDataTypeRef marshalled into an XML String
Returns:
True if the SDT was successfully inserted.

deleteSensorDataType

public void deleteSensorDataType(java.lang.String sdtName)
Ensures that the SensorDataType with the given name is no longer present in this manager.

Specified by:
deleteSensorDataType in class DbImplementation
Parameters:
sdtName - The SDT name.

getSensorDataTypeIndex

public java.lang.String getSensorDataTypeIndex()
Returns the XML SensorDataTypeIndex for all SDTs in this server.

Specified by:
getSensorDataTypeIndex in class DbImplementation
Returns:
The XML String containing an index to all SensorDataTypes.

getSensorDataType

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

Specified by:
getSensorDataType in class DbImplementation
Parameters:
sdtName - The SDT name.
Returns:
The SensorDataType XML string, or null.

deleteUser

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

Specified by:
deleteUser in class DbImplementation
Parameters:
email - The user's email address.

getUser

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

Specified by:
getUser in class DbImplementation
Parameters:
email - The user's email.
Returns:
The User XML string, or null.

getUserIndex

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

Specified by:
getUserIndex in class DbImplementation
Returns:
The XML String containing an index to all Users.

storeUser

public boolean storeUser(User user,
                         java.lang.String xmlUser,
                         java.lang.String xmlUserRef)
Persists a User instance. If a User with this name already exists in the storage system, it will be overwritten.

Specified by:
storeUser in class DbImplementation
Parameters:
user - The user
xmlUser - The User marshalled into an XML String.
xmlUserRef - The corresponding UserRef marshalled into an XML String
Returns:
True if the user was successfully inserted.

deleteProject

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

Specified by:
deleteProject in class DbImplementation
Parameters:
owner - The User who owns this project.
projectName - The name of the Project.

getProject

public java.lang.String getProject(User owner,
                                   java.lang.String projectName)
Returns the Project instance as XML string, or null if not found.

Specified by:
getProject in class DbImplementation
Parameters:
owner - The user who owns the project.
projectName - The name of the Project.
Returns:
The Project XML string, or null.

getProjectIndex

public java.lang.String getProjectIndex()
Returns the XML ProjectIndex for all Projects in this server.

Specified by:
getProjectIndex in class DbImplementation
Returns:
The XML String containing an index to all Projects.

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.

Specified by:
getProjectSummary in class DbImplementation
Parameters:
users - The 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.

storeProject

public boolean storeProject(Project project,
                            java.lang.String xmlProject,
                            java.lang.String xmlProjectRef)
Persists a Project instance. If a Project with this owner and name already exists in the storage system, it will be overwritten.

Specified by:
storeProject in class DbImplementation
Parameters:
project - The Project.
xmlProject - The Project marshalled into an XML String.
xmlProjectRef - The corresponding ProjectRef marshalled into an XML String
Returns:
True if the user was successfully inserted.

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.

Specified by:
compressTables in class DbImplementation
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.

Specified by:
indexTables in class DbImplementation
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.

Specified by:
getRowCount in class DbImplementation
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.

Specified by:
getTableNames in class DbImplementation
Returns:
A set of table names.