Search in sources :

Example 31 with Bytes

use of net.openhft.chronicle.bytes.Bytes in project Chronicle-Queue by OpenHFT.

the class SingleCQFormat2Test method expected.

private static void expected(@NotNull ExcerptTailer tailer, String expected) {
    try (DocumentContext dc = tailer.readingDocument()) {
        assertTrue(dc.isPresent());
        Bytes bytes2 = Bytes.allocateDirect(128);
        dc.wire().copyTo(new TextWire(bytes2));
        assertEquals(expected, bytes2.toString());
    }
}
Also used : MappedBytes(net.openhft.chronicle.bytes.MappedBytes) Bytes(net.openhft.chronicle.bytes.Bytes)

Example 32 with Bytes

use of net.openhft.chronicle.bytes.Bytes in project Chronicle-Queue by OpenHFT.

the class ChronicleQueueTwoThreads method doTest.

void doTest(boolean buffered) throws InterruptedException {
    File name = getTmpDir();
    AtomicLong counter = new AtomicLong();
    Thread tailerThread = new Thread(() -> {
        AffinityLock rlock = AffinityLock.acquireLock();
        Bytes bytes = NativeBytes.nativeBytes(BYTES_LENGTH).unchecked(true);
        try (ChronicleQueue rqueue = SingleChronicleQueueBuilder.fieldlessBinary(name).testBlockSize().build()) {
            ExcerptTailer tailer = rqueue.createTailer();
            while (!Thread.interrupted()) {
                bytes.clear();
                if (tailer.readBytes(bytes)) {
                    counter.incrementAndGet();
                }
            }
        } finally {
            if (rlock != null) {
                rlock.release();
            }
            System.out.printf("Read %,d messages", counter.intValue());
        }
    }, "tailer thread");
    long runs = 50_000;
    Thread appenderThread = new Thread(() -> {
        AffinityLock wlock = AffinityLock.acquireLock();
        try {
            ChronicleQueue wqueue = SingleChronicleQueueBuilder.fieldlessBinary(name).rollCycle(SMALL_DAILY).testBlockSize().buffered(buffered).build();
            ExcerptAppender appender = wqueue.acquireAppender();
            Bytes bytes = Bytes.allocateDirect(BYTES_LENGTH).unchecked(true);
            long next = System.nanoTime() + INTERVAL_US * 1000;
            for (int i = 0; i < runs; i++) {
                while (System.nanoTime() < next) /* busy wait*/
                ;
                long start = next;
                bytes.readPositionRemaining(0, BYTES_LENGTH);
                bytes.writeLong(0L, start);
                appender.writeBytes(bytes);
                next += INTERVAL_US * 1000;
            }
            wqueue.close();
        } finally {
            if (wlock != null) {
                wlock.release();
            }
        }
    }, "appender thread");
    tailerThread.start();
    Jvm.pause(100);
    appenderThread.start();
    appenderThread.join();
    // Pause to allow tailer to catch up (if needed)
    for (int i = 0; i < 10; i++) {
        if (runs != counter.get())
            Jvm.pause(Jvm.isDebug() ? 10000 : 100);
    }
    for (int i = 0; i < 10; i++) {
        tailerThread.interrupt();
        tailerThread.join(100);
    }
    assertEquals(runs, counter.get());
}
Also used : Bytes(net.openhft.chronicle.bytes.Bytes) NativeBytes(net.openhft.chronicle.bytes.NativeBytes) AtomicLong(java.util.concurrent.atomic.AtomicLong) File(java.io.File) AffinityLock(net.openhft.affinity.AffinityLock)

Example 33 with Bytes

use of net.openhft.chronicle.bytes.Bytes in project Chronicle-Queue by OpenHFT.

the class DirectChronicleQueueStringTest method readSome.

private void readSome(DirectChronicleQueue chronicle) {
    NativeBytesStore allocate = NativeBytesStore.nativeStoreWithFixedCapacity(EXPECTED_BYTES.length);
    final Bytes toRead = allocate.bytes();
    AtomicLong offset = new AtomicLong(chronicle.firstBytes());
    for (int i = 0; i < RUNS; i++) {
        toRead.clear();
        chronicle.readDocument(offset, toRead);
    }
}
Also used : Bytes(net.openhft.chronicle.bytes.Bytes) AtomicLong(java.util.concurrent.atomic.AtomicLong) NativeBytesStore(net.openhft.chronicle.bytes.NativeBytesStore)

