Search in sources :

Example 26 with Bytes

use of net.openhft.chronicle.bytes.Bytes 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);
    try (SingleChronicleQueue queue = ChronicleQueue.singleBuilder(getTmpDir()).rollCycle(RollCycles.TEST_SECONDLY).timeProvider(stp).build()) {
        final ExcerptAppender appender = queue.acquireAppender();
        for (int i = 0; i < numberOfMessages; i++) {
            try (final DocumentContext dc = appender.writingDocument()) {
                final MyData myData = new MyData();
                myData.key = i;
                myData.value = "some value where the key=" + i;
                dc.wire().getValueOut().typedMarshallable(myData);
                time += 300;
                stp.currentTimeMillis(time);
            }
        }
        final Comparator<Wire> comparator = (o1, o2) -> {
            final long readPositionO1 = o1.bytes().readPosition();
            final long readPositionO2 = o2.bytes().readPosition();
            try {
                final MyData myDataO1;
                try (final DocumentContext dc = o1.readingDocument()) {
                    myDataO1 = dc.wire().getValueIn().typedMarshallable();
                }
                final MyData myDataO2;
                try (final DocumentContext dc = o2.readingDocument()) {
                    myDataO2 = dc.wire().getValueIn().typedMarshallable();
                }
                final int compare = Integer.compare(myDataO1.key, myDataO2.key);
                return compare;
            } finally {
                o1.bytes().readPosition(readPositionO1);
                o2.bytes().readPosition(readPositionO2);
            }
        };
        try (final ExcerptTailer tailer = queue.createTailer()) {
            for (int j = 0; j < numberOfMessages; j++) {
                try (DocumentContext ignored = tailer.readingDocument()) {
                    Wire key = toWire(j);
                    long index = BinarySearch.search(queue, key, comparator);
                    Assert.assertEquals(tailer.index(), index);
                    key.bytes().releaseLast();
                }
            }
        }
        Wire key = toWire(numberOfMessages);
        Assert.assertTrue("Should not find non-existent", BinarySearch.search(queue, key, comparator) < 0);
    }
}
Also used : Arrays(java.util.Arrays) SetTimeProvider(net.openhft.chronicle.core.time.SetTimeProvider) DocumentContext(net.openhft.chronicle.wire.DocumentContext) SelfDescribingMarshallable(net.openhft.chronicle.wire.SelfDescribingMarshallable) Collection(java.util.Collection) RunWith(org.junit.runner.RunWith) Test(org.junit.Test) Wire(net.openhft.chronicle.wire.Wire) WireType(net.openhft.chronicle.wire.WireType) Bytes(net.openhft.chronicle.bytes.Bytes) net.openhft.chronicle.queue(net.openhft.chronicle.queue) NotNull(org.jetbrains.annotations.NotNull) Assert(org.junit.Assert) ParseException(java.text.ParseException) Comparator(java.util.Comparator) Parameterized(org.junit.runners.Parameterized) DocumentContext(net.openhft.chronicle.wire.DocumentContext) Wire(net.openhft.chronicle.wire.Wire) SetTimeProvider(net.openhft.chronicle.core.time.SetTimeProvider) Test(org.junit.Test)

Example 27 with Bytes

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

the class WriteBytesIndexTest method writeMultipleAppenders.

@Test
public void writeMultipleAppenders() {
    File path = IOTools.createTempFile("writeMultipleAppenders");
    try (ChronicleQueue q0 = createQueue(path);
        ExcerptAppender a0 = q0.acquireAppender();
        ExcerptTailer t0 = q0.createTailer();
        ChronicleQueue q1 = createQueue(path);
        ExcerptAppender a1 = q1.acquireAppender();
        ExcerptTailer t1 = q1.createTailer();
        ChronicleQueue q2 = createQueue(path)) {
        Bytes bytes = Bytes.allocateElasticOnHeap();
        Bytes bytes2 = Bytes.allocateElasticOnHeap();
        for (int cycle = 1; cycle < 10; cycle++) {
            for (int seq = 0; seq < cycle; seq++) {
                bytes.clear().append("Msg ").append(String.valueOf(cycle)).append(" ").append(String.valueOf(seq));
                long index = q0.rollCycle().toIndex(cycle, seq);
                if ((cycle + seq) % 5 < 2) {
                    ((InternalAppender) a0).writeBytes(index, bytes);
                }
                // try a1
                ((InternalAppender) a1).writeBytes(index, bytes);
                assertTrue(t1.readBytes(bytes2.clear()));
                if (!bytes.contentEquals(bytes2)) {
                    System.out.println(q2.dump());
                    assertEquals(bytes.toString(), bytes2.toString());
                }
                assertFalse(t1.readBytes(bytes2.clear()));
                assertTrue(t0.readBytes(bytes2.clear()));
                if (!bytes.contentEquals(bytes2)) {
                    System.out.println(q2.dump());
                    assertEquals(bytes.toString(), bytes2.toString());
                }
                assertFalse(t0.readBytes(bytes2.clear()));
            }
        }
    }
    IOTools.deleteDirWithFiles(path);
}
Also used : Bytes(net.openhft.chronicle.bytes.Bytes) File(java.io.File) Test(org.junit.Test)

