Search in sources :

Example 1 with Wire

use of net.openhft.chronicle.wire.Wire in project Chronicle-Queue by OpenHFT.

the class TestWriteWhenCurrentCycleGotEOF method createQueueWithOnlyHeaderFile.

private void createQueueWithOnlyHeaderFile(File dir) {
    SingleChronicleQueue queue = SingleChronicleQueueBuilder.binary(dir).testBlockSize().build();
    queue.storeForCycle(queue.cycle(), queue.epoch(), true);
    ExcerptTailer tailer = queue.acquireTailer();
    try (DocumentContext dc = tailer.readingDocument()) {
        assertFalse(dc.isPresent());
    }
    Wire wire;
    ExcerptAppender excerptAppender = queue.acquireAppender();
    try (DocumentContext dc = excerptAppender.writingDocument()) {
        wire = dc.wire();
    }
    // overwrite last record with EOF
    Bytes<?> bytes = wire.bytes();
    bytes.writeVolatileInt(bytes.writePosition() - 5, Wires.END_OF_DATA);
    bytes.writeVolatileInt(bytes.writePosition() - 1, 0);
}
Also used : ExcerptAppender(net.openhft.chronicle.queue.ExcerptAppender) DocumentContext(net.openhft.chronicle.wire.DocumentContext) Wire(net.openhft.chronicle.wire.Wire) ExcerptTailer(net.openhft.chronicle.queue.ExcerptTailer)

Example 2 with Wire

use of net.openhft.chronicle.wire.Wire in project Chronicle-Queue by OpenHFT.

the class FsFullReadTest method testFullReadFs.

@Ignore("broken test")
@Test
public void testFullReadFs() throws Exception {
    SingleChronicleQueue queue = SingleChronicleQueueBuilder.binary(basePath).blockSize(256 << 1000).rollCycle(RollCycles.DAILY).build();
    ExcerptTailer tailer = queue.createTailer();
    DocumentContext dc = tailer.readingDocument();
    boolean doExit = false;
    int entries = 0;
    while (!doExit) {
        try {
            if (dc.isPresent()) {
                entries++;
                Wire w = dc.wire();
                LocalDateTime dt = w.read().dateTime();
                assertNotNull(dt);
                byte[] b = w.read().bytes();
                assertEquals(1024, b.length);
            } else {
                System.out.println("Exiting");
                doExit = true;
            }
        } finally {
            dc.close();
        }
    }
    System.out.println(String.format("Read %d entries.", entries));
    CommonStore commonStore = queue.storeForCycle(queue.cycle(), 0, false);
    File file = commonStore.file();
    queue.close();
    int dumpEntries = 0;
    try {
        MappedBytes bytes = MappedBytes.mappedBytes(file, 4 << 20);
        bytes.readLimit(bytes.realCapacity());
        WireDumper dumper = WireDumper.of(bytes);
        Bytes<ByteBuffer> buffer = Bytes.elasticByteBuffer();
        while (bytes.readRemaining() >= 4) {
            StringBuilder sb = new StringBuilder();
            boolean last = dumper.dumpOne(sb, buffer);
            assertTrue(sb.length() > 0);
            if (last)
                break;
            dumpEntries++;
        }
    } catch (IOException ioe) {
        err.println("Failed to read " + file + " " + ioe);
    }
    assertEquals(dumpEntries, entries);
}
Also used : LocalDateTime(java.time.LocalDateTime) WireDumper(net.openhft.chronicle.wire.WireDumper) IOException(java.io.IOException) Wire(net.openhft.chronicle.wire.Wire) ByteBuffer(java.nio.ByteBuffer) CommonStore(net.openhft.chronicle.queue.impl.CommonStore) SingleChronicleQueue(net.openhft.chronicle.queue.impl.single.SingleChronicleQueue) DocumentContext(net.openhft.chronicle.wire.DocumentContext) File(java.io.File) MappedBytes(net.openhft.chronicle.bytes.MappedBytes) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 3 with Wire

use of net.openhft.chronicle.wire.Wire in project Chronicle-Queue by OpenHFT.

the class FsFullWriteTest method testAppenderFullFs.

@Ignore("flaky test")
@Test
public void testAppenderFullFs() throws Exception {
    ChronicleQueue queue = SingleChronicleQueueBuilder.binary(basePath).blockSize(256 << 1000).rollCycle(RollCycles.DAILY).build();
    ExcerptAppender appender = queue.acquireAppender();
    byte[] payload = new byte[1024];
    Random r = new Random();
    r.nextBytes(payload);
    final LocalDateTime now = LocalDateTime.now(Clock.systemUTC());
    for (int i = 0; i < 1024 * 200; i++) {
        DocumentContext dc = appender.writingDocument();
        try {
            Wire w = dc.wire();
            w.write().dateTime(now);
            w.write().bytes(payload);
        } finally {
            dc.close();
        }
    }
}
Also used : LocalDateTime(java.time.LocalDateTime) Random(java.util.Random) DocumentContext(net.openhft.chronicle.wire.DocumentContext) Wire(net.openhft.chronicle.wire.Wire) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 4 with Wire

