hps-java/src/main/java/org/lcsim/hps/conditions
diff -u -r1.4 -r1.5
--- DatabaseConditionsReader.java 19 Sep 2013 01:28:05 -0000 1.4
+++ DatabaseConditionsReader.java 20 Sep 2013 21:56:24 -0000 1.5
@@ -10,6 +10,11 @@
import java.sql.Statement;
import java.util.HashMap;
import java.util.Properties;
+import java.util.logging.ConsoleHandler;
+import java.util.logging.Formatter;
+import java.util.logging.Level;
+import java.util.logging.LogRecord;
+import java.util.logging.Logger;
import org.lcsim.conditions.ConditionsManager;
import org.lcsim.conditions.ConditionsReader;
@@ -28,18 +33,15 @@
*
* 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.
+ * Currently, this class should "know" directly about all the converters that are needed for loading
+ * conditions data via the <code>registerConditionsConverters</code> method. The SVT is the only example
+ * currently, but more will be added as they are created.
*
* @author jeremym
- * @version $Id: DatabaseConditionsReader.java,v 1.4 2013/09/19 01:28:05 jeremy Exp $
+ * @version $Id: DatabaseConditionsReader.java,v 1.5 2013/09/20 21:56:24 jeremy Exp $
*/
public class DatabaseConditionsReader extends ConditionsReader {
-
- /** Database connection parameters. */
- //private static ConnectionParameters connectionParameters = new ConnectionParameters();
-
+
/** Database connection. */
private Connection connection = null;
@@ -57,6 +59,9 @@
/** This maps conditions types to their table, field, and set id. */
private final HashMap<String, byte[]> propertyCache = new HashMap<String, byte[]>();
+
+ /** The logger for printing messages. */
+ static Logger logger = null;
/**
* Class constructor taking a ConditionsReader. This constructor is automatically called
@@ -66,18 +71,52 @@
* @param reader The basic ConditionsReader allowing access to the detector.
*/
public DatabaseConditionsReader(ConditionsReader reader) {
- this.reader = reader;
+ this.reader = reader;
+
+ setupLogger();
+ }
+
+ /**
+ * Setup the logger.
+ */
+ private final void setupLogger() {
+ if (logger == null) {
+ logger = Logger.getLogger(this.getClass().getSimpleName());
+ logger.setUseParentHandlers(false);
+ logger.setLevel(Level.ALL);
+ ConsoleHandler handler = new ConsoleHandler();
+ handler.setFormatter(new ConditionsFormatter());
+ logger.addHandler(handler);
+ }
+ }
+
+ /**
+ * Simple log formatter for this class.
+ */
+ private static final class ConditionsFormatter extends Formatter {
+
+ public String format(LogRecord record) {
+ StringBuilder sb = new StringBuilder();
+ sb.append(record.getLoggerName() + " [ " + record.getLevel() + " ] " + record.getMessage() + '\n');
+ return sb.toString();
+ }
}
/**
* Update conditions for possibly new detector and run number. This will cache the
* conditions meta data but will not automatically do any conversion.
+ * @param manager The current conditions manager.
+ * @param detectorName The detector name.
+ * @param run The run number.
*/
public boolean update(ConditionsManager manager, String detectorName, int run) throws IOException {
+ logger.info("update");
+
// Setup detector.
if (this.detectorName == null) {
this.detectorName = detectorName;
+ logger.info("set detector name: " + this.detectorName);
} else {
if (!this.detectorName.equals(detectorName))
throw new IllegalArgumentException();
@@ -142,42 +181,49 @@
*/
private final void cacheDataIdent(int run) throws SQLException, IOException {
+ logger.info("cacheDataIdent");
+
String db = ConnectionManager.getConnectionParameters().getDatabase();
String table = ConnectionManager.getConnectionParameters().getConditionsTable();
-
- // Query for fetching the validity records matching this run.
- String query = "SELECT data_ident, run_start, run_end, calib_type FROM "
- + db + "." + table
+
+ String query = "SELECT run_start, run_end, name, table_name, field_name, field_value FROM "
+ + db + "." + table
+ " WHERE "
+ "run_start <= "
+ run
+ " AND run_end >= "
- + run
- + " AND level = 'PROD'";
+ + run;
+
+ logger.info("query: " + query);
Statement statement = null;
- String data = null;
try {
statement = connection.createStatement();
ResultSet resultSet = statement.executeQuery(query);
// Iterate over conditions records.
while (resultSet.next()) {
-
- // The data_ident field.
- data = resultSet.getString(1);
-
+
// The minimum run number.
- minRun = resultSet.getInt(2);
+ minRun = resultSet.getInt(1);
// The maximum run number.
- maxRun = resultSet.getInt(3);
+ maxRun = resultSet.getInt(2);
+
+ // The name of the conditions used as a key for lookup.
+ String name = resultSet.getString(3);
+
+ // The name of the table containing the actual data.
+ String tableName = resultSet.getString(4);
- // The type of the calibration.
- String calibrationType = resultSet.getString(4);
+ // The field used to ID the data set.
+ String fieldName = resultSet.getString(5);
- // Cache the data_ident by condition type for later use by converters.
- addProperty(calibrationType, data);
+ // The value of the ID field.
+ int fieldValue = resultSet.getInt(6);
+
+ // Cache the meta data for later use by converters.
+ addDataKey(name, tableName, fieldName, fieldValue);
}
} finally {
if (statement != null) {
@@ -189,9 +235,9 @@
}
}
}
-
+
/**
- * Register data_ident properties for a certain condition type.
+ * Register meta data 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.
@@ -199,18 +245,20 @@
* @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 {
+ private void addDataKey(String key, String tableName, String columnName, int value) {
+
+ logger.info("addDataKey: " + key + " => " + tableName + ":" + columnName + ":" + value);
+
Properties p = new Properties();
- String[] d = data.trim().split(":");
+ p.put("table", tableName);
+ p.put("column", columnName);
+ p.put("id", new Integer(value).toString());
+ ByteArrayOutputStream stream = new ByteArrayOutputStream();
try {
- p.put("table", d[0]);
- p.put("column", d[1]);
- p.put("id", d[2]);
- } catch (IndexOutOfBoundsException x) {
- throw new IOException("Illegal data_ident format", x);
+ p.store(stream, null);
+ } catch (IOException e) {
+ throw new RuntimeException(e);
}
- ByteArrayOutputStream stream = new ByteArrayOutputStream();
- p.store(stream, null);
propertyCache.put(key, stream.toByteArray());
}