Example 28 with Bytes

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

the class TestTailAfterRoll method doTest.

public void doTest() {
    File tmpDir = getTmpDir();
    File[] files;
    try (ChronicleQueue writeQ = ChronicleQueue.singleBuilder(tmpDir).build()) {
        ExcerptAppender appender = writeQ.acquireAppender();
        long wp;
        Wire wire;
        try (DocumentContext dc = appender.writingDocument()) {
            wire = dc.wire();
            wire.write().text("hello world");
            Bytes<?> bytes = wire.bytes();
            wp = bytes.writePosition();
        }
        File dir = new File(appender.queue().fileAbsolutePath());
        files = dir.listFiles(pathname -> pathname.getAbsolutePath().endsWith(".cq4"));
        wire.bytes().writeInt(wp, Wires.END_OF_DATA);
        appender.writeText("hello world  2");
    }
    Assert.assertEquals(1, files.length);
    File file = files[0];
    file.delete();
    try (ChronicleQueue q = ChronicleQueue.singleBuilder(tmpDir).build()) {
        ExcerptTailer excerptTailer = q.createTailer().toEnd();
        q.acquireAppender().writeText(EXPECTED);
        Assert.assertEquals(EXPECTED, excerptTailer.readText());
    }
}
Also used : ExcerptTailer(net.openhft.chronicle.queue.ExcerptTailer) DocumentContext(net.openhft.chronicle.wire.DocumentContext) ChronicleQueue(net.openhft.chronicle.queue.ChronicleQueue) Test(org.junit.Test) FlakyTestRunner(net.openhft.chronicle.core.FlakyTestRunner) Wire(net.openhft.chronicle.wire.Wire) File(java.io.File) Bytes(net.openhft.chronicle.bytes.Bytes) ChronicleQueueTestBase(net.openhft.chronicle.queue.ChronicleQueueTestBase) OS(net.openhft.chronicle.core.OS) ExcerptAppender(net.openhft.chronicle.queue.ExcerptAppender) Assert(org.junit.Assert) Wires(net.openhft.chronicle.wire.Wires) ChronicleQueue(net.openhft.chronicle.queue.ChronicleQueue) ExcerptAppender(net.openhft.chronicle.queue.ExcerptAppender) Wire(net.openhft.chronicle.wire.Wire) DocumentContext(net.openhft.chronicle.wire.DocumentContext) File(java.io.File) ExcerptTailer(net.openhft.chronicle.queue.ExcerptTailer)

Example 29 with Bytes

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

the class RollCycleTest method testWriteToCorruptedFile.

@Test
public void testWriteToCorruptedFile() {
    File dir = DirectoryUtils.tempDir("testWriteToCorruptedFile");
    try (SingleChronicleQueue queue = SingleChronicleQueueBuilder.binary(dir).testBlockSize().rollCycle(RollCycles.TEST_DAILY).build()) {
        ExcerptAppender appender = queue.acquireAppender();
        try (DocumentContext dc = appender.writingDocument()) {
            dc.wire().write().text("hello world");
        }
        Bytes bytes;
        long pos;
        try (DocumentContext dc = appender.writingDocument()) {
            bytes = dc.wire().bytes();
            pos = bytes.writePosition() - 4;
        }
        // write as not complete.
        bytes.writeInt(pos, Wires.NOT_COMPLETE_UNKNOWN_LENGTH);
        try (DocumentContext dc = appender.writingDocument()) {
            dc.wire().write().text("hello world 2");
        }
        try (DocumentContext dc = appender.writingDocument()) {
            dc.wire().write().text("hello world 3");
        }
    }
}
Also used : Bytes(net.openhft.chronicle.bytes.Bytes) ExcerptAppender(net.openhft.chronicle.queue.ExcerptAppender) DocumentContext(net.openhft.chronicle.wire.DocumentContext) File(java.io.File) MappedFile(net.openhft.chronicle.bytes.MappedFile) Test(org.junit.Test)

Example 30 with Bytes

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

the class SingleCQFormat2Test method appendMessage.

public void appendMessage(@NotNull SingleChronicleQueue queue, long expectedIndex, String msg) {
    @NotNull ExcerptAppender appender = queue.acquireAppender().lazyIndexing(lazyIndexing);
    switch(appendMode) {
        case 1:
            appender.writeDocument(w -> w.write(() -> "msg").text(msg));
            break;
        case 2:
            Bytes bytes = Bytes.elasticByteBuffer();
            new BinaryWire(bytes).write(() -> "msg").text(msg);
            appender.writeBytes(bytes);
            bytes.release();
            break;
        default:
            try (DocumentContext dc = appender.writingDocument()) {
                Wire wire = dc.wire();
                wire.write(() -> "msg").text(msg);
            }
            break;
    }
    long index = appender.lastIndexAppended();
    assertHexEquals(expectedIndex, index);
}
Also used : MappedBytes(net.openhft.chronicle.bytes.MappedBytes) Bytes(net.openhft.chronicle.bytes.Bytes) NotNull(org.jetbrains.annotations.NotNull)

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