use of ca.nrc.cadc.caom2.Observation in project caom2db by opencadc.
the class ObservationDAO method getStateImpl.
private ObservationState getStateImpl(ObservationURI uri, UUID id) {
checkInit();
if (uri == null && id == null) {
throw new IllegalArgumentException("args cannot be null");
}
log.debug("GET: " + uri + " | " + id);
long t = System.currentTimeMillis();
try {
String sql = null;
if (uri != null) {
sql = gen.getSelectSQL(uri, 1, false);
} else {
sql = gen.getSelectSQL(id, 1, false);
}
log.debug("GET: " + sql);
JdbcTemplate jdbc = new JdbcTemplate(dataSource);
// ObservationSkeleton skel = (ObservationSkeleton) jdbc.query(sql, new ObservationSkeletonExtractor());
Observation obs = (Observation) jdbc.query(sql, gen.getObservationExtractor());
if (obs != null) {
ObservationState ret = new ObservationState(obs.getURI());
ret.id = obs.getID();
ret.accMetaChecksum = obs.getAccMetaChecksum();
ret.maxLastModified = obs.getMaxLastModified();
// ret.maxLastModified = skel.maxLastModified;
return ret;
}
return null;
} finally {
long dt = System.currentTimeMillis() - t;
log.debug("GET: " + uri + " | " + id + " " + dt + "ms");
}
}
use of ca.nrc.cadc.caom2.Observation in project caom2db by opencadc.
the class ObservationExtractor method extractObservations.
// obsolete: this extracts list of observations but ObservationDAO no longer supports that in a single query
private List<Observation> extractObservations(ResultSet rs) throws SQLException {
int ncol = rs.getMetaData().getColumnCount();
log.debug("extractData: ncol=" + ncol);
List<Observation> ret = new ArrayList<Observation>();
Observation curObs = null;
Plane curPlane = null;
Artifact curArtifact = null;
Part curPart = null;
Chunk curChunk = null;
int row = 0;
while (rs.next()) {
row++;
int col = 1;
UUID obsID = obsMapper.getID(rs, row, col);
if (curObs == null || !curObs.getID().equals(obsID)) {
log.debug("mapping Observation at column " + col);
if (curObs != null) {
log.debug("END observation: " + curObs.getID());
}
curObs = obsMapper.mapRow(rs, row, col);
ret.add(curObs);
log.debug("START observation: " + curObs.getID());
}
// else: obs content repeated due to join -- ignore it
col += obsMapper.getColumnCount();
if (ncol > col) {
log.debug("mapping Plane at column " + col);
UUID planeID = planeMapper.getID(rs, row, col);
if (planeID != null) {
if (curPlane == null || !curPlane.getID().equals(planeID)) {
if (curPlane != null) {
log.debug("END plane: " + curPlane.getID());
}
curPlane = planeMapper.mapRow(rs, row, col);
curObs.getPlanes().add(curPlane);
log.debug("START plane: " + curPlane.getID());
}
// else: plane content repeated due to join -- ignore it
} else {
log.debug("observation: " + curObs.getID() + ": no planes");
curPlane = null;
}
col += planeMapper.getColumnCount();
}
if (curPlane != null && ncol > col) {
log.debug("mapping Artifact at column " + col);
UUID artifactID = artifactMapper.getID(rs, row, col);
if (artifactID != null) {
if (curArtifact == null || !curArtifact.getID().equals(artifactID)) {
if (curArtifact != null) {
log.debug("END artifact: " + curArtifact.getID());
}
curArtifact = artifactMapper.mapRow(rs, row, col);
curPlane.getArtifacts().add(curArtifact);
log.debug("START artifact: " + curArtifact.getID());
}
// else: artifact content repeated due to join -- ignore it
} else {
log.debug("plane: " + curPlane.getID() + ": no artifacts");
curArtifact = null;
}
col += artifactMapper.getColumnCount();
}
if (curArtifact != null && ncol > col) {
log.debug("mapping Part at column " + col);
UUID partID = partMapper.getID(rs, row, col);
if (partID != null) {
if (curPart == null || !curPart.getID().equals(partID)) {
if (curPart != null) {
log.debug("END part: " + curPart.getID());
}
curPart = partMapper.mapRow(rs, row, col);
curArtifact.getParts().add(curPart);
log.debug("START part: " + curPart.getID());
}
// else: artifact content repeated due to join -- ignore it
} else {
log.debug("artifact: " + curArtifact.getID() + ": no parts");
curPart = null;
}
col += partMapper.getColumnCount();
}
if (curPart != null && ncol > col) {
log.debug("mapping Chunk at column " + col);
UUID chunkID = chunkMapper.getID(rs, row, col);
if (chunkID != null) {
if (curChunk == null || !curChunk.getID().equals(chunkID)) {
if (curChunk != null) {
log.debug("END part: " + curChunk.getID());
}
curChunk = chunkMapper.mapRow(rs, row, col);
curPart.getChunks().add(curChunk);
log.debug("START chunk: " + curChunk.getID());
}
// else: artifact content repeated due to join -- ignore it
} else {
log.debug("part: " + curPart.getID() + ": no chunks");
curChunk = null;
}
col += chunkMapper.getColumnCount();
}
}
return ret;
}
use of ca.nrc.cadc.caom2.Observation in project caom2db by opencadc.
the class AbstractObservationDAOTest method testPutGetDelete.
@Test
public void testPutGetDelete() {
try {
Observation orig = getTestObservation(false, 5, false, true);
// !EXISTS
Assert.assertNull(dao.getState(orig.getURI()));
log.info("not-exists");
ObservationResponse notFound = dao.getObservationResponse(orig.getURI());
Assert.assertNotNull("wrapper", notFound);
Assert.assertNotNull("wrapper", notFound.observationState);
Assert.assertNull("wrapped", notFound.observation);
// PUT
dao.put(orig);
// this is so we can detect incorrect timestamp round trips
// caused by assigning something other than what was stored
Thread.sleep(2 * TIME_TOLERANCE);
log.info("put + sleep");
// EXISTS
Assert.assertNotNull("exists-by-uri", dao.getState(orig.getURI()));
Assert.assertNotNull("exists-by-uuid", dao.getState(orig.getID()));
log.info("exists");
// GET by URI
ObservationState st = dao.getState(orig.getURI());
log.info("found: " + st);
Assert.assertNotNull("found ObservationState by URI", st);
ObservationResponse rs = dao.getObservationResponse(st);
log.info("found: " + rs);
Assert.assertNotNull("found ObservationResponse by URI", rs);
Observation retrieved = rs.observation;
log.info("found: " + retrieved);
Assert.assertNotNull("found Observation by URI", retrieved);
log.info("retrieved by URI");
testEqual(orig, retrieved);
log.info("equal");
// GET by ID
retrieved = dao.get(orig.getID());
Assert.assertNotNull("found by ID", retrieved);
log.info("retrieved by UUID");
testEqual(orig, retrieved);
log.info("equal");
// DELETE by ID
dao.delete(orig.getID());
// EXISTS
Assert.assertNull(dao.getState(orig.getURI()));
log.info("delete & not-exists");
log.info("check deletion track: " + orig.getID());
DeletedEntity de = ded.get(DeletedObservation.class, orig.getID());
Assert.assertNotNull("deletion tracker", de);
Assert.assertEquals("deleted.id", orig.getID(), de.getID());
Assert.assertNotNull("deleted.lastModified", de.lastModified);
DeletedObservation doe = (DeletedObservation) de;
Assert.assertEquals("deleted.uri", orig.getURI(), doe.getURI());
Assert.assertFalse("open transaction", txnManager.isOpen());
} catch (Exception unexpected) {
log.error("unexpected exception", unexpected);
Assert.fail("unexpected exception: " + unexpected);
}
}
use of ca.nrc.cadc.caom2.Observation in project caom2db by opencadc.
the class AbstractObservationDAOTest method testUpdateSimpleObservationAddRemovePlane.
@Test
public void testUpdateSimpleObservationAddRemovePlane() {
try {
int minDepth = 1;
int maxDepth = 5;
boolean full = true;
for (int i = minDepth; i <= maxDepth; i++) {
log.info("testUpdateSimpleObservationAddRemovePlane: full=" + full + ", depth=" + i);
Observation orig = getTestObservation(full, i, false, true);
int numPlanes = orig.getPlanes().size();
log.debug("put: orig");
// txnManager.startTransaction();
dao.put(orig);
// txnManager.commitTransaction();
log.debug("put: orig DONE");
// this is so we can detect incorrect timestamp round trips
// caused by assigning something other than what was stored
Thread.sleep(2 * TIME_TOLERANCE);
log.debug("get: orig");
Observation ret1 = dao.get(orig.getURI());
log.debug("get: orig DONE");
Assert.assertNotNull("found", ret1);
Assert.assertEquals(numPlanes, ret1.getPlanes().size());
testEqual(orig, ret1);
Plane newPlane = getTestPlane(full, "newPlane", i, false);
ret1.getPlanes().add(newPlane);
log.debug("put: added");
// txnManager.startTransaction();
dao.put(ret1);
// txnManager.commitTransaction();
log.debug("put: added DONE");
// this is so we can detect incorrect timestamp round trips
// caused by assigning something other than what was stored
Thread.sleep(2 * TIME_TOLERANCE);
log.debug("get: added");
Observation ret2 = dao.get(orig.getURI());
log.debug("get: added DONE");
Assert.assertNotNull("found", ret2);
Assert.assertEquals(numPlanes + 1, ret1.getPlanes().size());
testEqual(ret1, ret2);
ret2.getPlanes().remove(newPlane);
log.debug("put: removed");
// txnManager.startTransaction();
dao.put(ret2);
// txnManager.commitTransaction();
log.debug("put: removed DONE");
// this is so we can detect incorrect timestamp round trips
// caused by assigning something other than what was stored
Thread.sleep(2 * TIME_TOLERANCE);
log.debug("get: removed");
Observation ret3 = dao.get(orig.getURI());
log.debug("get: removed DONE");
Assert.assertNotNull("found", ret3);
Assert.assertEquals(numPlanes, ret3.getPlanes().size());
testEqual(orig, ret3);
// txnManager.startTransaction();
dao.delete(orig.getURI());
// txnManager.commitTransaction();
Observation deleted = dao.get(orig.getURI());
Assert.assertNull("deleted", deleted);
}
} catch (Exception unexpected) {
log.error("unexpected exception", unexpected);
if (txnManager.isOpen())
try {
txnManager.rollbackTransaction();
} catch (Throwable t) {
log.error("failed to rollback transaction", t);
}
Assert.fail("unexpected exception: " + unexpected);
}
}
use of ca.nrc.cadc.caom2.Observation in project caom2db by opencadc.
the class AbstractObservationDAOTest method testGetObservationList.
@Test
public void testGetObservationList() {
try {
log.info("testGetObservationList");
Integer batchSize = new Integer(3);
String collection = "FOO";
Observation o1 = new SimpleObservation(collection, "obs1");
Observation o2 = new SimpleObservation(collection, "obsA");
Observation o3 = new SimpleObservation(collection, "obs2");
Observation o4 = new SimpleObservation(collection, "obsB");
Observation o5 = new SimpleObservation(collection, "obs3");
// txnManager.startTransaction();
dao.put(o1);
Thread.sleep(10L);
dao.put(o2);
Thread.sleep(10L);
dao.put(o3);
Thread.sleep(10L);
dao.put(o4);
Thread.sleep(10L);
dao.put(o5);
// txnManager.commitTransaction();
List<ObservationResponse> obs;
// get first batch
obs = dao.getList(collection, null, null, batchSize);
Assert.assertNotNull(obs);
Assert.assertEquals(3, obs.size());
Assert.assertEquals(o1.getURI(), obs.get(0).observation.getURI());
Assert.assertEquals(o2.getURI(), obs.get(1).observation.getURI());
Assert.assertEquals(o3.getURI(), obs.get(2).observation.getURI());
// get next batch
obs = dao.getList(collection, o3.getMaxLastModified(), null, batchSize);
Assert.assertNotNull(obs);
// o3 gets picked up by the >=
Assert.assertEquals(3, obs.size());
Assert.assertEquals(o3.getURI(), obs.get(0).observation.getURI());
Assert.assertEquals(o4.getURI(), obs.get(1).observation.getURI());
Assert.assertEquals(o5.getURI(), obs.get(2).observation.getURI());
// txnManager.startTransaction();
dao.delete(o1.getURI());
dao.delete(o2.getURI());
dao.delete(o3.getURI());
dao.delete(o4.getURI());
dao.delete(o5.getURI());
// txnManager.commitTransaction();
} catch (Exception unexpected) {
log.error("unexpected exception", unexpected);
if (txnManager.isOpen())
try {
txnManager.rollbackTransaction();
} catch (Throwable t) {
log.error("failed to rollback transaction", t);
}
Assert.fail("unexpected exception: " + unexpected);
}
}
Aggregations