gov.sns.tools.pvlogger
Class SqlStateStore

java.lang.Object
  |
  +--gov.sns.tools.pvlogger.SqlStateStore
All Implemented Interfaces:
StateStore

public class SqlStateStore
extends java.lang.Object
implements StateStore

SqlStateStore is an implementation of StateStore that provides persistent storage of machine state in a SQL database.


Field Summary
protected  java.sql.Connection _connection
           
protected  DatabaseAdaptor _databaseAdaptor
           
protected  java.sql.PreparedStatement CHANNEL_SNAPSHOT_INSERT
           
protected  java.sql.PreparedStatement CHANNEL_SNAPSHOT_QUERY_BY_MACHINE_SNAPSHOT
           
protected static java.lang.String MACHINE_SNAPSHOT_COMMENT_COL
           
protected  java.sql.PreparedStatement MACHINE_SNAPSHOT_INSERT
           
protected  java.sql.PreparedStatement MACHINE_SNAPSHOT_NEXT_PKEY
           
protected static java.lang.String MACHINE_SNAPSHOT_PKEY
           
protected  java.sql.PreparedStatement MACHINE_SNAPSHOT_QUERY
           
protected  java.sql.PreparedStatement MACHINE_SNAPSHOT_QUERY_BY_PKEY
           
protected  java.sql.PreparedStatement MACHINE_SNAPSHOT_QUERY_BY_TIMERANGE
           
protected  java.sql.PreparedStatement MACHINE_SNAPSHOT_QUERY_BY_TYPE_TIMERANGE
           
protected static java.lang.String MACHINE_SNAPSHOT_SEQ
           
protected static java.lang.String MACHINE_SNAPSHOT_TABLE
           
protected static java.lang.String MACHINE_SNAPSHOT_TIME_COL
           
protected static java.lang.String MACHINE_SNAPSHOT_TYPE_COL
           
protected static java.lang.String PV_PKEY
           
protected  java.sql.PreparedStatement PV_QUERY
           
protected static java.lang.String PV_SNAPSHOT_MACHINE_SNAPSHOT_COL
           
protected static java.lang.String PV_SNAPSHOT_PV_COL
           
protected static java.lang.String PV_SNAPSHOT_SEVERITY_COL
           
protected static java.lang.String PV_SNAPSHOT_STATUS_COL
           
protected static java.lang.String PV_SNAPSHOT_TABLE
           
protected static java.lang.String PV_SNAPSHOT_TIMESTAMP_COL
           
protected static java.lang.String PV_SNAPSHOT_VALUE_COL
           
protected static java.lang.String PV_TABLE
           
protected static java.lang.String SGNL_VALUE_ARRAY_TYPE
           
protected static java.lang.String SNAPSHOT_TYPE_DESC_COL
           
protected static java.lang.String SNAPSHOT_TYPE_PERIOD_COL
           
protected static java.lang.String SNAPSHOT_TYPE_PKEY
           
protected static java.lang.String SNAPSHOT_TYPE_PV_NAME_PKEY
           
protected static java.lang.String SNAPSHOT_TYPE_PV_PV_PKEY
           
protected  java.sql.PreparedStatement SNAPSHOT_TYPE_PV_QUERY_BY_NAME
           
protected static java.lang.String SNAPSHOT_TYPE_PV_TABLE
           
protected  java.sql.PreparedStatement SNAPSHOT_TYPE_QUERY
           
protected  java.sql.PreparedStatement SNAPSHOT_TYPE_QUERY_BY_NAME
           
protected static java.lang.String SNAPSHOT_TYPE_TABLE
           
 
Constructor Summary
SqlStateStore(ConnectionDictionary dictionary)
          Construct a SqlStateStore and connect it to a database specified by the url and with user and password access.
SqlStateStore(DatabaseAdaptor adaptor, java.lang.String urlSpec, java.lang.String user, java.lang.String password)
          Construct a SqlStateStore and connect it to a database specified by the url and with user and password access.
SqlStateStore(java.lang.String urlSpec, java.lang.String user, java.lang.String password)
          Construct a SqlStateStore and connect it to a database specified by the url and with user and password access.
 
Method Summary
protected  void connect(java.lang.String urlSpec, java.lang.String user, java.lang.String password)
          Create a database connection to the persistent data storage.
protected  ChannelSnapshot[] fetchChannelSnapshotsForMachine(long id)
          Fetch the channel snapshots associated with a machine snapshot given by the machine snapshot's unique identifier.
 ChannelGroup fetchGroup(java.lang.String type)
          Fetch a channel group for the specified logger type
 MachineSnapshot fetchMachineSnapshot(long id)
          Fetch the machine snapshot associated with the unique machine snapshot identifier.
