Author: [log in to unmask]
Date: Tue Feb 23 10:25:15 2016
New Revision: 4240
Log:
Minor changes to run database builders (dev branch).
Added:
java/branches/jeremy-dev/run-database/src/main/java/org/hps/run/database/EvioDataBuilder.java
Removed:
java/branches/jeremy-dev/run-database/src/main/java/org/hps/run/database/DatabaseUpdater.java
Modified:
java/branches/jeremy-dev/run-database/src/main/java/org/hps/run/database/RunDatabaseBuilder.java
java/branches/jeremy-dev/run-database/src/main/java/org/hps/run/database/RunManager.java
java/branches/jeremy-dev/run-database/src/test/java/org/hps/run/database/RunBuilderTest.java
Added: java/branches/jeremy-dev/run-database/src/main/java/org/hps/run/database/EvioDataBuilder.java
=============================================================================
--- java/branches/jeremy-dev/run-database/src/main/java/org/hps/run/database/EvioDataBuilder.java (added)
+++ java/branches/jeremy-dev/run-database/src/main/java/org/hps/run/database/EvioDataBuilder.java Tue Feb 23 10:25:15 2016
@@ -0,0 +1,83 @@
+package org.hps.run.database;
+
+import java.io.File;
+import java.util.List;
+import java.util.logging.Logger;
+
+import org.hps.record.epics.EpicsData;
+import org.hps.record.epics.EpicsRunProcessor;
+import org.hps.record.evio.EvioFileSource;
+import org.hps.record.evio.EvioFileUtilities;
+import org.hps.record.evio.EvioLoop;
+import org.hps.record.scalers.ScalerData;
+import org.hps.record.scalers.ScalersEvioProcessor;
+
+/**
+ * Extracts lists of EPICS and scaler data in an EVIO file and insert
+ * them into the run database.
+ *
+ * @author Jeremy McCormick, SLAC
+ */
+public class EvioDataBuilder extends AbstractRunBuilder {
+
+ private Logger LOGGER = Logger.getLogger(EvioDataBuilder.class.getPackage().getName());
+ private File evioFile;
+ private List<EpicsData> epicsData;
+ private List<ScalerData> scalerData;
+
+ void setEvioFile(File evioFile) {
+ this.evioFile = evioFile;
+ }
+
+ List<EpicsData> getEpicsData() {
+ return epicsData;
+ }
+
+ List<ScalerData> getScalerData() {
+ return scalerData;
+ }
+
+ @Override
+ void build() {
+ if (evioFile == null) {
+ throw new RuntimeException("The EVIO file was not set.");
+ }
+ EvioLoop loop = new EvioLoop();
+ EvioFileSource src = new EvioFileSource(evioFile);
+ loop.setEvioFileSource(src);
+ ScalersEvioProcessor scalersProcessor = new ScalersEvioProcessor();
+ scalersProcessor.setResetEveryEvent(false);
+ EpicsRunProcessor epicsProcessor = new EpicsRunProcessor();
+ loop.addProcessor(epicsProcessor);
+ loop.loop(-1);
+ this.epicsData = epicsProcessor.getEpicsData();
+ this.scalerData = scalersProcessor.getScalerData();
+ }
+
+ public void main(String args[]) {
+
+ if (args.length == 0) {
+ throw new RuntimeException("No command line arguments provided.");
+ }
+ String path = args[0];
+ File file = new File(path);
+ int run = EvioFileUtilities.getRunFromName(file);
+
+ EvioDataBuilder builder = new EvioDataBuilder();
+ builder.setEvioFile(file);
+ builder.build();
+
+ if (!builder.getEpicsData().isEmpty()) {
+ RunManager runManager = null;
+ try {
+ runManager = new RunManager();
+ runManager.setRun(run);
+ runManager.updateEpicsData(epicsData);
+ } finally {
+ runManager.closeConnection();
+ }
+ } else {
+ LOGGER.warning("No EPICS data was found to insert into run database.");
+ }
+ }
+}
Modified: java/branches/jeremy-dev/run-database/src/main/java/org/hps/run/database/RunDatabaseBuilder.java
=============================================================================
--- java/branches/jeremy-dev/run-database/src/main/java/org/hps/run/database/RunDatabaseBuilder.java (original)
+++ java/branches/jeremy-dev/run-database/src/main/java/org/hps/run/database/RunDatabaseBuilder.java Tue Feb 23 10:25:15 2016
@@ -6,7 +6,6 @@
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
-import java.util.Map;
import java.util.Map.Entry;
import java.util.logging.Level;
import java.util.logging.Logger;
@@ -30,7 +29,6 @@
import org.hps.record.scalers.ScalersEvioProcessor;
import org.hps.record.triggerbank.AbstractIntData.IntBankDefinition;
import org.hps.record.triggerbank.HeadBankData;
-import org.hps.record.triggerbank.TiTimeOffsetCalculator;
import org.hps.record.triggerbank.TiTimeOffsetEvioProcessor;
import org.hps.record.triggerbank.TriggerConfigData;
import org.hps.record.triggerbank.TriggerConfigData.Crate;
@@ -42,7 +40,6 @@
import org.srs.datacat.model.DatasetModel;
import org.srs.datacat.model.DatasetResultSetModel;
import org.srs.datacat.model.dataset.DatasetWithViewModel;
-import org.srs.datacat.shared.DatasetLocation;
/**
* Builds a complete {@link RunSummary} object from various data sources, including the data catalog and the run
Modified: java/branches/jeremy-dev/run-database/src/main/java/org/hps/run/database/RunManager.java
=============================================================================
--- java/branches/jeremy-dev/run-database/src/main/java/org/hps/run/database/RunManager.java (original)
+++ java/branches/jeremy-dev/run-database/src/main/java/org/hps/run/database/RunManager.java Tue Feb 23 10:25:15 2016
@@ -15,7 +15,7 @@
import org.lcsim.conditions.ConditionsListener;
/**
- * Manages read-only access to the run database and creates a {@link RunSummary} for a specific run.
+ * Manages access to the run database.
*
* @author Jeremy McCormick, SLAC
*/
@@ -38,8 +38,7 @@
private static final Logger LOGGER = Logger.getLogger(RunManager.class.getPackage().getName());
/**
- * Get the global instance of the {@link RunManager}.
- *
+ * Get the global instance of the {@link RunManager}.
* @return the global instance of the {@link RunManager}
*/
public static RunManager getRunManager() {
@@ -53,12 +52,7 @@
* The active database connection.
*/
private Connection connection;
-
- /**
- * The database connection parameters, initially set to the default parameters.
- */
- private final ConnectionParameters connectionParameters = DEFAULT_CONNECTION_PARAMETERS;
-
+
/**
* Factory for creating database API objects.
*/
@@ -70,34 +64,28 @@
private Integer run = null;
/**
+ * Class constructor.
+ * @param connection the database connection
+ */
+ public RunManager(final Connection connection) {
+ try {
+ if (connection.isClosed()) {
+ throw new RuntimeException("The connection is already closed and cannot be used.");
+ }
+ } catch (SQLException e) {
+ throw new RuntimeException(e);
+ }
+ this.connection = connection;
+ factory = new DaoProvider(this.connection);
+ }
+
+ /**
* Class constructor using default connection parameters.
*/
public RunManager() {
- this.connection = DEFAULT_CONNECTION_PARAMETERS.createConnection();
- this.openConnection();
- factory = new DaoProvider(this.connection);
- }
-
- /**
- * Class constructor.
- *
- * @param connection the database connection
- */
- public RunManager(final Connection connection) {
- this.connection = connection;
- this.openConnection();
- factory = new DaoProvider(this.connection);
- }
-
- /**
- * Check if the run number has been set.
- */
- private void checkRunNumber() {
- if (this.run == null) {
- throw new IllegalStateException("The run number was never set.");
- }
- }
-
+ this(DEFAULT_CONNECTION_PARAMETERS.createConnection());
+ }
+
/**
* Close the database connection.
*/
@@ -113,7 +101,6 @@
/**
* Load new run information when conditions have changed.
- *
* @param conditionsEvent the event with new conditions information
*/
@Override
@@ -122,8 +109,159 @@
}
/**
+ * Return the database connection.
+ * @return the database connection
+ */
+ Connection getConnection() {
+ return this.connection;
+ }
+
+ /**
+ * 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(final EpicsType epicsType) {
+ return factory.getEpicsDataDao().getEpicsData(epicsType, this.run);
+ }
+
+ /**
+ * Get the list of EPICS variables definitions.
+ * @param epicsType the type of EPICS data
+ * @return the list of EPICS variable definitions
+ */
+ public List<EpicsVariable> getEpicsVariables(final EpicsType epicsType) {
+ return factory.getEpicsVariableDao().getEpicsVariables(epicsType);
+ }
+
+ /**
+ * Get the full list of run numbers from the database.
+ * @return the complete list of run numbers
+ */
+ public List<Integer> getRuns() {
+ return factory.getRunSummaryDao().getRuns();
+ }
+
+ /**
+ * Get the run summary for the current run.
+ * @return the run summary for the current run
+ */
+ public RunSummary getRunSummary() {
+ return factory.getRunSummaryDao().getRunSummary(this.run);
+ }
+
+ /**
+ * Get the scaler data for the current run.
+ * @return the scaler data for the current run
+ */
+ public List<ScalerData> getScalerData() {
+ return factory.getScalerDataDao().getScalerData(this.run);
+ }
+
+ /**
+ * Get SVT configuration data.
+ * @return the SVT configuration data
+ */
+ public List<SvtConfigData> getSvtConfigData() {
+ return factory.getSvtConfigDao().getSvtConfigs(this.run);
+ }
+
+ /**
+ * Get the DAQ (trigger) configuration for the run.
+ * @return the DAQ configuration for the run
+ */
+ public DAQConfig getDAQConfig() {
+ TriggerConfigData config = factory.getTriggerConfigDao().getTriggerConfig(this.run);
+ return config.loadDAQConfig(this.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.getRunSummaryDao().runSummaryExists(this.run);
+ }
+
+ /**
+ * Set the run number and then load the applicable {@link RunSummary} from the database.
+ * @param run the run number
+ */
+ public void setRun(final int run) {
+ if (this.run == null || run != this.run) {
+ LOGGER.info("setting run " + run);
+ // Set the run number.
+ this.run = run;
+ }
+ }
+
+ /**
+ * Get the currently active run number or <code>null</code>.
+ * @return the currently active run number of <code>null</code>
+ */
+ public Integer getRun() {
+ return this.run;
+ }
+
+ /**
+ * Create or replace a run summary in the database.
+ * @param runSummary the run summary to update
+ * @param replaceExisting <code>true</code> to allow an existing run summary to be replaced
+ */
+ void updateRunSummary(RunSummary runSummary, boolean replaceExisting) {
+ final RunSummaryDao runSummaryDao = factory.getRunSummaryDao();
+ RunManager runManager = new RunManager();
+ runManager.setRun(runSummary.getRun());
+ if (runManager.runExists()) {
+ if (replaceExisting) {
+ runSummaryDao.updateRunSummary(runSummary);
+ } else {
+ throw new RuntimeException("Run already exists and replacement is not allowed.");
+ }
+ } else {
+ runSummaryDao.insertRunSummary(runSummary);
+ }
+ }
+
+ /**
+ * Create or replace the trigger config for the run.
+ * @param triggerConfig the trigger config
+ * @param replaceExisting <code>true</code> to allow an existing trigger to be replaced
+ */
+ void updateTriggerConfig(TriggerConfigData triggerConfig, boolean replaceExisting) {
+ final TriggerConfigDao configDao = factory.getTriggerConfigDao();
+ if (configDao.getTriggerConfig(run) != null) {
+ if (replaceExisting) {
+ configDao.deleteTriggerConfig(run);
+ } else {
+ throw new RuntimeException("Run already exists and replacement is not allowed.");
+ }
+ }
+ configDao.insertTriggerConfig(triggerConfig, run);
+ }
+
+ /**
+ * Create or replace EPICS data for the run.
+ * @param epicsData the EPICS data
+ */
+ void updateEpicsData(List<EpicsData> epicsData) {
+ if (epicsData != null && !epicsData.isEmpty()) {
+ factory.getEpicsDataDao().insertEpicsData(epicsData, this.run);
+ }
+ }
+
+ /**
+ * Create or replace scaler data for the run.
+ * @param scalerData the scaler data
+ */
+ void updateScalerData(List<ScalerData> scalerData) {
+ if (scalerData != null) {
+ factory.getScalerDataDao().insertScalerData(scalerData, this.run);
+ }
+ }
+
+ /**
* Delete a run from the database.
- *
* @param run the run number
*/
void deleteRun() {
@@ -134,162 +272,5 @@
factory.getTriggerConfigDao().deleteTriggerConfig(run);
factory.getRunSummaryDao().deleteRunSummary(run);
}
-
- /**
- * Return the database connection.
- *
- * @return the database connection
- */
- Connection getConnection() {
- return this.connection;
- }
-
- /**
- * 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(final EpicsType epicsType) {
- this.checkRunNumber();
- return factory.getEpicsDataDao().getEpicsData(epicsType, this.run);
- }
-
- /**
- * Get the EPICS variables.
- *
- * @param epicsType the type of EPICS data
- * @return the EPICS data for the current run
- */
- public List<EpicsVariable> getEpicsVariables(final EpicsType epicsType) {
- this.checkRunNumber();
- return factory.getEpicsVariableDao().getEpicsVariables(epicsType);
- }
-
- /**
- * Get the current run number.
- *
- * @return the run number
- */
- public int getCurrentRun() {
- return run;
- }
-
- /**
- * Get the complete list of run numbers from the database.
- *
- * @return the complete list of run numbers
- */
- public List<Integer> getRuns() {
- return factory.getRunSummaryDao().getRuns();
- }
-
- /**
- * 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() {
- this.checkRunNumber();
- return factory.getRunSummaryDao().getRunSummary(this.run);
- }
-
- /**
- * Get the scaler data for the current run.
- *
- * @return the scaler data for the current run
- */
- public List<ScalerData> getScalerData() {
- this.checkRunNumber();
- return factory.getScalerDataDao().getScalerData(run);
- }
-
- /**
- * Get SVT configuration data.
- *
- * @return the SVT configuration data
- */
- public List<SvtConfigData> getSvtConfigData() {
- this.checkRunNumber();
- return factory.getSvtConfigDao().getSvtConfigs(run);
- }
-
- /**
- * Get the DAQ configuration for the run.
- *
- * @return the DAQ configuration for the run
- */
- public DAQConfig getDAQConfig() {
- this.checkRunNumber();
- TriggerConfigData config = factory.getTriggerConfigDao().getTriggerConfig(run);
- return config.loadDAQConfig(run);
- }
-
- /**
- * Open a new database connection from the connection parameters if the current one is closed or <code>null</code>.
- * <p>
- * This method does nothing if the connection is already open.
- */
- public void openConnection() {
- try {
- if (this.connection.isClosed()) {
- LOGGER.info("creating new database connection");
- this.connection = connectionParameters.createConnection();
- }
- } catch (final SQLException e) {
- throw new RuntimeException("Error opening database connection.", e);
- }
- }
-
- /**
- * 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() {
- if (factory == null) {
- throw new RuntimeException("factory is null");
- }
- if (factory.getRunSummaryDao() == null) {
- throw new RuntimeException("RunSummaryDao is null");
- }
- if (this.run == null) {
- throw new RuntimeException("run is null");
- }
- return factory.getRunSummaryDao().runSummaryExists(this.run);
- }
-
- /**
- * Return <code>true</code> if the run exists in the database.
- *
- * @param run the run number
- * @return <code>true</code> if the run exists in the database
- */
- boolean runExists(final int run) {
- return factory.getRunSummaryDao().runSummaryExists(run);
- }
-
- /**
- * Set the run number and then load the applicable {@link RunSummary} from the database.
- *
- * @param run the run number
- */
- public void setRun(final int run) {
-
- if (this.run == null || run != this.run) {
-
- LOGGER.info("setting new run " + run);
-
- // Set the run number.
- this.run = run;
- }
- }
-
- /**
- * Get the currently active run number or <code>null</code>.
- * @return the currently active run number of <code>null</code>
- */
- public Integer getRun() {
- return this.run;
- }
+
}
Modified: java/branches/jeremy-dev/run-database/src/test/java/org/hps/run/database/RunBuilderTest.java
=============================================================================
--- java/branches/jeremy-dev/run-database/src/test/java/org/hps/run/database/RunBuilderTest.java (original)
+++ java/branches/jeremy-dev/run-database/src/test/java/org/hps/run/database/RunBuilderTest.java Tue Feb 23 10:25:15 2016
@@ -1,7 +1,6 @@
package org.hps.run.database;
import java.io.File;
-import java.sql.Connection;
import java.util.List;
import junit.framework.TestCase;
@@ -16,11 +15,12 @@
private static String SPREADSHEET = "/work/hps/rundb/HPS_Runs_2015_Sheet1.csv";
private static String FOLDER = "/HPS/test";
private static String SITE = "SLAC";
+ private static String EVIO_TEST_FILE = "/nfs/slac/g/hps3/data/engrun2015/evio/hps_005403.evio.0";
private static final ConnectionParameters CONNECTION_PARAMETERS =
new ConnectionParameters("root", "derp", "hps_run_db", "localhost");
- public void testRunBuilder() throws Exception {
+ public void testRunBuilders() throws Exception {
RunSummaryImpl runSummary = new RunSummaryImpl(RUN);
@@ -31,7 +31,6 @@
datacatBuilder.setSite(SITE);
datacatBuilder.setRunSummary(runSummary);
datacatBuilder.build();
-
List<File> files = datacatBuilder.getFileList();
// livetime measurements
@@ -50,14 +49,17 @@
spreadsheetBuilder.setSpreadsheetFile(new File(SPREADSHEET));
spreadsheetBuilder.setRunSummary(datacatBuilder.getRunSummary());
spreadsheetBuilder.build();
+
+ // EPICS and scalers (would actually run this in a separate job/process per file)
+ EvioDataBuilder dataBuilder = new EvioDataBuilder();
+ dataBuilder.setEvioFile(new File(EVIO_TEST_FILE));
+ dataBuilder.build();
- // database updater
- Connection connection = CONNECTION_PARAMETERS.createConnection();
- DatabaseUpdater updater = new DatabaseUpdater(connection);
- updater.setRunSummary(runSummary);
- System.out.println("built run summary ...");
- System.out.println(runSummary);
- //updater.setTriggerConfigData(configBuilder.getTriggerConfigData());
- //updater.update();
+ // update in database
+ RunManager runManager = new RunManager(CONNECTION_PARAMETERS.createConnection());
+ runManager.updateRunSummary(runSummary, true);
+ runManager.updateTriggerConfig(configBuilder.getTriggerConfigData(), true);
+ runManager.updateEpicsData(dataBuilder.getEpicsData());
+ runManager.updateScalerData(dataBuilder.getScalerData());
}
}
|