Search in sources :

Example 36 with Buffer

use of org.fusesource.hawtbuf.Buffer in project camel by apache.

the class LevelDBAggregationRepository method remove.

public void remove(final CamelContext camelContext, final String key, final Exchange exchange) {
    LOG.debug("Removing key [{}]", key);
    try {
        byte[] lDbKey = keyBuilder(repositoryName, key);
        final String exchangeId = exchange.getExchangeId();
        final Buffer exchangeBuffer = codec.marshallExchange(camelContext, exchange, allowSerializedHeaders);
        // remove the exchange
        byte[] rc = levelDBFile.getDb().get(lDbKey);
        if (rc != null) {
            WriteBatch batch = levelDBFile.getDb().createWriteBatch();
            try {
                batch.delete(lDbKey);
                LOG.trace("Removed key index {} -> {}", key, new Buffer(rc));
                // add exchange to confirmed index
                byte[] confirmedLDBKey = keyBuilder(getRepositoryNameCompleted(), exchangeId);
                batch.put(confirmedLDBKey, exchangeBuffer.toByteArray());
                LOG.trace("Added confirm index {} for repository {}", exchangeId, getRepositoryNameCompleted());
                levelDBFile.getDb().write(batch, levelDBFile.getWriteOptions());
            } finally {
                batch.close();
            }
        }
    } catch (IOException e) {
        throw new RuntimeException("Error removing key " + key + " from repository " + repositoryName, e);
    }
}
Also used : Buffer(org.fusesource.hawtbuf.Buffer) JniDBFactory.asString(org.fusesource.leveldbjni.JniDBFactory.asString) IOException(java.io.IOException) WriteBatch(org.iq80.leveldb.WriteBatch)

Example 37 with Buffer

use of org.fusesource.hawtbuf.Buffer 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 38 with Buffer

use of org.fusesource.hawtbuf.Buffer 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 39 with Buffer

use of org.fusesource.hawtbuf.Buffer 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)

Example 40 with Buffer

use of org.fusesource.hawtbuf.Buffer in project fabric8 by jboss-fuse.

the class ActiveMQTextMessage method beforeMarshall.

public void beforeMarshall(OpenWireFormat wireFormat) throws IOException {
    super.beforeMarshall(wireFormat);
    Buffer content = getContent();
    if (content == null && text != null) {
        ByteArrayOutputStream bytesOut = new ByteArrayOutputStream();
        OutputStream os = bytesOut;
        if (Settings.enable_compression()) {
            compressed = true;
            os = new DeflaterOutputStream(os);
        }
        DataOutputStream dataOut = new DataOutputStream(os);
        MarshallingSupport.writeUTF8(dataOut, this.text);
        dataOut.close();
        setContent(bytesOut.toBuffer());
    }
}
Also used : Buffer(org.fusesource.hawtbuf.Buffer) ByteArrayOutputStream(org.fusesource.hawtbuf.ByteArrayOutputStream) DeflaterOutputStream(java.util.zip.DeflaterOutputStream) DeflaterOutputStream(java.util.zip.DeflaterOutputStream) ByteArrayOutputStream(org.fusesource.hawtbuf.ByteArrayOutputStream)

Aggregations

Buffer (org.fusesource.hawtbuf.Buffer)45 IOException (java.io.IOException)17 ByteBuffer (java.nio.ByteBuffer)10 Exchange (org.apache.camel.Exchange)9 Test (org.junit.Test)9 Transaction (org.fusesource.hawtdb.api.Transaction)8 UTF8Buffer (org.fusesource.hawtbuf.UTF8Buffer)6 SortedIndex (org.fusesource.hawtdb.api.SortedIndex)6 InflaterInputStream (java.util.zip.InflaterInputStream)5 ByteArrayInputStream (org.fusesource.hawtbuf.ByteArrayInputStream)5 BufferState (io.fabric8.dosgi.io.ProtocolCodec.BufferState)4 OpenwireException (io.fabric8.gateway.handlers.detecting.protocol.openwire.support.OpenwireException)3 DataStructure (io.fabric8.gateway.handlers.detecting.protocol.openwire.command.DataStructure)2 RandomAccessFile (java.io.RandomAccessFile)2 DeflaterOutputStream (java.util.zip.DeflaterOutputStream)2 BufferEditor (org.fusesource.hawtbuf.BufferEditor)2 ByteArrayOutputStream (org.fusesource.hawtbuf.ByteArrayOutputStream)2 DataByteArrayOutputStream (org.fusesource.hawtbuf.DataByteArrayOutputStream)2 Index (org.fusesource.hawtdb.api.Index)2 ObjectSerializationStrategy (io.fabric8.dosgi.api.ObjectSerializationStrategy)1