use of net.openhft.chronicle.wire.Wire in project Chronicle-Queue by OpenHFT.

the class SingleChronicleQueueTest method testScanFromLastKnownIndex.

@Test
public void testScanFromLastKnownIndex() {
    final File file = createTempFile("testScanFromLastKnownIndex");
    try {
        final SingleChronicleQueue chronicle = (SingleChronicleQueue) createQueue(file);
        final ExcerptAppender appender = chronicle.acquireAppender();
        // create 100 documents
        for (int i = 0; i < 65; i++) {
            final int j = i;
            appender.writeDocument(wire -> wire.write("key").text("value=" + j));
        }
        // creates the indexes - index's 1 and 2 are created by the indexer
        new Indexer(chronicle).index();
        // create 100 documents
        for (long i = chronicle.lastIndex() + 1; i < 200; i++) {
            final long j = i;
            appender.writeDocument(wire -> wire.write("key").text("value=" + j));
        }
        final ExcerptTailer tailer = chronicle.createTailer();
        {
            int expected = 150;
            tailer.index(expected);
            StringBuilder sb = new StringBuilder();
            tailer.readDocument(wire -> wire.read("key").text(sb));
            Assert.assertEquals("value=" + expected, sb.toString());
        }
        // read back earlier
        {
            int expected = 167;
            tailer.index(expected);
            StringBuilder sb = new StringBuilder();
            tailer.readDocument(wire -> wire.read("key").text(sb));
            Assert.assertEquals("value=" + expected, sb.toString());
        }
    } finally {
        file.delete();
    }
}
Also used : BinaryWire(net.openhft.chronicle.wire.BinaryWire) ExcerptTailer(net.openhft.chronicle.queue.ExcerptTailer) Arrays(java.util.Arrays) ChronicleQueue(net.openhft.chronicle.queue.ChronicleQueue) Collection(java.util.Collection) RunWith(org.junit.runner.RunWith) Test(org.junit.Test) IOException(java.io.IOException) Wire(net.openhft.chronicle.wire.Wire) File(java.io.File) TextWire(net.openhft.chronicle.wire.TextWire) ExcerptAppender(net.openhft.chronicle.queue.ExcerptAppender) Assert(org.junit.Assert) Parameterized(org.junit.runners.Parameterized) ExcerptAppender(net.openhft.chronicle.queue.ExcerptAppender) File(java.io.File) ExcerptTailer(net.openhft.chronicle.queue.ExcerptTailer) Test(org.junit.Test)

Example 5 with Wire

use of net.openhft.chronicle.wire.Wire in project Chronicle-Queue by OpenHFT.

the class StagedPerformanceMain method applyFlyweight.

private static void applyFlyweight(IFacadeAll datum, long datumSize, DocumentContext dc) {
    Wire wire = dc.wire();
    String event = wire.readEvent(String.class);
    if (!event.equals("data"))
        throw new IllegalStateException("Expected ata not " + event);
    wire.consumePadding();
    Bytes<?> bytes = wire.bytes();
    datum.bytesStore(bytes, bytes.readPosition(), datumSize);
}
Also used : Wire(net.openhft.chronicle.wire.Wire)

Aggregations

Wire (net.openhft.chronicle.wire.Wire)33 DocumentContext (net.openhft.chronicle.wire.DocumentContext)22 Test (org.junit.Test)11 File (java.io.File)9 ExcerptAppender (net.openhft.chronicle.queue.ExcerptAppender)8 NotNull (org.jetbrains.annotations.NotNull)8 Bytes (net.openhft.chronicle.bytes.Bytes)7 MappedBytes (net.openhft.chronicle.bytes.MappedBytes)7 ExcerptTailer (net.openhft.chronicle.queue.ExcerptTailer)6 IOException (java.io.IOException)5 ChronicleQueue (net.openhft.chronicle.queue.ChronicleQueue)5 BytesStore (net.openhft.chronicle.bytes.BytesStore)3 SingleChronicleQueue (net.openhft.chronicle.queue.impl.single.SingleChronicleQueue)3 Assert (org.junit.Assert)3 Ignore (org.junit.Ignore)3 LocalDateTime (java.time.LocalDateTime)2 Arrays (java.util.Arrays)2 Collection (java.util.Collection)2 Jvm (net.openhft.chronicle.core.Jvm)2 OS (net.openhft.chronicle.core.OS)2