protected  MachineSnapshot[] fetchMachineSnapshotsInRange(java.util.Date startTime, java.util.Date endTime)
          Fetch the machine snapshots within the specified time range.
 MachineSnapshot[] fetchMachineSnapshotsInRange(java.lang.String type, java.util.Date startTime, java.util.Date endTime)
          Fetch the machine snapshots within the specified time range.
 java.lang.String[] fetchTypes()
          Fetch an array of logger types
protected  java.sql.PreparedStatement getChannelSnapshotInsertStatement()
          Create the prepared statement if it does not already exist.
protected  java.sql.PreparedStatement getChannelSnapshotQueryByMachineSnapshotStatement()
          Create the prepared statement if it does not already exist.
protected  java.sql.PreparedStatement getMachineSnapshotInsertStatement()
          Create the prepared statement if it does not already exist.
protected  java.sql.PreparedStatement getMachineSnapshotNextPrimaryKeyStatement()
          Create the prepared statement if it does not already exist.
protected  java.sql.PreparedStatement getMachineSnapshotQueryByPkeyStatement()
          Create the prepared statement if it does not already exist.
protected  java.sql.PreparedStatement getMachineSnapshotQueryByTimerangeStatement()
          Create the prepared statement if it does not already exist.
protected  java.sql.PreparedStatement getMachineSnapshotQueryByTypeTimerangeStatement()
          Create the prepared statement if it does not already exist.
protected  java.sql.PreparedStatement getMachineSnapshotQueryStatement()
          Create the prepared statement if it does not already exist.
protected  java.sql.PreparedStatement getPvQueryStatement()
          Create the prepared statement if it does not already exist.
protected  java.sql.PreparedStatement getSnapshotTypePvQueryByNameStatement()
          Create the prepared statement if it does not already exist.
protected  java.sql.PreparedStatement getSnapshotTypeQueryByNameStatement()
          Create the prepared statement if it does not already exist.
protected  java.sql.PreparedStatement getSnapshotTypeQueryStatement()
          Create the prepared statement if it does not already exist.
 MachineSnapshot loadChannelSnapshotsInto(MachineSnapshot machineSnapshot)
          Fetch the channel snapshots from the data source and populate the machine snapshot
 void publish(ChannelSnapshot snapshot, long machineId)
          Publish the channel snapshot and associate it with the machine snapshot given by the machine snapshop id.
 void publish(MachineSnapshot machineSnapshot)
          Publish the machine snapshot.
protected  double[] toDoubleArray(java.lang.Number[] numbers)
          Convert an array of numbers to an array of double values.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

PV_TABLE

protected static final java.lang.String PV_TABLE
See Also:
Constant Field Values

MACHINE_SNAPSHOT_TABLE

protected static final java.lang.String MACHINE_SNAPSHOT_TABLE
See Also:
Constant Field Values

PV_SNAPSHOT_TABLE

protected static final java.lang.String PV_SNAPSHOT_TABLE
See Also:
Constant Field Values

SNAPSHOT_TYPE_TABLE

protected static final java.lang.String SNAPSHOT_TYPE_TABLE
See Also:
Constant Field Values

SNAPSHOT_TYPE_PV_TABLE

protected static final java.lang.String SNAPSHOT_TYPE_PV_TABLE
See Also:
Constant Field Values

SGNL_VALUE_ARRAY_TYPE

protected static final java.lang.String SGNL_VALUE_ARRAY_TYPE
See Also:
Constant Field Values

PV_PKEY

protected static final java.lang.String PV_PKEY
See Also:
Constant Field Values

PV_SNAPSHOT_MACHINE_SNAPSHOT_COL

protected static final java.lang.String PV_SNAPSHOT_MACHINE_SNAPSHOT_COL
See Also:
Constant Field Values

PV_SNAPSHOT_PV_COL

protected static final java.lang.String PV_SNAPSHOT_PV_COL
See Also:
Constant Field Values

PV_SNAPSHOT_TIMESTAMP_COL

protected static final java.lang.String PV_SNAPSHOT_TIMESTAMP_COL
See Also:
Constant Field Values

PV_SNAPSHOT_VALUE_COL

protected static final java.lang.String PV_SNAPSHOT_VALUE_COL
See Also:
Constant Field Values

PV_SNAPSHOT_STATUS_COL

protected static final java.lang.String PV_SNAPSHOT_STATUS_COL
See Also:
Constant Field Values

PV_SNAPSHOT_SEVERITY_COL

protected static final java.lang.String PV_SNAPSHOT_SEVERITY_COL
See Also:
Constant Field Values

