Search in sources :

Example 1 with ExcerptAppender

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

the class TestBinarySearch method testBinarySearch.

@Test
public void testBinarySearch() throws ParseException {
    final SetTimeProvider stp = new SetTimeProvider();
    long time = 0;
    stp.currentTimeMillis(time);
    final File tmpDir = getTmpDir();
    try (SingleChronicleQueue queue = SingleChronicleQueueBuilder.binary(getTmpDir()).rollCycle(RollCycles.TEST_SECONDLY).timeProvider(stp).build()) {
        final ExcerptAppender appender = queue.acquireAppender();
        for (int i = 0; i < MAX_NUMBER_OF_TESTED_MESSAGES; i++) {
            try (final DocumentContext dc = appender.writingDocument()) {
                final MyData myData = new MyData();
                myData.key = i;
                myData.value = "some value where the key=" + String.valueOf(i);
                dc.wire().getValueOut().typedMarshallable(myData);
                time += 300;
                stp.currentTimeMillis(time);
            }
        }
        for (int j = 0; j < MAX_NUMBER_OF_TESTED_MESSAGES; j++) {
            Wire key = toWire(j);
            final Comparator<Wire> comparator = (o1, o2) -> {
                final long readPositionO1 = o1.bytes().readPosition();
                final long readPositionO2 = o2.bytes().readPosition();
                try {
                    MyData myDataO1 = null;
                    MyData myDataO2 = null;
                    try (final DocumentContext dc = o1.readingDocument()) {
                        myDataO1 = dc.wire().getValueIn().typedMarshallable();
                        assert myDataO1.value != null;
                    }
                    try (final DocumentContext dc = o2.readingDocument()) {
                        myDataO2 = dc.wire().getValueIn().typedMarshallable();
                        assert myDataO2.value != null;
                    }
                    final int compare = Integer.compare(myDataO1.key, myDataO2.key);
                    return compare;
                } finally {
                    o1.bytes().readPosition(readPositionO1);
                    o2.bytes().readPosition(readPositionO2);
                }
            };
            long index = BinarySearch.search(queue, key, comparator);
            // assert index != -1 : "i=" + j;
            final ExcerptTailer tailer = queue.createTailer();
            tailer.moveToIndex(index);
            try (final DocumentContext documentContext = tailer.readingDocument()) {
                Assert.assertTrue(documentContext.toString().contains("some value where the key=" + j));
            }
            key.bytes().release();
        }
    } finally {
        System.gc();
        deleteDir(tmpDir);
    }
}
Also used : ExcerptTailer(net.openhft.chronicle.queue.ExcerptTailer) SetTimeProvider(net.openhft.chronicle.core.time.SetTimeProvider) DocumentContext(net.openhft.chronicle.wire.DocumentContext) AbstractMarshallable(net.openhft.chronicle.wire.AbstractMarshallable) Test(org.junit.Test) Wire(net.openhft.chronicle.wire.Wire) WireType(net.openhft.chronicle.wire.WireType) File(java.io.File) MappedFile(net.openhft.chronicle.bytes.MappedFile) Bytes(net.openhft.chronicle.bytes.Bytes) ChronicleQueueTestBase(net.openhft.chronicle.queue.ChronicleQueueTestBase) RollCycles(net.openhft.chronicle.queue.RollCycles) After(org.junit.After) ExcerptAppender(net.openhft.chronicle.queue.ExcerptAppender) NotNull(org.jetbrains.annotations.NotNull) Assert(org.junit.Assert) ParseException(java.text.ParseException) Comparator(java.util.Comparator) ExcerptAppender(net.openhft.chronicle.queue.ExcerptAppender) DocumentContext(net.openhft.chronicle.wire.DocumentContext) Wire(net.openhft.chronicle.wire.Wire) SetTimeProvider(net.openhft.chronicle.core.time.SetTimeProvider) File(java.io.File) MappedFile(net.openhft.chronicle.bytes.MappedFile) ExcerptTailer(net.openhft.chronicle.queue.ExcerptTailer) Test(org.junit.Test)

Example 2 with ExcerptAppender

use of net.openhft.chronicle.queue.ExcerptAppender 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 3 with ExcerptAppender

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

the class ToEndTest method toEndTest.

@Test
public void toEndTest() {
    File baseDir = DirectoryUtils.tempDir("toEndTest");
    List<Integer> results = new ArrayList<>();
    final SingleChronicleQueue singleChronicleQueue = null;
    try (RollingChronicleQueue queue = SingleChronicleQueueBuilder.binary(baseDir).testBlockSize().rollCycle(RollCycles.TEST_DAILY).indexCount(8).indexSpacing(1).build()) {
        checkOneFile(baseDir);
        ExcerptAppender appender = queue.acquireAppender();
        checkOneFile(baseDir);
        for (int i = 0; i < 10; i++) {
            final int j = i;
            appender.writeDocument(wire -> wire.write(() -> "msg").int32(j));
        }
        checkOneFile(baseDir);
        ExcerptTailer tailer = queue.createTailer();
        checkOneFile(baseDir);
        ExcerptTailer atEnd = tailer.toEnd();
        assertEquals(10, queue.rollCycle().toSequenceNumber(atEnd.index()));
        checkOneFile(baseDir);
        fillResults(atEnd, results);
        checkOneFile(baseDir);
        assertEquals(0, results.size());
        tailer.toStart();
        checkOneFile(baseDir);
        fillResults(tailer, results);
        assertEquals(10, results.size());
        checkOneFile(baseDir);
    }
    System.gc();
    try {
        IOTools.shallowDeleteDirWithFiles(baseDir);
    } catch (Exception e) {
        e.printStackTrace();
    }
}
Also used : ExcerptAppender(net.openhft.chronicle.queue.ExcerptAppender) ArrayList(java.util.ArrayList) RollingChronicleQueue(net.openhft.chronicle.queue.impl.RollingChronicleQueue) File(java.io.File) MappedFile(net.openhft.chronicle.bytes.MappedFile) ExcerptTailer(net.openhft.chronicle.queue.ExcerptTailer) Test(org.junit.Test)

