use of ca.nrc.cadc.caom2.Plane in project caom2db by opencadc.
the class ObservationDAO method updateEntity.
private void updateEntity(Plane entity, PlaneSkeleton s, Date now) {
if (origin && s == null) {
CaomUtil.assignID(entity, gen.generateID(entity.getID()));
}
if (origin && s != null) {
Util.assignLastModified(entity, s.lastModified, "lastModified");
Util.assignLastModified(entity, s.maxLastModified, "maxLastModified");
}
for (Artifact artifact : entity.getArtifacts()) {
ArtifactSkeleton skel = null;
if (s != null) {
for (ArtifactSkeleton ss : s.artifacts) {
if (artifact.getID().equals(ss.id)) {
skel = ss;
}
}
}
updateEntity(artifact, skel, now);
}
// new or changed
// just in case
digest.reset();
Util.assignMetaChecksum(entity, entity.computeMetaChecksum(digest), "metaChecksum");
Util.assignMetaChecksum(entity, entity.computeAccMetaChecksum(digest), "accMetaChecksum");
boolean delta = false;
if (s == null || s.metaChecksum == null) {
delta = true;
} else {
delta = !entity.getMetaChecksum().equals(s.metaChecksum);
}
if (delta && (origin || entity.getLastModified() == null)) {
Util.assignLastModified(entity, now, "lastModified");
}
boolean accDelta = false;
if (s == null || s.accMetaChecksum == null) {
accDelta = true;
} else {
accDelta = !entity.getAccMetaChecksum().equals(s.accMetaChecksum);
}
if (accDelta && (origin || entity.getMaxLastModified() == null)) {
Util.assignLastModified(entity, now, "maxLastModified");
}
}
use of ca.nrc.cadc.caom2.Plane 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.Plane in project caom2db by opencadc.
the class ObservationSkeletonExtractor method extractData.
@Override
public Object extractData(ResultSet rs) throws SQLException, DataAccessException {
ObservationSkeleton ret = null;
PlaneSkeleton curPlane = null;
ArtifactSkeleton curArtifact = null;
PartSkeleton curPart = null;
ChunkSkeleton curChunk = null;
int ncol = rs.getMetaData().getColumnCount();
while (rs.next()) {
if (ret == null) {
ret = new ObservationSkeleton();
}
Date d;
Date md;
UUID id;
URI cs;
URI acs;
int col = 1;
if (ret.id == null) {
d = Util.getDate(rs, col++, utcCalendar);
md = Util.getDate(rs, col++, utcCalendar);
cs = Util.getURI(rs, col++);
acs = Util.getURI(rs, col++);
id = Util.getUUID(rs, col++);
ret.id = id;
ret.lastModified = d;
ret.maxLastModified = md;
ret.metaChecksum = cs;
ret.accMetaChecksum = acs;
} else {
// skip
col += 5;
}
if (ncol > col) {
// plane
d = Util.getDate(rs, col++, utcCalendar);
md = Util.getDate(rs, col++, utcCalendar);
cs = Util.getURI(rs, col++);
acs = Util.getURI(rs, col++);
id = Util.getUUID(rs, col++);
if (id != null) {
if (curPlane == null || !curPlane.id.equals(id)) {
curPlane = new PlaneSkeleton();
curPlane.id = id;
curPlane.lastModified = d;
curPlane.maxLastModified = md;
curPlane.metaChecksum = cs;
curPlane.accMetaChecksum = acs;
log.debug("add: " + curPlane + " to " + ret);
ret.planes.add(curPlane);
}
if (ncol > col) {
// artifact
d = Util.getDate(rs, col++, utcCalendar);
md = Util.getDate(rs, col++, utcCalendar);
cs = Util.getURI(rs, col++);
acs = Util.getURI(rs, col++);
id = Util.getUUID(rs, col++);
if (id != null) {
if (curArtifact == null || !curArtifact.id.equals(id)) {
curArtifact = new ArtifactSkeleton();
curArtifact.id = id;
curArtifact.lastModified = d;
curArtifact.maxLastModified = md;
curArtifact.metaChecksum = cs;
curArtifact.accMetaChecksum = acs;
log.debug("add: " + curArtifact + " to " + curPlane);
curPlane.artifacts.add(curArtifact);
}
if (ncol > col) {
// part
d = Util.getDate(rs, col++, utcCalendar);
md = Util.getDate(rs, col++, utcCalendar);
cs = Util.getURI(rs, col++);
acs = Util.getURI(rs, col++);
id = Util.getUUID(rs, col++);
if (id != null) {
if (curPart == null || !curPart.id.equals(id)) {
curPart = new PartSkeleton();
curPart.id = id;
curPart.lastModified = d;
curPart.maxLastModified = md;
curPart.metaChecksum = cs;
curPart.accMetaChecksum = acs;
log.debug("add: " + curPart + " to " + curArtifact);
curArtifact.parts.add(curPart);
}
if (ncol > col) {
// chunk
d = Util.getDate(rs, col++, utcCalendar);
md = Util.getDate(rs, col++, utcCalendar);
cs = Util.getURI(rs, col++);
acs = Util.getURI(rs, col++);
id = Util.getUUID(rs, col++);
if (id != null) {
curChunk = new ChunkSkeleton();
curChunk.id = id;
curChunk.lastModified = d;
curChunk.maxLastModified = md;
curChunk.metaChecksum = cs;
curChunk.accMetaChecksum = acs;
log.debug("add: " + curChunk + " to " + curPart);
curPart.chunks.add(curChunk);
}
}
}
}
}
}
}
}
}
return ret;
}
use of ca.nrc.cadc.caom2.Plane in project caom2db by opencadc.
the class PlaneDAO method put.
@Override
public void put(Skeleton cur, Plane p, LinkedList<CaomEntity> parents, JdbcTemplate jdbc) {
if (p == null) {
throw new IllegalArgumentException("arg cannot be null");
}
log.debug("PUT: " + p.getID());
long t = System.currentTimeMillis();
try {
// delete obsolete children
List<Pair<Artifact>> pairs = new ArrayList<Pair<Artifact>>();
if (cur != null) {
PlaneSkeleton cs = (PlaneSkeleton) cur;
// delete the skeletons that are not in p.getArtifacts()
for (ArtifactSkeleton as : cs.artifacts) {
Artifact a = Util.findArtifact(p.getArtifacts(), as.id);
if (a == null) {
log.debug("put caused delete artifact: " + a);
artifactDAO.delete(as, jdbc);
}
}
// pair up planes and skeletons for insert/update
for (Artifact a : p.getArtifacts()) {
ArtifactSkeleton as = Util.findArtifactSkel(cs.artifacts, a.getID());
// null ok
pairs.add(new Pair<Artifact>(as, a));
}
} else {
for (Artifact a : p.getArtifacts()) {
pairs.add(new Pair<Artifact>(null, a));
}
}
super.put(cur, p, parents, jdbc);
parents.push(p);
for (Pair<Artifact> a : pairs) {
artifactDAO.put(a.cur, a.val, parents, jdbc);
}
parents.pop();
} finally {
long dt = System.currentTimeMillis() - t;
log.debug("PUT: " + p.getID() + " " + dt + "ms");
}
}
use of ca.nrc.cadc.caom2.Plane 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);
}
}
Aggregations