MACHINE_SNAPSHOT_PKEY

protected static final java.lang.String MACHINE_SNAPSHOT_PKEY
See Also:
Constant Field Values

MACHINE_SNAPSHOT_TIME_COL

protected static final java.lang.String MACHINE_SNAPSHOT_TIME_COL
See Also:
Constant Field Values

MACHINE_SNAPSHOT_TYPE_COL

protected static final java.lang.String MACHINE_SNAPSHOT_TYPE_COL
See Also:
Constant Field Values

MACHINE_SNAPSHOT_COMMENT_COL

protected static final java.lang.String MACHINE_SNAPSHOT_COMMENT_COL
See Also:
Constant Field Values

SNAPSHOT_TYPE_PV_NAME_PKEY

protected static final java.lang.String SNAPSHOT_TYPE_PV_NAME_PKEY
See Also:
Constant Field Values

SNAPSHOT_TYPE_PV_PV_PKEY

protected static final java.lang.String SNAPSHOT_TYPE_PV_PV_PKEY
See Also:
Constant Field Values

SNAPSHOT_TYPE_PKEY

protected static final java.lang.String SNAPSHOT_TYPE_PKEY
See Also:
Constant Field Values

SNAPSHOT_TYPE_DESC_COL

protected static final java.lang.String SNAPSHOT_TYPE_DESC_COL
See Also:
Constant Field Values

SNAPSHOT_TYPE_PERIOD_COL

protected static final java.lang.String SNAPSHOT_TYPE_PERIOD_COL
See Also:
Constant Field Values

MACHINE_SNAPSHOT_SEQ

protected static final java.lang.String MACHINE_SNAPSHOT_SEQ
See Also:
Constant Field Values

_databaseAdaptor

protected DatabaseAdaptor _databaseAdaptor

MACHINE_SNAPSHOT_NEXT_PKEY

protected java.sql.PreparedStatement MACHINE_SNAPSHOT_NEXT_PKEY

MACHINE_SNAPSHOT_INSERT

protected java.sql.PreparedStatement MACHINE_SNAPSHOT_INSERT

CHANNEL_SNAPSHOT_INSERT

protected java.sql.PreparedStatement CHANNEL_SNAPSHOT_INSERT

PV_QUERY

protected java.sql.PreparedStatement PV_QUERY

MACHINE_SNAPSHOT_QUERY

protected java.sql.PreparedStatement MACHINE_SNAPSHOT_QUERY

MACHINE_SNAPSHOT_QUERY_BY_PKEY

protected java.sql.PreparedStatement MACHINE_SNAPSHOT_QUERY_BY_PKEY

MACHINE_SNAPSHOT_QUERY_BY_TYPE_TIMERANGE

protected java.sql.PreparedStatement MACHINE_SNAPSHOT_QUERY_BY_TYPE_TIMERANGE

MACHINE_SNAPSHOT_QUERY_BY_TIMERANGE

protected java.sql.PreparedStatement MACHINE_SNAPSHOT_QUERY_BY_TIMERANGE

CHANNEL_SNAPSHOT_QUERY_BY_MACHINE_SNAPSHOT

protected java.sql.PreparedStatement CHANNEL_SNAPSHOT_QUERY_BY_MACHINE_SNAPSHOT

SNAPSHOT_TYPE_QUERY

protected java.sql.PreparedStatement SNAPSHOT_TYPE_QUERY

SNAPSHOT_TYPE_QUERY_BY_NAME

protected java.sql.PreparedStatement SNAPSHOT_TYPE_QUERY_BY_NAME

SNAPSHOT_TYPE_PV_QUERY_BY_NAME

protected java.sql.PreparedStatement SNAPSHOT_TYPE_PV_QUERY_BY_NAME

_connection

protected java.sql.Connection _connection
Constructor Detail

SqlStateStore

public SqlStateStore(DatabaseAdaptor adaptor,
                     java.lang.String urlSpec,
                     java.lang.String user,
                     java.lang.String password)
              throws StateStoreException
Construct a SqlStateStore and connect it to a database specified by the url and with user and password access.

Parameters:
adaptor - The database adaptor
urlSpec - The url of the database
user - The user to login
password - The user's password for login
Throws:
StateStoreException - if a SQL exception is thrown

SqlStateStore

public SqlStateStore(java.lang.String urlSpec,
                     java.lang.String user,
                     java.lang.String password)
              throws StateStoreException
Construct a SqlStateStore and connect it to a database specified by the url and with user and password access.

Parameters:
urlSpec - The url of the database
user - The user to login
password - The user's password for login
Throws:
StateStoreException - if a SQL exception is thrown