Example 4 with ExcerptAppender

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

the class ToEndTest method toEndAfterWriteTest.

@Test
public void toEndAfterWriteTest() {
    File file = DirectoryUtils.tempDir("toEndAfterWriteTest");
    IOTools.shallowDeleteDirWithFiles(file);
    final SetTimeProvider stp = new SetTimeProvider();
    stp.currentTimeMillis(1470757797000L);
    try (RollingChronicleQueue wqueue = SingleChronicleQueueBuilder.binary(file).testBlockSize().rollCycle(RollCycles.TEST_SECONDLY).timeProvider(stp).build()) {
        ExcerptAppender appender = wqueue.acquireAppender();
        for (int i = 0; i < 10; i++) {
            try (DocumentContext dc = appender.writingDocument()) {
                dc.wire().write().text("hi-" + i);
                lastCycle = wqueue.rollCycle().toCycle(dc.index());
            }
            stp.currentTimeMillis(stp.currentTimeMillis() + 1000);
        }
    }
    try (ChronicleQueue rqueue = SingleChronicleQueueBuilder.binary(file).testBlockSize().rollCycle(RollCycles.TEST_SECONDLY).timeProvider(stp).build()) {
        ExcerptTailer tailer = rqueue.createTailer();
        stp.currentTimeMillis(stp.currentTimeMillis() + 1000);
        // noinspection StatementWithEmptyBody
        while (tailer.readText() != null) ;
        assertNull(tailer.readText());
        stp.currentTimeMillis(stp.currentTimeMillis() + 1000);
        ExcerptTailer tailer1 = rqueue.createTailer();
        ExcerptTailer excerptTailer = tailer1.toEnd();
        assertNull(excerptTailer.readText());
    }
    System.gc();
    IOTools.shallowDeleteDirWithFiles(file);
}
Also used : RollingChronicleQueue(net.openhft.chronicle.queue.impl.RollingChronicleQueue) ChronicleQueue(net.openhft.chronicle.queue.ChronicleQueue) ExcerptAppender(net.openhft.chronicle.queue.ExcerptAppender) RollingChronicleQueue(net.openhft.chronicle.queue.impl.RollingChronicleQueue) DocumentContext(net.openhft.chronicle.wire.DocumentContext) File(java.io.File) MappedFile(net.openhft.chronicle.bytes.MappedFile) SetTimeProvider(net.openhft.chronicle.core.time.SetTimeProvider) ExcerptTailer(net.openhft.chronicle.queue.ExcerptTailer) Test(org.junit.Test)

Example 5 with ExcerptAppender

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

the class WriteBytesTest method shouldWriteBytes.

@Test
public void shouldWriteBytes() throws IOException {
    try (final SingleChronicleQueue queue = SingleChronicleQueueBuilder.binary(tmpDir.newFolder()).testBlockSize().build()) {
        final ExcerptAppender appender = queue.acquireAppender();
        final HeapBytesStore<ByteBuffer> store = HeapBytesStore.uninitialized();
        store.init(ByteBuffer.wrap(PAYLOAD));
        appender.writeBytes(store);
        final ExcerptTailer tailer = queue.createTailer();
        final HeapBytesStore<byte[]> copy = HeapBytesStore.uninitialized();
        copy.init(new byte[4]);
        tailer.readBytes(copy.bytesForWrite());
        assertTrue(Arrays.equals(PAYLOAD, copy.underlyingObject()));
    }
}
Also used : ExcerptAppender(net.openhft.chronicle.queue.ExcerptAppender) ByteBuffer(java.nio.ByteBuffer) ExcerptTailer(net.openhft.chronicle.queue.ExcerptTailer) Test(org.junit.Test)

Aggregations

ExcerptAppender (net.openhft.chronicle.queue.ExcerptAppender)63 Test (org.junit.Test)55 ExcerptTailer (net.openhft.chronicle.queue.ExcerptTailer)34 File (java.io.File)33 DocumentContext (net.openhft.chronicle.wire.DocumentContext)28 ChronicleQueue (net.openhft.chronicle.queue.ChronicleQueue)21 MappedFile (net.openhft.chronicle.bytes.MappedFile)15 SingleChronicleQueue (net.openhft.chronicle.queue.impl.single.SingleChronicleQueue)6 SetTimeProvider (net.openhft.chronicle.core.time.SetTimeProvider)5 RollingChronicleQueue (net.openhft.chronicle.queue.impl.RollingChronicleQueue)5 Ignore (org.junit.Ignore)5 IOException (java.io.IOException)4 ArrayList (java.util.ArrayList)4 ValueOut (net.openhft.chronicle.wire.ValueOut)4 Wire (net.openhft.chronicle.wire.Wire)4 Arrays (java.util.Arrays)3 Collection (java.util.Collection)3 ExecutorService (java.util.concurrent.ExecutorService)3 Bytes (net.openhft.chronicle.bytes.Bytes)3 ChronicleQueueTestBase (net.openhft.chronicle.queue.ChronicleQueueTestBase)3