Example 34 with Bytes

use of net.openhft.chronicle.bytes.Bytes in project kie-wb-common by kiegroup.

the class ClientIPCImpl method readThisDocument.

private DefaultKieCompilationResponseOffProcess readThisDocument(ExcerptTailer tailer) {
    if (logger.isDebugEnabled()) {
        logger.debug("current index on readThisDocument:{}", tailer.index());
    }
    DefaultKieCompilationResponseOffProcess res = null;
    try (DocumentContext dc = tailer.readingDocument()) {
        if (dc.isPresent()) {
            if (logger.isDebugEnabled()) {
                logger.debug("Document Context index:{}", dc.index());
            }
            Wire wire = dc.wire();
            Bytes bytes = wire.bytes();
            if (!bytes.isEmpty()) {
                try {
                    Object obj = deserialize(bytes.toByteArray());
                    res = (DefaultKieCompilationResponseOffProcess) obj;
                } catch (Exception e) {
                    logger.error(e.getMessage(), e);
                }
            }
        }
    }
    if (res == null) {
        res = new DefaultKieCompilationResponseOffProcess(false, "");
    }
    return res;
}
Also used : Bytes(net.openhft.chronicle.bytes.Bytes) DefaultKieCompilationResponseOffProcess(org.kie.workbench.common.services.backend.compiler.impl.DefaultKieCompilationResponseOffProcess) DocumentContext(net.openhft.chronicle.wire.DocumentContext) Wire(net.openhft.chronicle.wire.Wire) IOException(java.io.IOException)

Example 35 with Bytes

use of net.openhft.chronicle.bytes.Bytes in project Chronicle-Queue by OpenHFT.

the class MicroToucher method execute.

public boolean execute() {
    final Wire bufferWire = appender.wire();
    if (bufferWire == null)
        return false;
    final long lastPosition = appender.lastPosition;
    final long lastPage = lastPosition & ~0xFFF;
    final long nextPage = (lastPosition + 0xFFF) & ~0xFFF;
    Bytes bytes = bufferWire.bytes();
    if (nextPage != lastPageTouched) {
        lastPageTouched = nextPage;
        try {
            // best effort
            final BytesStore bs = bytes.bytesStore();
            if (bs.inside(nextPage, 8))
                touchPage(nextPage, bs);
        } catch (Throwable ignored) {
        }
        return true;
    }
    lastPageToSync = lastPage;
    return false;
}
Also used : Bytes(net.openhft.chronicle.bytes.Bytes) Wire(net.openhft.chronicle.wire.Wire) BytesStore(net.openhft.chronicle.bytes.BytesStore)

Aggregations

Bytes (net.openhft.chronicle.bytes.Bytes)53 Test (org.junit.Test)23 NotNull (org.jetbrains.annotations.NotNull)16 File (java.io.File)13 DocumentContext (net.openhft.chronicle.wire.DocumentContext)10 BytesRingBuffer (net.openhft.chronicle.queue.impl.ringbuffer.BytesRingBuffer)9 MappedBytes (net.openhft.chronicle.bytes.MappedBytes)8 Wire (net.openhft.chronicle.wire.Wire)7 ByteBuffer (java.nio.ByteBuffer)6 AtomicLong (java.util.concurrent.atomic.AtomicLong)6 ChronicleQueue (net.openhft.chronicle.queue.ChronicleQueue)6 ExcerptTailer (net.openhft.chronicle.queue.ExcerptTailer)6 InternalAppender (net.openhft.chronicle.queue.impl.single.InternalAppender)6 SingleChronicleQueue (net.openhft.chronicle.queue.impl.single.SingleChronicleQueue)6 ExcerptAppender (net.openhft.chronicle.queue.ExcerptAppender)5 NativeBytes (net.openhft.chronicle.bytes.NativeBytes)4 SingleChronicleQueueBuilder (net.openhft.chronicle.queue.impl.single.SingleChronicleQueueBuilder)4 ParseException (java.text.ParseException)3 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)3 AffinityLock (net.openhft.affinity.AffinityLock)3