Search in sources :

Example 6 with Transaction

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

the class HawtDBAggregateNotLostTest method testHawtDBAggregateNotLost.

@Test
public void testHawtDBAggregateNotLost() throws Exception {
    getMockEndpoint("mock:aggregated").expectedBodiesReceived("ABCDE");
    getMockEndpoint("mock:result").expectedMessageCount(0);
    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:aggregated").getReceivedExchanges().get(0).getExchangeId();
    // the exchange should be in the completed repo where we should be able to find it
    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 not lost and we got all the information still
    assertNotNull(bf);
    Exchange completed = codec.unmarshallExchange(context, bf);
    assertNotNull(completed);
    // should retain the exchange id
    assertEquals(exchangeId, completed.getExchangeId());
    assertEquals("ABCDE", completed.getIn().getBody());
    assertEquals(123, completed.getIn().getHeader("id"));
    assertEquals("size", completed.getProperty(Exchange.AGGREGATED_COMPLETED_BY));
    assertEquals(5, completed.getProperty(Exchange.AGGREGATED_SIZE));
    // will store correlation keys as String
    assertEquals("123", completed.getProperty(Exchange.AGGREGATED_CORRELATION_KEY));
}
Also used : Buffer(org.fusesource.hawtbuf.Buffer) Exchange(org.apache.camel.Exchange) Transaction(org.fusesource.hawtdb.api.Transaction) Index(org.fusesource.hawtdb.api.Index) Test(org.junit.Test)

Example 7 with Transaction

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

the class HawtDBAggregationRepository method remove.

public void remove(final CamelContext camelContext, final String key, final Exchange exchange) {
    LOG.debug("Removing key [{}]", key);
    try {
        final Buffer keyBuffer = codec.marshallKey(key);
        final Buffer confirmKeyBuffer = codec.marshallKey(exchange.getExchangeId());
        final Buffer exchangeBuffer = codec.marshallExchange(camelContext, exchange, allowSerializedHeaders);
        hawtDBFile.execute(new Work<Buffer>() {

            public Buffer execute(Transaction tx) {
                SortedIndex<Buffer, Buffer> index = hawtDBFile.getRepositoryIndex(tx, repositoryName, true);
                // remove from the in progress index
                Buffer buffer = index.remove(keyBuffer);
                LOG.trace("Removed key index {} -> {}", keyBuffer, buffer);
                // and add it to the confirmed index
                SortedIndex<Buffer, Buffer> indexCompleted = hawtDBFile.getRepositoryIndex(tx, getRepositoryNameCompleted(), true);
                indexCompleted.put(confirmKeyBuffer, exchangeBuffer);
                LOG.trace("Added confirm index {}", confirmKeyBuffer);
                return null;
            }

            @Override
            public String toString() {
                return "Removing key [" + key + "]";
            }
        });
    } catch (IOException e) {
        throw new RuntimeException("Error removing key " + key + " 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 8 with Transaction

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

the class HawtDBAggregationRepository method get.

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

            public Buffer execute(Transaction tx) {
                SortedIndex<Buffer, Buffer> index = hawtDBFile.getRepositoryIndex(tx, repositoryName, false);
                if (index == null) {
                    return null;
                }
                Buffer buffer = index.get(keyBuffer);
                LOG.trace("Getting key index {}", keyBuffer);
                return buffer;
            }

            @Override
            public String toString() {
                return "Getting key [" + key + "]";
            }
        });
        if (rc != null) {
            answer = codec.unmarshallExchange(camelContext, rc);
        }
    } catch (IOException e) {
        throw new RuntimeException("Error getting key " + key + " from repository " + repositoryName, e);
    }
    LOG.debug("Getting key  [{}] -> {}", key, 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 9 with Transaction

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

the class HawtDBGrowIssueTest method testGrowIssue.

@Test
public void testGrowIssue() throws Exception {
    // a 1kb string for testing
    StringBuilder sb = new StringBuilder(size);
    for (int i = 0; i < 1024; i++) {
        sb.append("X");
    }
    // the key
    final Buffer key = codec.marshallKey("foo");
    // we update using the same key, which means we should be able to do this within the file size limit
    for (int i = 0; i < size; i++) {
        final Buffer data = codec.marshallKey(i + "-" + sb.toString());
        log.debug("Updating " + i);
        hawtDBFile.execute(new Work<Object>() {

            public Object execute(Transaction tx) {
                SortedIndex<Buffer, Buffer> index = hawtDBFile.getRepositoryIndex(tx, "repo", true);
                return index.put(key, data);
            }
        });
    }
    // get the last
    Buffer out = hawtDBFile.execute(new Work<Buffer>() {

        public Buffer execute(Transaction tx) {
            SortedIndex<Buffer, Buffer> index = hawtDBFile.getRepositoryIndex(tx, "repo", true);
            return index.get(key);
        }
    });
    String data = codec.unmarshallKey(out);
    log.info(data);
    assertTrue("Should be 1023", data.startsWith("1023"));
    assertEquals(1029, data.length());
}
Also used : Buffer(org.fusesource.hawtbuf.Buffer) Transaction(org.fusesource.hawtdb.api.Transaction) SortedIndex(org.fusesource.hawtdb.api.SortedIndex) 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