Commit in hps-java/src/main/java/org/lcsim/hps/conditions on MAIN | |||
DatabaseConditionsReader.java | +147 | added 1.1 | |
HPSConditionsReader.java | -147 | 1.1 removed | |
+147 | -147 |
Rename class to fix filename collision on case-insensitive systems
diff -N DatabaseConditionsReader.java --- /dev/null 1 Jan 1970 00:00:00 -0000 +++ DatabaseConditionsReader.java 27 Mar 2013 04:01:56 -0000 1.1 @@ -0,0 +1,147 @@
+package org.lcsim.hps.conditions; + +import java.io.ByteArrayInputStream; +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.io.InputStream; +import java.sql.Connection; +import java.sql.DriverManager; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.sql.Statement; +import java.util.*; +import org.lcsim.conditions.ConditionsConverter; +import org.lcsim.conditions.ConditionsManager; +import org.lcsim.conditions.ConditionsReader; + +/** + * HPS-specific conditions reader. Wraps around any standard reader and adds an ability to + * fetch calibration data from the database. + * <p> + * TEMPORARY IMPLEMENTATION - DEMO. + * + * @author onoprien + * @version $Id: DatabaseConditionsReader.java,v 1.1 2013/03/27 04:01:56 meeg Exp $ + */ +public class DatabaseConditionsReader extends ConditionsReader { + +// -- Private parts : ---------------------------------------------------------- + + static ConditionsConverter _converterCalibration = new CalibrationConverter(); + + private final ConditionsReader _reader; + + private String _detectorName; + private int _minRun = Integer.MAX_VALUE; + private int _maxRun = Integer.MIN_VALUE; + + private final HashMap<String, byte[]> _propCache = new HashMap<String, byte[]>(); + +// -- Construction and initialization : ---------------------------------------- + + public DatabaseConditionsReader(ConditionsReader reader) { + _reader = reader; + } + + +// -- Updating to a new detector/run : ----------------------------------------- + + public boolean update(ConditionsManager manager, String detectorName, int run) throws IOException { + + if (_detectorName == null) { + _detectorName = detectorName; + } else { + if (!_detectorName.equals(detectorName)) throw new IllegalArgumentException(); + } + if ( run <= _maxRun && run >= _minRun) return false; + manager.registerConditionsConverter(_converterCalibration); + + _propCache.clear(); + + // Open connection to the db + + Properties connectionProps = new Properties(); + connectionProps.put("user", "rd_hps_cond_ro"); + connectionProps.put("password", "2jumpinphotons."); + Connection con = null; + try { + con = DriverManager.getConnection("jdbc:mysql://mysql-node03.slac.stanford.edu:3306/", connectionProps); + } catch (SQLException x) { + throw new IOException("Failed to connect to database", x); + } + + // Fetch whatever database-kept conditions should be accessible through ConditionsSet interface. + // 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'"; + + Statement stmt = null; + int count = 0; + String data = null; + try { + stmt = con.createStatement(); + ResultSet rs = stmt.executeQuery(query); + while (rs.next()) { + count++; + data = rs.getString(1); + _minRun = rs.getInt(2); + _maxRun = rs.getInt(3); + } + } catch (SQLException x) { + throw new IOException("Failed to execute query", x); + } finally { + if (stmt != null) { + try { + stmt.close(); + } catch (SQLException x) { + throw new IOException("Failed to close statement", x); + } + } + } + + // Need to get requirements from HPS people - how is this case supposed to be handled ? + if (count != 1) throw new IOException("Found "+ count +" valid calibrations"); + + Properties p = new Properties(); + String[] d = data.trim().split(":"); + 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); + } + ByteArrayOutputStream stream = new ByteArrayOutputStream(); + p.store(stream, null); + _propCache.put("calibration", stream.toByteArray()); + + // Close connection to the db + + try { + con.close(); + } catch (SQLException x) { + throw new IOException("Failed to close connection", x); + } + + return true; + } + +// -- Implementing ConditionsReader : ------------------------------------------ + + public void close() throws IOException { + _reader.close(); + } + + public InputStream open(String name, String type) throws IOException { + byte[] ba = _propCache.get(name); + if (ba == null) { + return _reader.open(name, type); + } else { + return new ByteArrayInputStream(ba); + } + } + +// ----------------------------------------------------------------------------- +}
diff -N HPSConditionsReader.java --- HPSConditionsReader.java 19 Mar 2012 20:58:11 -0000 1.1 +++ /dev/null 1 Jan 1970 00:00:00 -0000 @@ -1,147 +0,0 @@
-package org.lcsim.hps.conditions; - -import java.io.ByteArrayInputStream; -import java.io.ByteArrayOutputStream; -import java.io.IOException; -import java.io.InputStream; -import java.sql.Connection; -import java.sql.DriverManager; -import java.sql.ResultSet; -import java.sql.SQLException; -import java.sql.Statement; -import java.util.*; -import org.lcsim.conditions.ConditionsConverter; -import org.lcsim.conditions.ConditionsManager; -import org.lcsim.conditions.ConditionsReader; - -/** - * HPS-specific conditions reader. Wraps around any standard reader and adds an ability to - * fetch calibration data from the database. - * <p> - * TEMPORARY IMPLEMENTATION - DEMO. - * - * @author onoprien - * @version $Id: HPSConditionsReader.java,v 1.1 2012/03/19 20:58:11 onoprien Exp $ - */ -public class HPSConditionsReader extends ConditionsReader { - -// -- Private parts : ---------------------------------------------------------- - - static ConditionsConverter _converterCalibration = new CalibrationConverter(); - - private final ConditionsReader _reader; - - private String _detectorName; - private int _minRun = Integer.MAX_VALUE; - private int _maxRun = Integer.MIN_VALUE; - - private final HashMap<String, byte[]> _propCache = new HashMap<String, byte[]>(); - -// -- Construction and initialization : ---------------------------------------- - - public HPSConditionsReader(ConditionsReader reader) { - _reader = reader; - } - - -// -- Updating to a new detector/run : ----------------------------------------- - - public boolean update(ConditionsManager manager, String detectorName, int run) throws IOException { - - if (_detectorName == null) { - _detectorName = detectorName; - } else { - if (!_detectorName.equals(detectorName)) throw new IllegalArgumentException(); - } - if ( run <= _maxRun && run >= _minRun) return false; - manager.registerConditionsConverter(_converterCalibration); - - _propCache.clear(); - - // Open connection to the db - - Properties connectionProps = new Properties(); - connectionProps.put("user", "rd_hps_cond_ro"); - connectionProps.put("password", "2jumpinphotons."); - Connection con = null; - try { - con = DriverManager.getConnection("jdbc:mysql://mysql-node03.slac.stanford.edu:3306/", connectionProps); - } catch (SQLException x) { - throw new IOException("Failed to connect to database", x); - } - - // Fetch whatever database-kept conditions should be accessible through ConditionsSet interface. - // 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'"; - - Statement stmt = null; - int count = 0; - String data = null; - try { - stmt = con.createStatement(); - ResultSet rs = stmt.executeQuery(query); - while (rs.next()) { - count++; - data = rs.getString(1); - _minRun = rs.getInt(2); - _maxRun = rs.getInt(3); - } - } catch (SQLException x) { - throw new IOException("Failed to execute query", x); - } finally { - if (stmt != null) { - try { - stmt.close(); - } catch (SQLException x) { - throw new IOException("Failed to close statement", x); - } - } - } - - // Need to get requirements from HPS people - how is this case supposed to be handled ? - if (count != 1) throw new IOException("Found "+ count +" valid calibrations"); - - Properties p = new Properties(); - String[] d = data.trim().split(":"); - 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); - } - ByteArrayOutputStream stream = new ByteArrayOutputStream(); - p.store(stream, null); - _propCache.put("calibration", stream.toByteArray()); - - // Close connection to the db - - try { - con.close(); - } catch (SQLException x) { - throw new IOException("Failed to close connection", x); - } - - return true; - } - -// -- Implementing ConditionsReader : ------------------------------------------ - - public void close() throws IOException { - _reader.close(); - } - - public InputStream open(String name, String type) throws IOException { - byte[] ba = _propCache.get(name); - if (ba == null) { - return _reader.open(name, type); - } else { - return new ByteArrayInputStream(ba); - } - } - -// ----------------------------------------------------------------------------- -}
Use REPLY-ALL to reply to list
To unsubscribe from the LCD-CVS list, click the following link:
https://listserv.slac.stanford.edu/cgi-bin/wa?SUBED1=LCD-CVS&A=1