hps-java/src/main/java/org/lcsim/hps/conditions
diff -u -r1.2 -r1.3
--- DatabaseConditionsReader.java 18 Sep 2013 02:33:16 -0000 1.2
+++ DatabaseConditionsReader.java 18 Sep 2013 21:03:23 -0000 1.3
@@ -2,6 +2,9 @@
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
@@ -16,11 +19,24 @@
import org.lcsim.hps.conditions.svt.SvtCalibrationConverter;
/**
- * This a refactored version of Dima's ExampleDatabaseConditionsReader that attempts
- * to handle the conditions and their meta data more generically. (Work in progress!)
+ * This a rewritten version of Dima's ExampleDatabaseConditionsReader that attempts to handle
+ * the conditions and their meta data in a fully generic fashion.
+ *
+ * In order to override the default database connection parameters, the system property
+ * <code>hps.conditions.db.configuration</code> should point to a properties file defining
+ * the variables read by ConnectionParameters (see that class for details). Otherwise, the
+ * defaults will be used to connect to a test database at SLAC.
+ *
+ * Setting custom connection properties would look something like the following from the CL:
+ *
+ * java -Dhps.conditions.db.configuration=/path/to/my/config.prop [...]
+ *
+ * Currently, this class should "know" about all the converters that are needed for loading
+ * conditions data. The SVT is the only example currently, but more will be added as they
+ * are created.
*
* @author jeremym
- * @version $Id: DatabaseConditionsReader.java,v 1.2 2013/09/18 02:33:16 jeremy Exp $
+ * @version $Id: DatabaseConditionsReader.java,v 1.3 2013/09/18 21:03:23 jeremy Exp $
*/
public class DatabaseConditionsReader extends ConditionsReader {
@@ -42,17 +58,45 @@
/** Current maximum run number. This is updated as conditions are loaded. */
private int maxRun = Integer.MIN_VALUE;
+ /** This maps conditions types to their table, field, and set id. */
private final HashMap<String, byte[]> propertyCache = new HashMap<String, byte[]>();
/**
- * Class constructor taking a ConditionsReader, which is probably a ZipFileConditionsReader.
- * This constructor is automatically called by the ConditionsManager when this type has been
- * requested via the detector properties file.
+ * Class constructor taking a ConditionsReader. This constructor is automatically called
+ * by the ConditionsManager when this type of reader has been requested via the detector
+ * properties file.
*
* @param reader The basic ConditionsReader allowing access to the detector.
*/
public DatabaseConditionsReader(ConditionsReader reader) {
this.reader = reader;
+
+ // Setup possible custom database parameters.
+ setupConnectionParameters();
+ }
+
+ /**
+ * Setup custom database connection parameters if there is a configuration file pointed to by
+ * a system property.
+ */
+ public void setupConnectionParameters() {
+ Object o = System.getProperties().get("hps.conditions.db.configuration");
+ if (o != null) {
+ String config = o.toString();
+ Properties p;
+ if (config != null) {
+ p = new Properties();
+ try {
+ p.load(new FileInputStream(new File(config)));
+ } catch (FileNotFoundException e) {
+ throw new RuntimeException(e);
+ } catch (IOException e) {
+ throw new RuntimeException(e);
+ }
+ this.connectionParameters = ConnectionParameters
+ .fromProperties(p);
+ }
+ }
}
/**
@@ -128,10 +172,11 @@
*/
private final void cacheMetadata(int run) throws SQLException, IOException {
- String query = "SELECT data_ident, runStart, runEnd, calib_type FROM rd_hps_cond.conditions_test WHERE "
- + "runStart <= "
+ // Query for fetching the validity records matching this run.
+ String query = "SELECT data_ident, run_start, run_end, calib_type FROM rd_hps_cond.conditions_test WHERE "
+ + "run_start <= "
+ run
- + " AND runEnd >= "
+ + " AND run_end >= "
+ run
+ " AND level = 'PROD'";
@@ -140,21 +185,23 @@
try {
statement = connection.createStatement();
ResultSet resultSet = statement.executeQuery(query);
+
+ // Iterate over conditions records.
while (resultSet.next()) {
- // String containing conditions set meta data.
+ // The data_ident field.
data = resultSet.getString(1);
- // The minimum run.
+ // The minimum run number.
minRun = resultSet.getInt(2);
- // The maximum run.
+ // The maximum run number.
maxRun = resultSet.getInt(3);
// The type of the calibration.
String calibrationType = resultSet.getString(4);
- // Cache the properties for this condition by type.
+ // Cache the data_ident by condition type for later use by converters.
addProperty(calibrationType, data);
}
} finally {
@@ -168,6 +215,15 @@
}
}
+ /**
+ * Register data_ident properties for a certain condition type.
+ * This includes table name, column name, and the id field.
+ * The information is later used by the converters to create the application
+ * data objects from SQL queries.
+ * @param key The key of the conditions. (for instance "svt_calibration")
+ * @param data The raw string data from the data_ident field in the database.
+ * @throws IOException if the data format is wrong.
+ */
private void addProperty(String key, String data) throws IOException {
Properties p = new Properties();
String[] d = data.trim().split(":");
@@ -183,7 +239,12 @@
propertyCache.put(key, stream.toByteArray());
}
+ /**
+ * Register the default conditions converters.
+ * @param manager The ConditionsManager.
+ */
private void registerConditionsConverters(ConditionsManager manager) {
manager.registerConditionsConverter(new SvtCalibrationConverter(connectionParameters));
+ // TODO: Add other converters here as they are made available.
}
}
hps-java/src/main/java/org/lcsim/hps/conditions/demo
diff -u -r1.1 -r1.2
--- DemoDatabaseConditionsReader.java 18 Sep 2013 02:33:16 -0000 1.1
+++ DemoDatabaseConditionsReader.java 18 Sep 2013 21:03:23 -0000 1.2
@@ -22,7 +22,7 @@
* TEMPORARY IMPLEMENTATION - DEMO.
*
* @author onoprien
- * @version $Id: DemoDatabaseConditionsReader.java,v 1.1 2013/09/18 02:33:16 jeremy Exp $
+ * @version $Id: DemoDatabaseConditionsReader.java,v 1.2 2013/09/18 21:03:23 jeremy Exp $
*/
public class DemoDatabaseConditionsReader extends ConditionsReader {
@@ -75,8 +75,8 @@
// This needs to be optimized once the db structure and data requirement are known.
// (can be templated if the db structure is standardized)
- String query = "SELECT data_ident, runStart, runEnd FROM rd_hps_cond.conditions_test WHERE "+
- "runStart <= "+ run +" AND runEnd >= "+ run + " AND level = 'PROD'";
+ String query = "SELECT data_ident, run_start, run_end FROM rd_hps_cond.conditions_test WHERE "+
+ "run_start <= "+ run +" AND run_end >= "+ run + " AND level = 'PROD'";
Statement stmt = null;
int count = 0;