Search in sources :

Example 51 with DocumentContext

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

the class TestBinarySearch method toWire.

@NotNull
private Wire toWire(int key) {
    final MyData myData = new MyData();
    myData.key = key;
    myData.value = Integer.toString(key);
    Wire result = WireType.BINARY.apply(Bytes.elasticByteBuffer());
    try (final DocumentContext dc = result.writingDocument()) {
        dc.wire().getValueOut().typedMarshallable(myData);
    }
    return result;
}
Also used : Wire(net.openhft.chronicle.wire.Wire) DocumentContext(net.openhft.chronicle.wire.DocumentContext) NotNull(org.jetbrains.annotations.NotNull)

Example 52 with DocumentContext

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

the class TestWriteWhenCurrentCycleGotEOF method shouldBeAbleToWriteIfCurrentCycleGotEOF.

@Test
public void shouldBeAbleToWriteIfCurrentCycleGotEOF() throws TimeoutException {
    File tmpDir = getTmpDir();
    createQueueWithOnlyHeaderFile(tmpDir);
    SingleChronicleQueue queue = SingleChronicleQueueBuilder.binary(tmpDir).testBlockSize().build();
    ExcerptAppender appender = queue.acquireAppender();
    try (DocumentContext dc = appender.writingDocument()) {
        dc.wire().write("test").text("Hello world");
    }
    try (DocumentContext dc = queue.acquireTailer().readingDocument()) {
        assertEquals("Hello world", dc.wire().read("test").text());
    }
}
Also used : ExcerptAppender(net.openhft.chronicle.queue.ExcerptAppender) DocumentContext(net.openhft.chronicle.wire.DocumentContext) File(java.io.File) Test(org.junit.Test)

Example 53 with DocumentContext

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

the class ToEndTest method missingCyclesToEndTest.

@Test
public void missingCyclesToEndTest() throws InterruptedException {
    String path = OS.TARGET + "/missingCyclesToEndTest-" + System.nanoTime();
    IOTools.shallowDeleteDirWithFiles(path);
    final SetTimeProvider timeProvider = new SetTimeProvider();
    long now = 1470757797000L;
    long timeIncMs = 1001;
    timeProvider.currentTimeMillis(now);
    final RollingChronicleQueue queue = SingleChronicleQueueBuilder.binary(path).testBlockSize().rollCycle(RollCycles.TEST_SECONDLY).timeProvider(timeProvider).build();
    final ExcerptAppender appender = queue.acquireAppender();
    appender.writeDocument(wire -> wire.write(() -> "msg").int32(1));
    // roll
    timeProvider.currentTimeMillis(now += timeIncMs);
    appender.writeDocument(wire -> wire.write(() -> "msg").int32(2));
    appender.writeDocument(wire -> wire.write(() -> "msg").int32(3));
    final ExcerptTailer tailer = queue.createTailer().toEnd();
    try (DocumentContext dc = tailer.readingDocument()) {
        if (dc.isPresent()) {
            fail("Should be at the end of the queue but dc.isPresent and we read: " + String.valueOf(dc.wire().read(() -> "msg").int32()));
        }
    }
    // append same cycle.
    appender.writeDocument(wire -> wire.write(() -> "msg").int32(4));
    try (DocumentContext dc = tailer.readingDocument()) {
        assertTrue("Should be able to read entry in this cycle. Got NoDocumentContext.", dc.isPresent());
        int i = dc.wire().read(() -> "msg").int32();
        assertEquals("Should've read 4, instead we read: " + i, 4, i);
    }
    // read from the beginning
    tailer.toStart();
    for (int j = 1; j <= 4; j++) {
        try (DocumentContext dc = tailer.readingDocument()) {
            assertTrue(dc.isPresent());
            int i = dc.wire().read(() -> "msg").int32();
            assertEquals(j, i);
        }
    }
    try (DocumentContext dc = tailer.readingDocument()) {
        if (dc.isPresent()) {
            fail("Should be at the end of the queue but dc.isPresent and we read: " + String.valueOf(dc.wire().read(() -> "msg").int32()));
        }
    }
    // write another
    appender.writeDocument(wire -> wire.write(() -> "msg").int32(5));
    // roll 5 cycles
    timeProvider.currentTimeMillis(now += timeIncMs * 5);
    try (DocumentContext dc = tailer.readingDocument()) {
        assertTrue(dc.isPresent());
        assertEquals(5, dc.wire().read(() -> "msg").int32());
    }
    try (DocumentContext dc = tailer.readingDocument()) {
        assertFalse(dc.isPresent());
    }
}
Also used : ExcerptAppender(net.openhft.chronicle.queue.ExcerptAppender) RollingChronicleQueue(net.openhft.chronicle.queue.impl.RollingChronicleQueue) DocumentContext(net.openhft.chronicle.wire.DocumentContext) SetTimeProvider(net.openhft.chronicle.core.time.SetTimeProvider) ExcerptTailer(net.openhft.chronicle.queue.ExcerptTailer) Test(org.junit.Test)

Example 54 with DocumentContext

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

the class RollEOFTest method createQueueAndWriteData.

private void createQueueAndWriteData(MutableTimeProvider timeProvider) {
    final SingleChronicleQueue queue = SingleChronicleQueueBuilder.binary(path).testBlockSize().rollCycle(RollCycles.TEST_DAILY).timeProvider(timeProvider).build();
    ExcerptAppender excerptAppender = queue.acquireAppender();
    try (DocumentContext dc = excerptAppender.writingDocument(false)) {
        dc.wire().write(() -> "test").int64(0);
    }
}
Also used : ExcerptAppender(net.openhft.chronicle.queue.ExcerptAppender) SingleChronicleQueue(net.openhft.chronicle.queue.impl.single.SingleChronicleQueue) DocumentContext(net.openhft.chronicle.wire.DocumentContext)

Aggregations

DocumentContext (net.openhft.chronicle.wire.DocumentContext)54 Test (org.junit.Test)41 ExcerptAppender (net.openhft.chronicle.queue.ExcerptAppender)28 File (java.io.File)22 ExcerptTailer (net.openhft.chronicle.queue.ExcerptTailer)22 MappedFile (net.openhft.chronicle.bytes.MappedFile)12 ChronicleQueue (net.openhft.chronicle.queue.ChronicleQueue)9 SingleChronicleQueue (net.openhft.chronicle.queue.impl.single.SingleChronicleQueue)8 Wire (net.openhft.chronicle.wire.Wire)8 NotNull (org.jetbrains.annotations.NotNull)6 Ignore (org.junit.Ignore)6 Future (java.util.concurrent.Future)4 AtomicLong (java.util.concurrent.atomic.AtomicLong)4 Bytes (net.openhft.chronicle.bytes.Bytes)4 SetTimeProvider (net.openhft.chronicle.core.time.SetTimeProvider)4 ValueOut (net.openhft.chronicle.wire.ValueOut)4 IOException (java.io.IOException)3 ArrayList (java.util.ArrayList)3 ExecutorService (java.util.concurrent.ExecutorService)3 RollingChronicleQueue (net.openhft.chronicle.queue.impl.RollingChronicleQueue)3