Search in sources :

Example 1 with ChronicleQueueBuilder

use of net.openhft.chronicle.queue.ChronicleQueueBuilder in project Chronicle-Queue by OpenHFT.

the class QueueWireHandler method getQueue.

private ChronicleQueue getQueue(StringBuilder cspText) {
    ChronicleQueue queue;
    if (cid == 0) {
        // cid hasn't been passed in need to map it from csp
        cid = cspToCid.computeIfAbsent(cspText.toString(), s -> cidCounter.incrementAndGet());
        String[] parts = cspText.toString().split("/");
        String filename = "/tmp/" + parts[1] + "/" + parts[2] + ".q";
        queue = fileNameToChronicle.computeIfAbsent(filename, s -> {
            try {
                return new ChronicleQueueBuilder(filename).build();
            } catch (IOException e) {
                e.printStackTrace();
            }
            return null;
        });
        cidToQueue.put(cid, queue);
    } else {
        // if the cid has been created then there must be a corresponding queue
        queue = cidToQueue.get(cid);
        assert queue != null;
    }
    return queue;
}
Also used : ExcerptTailer(net.openhft.chronicle.queue.ExcerptTailer) Logger(org.slf4j.Logger) StreamCorruptedException(java.io.StreamCorruptedException) EventGroup(net.openhft.chronicle.network.event.EventGroup) ChronicleQueue(net.openhft.chronicle.queue.ChronicleQueue) ChronicleQueueBuilder(net.openhft.chronicle.queue.ChronicleQueueBuilder) LoggerFactory(org.slf4j.LoggerFactory) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) IOException(java.io.IOException) Consumer(java.util.function.Consumer) Bytes(net.openhft.chronicle.bytes.Bytes) EventId(net.openhft.chronicle.engine.client.internal.ClientWiredChronicleQueueStateless.EventId) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) net.openhft.chronicle.wire(net.openhft.chronicle.wire) Map(java.util.Map) ClientWiredStatelessTcpConnectionHub(net.openhft.chronicle.engine.client.ClientWiredStatelessTcpConnectionHub) ExcerptAppender(net.openhft.chronicle.queue.ExcerptAppender) ChronicleQueue(net.openhft.chronicle.queue.ChronicleQueue) IOException(java.io.IOException) ChronicleQueueBuilder(net.openhft.chronicle.queue.ChronicleQueueBuilder)

Example 2 with ChronicleQueueBuilder

use of net.openhft.chronicle.queue.ChronicleQueueBuilder in project Chronicle-Queue by OpenHFT.

the class ZipBytesRingBufferTest method testZipAndAppend.

@Test
public void testZipAndAppend() {
    File file = null;
    try {
        NativeBytesStore allocate = NativeBytesStore.nativeStoreWithFixedCapacity(1024);
        NativeBytesStore msgBytes = NativeBytesStore.nativeStoreWithFixedCapacity(150);
        net.openhft.chronicle.bytes.Bytes message = msgBytes.bytes();
        message.writeUTFΔ("Hello World");
        message.flip();
        file = File.createTempFile("chronicle", "q");
        DirectChronicleQueue chronicle = (DirectChronicleQueue) new ChronicleQueueBuilder(file.getName()).build();
        final long writeAddress = getHeader((SingleChronicleQueue) chronicle).getWriteByte();
        final BytesRingBuffer ring = new BytesRingBuffer(allocate.bytes());
        final ZippedDocumentAppender zippedDocumentAppender = new ZippedDocumentAppender(ring, chronicle);
        zippedDocumentAppender.append(message);
        long initialValue = chronicle.firstBytes();
        AtomicLong offset = new AtomicLong(initialValue);
        while (lastWrite((SingleChronicleQueue) chronicle) == writeAddress) {
        // wait for data to be written ( via another thread )
        }
        // read the data from chronicle into actual
        Bytes actual = NativeBytesStore.nativeStoreWithFixedCapacity(100).bytes();
        chronicle.readDocument(offset, actual);
        // "Hello World" zipped should be 12 chars
        Assert.assertEquals(12, actual.flip().remaining());
    } finally {
        if (file != null)
            file.delete();
    }
}
Also used : BytesRingBuffer(net.openhft.chronicle.queue.impl.ringbuffer.BytesRingBuffer) ChronicleQueueBuilder(net.openhft.chronicle.queue.ChronicleQueueBuilder) Bytes(net.openhft.chronicle.bytes.Bytes) Bytes(net.openhft.chronicle.bytes.Bytes) AtomicLong(java.util.concurrent.atomic.AtomicLong) NativeBytesStore(net.openhft.chronicle.bytes.NativeBytesStore) File(java.io.File) Test(org.junit.Test)

Aggregations

Bytes (net.openhft.chronicle.bytes.Bytes)2 ChronicleQueueBuilder (net.openhft.chronicle.queue.ChronicleQueueBuilder)2 File (java.io.File)1 IOException (java.io.IOException)1 StreamCorruptedException (java.io.StreamCorruptedException)1 Map (java.util.Map)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 AtomicLong (java.util.concurrent.atomic.AtomicLong)1 Consumer (java.util.function.Consumer)1 NativeBytesStore (net.openhft.chronicle.bytes.NativeBytesStore)1 ClientWiredStatelessTcpConnectionHub (net.openhft.chronicle.engine.client.ClientWiredStatelessTcpConnectionHub)1 EventId (net.openhft.chronicle.engine.client.internal.ClientWiredChronicleQueueStateless.EventId)1 EventGroup (net.openhft.chronicle.network.event.EventGroup)1 ChronicleQueue (net.openhft.chronicle.queue.ChronicleQueue)1 ExcerptAppender (net.openhft.chronicle.queue.ExcerptAppender)1 ExcerptTailer (net.openhft.chronicle.queue.ExcerptTailer)1 BytesRingBuffer (net.openhft.chronicle.queue.impl.ringbuffer.BytesRingBuffer)1 net.openhft.chronicle.wire (net.openhft.chronicle.wire)1 Test (org.junit.Test)1