use of ca.nrc.cadc.caom2.DeletedObservation in project caom2db by opencadc.
the class ObservationDAO method deleteImpl.
private void deleteImpl(UUID id, ObservationURI uri) {
if (readOnly) {
throw new UnsupportedOperationException("delete in readOnly mode");
}
checkInit();
// null check in public methods above
log.debug("DELETE: " + id);
long t = System.currentTimeMillis();
boolean txnOpen = false;
try {
JdbcTemplate jdbc = new JdbcTemplate(dataSource);
String sql = null;
if (id != null) {
sql = gen.getSelectSQL(id, SQLGenerator.MAX_DEPTH, true);
} else {
sql = gen.getSelectSQL(uri, SQLGenerator.MAX_DEPTH, true);
}
log.debug("DELETE: " + sql);
final ObservationSkeleton dirtyRead = (ObservationSkeleton) jdbc.query(sql, new ObservationSkeletonExtractor());
log.debug("starting transaction");
getTransactionManager().startTransaction();
txnOpen = true;
// obtain row lock on observation update
ObservationSkeleton skel = null;
if (dirtyRead != null) {
String lock = gen.getUpdateLockSQL(dirtyRead.id);
log.debug("LOCK SQL: " + lock);
jdbc.update(lock);
// req-acquire current state after obtaining lock
skel = (ObservationSkeleton) jdbc.query(sql, gen.getSkeletonExtractor(ObservationSkeleton.class));
}
if (skel != null) {
if (uri == null) {
// null state not possible
uri = getState(id).getURI();
}
if (id == null) {
id = skel.id;
}
DeletedObservation de = new DeletedObservation(id, uri);
deletedDAO.put(de, jdbc);
delete(skel, jdbc);
} else {
log.debug("DELETE: not found: " + id);
}
log.debug("committing transaction");
getTransactionManager().commitTransaction();
log.debug("commit: OK");
txnOpen = false;
} catch (DataAccessException e) {
log.debug("failed to delete " + id + ": ", e);
getTransactionManager().rollbackTransaction();
log.debug("rollback: OK");
txnOpen = false;
throw e;
} finally {
if (txnOpen) {
log.error("BUG - open transaction in finally");
getTransactionManager().rollbackTransaction();
log.error("rollback: OK");
}
long dt = System.currentTimeMillis() - t;
log.debug("DELETE: " + id + " " + dt + "ms");
}
}
use of ca.nrc.cadc.caom2.DeletedObservation in project caom2db by opencadc.
the class GetDeletedAction method writeDeleted.
private long writeDeleted(List<DeletedObservation> dels) throws IOException {
DateFormat df = DateUtil.getDateFormat(DateUtil.IVOA_DATE_FORMAT, DateUtil.UTC);
syncOutput.setHeader("Content-Type", "text/tab-separated-values");
OutputStream os = syncOutput.getOutputStream();
ByteCountOutputStream bc = new ByteCountOutputStream(os);
OutputStreamWriter out = new OutputStreamWriter(bc, "US-ASCII");
CsvWriter writer = new CsvWriter(out, '\t');
for (DeletedObservation ddo : dels) {
writer.write(ddo.getID().toString());
writer.write(ddo.getURI().getCollection());
writer.write(ddo.getURI().getObservationID());
if (ddo.lastModified != null) {
writer.write(df.format(ddo.lastModified));
} else {
writer.write("");
}
writer.endRecord();
}
writer.flush();
return bc.getByteCount();
}
use of ca.nrc.cadc.caom2.DeletedObservation in project caom2db by opencadc.
the class ObservationRemover method deleteDeletedObservations.
private Progress deleteDeletedObservations() {
Progress ret = new Progress();
List<DeletedObservation> obsList;
if (obsDAO != null) {
try {
obsList = deletedDAO.getList(target.getCollection(), null, null, batchSize);
ret.found = obsList.size();
for (DeletedObservation o : obsList) {
deletedDAO.delete(o);
log.info("removed: DeletedObservation: " + o);
ret.removed++;
}
} catch (Exception e) {
log.error("failed to list && delete observations", e);
ret.abort = true;
}
} else {
log.error("destination DAO is null: Quitting....");
ret.abort = true;
}
return ret;
}
use of ca.nrc.cadc.caom2.DeletedObservation in project caom2db by opencadc.
the class AbstractObservationDAOTest method testNonOriginPut.
@Test
public void testNonOriginPut() {
try {
dao.setOrigin(false);
Observation orig = getTestObservation(false, 5, false, true);
UUID externalID = orig.getID();
// !EXISTS
// txnManager.startTransaction();
Assert.assertNull(dao.getState(orig.getURI()));
// txnManager.commitTransaction();
// PUT
// txnManager.startTransaction();
dao.put(orig);
// txnManager.commitTransaction();
// this is so we can detect incorrect timestamp round trips
// caused by assigning something other than what was stored
Thread.sleep(2 * TIME_TOLERANCE);
// EXISTS
// txnManager.startTransaction();
ObservationState os1 = dao.getState(orig.getURI());
Assert.assertNotNull("exists", os1);
// txnManager.commitTransaction();
// GET by URI
ObservationState st = dao.getState(orig.getURI());
Assert.assertNotNull("found by URI", st);
ObservationResponse rs = dao.getObservationResponse(st);
Assert.assertNotNull("found by URI", rs);
Observation retrieved = rs.observation;
Assert.assertNotNull("found by URI", retrieved);
testEqual(orig, retrieved);
// GET by ID
retrieved = dao.get(orig.getID());
Assert.assertNotNull("found by ID", retrieved);
testEqual(orig, retrieved);
// non-origin: make sure UUID did not change
Assert.assertEquals("non-origin UUID", externalID, retrieved.getID());
// DELETE by ID
// txnManager.startTransaction();
dao.delete(orig.getID());
// txnManager.commitTransaction();
// EXISTS
// txnManager.startTransaction();
ObservationState os2 = dao.getState(orig.getURI());
Assert.assertNull("!exists", os2);
// txnManager.commitTransaction();
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);
} finally {
dao.setOrigin(true);
}
}
use of ca.nrc.cadc.caom2.DeletedObservation in project caom2db by opencadc.
the class DeletionHarvester method detectLoop.
/**
* detects loops
*
* @param entityList
* list of entities to detect loops with
*/
private void detectLoop(List<DeletedObservation> entityList) {
if (entityList.size() < 2) {
return;
}
DeletedEntity start = entityList.get(0);
DeletedEntity end = entityList.get(entityList.size() - 1);
if (start.getLastModified().equals(end.getLastModified())) {
throw new RuntimeException("detected infinite harvesting loop: " + entityClass.getSimpleName() + " at " + format(start.getLastModified()));
}
}
Aggregations