Search in sources :

Example 11 with DeletedObservation

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");
    }
}
Also used : ObservationSkeleton(ca.nrc.cadc.caom2.persistence.skel.ObservationSkeleton) DeletedObservation(ca.nrc.cadc.caom2.DeletedObservation) JdbcTemplate(org.springframework.jdbc.core.JdbcTemplate) DataAccessException(org.springframework.dao.DataAccessException)

Example 12 with DeletedObservation

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();
}
Also used : CsvWriter(com.csvreader.CsvWriter) ByteCountOutputStream(ca.nrc.cadc.io.ByteCountOutputStream) DateFormat(java.text.DateFormat) OutputStream(java.io.OutputStream) ByteCountOutputStream(ca.nrc.cadc.io.ByteCountOutputStream) OutputStreamWriter(java.io.OutputStreamWriter) DeletedObservation(ca.nrc.cadc.caom2.DeletedObservation)

Example 13 with DeletedObservation

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;
}
Also used : DeletedObservation(ca.nrc.cadc.caom2.DeletedObservation) URISyntaxException(java.net.URISyntaxException) IOException(java.io.IOException)

Example 14 with DeletedObservation

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);
    }
}
Also used : ObservationResponse(ca.nrc.cadc.caom2.ObservationResponse) DerivedObservation(ca.nrc.cadc.caom2.DerivedObservation) Observation(ca.nrc.cadc.caom2.Observation) DeletedObservation(ca.nrc.cadc.caom2.DeletedObservation) SimpleObservation(ca.nrc.cadc.caom2.SimpleObservation) ObservationState(ca.nrc.cadc.caom2.ObservationState) DeletedObservation(ca.nrc.cadc.caom2.DeletedObservation) UUID(java.util.UUID) SQLException(java.sql.SQLException) PreconditionFailedException(ca.nrc.cadc.net.PreconditionFailedException) DeletedEntity(ca.nrc.cadc.caom2.DeletedEntity) Test(org.junit.Test)

Example 15 with DeletedObservation

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()));
    }
}
Also used : DeletedEntity(ca.nrc.cadc.caom2.DeletedEntity)

Aggregations

DeletedObservation (ca.nrc.cadc.caom2.DeletedObservation)13 DeletedEntity (ca.nrc.cadc.caom2.DeletedEntity)5 UUID (java.util.UUID)4 Test (org.junit.Test)4 ObservationState (ca.nrc.cadc.caom2.ObservationState)3 ObservationURI (ca.nrc.cadc.caom2.ObservationURI)3 ArrayList (java.util.ArrayList)3 Date (java.util.Date)3 DerivedObservation (ca.nrc.cadc.caom2.DerivedObservation)2 Observation (ca.nrc.cadc.caom2.Observation)2 ObservationResponse (ca.nrc.cadc.caom2.ObservationResponse)2 SimpleObservation (ca.nrc.cadc.caom2.SimpleObservation)2 HarvestState (ca.nrc.cadc.caom2.harvester.state.HarvestState)2 PreconditionFailedException (ca.nrc.cadc.net.PreconditionFailedException)2 IOException (java.io.IOException)2 URISyntaxException (java.net.URISyntaxException)2 SQLException (java.sql.SQLException)2 JdbcTemplate (org.springframework.jdbc.core.JdbcTemplate)2 HarvestStateDAO (ca.nrc.cadc.caom2.harvester.state.HarvestStateDAO)1 PostgresqlHarvestStateDAO (ca.nrc.cadc.caom2.harvester.state.PostgresqlHarvestStateDAO)1