Search in sources :

Example 6 with BytesRingBuffer

use of net.openhft.chronicle.queue.impl.ringbuffer.BytesRingBuffer 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)

Example 7 with BytesRingBuffer

use of net.openhft.chronicle.queue.impl.ringbuffer.BytesRingBuffer in project Chronicle-Queue by OpenHFT.

the class BytesRingBufferTest method testFlowAroundSingleThreadedWriteDifferentSizeBuffers.

@Test
public void testFlowAroundSingleThreadedWriteDifferentSizeBuffers() {
    try (NativeBytesStore<Void> nativeStore = NativeBytesStore.nativeStoreWithFixedCapacity(150)) {
        System.out.println(nativeStore.realCapacity());
        System.out.println(nativeStore.capacity());
        System.out.println(nativeStore.limit());
        assert !nativeStore.isElastic();
        Bytes<Void> bytes = nativeStore.bytes();
        System.out.println(bytes);
        for (int j = 23 + 34; j < 100; j++) {
            final BytesRingBuffer bytesRingBuffer = new BytesRingBuffer(nativeStore.bytes());
            for (int i = 0; i < 50; i++) {
                bytesRingBuffer.offer(data());
                String actual = bytesRingBuffer.take(maxSize -> input.clear()).readUTFΔ();
                assertEquals(EXPECTED, actual);
            }
        }
    }
}
Also used : TimeUnit(java.util.concurrent.TimeUnit) NativeBytesStore(net.openhft.chronicle.bytes.NativeBytesStore) CountDownLatch(java.util.concurrent.CountDownLatch) Bytes(net.openhft.chronicle.bytes.Bytes) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) BytesRingBuffer(net.openhft.chronicle.queue.impl.ringbuffer.BytesRingBuffer) Assert.fail(org.junit.Assert.fail) org.junit(org.junit) ByteBuffer(java.nio.ByteBuffer) ExecutorService(java.util.concurrent.ExecutorService) Executors(java.util.concurrent.Executors) Assert.assertEquals(org.junit.Assert.assertEquals) BytesRingBuffer(net.openhft.chronicle.queue.impl.ringbuffer.BytesRingBuffer)

Example 8 with BytesRingBuffer

use of net.openhft.chronicle.queue.impl.ringbuffer.BytesRingBuffer in project Chronicle-Queue by OpenHFT.

the class BytesRingBufferTest method testWriteAndRead.

@Test
public void testWriteAndRead() {
    try (NativeBytesStore<Void> nativeStore = NativeBytesStore.nativeStoreWithFixedCapacity(150)) {
        assert nativeStore.isNative();
        final BytesRingBuffer bytesRingBuffer = new BytesRingBuffer(nativeStore.bytes());
        data();
        bytesRingBuffer.offer(data());
        Bytes actual = bytesRingBuffer.take(maxSize -> input.clear());
        assertEquals(EXPECTED, actual.readUTFΔ());
    }
}
Also used : Bytes(net.openhft.chronicle.bytes.Bytes) BytesRingBuffer(net.openhft.chronicle.queue.impl.ringbuffer.BytesRingBuffer)

Example 9 with BytesRingBuffer

use of net.openhft.chronicle.queue.impl.ringbuffer.BytesRingBuffer in project Chronicle-Queue by OpenHFT.

the class BytesRingBufferTest method testPollWithNoData.

@Test
public void testPollWithNoData() {
    try (NativeBytesStore<Void> nativeStore = NativeBytesStore.nativeStoreWithFixedCapacity(150)) {
        assert nativeStore.isNative();
        final BytesRingBuffer bytesRingBuffer = new BytesRingBuffer(nativeStore.bytes());
        Bytes actual = bytesRingBuffer.poll(maxSize -> input.clear());
        assertEquals(null, actual);
    }
}
Also used : Bytes(net.openhft.chronicle.bytes.Bytes) BytesRingBuffer(net.openhft.chronicle.queue.impl.ringbuffer.BytesRingBuffer)

Aggregations

Bytes (net.openhft.chronicle.bytes.Bytes)9 BytesRingBuffer (net.openhft.chronicle.queue.impl.ringbuffer.BytesRingBuffer)9 NativeBytesStore (net.openhft.chronicle.bytes.NativeBytesStore)4 ByteBuffer (java.nio.ByteBuffer)3 CountDownLatch (java.util.concurrent.CountDownLatch)3 ExecutorService (java.util.concurrent.ExecutorService)3 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)2 File (java.io.File)1 Executors (java.util.concurrent.Executors)1 TimeUnit (java.util.concurrent.TimeUnit)1 AtomicLong (java.util.concurrent.atomic.AtomicLong)1 ChronicleQueueBuilder (net.openhft.chronicle.queue.ChronicleQueueBuilder)1 org.junit (org.junit)1 Assert.assertEquals (org.junit.Assert.assertEquals)1 Assert.fail (org.junit.Assert.fail)1 Test (org.junit.Test)1