Print

Print


Author: [log in to unmask]
Date: Wed Sep 23 12:57:18 2015
New Revision: 3683

Log:
Simplify run database API so data is directly available from run manager.

Modified:
    java/trunk/crawler/src/main/java/org/hps/crawler/Crawler.java
    java/trunk/run-database/src/main/java/org/hps/run/database/EpicsDataDao.java
    java/trunk/run-database/src/main/java/org/hps/run/database/EpicsVariableDao.java
    java/trunk/run-database/src/main/java/org/hps/run/database/RunDatabaseDaoFactory.java
    java/trunk/run-database/src/main/java/org/hps/run/database/RunManager.java
    java/trunk/run-database/src/main/java/org/hps/run/database/RunSummaryDao.java
    java/trunk/run-database/src/main/java/org/hps/run/database/RunSummaryDaoImpl.java
    java/trunk/run-database/src/main/java/org/hps/run/database/ScalerDataDao.java
    java/trunk/run-database/src/main/java/org/hps/run/database/TriggerConfigDao.java
    java/trunk/run-database/src/test/java/org/hps/run/database/TiTriggerOffsetTest.java

Modified: java/trunk/crawler/src/main/java/org/hps/crawler/Crawler.java
 =============================================================================
--- java/trunk/crawler/src/main/java/org/hps/crawler/Crawler.java	(original)
+++ java/trunk/crawler/src/main/java/org/hps/crawler/Crawler.java	Wed Sep 23 12:57:18 2015
@@ -23,11 +23,9 @@
 import org.hps.datacat.client.DatacatClient;
 import org.hps.datacat.client.DatacatClientFactory;
 import org.hps.datacat.client.DatasetFileFormat;
-import org.hps.run.database.RunDatabaseDaoFactory;
 import org.hps.run.database.RunManager;
 import org.hps.run.database.RunProcessor;
 import org.hps.run.database.RunSummary;
-import org.hps.run.database.RunSummaryDao;
 import org.hps.run.database.RunSummaryImpl;
 import org.lcsim.util.log.DefaultLogFormatter;
 import org.lcsim.util.log.LogUtil;
@@ -558,29 +556,21 @@
         final Connection connection = config.connectionParameters().createConnection();
 
         // Create factory for interfacing to run database.
-        final RunManager runManager = new RunManager();
-        runManager.setConnection(connection);
-        final RunDatabaseDaoFactory dbFactory = runManager.createDaoFactory();
-
-        // Create object for updating run info in the database.
-        final RunSummaryDao runSummaryDao = dbFactory.createRunSummaryDao();
+        final RunManager runManager = new RunManager(connection);
+        runManager.setRun(runSummary.getRun());
 
         // Delete existing run summary if necessary.
-        if (runSummaryDao.runSummaryExists(runSummary.getRun())) {
+        if (runManager.runExists()) {
             if (this.config.features.contains(CrawlerFeature.RUNDB_UPDATE)) {
                 LOGGER.info("deleting existing information for run " + runSummary.getRun());
-                runSummaryDao.deleteFullRunSummary(runSummary);
-            } else {
-                throw new RuntimeException("Run " + runSummary.getRun()
-                        + " exists in database and deletion is not enabled.");
-            }
-        }
-
-        // Insert run summary into database.
-        runSummaryDao.insertFullRunSummary(runSummary);
-
-        // Close the DB connection.
-        connection.close();
+                runManager.deleteRun();
+            }
+        } else {
+            throw new RuntimeException("Run " + runSummary.getRun()
+                    + " exists in database and deletion is not enabled.");
+        }
+    
+        runManager.closeConnection();
 
         LOGGER.info("done updating run database");
 

Modified: java/trunk/run-database/src/main/java/org/hps/run/database/EpicsDataDao.java
 =============================================================================
--- java/trunk/run-database/src/main/java/org/hps/run/database/EpicsDataDao.java	(original)
+++ java/trunk/run-database/src/main/java/org/hps/run/database/EpicsDataDao.java	Wed Sep 23 12:57:18 2015
@@ -9,7 +9,7 @@
  *
  * @author Jeremy McCormick, SLAC
  */
