Search in sources :

Example 1 with Transaction

use of org.fusesource.hawtdb.api.Transaction in project camel by apache.

the class HawtDBAggregationRepository method confirm.

public void confirm(final CamelContext camelContext, final String exchangeId) {
    LOG.debug("Confirming exchangeId [{}]", exchangeId);
    try {
        final Buffer confirmKeyBuffer = codec.marshallKey(exchangeId);
        hawtDBFile.execute(new Work<Buffer>() {

            public Buffer execute(Transaction tx) {
                SortedIndex<Buffer, Buffer> indexCompleted = hawtDBFile.getRepositoryIndex(tx, getRepositoryNameCompleted(), true);
                Buffer buffer = indexCompleted.remove(confirmKeyBuffer);
                LOG.trace("Removed confirm index {} -> {}", confirmKeyBuffer, buffer);
                return buffer;
            }

            @Override
            public String toString() {
                return "Confirming exchangeId [" + exchangeId + "]";
            }
        });
    } catch (IOException e) {
        throw new RuntimeException("Error confirming exchangeId " + exchangeId + " from repository " + repositoryName, e);
    }
}
Also used : Buffer(org.fusesource.hawtbuf.Buffer) Transaction(org.fusesource.hawtdb.api.Transaction) SortedIndex(org.fusesource.hawtdb.api.SortedIndex) IOException(java.io.IOException)

Example 2 with Transaction

use of org.fusesource.hawtdb.api.Transaction in project camel by apache.

the class HawtDBAggregationRepository method recover.

public Exchange recover(CamelContext camelContext, final String exchangeId) {
    Exchange answer = null;
    try {
        final Buffer confirmKeyBuffer = codec.marshallKey(exchangeId);
        Buffer rc = hawtDBFile.execute(new Work<Buffer>() {

            public Buffer execute(Transaction tx) {
                SortedIndex<Buffer, Buffer> indexCompleted = hawtDBFile.getRepositoryIndex(tx, getRepositoryNameCompleted(), false);
                if (indexCompleted == null) {
                    return null;
                }
                return indexCompleted.get(confirmKeyBuffer);
            }

            @Override
            public String toString() {
                return "Recovering exchangeId [" + exchangeId + "]";
            }
        });
        if (rc != null) {
            answer = codec.unmarshallExchange(camelContext, rc);
        }
    } catch (IOException e) {
        throw new RuntimeException("Error recovering exchangeId " + exchangeId + " from repository " + repositoryName, e);
    }
    LOG.debug("Recovering exchangeId [{}] -> {}", exchangeId, answer);
    return answer;
}
Also used : Exchange(org.apache.camel.Exchange) Buffer(org.fusesource.hawtbuf.Buffer) Transaction(org.fusesource.hawtdb.api.Transaction) SortedIndex(org.fusesource.hawtdb.api.SortedIndex) IOException(java.io.IOException)

Example 3 with Transaction

use of org.fusesource.hawtdb.api.Transaction in project camel by apache.

the class HawtDBAggregationRepository method doAdd.

protected Exchange doAdd(final CamelContext camelContext, final String key, final Exchange exchange, final boolean handleOptimisticLockingException) {
    LOG.debug("Adding key [{}] -> {}", key, exchange);
    try {
        // If we could guarantee that the key and exchange are immutable,
        // then we could have stuck them directly into the index, 
        // HawtDB could then eliminate the need to marshal and un-marshal  
        // in some cases.  But since we can't.. we are going to force
        // early marshaling.
        final Buffer keyBuffer = codec.marshallKey(key);
        final Buffer exchangeBuffer = codec.marshallExchange(camelContext, exchange, allowSerializedHeaders);
        Buffer rc = hawtDBFile.execute(new Work<Buffer>() {

            public Buffer execute(Transaction tx) {
                SortedIndex<Buffer, Buffer> index = hawtDBFile.getRepositoryIndex(tx, repositoryName, true);
                Buffer buffer = index.put(keyBuffer, exchangeBuffer);
                LOG.trace("Added key index {}", keyBuffer);
                return buffer;
            }

            @Override
            public String toString() {
                return "Adding key [" + key + "]";
            }
        }, handleOptimisticLockingException);
        if (rc == null) {
            return null;
        }
        // only return old exchange if enabled
        if (isReturnOldExchange()) {
            return codec.unmarshallExchange(camelContext, rc);
        }
    } catch (IOException e) {
        throw new RuntimeException("Error adding to repository " + repositoryName + " with key " + key, e);
    }
    return null;
}
Also used : Buffer(org.fusesource.hawtbuf.Buffer) Transaction(org.fusesource.hawtdb.api.Transaction) SortedIndex(org.fusesource.hawtdb.api.SortedIndex) IOException(java.io.IOException)

Example 4 with Transaction

use of org.fusesource.hawtdb.api.Transaction in project camel by apache.

the class HawtDBFile method execute.

public <T> T execute(Work<T> work, boolean rollbackOnOptimisticUpdateException) {
    LOG.trace("Executing work +++ start +++ {}", work);
    Transaction tx = pageFile.tx();
    T answer = doExecute(work, tx, pageFile, rollbackOnOptimisticUpdateException);
    LOG.trace("Executing work +++ done  +++ {}", work);
    return answer;
}
Also used : Transaction(org.fusesource.hawtdb.api.Transaction)

Example 5 with Transaction

use of org.fusesource.hawtdb.api.Transaction in project camel by apache.

the class HawtDBAggregateNotLostRemovedWhenConfirmedTest method testHawtDBAggregateNotLostRemovedWhenConfirmed.

@Test
public void testHawtDBAggregateNotLostRemovedWhenConfirmed() throws Exception {
    getMockEndpoint("mock:result").expectedBodiesReceived("ABCDE");
    template.sendBodyAndHeader("direct:start", "A", "id", 123);
    template.sendBodyAndHeader("direct:start", "B", "id", 123);
    template.sendBodyAndHeader("direct:start", "C", "id", 123);
    template.sendBodyAndHeader("direct:start", "D", "id", 123);
    template.sendBodyAndHeader("direct:start", "E", "id", 123);
    assertMockEndpointsSatisfied(30, TimeUnit.SECONDS);
    Thread.sleep(1000);
    String exchangeId = getMockEndpoint("mock:result").getReceivedExchanges().get(0).getExchangeId();
    // the exchange should NOT be in the completed repo as it was confirmed
    final HawtDBFile hawtDBFile = repo.getHawtDBFile();
    final HawtDBCamelCodec codec = new HawtDBCamelCodec();
    final Buffer confirmKeyBuffer = codec.marshallKey(exchangeId);
    Buffer bf = hawtDBFile.execute(new Work<Buffer>() {

        public Buffer execute(Transaction tx) {
            Index<Buffer, Buffer> index = hawtDBFile.getRepositoryIndex(tx, "repo1-completed", false);
            return index.get(confirmKeyBuffer);
        }
    });
    // assert the exchange was deleted
    assertNull(bf);
}
Also used : Buffer(org.fusesource.hawtbuf.Buffer) Transaction(org.fusesource.hawtdb.api.Transaction) Index(org.fusesource.hawtdb.api.Index) Test(org.junit.Test)

Aggregations

Transaction (org.fusesource.hawtdb.api.Transaction)9 Buffer (org.fusesource.hawtbuf.Buffer)8 SortedIndex (org.fusesource.hawtdb.api.SortedIndex)6 IOException (java.io.IOException)5 Exchange (org.apache.camel.Exchange)3 Test (org.junit.Test)3 Index (org.fusesource.hawtdb.api.Index)2