SqlStateStore

public SqlStateStore(ConnectionDictionary dictionary)
              throws StateStoreException
Construct a SqlStateStore and connect it to a database specified by the url and with user and password access.

Parameters:
dictionary - The dictionary containing the connection information
Throws:
StateStoreException - if a SQL exception is thrown
Method Detail

connect

protected void connect(java.lang.String urlSpec,
                       java.lang.String user,
                       java.lang.String password)
                throws StateStoreException
Create a database connection to the persistent data storage.

Parameters:
urlSpec - The url of the database
user - The user to login
password - The user's password for login
Throws:
StateStoreException - if a SQL exception is thrown

getMachineSnapshotNextPrimaryKeyStatement

protected java.sql.PreparedStatement getMachineSnapshotNextPrimaryKeyStatement()
                                                                        throws java.sql.SQLException
Create the prepared statement if it does not already exist.

Returns:
the prepared statement for making a new machine snapshot primary key and fetching it
Throws:
java.sql.SQLException - if an exception occurs during a SQL evaluation

getMachineSnapshotInsertStatement

protected java.sql.PreparedStatement getMachineSnapshotInsertStatement()
                                                                throws java.sql.SQLException
Create the prepared statement if it does not already exist.

Returns:
the prepared statement for inserting a new machine snapshot
Throws:
java.sql.SQLException - if an exception occurs during a SQL evaluation

getChannelSnapshotInsertStatement

protected java.sql.PreparedStatement getChannelSnapshotInsertStatement()
                                                                throws java.sql.SQLException
Create the prepared statement if it does not already exist.

Returns:
the prepared statement for inserting a new channel snapshot
Throws:
java.sql.SQLException - if an exception occurs during a SQL evaluation

getPvQueryStatement

protected java.sql.PreparedStatement getPvQueryStatement()
                                                  throws java.sql.SQLException
Create the prepared statement if it does not already exist.

Returns:
the prepared statement for querying the PV table for the PV record
Throws:
java.sql.SQLException - if an exception occurs during a SQL evaluation

getMachineSnapshotQueryStatement

protected java.sql.PreparedStatement getMachineSnapshotQueryStatement()
                                                               throws java.sql.SQLException
Create the prepared statement if it does not already exist.

Returns:
the prepared statement to query for all machine snapshots
Throws:
java.sql.SQLException - if an exception occurs during a SQL evaluation

getMachineSnapshotQueryByPkeyStatement

protected java.sql.PreparedStatement getMachineSnapshotQueryByPkeyStatement()
                                                                     throws java.sql.SQLException
Create the prepared statement if it does not already exist.

Returns:
the prepared statement to query for machine snapshots by primary key
Throws:
java.sql.SQLException - if an exception occurs during a SQL evaluation

getMachineSnapshotQueryByTypeTimerangeStatement

protected java.sql.PreparedStatement getMachineSnapshotQueryByTypeTimerangeStatement()
                                                                              throws java.sql.SQLException
Create the prepared statement if it does not already exist.

Returns:
the prepared statement to query for machine snapshots by type and time range
Throws:
java.sql.SQLException - if an exception occurs during a SQL evaluation

getMachineSnapshotQueryByTimerangeStatement

protected java.sql.PreparedStatement getMachineSnapshotQueryByTimerangeStatement()
                                                                          throws java.sql.SQLException
Create the prepared statement if it does not already exist.

Returns:
the prepared statement to query for machine snapshots by time range
Throws:
java.sql.SQLException - if an exception occurs during a SQL evaluation

getChannelSnapshotQueryByMachineSnapshotStatement

protected java.sql.PreparedStatement getChannelSnapshotQueryByMachineSnapshotStatement()
                                                                                throws java.sql.SQLException
Create the prepared statement if it does not already exist.

Returns:
the prepared statement to query for channel snapshots by machine snapshot
Throws:
java.sql.SQLException - if an exception occurs during a SQL evaluation

getSnapshotTypeQueryStatement

protected java.sql.PreparedStatement getSnapshotTypeQueryStatement()
                                                            throws java.sql.SQLException
Create the prepared statement if it does not already exist.

Returns:
the prepared statement to query for available snapshot types
Throws:
java.sql.SQLException - if an exception occurs during a SQL evaluation

getSnapshotTypeQueryByNameStatement

protected java.sql.PreparedStatement getSnapshotTypeQueryByNameStatement()
                                                                  throws java.sql.SQLException
Create the prepared statement if it does not already exist.

Returns:
the prepared statement to query for available snapshot types
Throws:
java.sql.SQLException - if an exception occurs during a SQL evaluation