-public interface EpicsDataDao {
+interface EpicsDataDao {
 
     /**
      * Delete all EPICS data for a run from the database.

Modified: java/trunk/run-database/src/main/java/org/hps/run/database/EpicsVariableDao.java
 =============================================================================
--- java/trunk/run-database/src/main/java/org/hps/run/database/EpicsVariableDao.java	(original)
+++ java/trunk/run-database/src/main/java/org/hps/run/database/EpicsVariableDao.java	Wed Sep 23 12:57:18 2015
@@ -7,7 +7,7 @@
  * 
  * @author Jeremy McCormick, SLAC
  */
-public interface EpicsVariableDao {
+interface EpicsVariableDao {
 
     /**
      * Get the full list of EPICs variables.

Modified: java/trunk/run-database/src/main/java/org/hps/run/database/RunDatabaseDaoFactory.java
 =============================================================================
--- java/trunk/run-database/src/main/java/org/hps/run/database/RunDatabaseDaoFactory.java	(original)
+++ java/trunk/run-database/src/main/java/org/hps/run/database/RunDatabaseDaoFactory.java	Wed Sep 23 12:57:18 2015
@@ -12,7 +12,7 @@
  * @see EpicsDataDao
  * @see EpicsVariableDao
  */
-public final class RunDatabaseDaoFactory {
+final class RunDatabaseDaoFactory {
 
     /**
      * The database connection.
@@ -43,7 +43,7 @@
      *
      * @return the EPICS DAO
      */
-    public EpicsDataDao createEpicsDataDao() {
+    EpicsDataDao createEpicsDataDao() {
         return new EpicsDataDaoImpl(connection);
     }
 
@@ -52,7 +52,7 @@
      *
      * @return the EPICS variable DAO
      */
-    public EpicsVariableDao createEpicsVariableDao() {
+    EpicsVariableDao createEpicsVariableDao() {
         return new EpicsVariableDaoImpl(connection);
     }
 
@@ -61,7 +61,7 @@
      *
      * @return the run summary DAO
      */
-    public RunSummaryDao createRunSummaryDao() {
+    RunSummaryDao createRunSummaryDao() {
         return new RunSummaryDaoImpl(connection);
     }
 
@@ -70,7 +70,7 @@
      *
      * @return the scaler data DAO
      */
-    public ScalerDataDao createScalerDataDao() {
+    ScalerDataDao createScalerDataDao() {
         return new ScalerDataDaoImpl(connection);
     }
 
@@ -79,7 +79,7 @@
      *
      * @return the trigger config DAO
      */
-    public TriggerConfigDao createTriggerConfigDao() {
+    TriggerConfigDao createTriggerConfigDao() {
         return new TriggerConfigDaoImpl(connection);
     }
 }

Modified: java/trunk/run-database/src/main/java/org/hps/run/database/RunManager.java
 =============================================================================
--- java/trunk/run-database/src/main/java/org/hps/run/database/RunManager.java	(original)
+++ java/trunk/run-database/src/main/java/org/hps/run/database/RunManager.java	Wed Sep 23 12:57:18 2015
@@ -7,6 +7,9 @@
 import java.util.logging.Logger;
 
 import org.hps.conditions.database.ConnectionParameters;
+import org.hps.record.epics.EpicsData;
+import org.hps.record.scalers.ScalerData;
+import org.hps.record.triggerbank.TriggerConfig;
 import org.lcsim.conditions.ConditionsEvent;
 import org.lcsim.conditions.ConditionsListener;
 import org.lcsim.util.log.DefaultLogFormatter;
@@ -18,7 +21,7 @@
  * @author Jeremy McCormick, SLAC
  */
 public final class RunManager implements ConditionsListener {
-
+   
     /**
      * The default connection parameters for read-only access to the run database.
      */
@@ -60,12 +63,12 @@
     /**
      * The run number; the -1 value indicates that this has not been set externally yet.
      */
-    private int run = -1;
-
-    /**
-     * The {@link RunSummary} for the current run.
-     */
-    private RunSummary runSummary = null;
+    private Integer run = null;
+
+    /**
+     * Factory for creating database API objects.
+     */
+    private RunDatabaseDaoFactory factory;
     
     /**
      * Class constructor.
@@ -74,31 +77,36 @@
      */
     public RunManager(Connection connection) {
         this.connection = connection;
-    }
-    
-    /**
-     * Class constructor.
+        openConnection();
+        factory = new RunDatabaseDaoFactory(this.connection);
+    }
+    
+    /**
+     * Class constructor using default connection parameters.
      */
     public RunManager() {
+        this.connection = DEFAULT_CONNECTION_PARAMETERS.createConnection();
+        openConnection();
+        factory = new RunDatabaseDaoFactory(this.connection);
     }
 
     /**
      * Close the database connection.
      */
     public void closeConnection() {
-        if (!(this.connection == null)) {
-            try {
-                if (!this.connection.isClosed()) {
-                    this.connection.close();
-                }
-            } catch (final SQLException e) {
-                e.printStackTrace();
+        try {
+            if (!this.connection.isClosed()) {
+                this.connection.close();
             }
-        }
+        } catch (final SQLException e) {
+            e.printStackTrace();
+        }        
     }
 
     /**
      * Load new run information when conditions have changed.
+     * 
+     * @param conditionsEvent the event with new conditions information
      */
     @Override
     public synchronized void conditionsChanged(final ConditionsEvent conditionsEvent) {
@@ -119,18 +127,9 @@
      *
      * @return the complete list of run numbers
      */
-    List<Integer> getRuns() {
+    public List<Integer> getRuns() {
         openConnection();
         return new RunSummaryDaoImpl(this.connection).getRuns();
-    }
-
-    /**
-     * Get the current {@link RunSummary}.
-     *
-     * @return the current {@link RunSummary} or <code>null</code> if it is not set
-     */
-    public RunSummary getRunSummary() {
-        return this.runSummary;
     }
 
     /**
@@ -138,10 +137,10 @@
      * <p>
      * This method does nothing if the connection is already open.
      */
-    private void openConnection() {
+    public void openConnection() {
         try {
-            if (this.connection == null || this.connection.isClosed()) {
-                LOGGER.info("creating database connection");
+            if (this.connection.isClosed()) {
+                LOGGER.info("creating new database connection");
                 this.connection = connectionParameters.createConnection();
             } else {
                 LOGGER.warning("connection already open");
@@ -152,66 +151,116 @@
     }
 
     /**
-     * Set the connection externally if using a database other than the default one at JLAB.
-     *
-     * @param connection the database connection
-     */
-    public void setConnection(final Connection connection) {
-        this.connection = connection;
-    }
-
-    /**
      * Set the run number and then load the applicable {@link RunSummary} from the database.
      *
      * @param run the run number
      */
-    public synchronized void setRun(final int run) {
+    public void setRun(final int run) {
+        this.run = run;
+    }
+    
+    /**
+     * Get the run summary for the current run not including its sub-objects like scaler data.
+     * 
+     * @return the run summary for the current run
+     */
+    public RunSummary getRunSummary() {
+        checkRunNumber();
+        return factory.createRunSummaryDao().getRunSummary(this.run);
+    }
+    
+    /**
+     * Get the full run summary for the current run including scaler data, etc.
+     * 
+     * @return the full run summary for the current run
+     */
+    public RunSummary getFullRunSummary() {
+        checkRunNumber();
+        return factory.createRunSummaryDao().readFullRunSummary(this.run);
+    }
+    
+    /**
+     * Get the trigger config for the current run.
+     * 
+     * @return the trigger config for the current run
+     */
+    public TriggerConfig getTriggerConfig() {
+        checkRunNumber();
+        return factory.createTriggerConfigDao().getTriggerConfig(run);
+    }
+    
+    /**
+     * Get the EPICS data for the current run.
+     * 
+     * @param epicsType the type of EPICS data
+     * @return the EPICS data for the current run
+     */
+    public List<EpicsData> getEpicsData(EpicsType epicsType) {
+        checkRunNumber();
+        return factory.createEpicsDataDao().getEpicsData(epicsType, this.run);
+    }
+    
+    /**
+     * Get the scaler data for the current run.
+     * 
+     * @return the scaler data for the current run
+     */
+    public List<ScalerData> getScalerData() {
+        checkRunNumber();
+        return factory.createScalerDataDao().getScalerData(run);
+    }
+    
+    /**
+     * Update the database with information found from crawling the files.
+     *
+     * @param runs the list of runs to update
+     * @throws SQLException if there is a database query error
+     */
+    public void insertRun(final RunSummary runSummary) throws SQLException {
         // Don't do anything if the run number has already been set.
         if (run == this.run) {
             return;
         }
 
-        // Check if run number is valid.
-        if (run < 0) {
-            throw new IllegalArgumentException("invalid run number: " + run);
-        }
-
-        // Setup the database connection.
-        this.openConnection();
-
-        // Initialize database interface.
-        final RunSummaryDao runSummaryDao = new RunSummaryDaoImpl(this.connection);
-
-        // Set the current run number.
-        this.run = run;
-
-        // Does the current run exist in the database?
-        if (runSummaryDao.runSummaryExists(this.getRun())) {
-            LOGGER.info("run " + run + " found in database");
-            try {
-                // Read the records from the database and convert into complex Java object.
-                this.runSummary = runSummaryDao.readFullRunSummary(this.getRun());
-            } catch (final Exception e) {
-                // There was some unknown error when reading in the run records.
-                LOGGER.log(Level.SEVERE, "Error reading from run database.", e);
-                throw new RuntimeException(e);
-            }
-        } else {
-            // Run is not in the database.
-            LOGGER.warning("run database record does not exist for run " + run);
-        }
-
-        // Close the database connection.
-        this.closeConnection();
-    }
-    
-    /**
-     * Get the database connection.
-     * 
-     * @return the database connection
-     */
-    public RunDatabaseDaoFactory createDaoFactory() {
-        openConnection();
-        return new RunDatabaseDaoFactory(this.connection);
-    }
+        LOGGER.info("updating run database for run " + runSummary.getRun());
+
+        // Create object for updating run info in the database.
+        final RunSummaryDao runSummaryDao = factory.createRunSummaryDao();
+
+        // Insert run summary into database.
+        runSummaryDao.insertFullRunSummary(runSummary);
+
+        LOGGER.info("done updating run database");
+    }
+    
+    /**
+     * Delete a run from the database.
+     * 
+     * @param run the run number
+     */
+    public void deleteRun() {
+        // Create object for updating run info in the database.
+        final RunSummaryDao runSummaryDao = factory.createRunSummaryDao();
+
+        // Delete run from the database.
+        runSummaryDao.deleteFullRun(run);
+    }
+    
+    /**
+     * Return <code>true</code> if the run exists in the database.
+     * 
+     * @return <code>true</code> if the run exists in the database
+     */
+    public boolean runExists() {
+        return factory.createRunSummaryDao().runSummaryExists(this.run);
+    }
+        
+    /**
+     * Check if the run number has been set.
+     */
+    private void checkRunNumber() {
+        if (this.run == null) {
+            throw new IllegalStateException("The run number was not set.");
+        }
+    }          
 }

Modified: java/trunk/run-database/src/main/java/org/hps/run/database/RunSummaryDao.java
 =============================================================================
--- java/trunk/run-database/src/main/java/org/hps/run/database/RunSummaryDao.java	(original)
+++ java/trunk/run-database/src/main/java/org/hps/run/database/RunSummaryDao.java	Wed Sep 23 12:57:18 2015
@@ -7,14 +7,14 @@
  *
  * @author Jeremy McCormick, SLAC
  */
-public interface RunSummaryDao {
+interface RunSummaryDao {
 
     /**
      * Delete a run summary from the database including its referenced objects such as EPICS data.
      *
      * @param runSummary the run summary to delete
      */
-    void deleteFullRunSummary(RunSummary runSummary);
+    void deleteFullRun(int run);
 
     /**
      * Delete a run summary by run number.

Modified: java/trunk/run-database/src/main/java/org/hps/run/database/RunSummaryDaoImpl.java
 =============================================================================
--- java/trunk/run-database/src/main/java/org/hps/run/database/RunSummaryDaoImpl.java	(original)
+++ java/trunk/run-database/src/main/java/org/hps/run/database/RunSummaryDaoImpl.java	Wed Sep 23 12:57:18 2015
@@ -85,7 +85,7 @@
      *
      * @param connection the database connection
      */
-    public RunSummaryDaoImpl(final Connection connection) {
+    RunSummaryDaoImpl(final Connection connection) {
         // Set the connection.
         if (connection == null) {
             throw new IllegalArgumentException("The connection is null.");
@@ -99,14 +99,12 @@
     }
 
     /**
-     * Delete a run summary from the database including its referenced objects such as EPICS data.
+     * Delete a run from the database including its referenced objects such as EPICS data.
      *
      * @param runSummary the run summary to delete
      */
     @Override
-    public void deleteFullRunSummary(final RunSummary runSummary) {
-
-        final int run = runSummary.getRun();
+    public void deleteFullRun(int run) {
 
         // Delete EPICS log.
         this.epicsDataDao.deleteEpicsData(EpicsType.EPICS_1S, run);
@@ -320,7 +318,7 @@
                 if (deleteExisting) {
                     LOGGER.info("deleting existing run summary");
                     // Delete the existing rows.
-                    this.deleteFullRunSummary(runSummary);
+                    this.deleteFullRun(runSummary.getRun());
                 } else {
                     // Rows exist but updating is disallowed which is a fatal error.
                     throw new IllegalStateException("Run " + runSummary.getRun()

Modified: java/trunk/run-database/src/main/java/org/hps/run/database/ScalerDataDao.java
 =============================================================================
--- java/trunk/run-database/src/main/java/org/hps/run/database/ScalerDataDao.java	(original)
+++ java/trunk/run-database/src/main/java/org/hps/run/database/ScalerDataDao.java	Wed Sep 23 12:57:18 2015
@@ -9,7 +9,7 @@
  *
  * @author Jeremy McCormick, SLAC
  */
-public interface ScalerDataDao {
+interface ScalerDataDao {
 
     /**
      * Delete scaler data for the run.

Modified: java/trunk/run-database/src/main/java/org/hps/run/database/TriggerConfigDao.java
 =============================================================================
--- java/trunk/run-database/src/main/java/org/hps/run/database/TriggerConfigDao.java	(original)
+++ java/trunk/run-database/src/main/java/org/hps/run/database/TriggerConfigDao.java	Wed Sep 23 12:57:18 2015
@@ -7,7 +7,7 @@
  * 
  * @author Jeremy McCormick, SLAC
  */
-public interface TriggerConfigDao {
+interface TriggerConfigDao {
    
     /**
      * Get the trigger config by run.

Modified: java/trunk/run-database/src/test/java/org/hps/run/database/TiTriggerOffsetTest.java
 =============================================================================
--- java/trunk/run-database/src/test/java/org/hps/run/database/TiTriggerOffsetTest.java	(original)
+++ java/trunk/run-database/src/test/java/org/hps/run/database/TiTriggerOffsetTest.java	Wed Sep 23 12:57:18 2015
@@ -18,10 +18,10 @@
      */
     public void testAllRuns() {
         RunManager runManager = new RunManager();
-        List<Integer> runs = runManager.getRuns();
-        TriggerConfigDao triggerConfigDao = runManager.createDaoFactory().createTriggerConfigDao();
+        List<Integer> runs = runManager.getRuns();  
         for (Integer run : runs) {
-            TriggerConfig triggerConfig = triggerConfigDao.getTriggerConfig(run);
+            runManager.setRun(run);
+            TriggerConfig triggerConfig = runManager.getTriggerConfig();
             Long tiTimeOffset = triggerConfig.getTiTimeOffset();
             System.out.println("run " + run + " tiTriggerOffset = " + tiTimeOffset);
         }
@@ -33,8 +33,8 @@
     public void testSingleRun() {
         int run = 5772;
         RunManager runManager = new RunManager();
-        TriggerConfigDao triggerConfigDao = runManager.createDaoFactory().createTriggerConfigDao();
-        TriggerConfig triggerConfig = triggerConfigDao.getTriggerConfig(run);
+        runManager.setRun(run);
+        TriggerConfig triggerConfig = runManager.getTriggerConfig();
         Long tiTimeOffset = triggerConfig.getTiTimeOffset();
         System.out.println("run " + run + " tiTriggerOffset = " + tiTimeOffset);
     }