Search in sources :

Example 31 with Buffer

use of org.fusesource.hawtbuf.Buffer in project hawtjournal by fusesource.

the class DataFileAccessor method readLocation.

Buffer readLocation(Location location) throws IOException {
    WriteCommand asyncWrite = journal.getInflightWrites().get(location);
    Buffer result = null;
    if (asyncWrite != null) {
        result = asyncWrite.getData();
    } else {
        RandomAccessFile raf = getOrCreateRaf(Thread.currentThread(), location.getDataFileId());
        Lock threadLock = getOrCreateLock(Thread.currentThread(), location.getDataFileId());
        accessorLock.lock();
        threadLock.lock();
        try {
            if (location.getSize() == Location.NOT_SET) {
                raf.seek(location.getOffset());
                location.setSize(raf.readInt());
                location.setType(raf.readByte());
            } else {
                raf.seek(Journal.HEADER_SIZE + location.getOffset());
            }
            if (location.isBatchControlRecord()) {
                byte[] data = new byte[raf.readInt()];
                raf.readFully(data);
                result = new Buffer(data, 0, data.length);
            } else {
                byte[] data = new byte[location.getSize() - Journal.HEADER_SIZE];
                raf.readFully(data);
                result = new Buffer(data, 0, data.length);
            }
        } catch (RuntimeException e) {
            throw new IOException("Invalid location: " + location + ", : " + e);
        } finally {
            threadLock.unlock();
            accessorLock.unlock();
        }
    }
    if (!location.isDeletedRecord()) {
        return result;
    } else {
        throw new IOException("Deleted location: " + location);
    }
}
Also used : Buffer(org.fusesource.hawtbuf.Buffer) WriteCommand(org.fusesource.hawtjournal.api.Journal.WriteCommand) RandomAccessFile(java.io.RandomAccessFile) IOException(java.io.IOException) ReentrantLock(java.util.concurrent.locks.ReentrantLock) ReentrantReadWriteLock(java.util.concurrent.locks.ReentrantReadWriteLock) Lock(java.util.concurrent.locks.Lock) ReadWriteLock(java.util.concurrent.locks.ReadWriteLock)

Example 32 with Buffer

use of org.fusesource.hawtbuf.Buffer in project hawtjournal by fusesource.

the class Journal method compactDataFile.

private void compactDataFile(DataFile currentFile, Location firstUserLocation) throws IOException {
    DataFile tmpFile = new DataFile(new File(currentFile.getFile().getParent(), filePrefix + currentFile.getDataFileId() + ".tmp" + fileSuffix), currentFile.getDataFileId());
    RandomAccessFile raf = tmpFile.openRandomAccessFile();
    try {
        Location currentUserLocation = firstUserLocation;
        WriteBatch batch = new WriteBatch(tmpFile, 0);
        batch.prepareBatch();
        while (currentUserLocation != null) {
            Buffer data = accessor.readLocation(currentUserLocation);
            WriteCommand write = new WriteCommand(new Location(currentUserLocation), data, true);
            batch.appendBatch(write);
            currentUserLocation = goToNextLocation(currentUserLocation, Location.USER_RECORD_TYPE, false);
        }
        batch.perform(raf, null, true);
    } finally {
        if (raf != null) {
            raf.close();
        }
    }
    if (currentFile.getFile().delete()) {
        accessor.dispose(currentFile);
        totalLength.addAndGet(-currentFile.getLength());
        totalLength.addAndGet(tmpFile.getLength());
        if (tmpFile.getFile().renameTo(currentFile.getFile())) {
            currentFile.setLength(tmpFile.getLength());
        } else {
            throw new IOException("Cannot rename file: " + tmpFile.getFile());
        }
    } else {
        throw new IOException("Cannot remove file: " + currentFile.getFile());
    }
}
Also used : Buffer(org.fusesource.hawtbuf.Buffer) ByteBuffer(java.nio.ByteBuffer) RandomAccessFile(java.io.RandomAccessFile) IOException(java.io.IOException) RandomAccessFile(java.io.RandomAccessFile) File(java.io.File)

Example 33 with Buffer

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

the class LevelDBAggregateNotLostTest method testLevelDBAggregateNotLost.

@Test
public void testLevelDBAggregateNotLost() 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 LevelDBFile levelDBFile = repo.getLevelDBFile();
    final LevelDBCamelCodec codec = new LevelDBCamelCodec();
    byte[] bf = levelDBFile.getDb().get(keyBuilder("repo1-completed", exchangeId));
    // assert the exchange was not lost and we got all the information still
    assertNotNull(bf);
    Exchange completed = codec.unmarshallExchange(context, new Buffer(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 : Exchange(org.apache.camel.Exchange) Buffer(org.fusesource.hawtbuf.Buffer) Test(org.junit.Test)

Example 34 with Buffer

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

the class LevelDBAggregationRepository method confirm.

public void confirm(final CamelContext camelContext, final String exchangeId) {
    LOG.debug("Confirming exchangeId [{}]", exchangeId);
    byte[] confirmedLDBKey = keyBuilder(getRepositoryNameCompleted(), exchangeId);
    byte[] rc = levelDBFile.getDb().get(confirmedLDBKey);
    if (rc != null) {
        levelDBFile.getDb().delete(confirmedLDBKey);
        LOG.trace("Removed confirm index {} -> {}", exchangeId, new Buffer(rc));
    }
}
Also used : Buffer(org.fusesource.hawtbuf.Buffer)

Example 35 with Buffer

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

the class LevelDBAggregationRepository method recover.

public Exchange recover(CamelContext camelContext, final String exchangeId) {
    Exchange answer = null;
    try {
        byte[] completedLDBKey = keyBuilder(getRepositoryNameCompleted(), exchangeId);
        byte[] rc = levelDBFile.getDb().get(completedLDBKey);
        if (rc != null) {
            answer = codec.unmarshallExchange(camelContext, new Buffer(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) IOException(java.io.IOException)

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