getSnapshotTypePvQueryByNameStatement

protected java.sql.PreparedStatement getSnapshotTypePvQueryByNameStatement()
                                                                    throws java.sql.SQLException
Create the prepared statement if it does not already exist.

Returns:
the prepared statement to query for machine snapshot type-PV record by type
Throws:
java.sql.SQLException - if an exception occurs during a SQL evaluation

publish

public void publish(ChannelSnapshot snapshot,
                    long machineId)
             throws StateStoreException
Publish the channel snapshot and associate it with the machine snapshot given by the machine snapshop id.

Specified by:
publish in interface StateStore
Parameters:
snapshot - The channel snapshot to publish
machineId - The unique id of the associated machine snapshot
Throws:
StateStoreException - if a SQL exception is thrown

publish

public void publish(MachineSnapshot machineSnapshot)
             throws StateStoreException
Publish the machine snapshot.

Specified by:
publish in interface StateStore
Parameters:
machineSnapshot - The machine snapshot to publish.
Throws:
StateStoreException - if a SQL exception is thrown

fetchTypes

public java.lang.String[] fetchTypes()
                              throws StateStoreException
Fetch an array of logger types

Specified by:
fetchTypes in interface StateStore
Returns:
an array of available logger types
Throws:
StateStoreException - if a SQL exception is thrown

fetchGroup

public ChannelGroup fetchGroup(java.lang.String type)
                        throws StateStoreException
Fetch a channel group for the specified logger type

Specified by:
fetchGroup in interface StateStore
Parameters:
type - the logger type
Returns:
a channel group for the logger type which includes the type, description and the pvs to log
Throws:
StateStoreException - if a SQL exception is thrown

fetchMachineSnapshot

public MachineSnapshot fetchMachineSnapshot(long id)
                                     throws StateStoreException
Fetch the machine snapshot associated with the unique machine snapshot identifier.

Specified by:
fetchMachineSnapshot in interface StateStore
Parameters:
id - The unique machine snapshot identifier
Returns:
The machine snapshop read from the persistent store.
Throws:
StateStoreException - if a SQL exception is thrown

loadChannelSnapshotsInto

public MachineSnapshot loadChannelSnapshotsInto(MachineSnapshot machineSnapshot)
                                         throws StateStoreException
Fetch the channel snapshots from the data source and populate the machine snapshot

Specified by:
loadChannelSnapshotsInto in interface StateStore
Parameters:
machineSnapshot - The machine snapshot for which to fetch the channel snapshots and load them
Returns:
the machineSnapshot which is the same as the parameter returned for convenience
StateStoreException

fetchMachineSnapshotsInRange

public MachineSnapshot[] fetchMachineSnapshotsInRange(java.lang.String type,
                                                      java.util.Date startTime,
                                                      java.util.Date endTime)
                                               throws StateStoreException
Fetch the machine snapshots within the specified time range. If the type is not null, then restrict the machine snapshots to those of the specified type. The machine snapshots do not include the channel snapshots. A complete snapshot can be obtained using the fetchMachineSnapshot(id) method.

Specified by:
fetchMachineSnapshotsInRange in interface StateStore
Parameters:
type - The type of machine snapshots to fetch or null for no restriction
startTime - The start time of the time range
endTime - The end time of the time range
Returns:
An array of machine snapshots meeting the specified criteria
Throws:
StateStoreException - if a SQL exception is thrown

fetchMachineSnapshotsInRange

protected MachineSnapshot[] fetchMachineSnapshotsInRange(java.util.Date startTime,
                                                         java.util.Date endTime)
                                                  throws StateStoreException
Fetch the machine snapshots within the specified time range. The machine snapshots do not include the channel snapshots. A complete snapshot can be obtained using the fetchMachineSnapshot(id) method.

Parameters:
startTime - The start time of the time range
endTime - The end time of the time range
Returns:
An array of machine snapshots meeting the specified criteria
Throws:
StateStoreException - if a SQL exception is thrown

fetchChannelSnapshotsForMachine

protected ChannelSnapshot[] fetchChannelSnapshotsForMachine(long id)
                                                     throws StateStoreException
Fetch the channel snapshots associated with a machine snapshot given by the machine snapshot's unique identifier.

Parameters:
id - The unique machine identifier.
Returns:
The channel snapshots associated with the machine snapshop
Throws:
StateStoreException - if a SQL exception is thrown

toDoubleArray

protected double[] toDoubleArray(java.lang.Number[] numbers)
Convert an array of numbers to an array of double values.

Parameters:
numbers - The array of numbers to convert
Returns:
An array of double values corresponding to the input array of numbers.