Search in sources :

Example 1 with InternalAppender

use of net.openhft.chronicle.queue.impl.single.SingleChronicleQueueExcerpts.InternalAppender in project Chronicle-Queue by OpenHFT.

the class SingleChronicleQueueTest method writeBytesAndIndexFiveTimesWithOverwriteTest.

@Test
public void writeBytesAndIndexFiveTimesWithOverwriteTest() {
    try (final SingleChronicleQueue sourceQueue = builder(DirectoryUtils.tempDir("to-be-deleted"), wireType).testBlockSize().build()) {
        for (int i = 0; i < 5; i++) {
            ExcerptAppender excerptAppender = sourceQueue.acquireAppender();
            try (DocumentContext dc = excerptAppender.writingDocument()) {
                dc.wire().write("hello").text("world" + i);
            }
        }
        ExcerptTailer tailer = sourceQueue.createTailer();
        try (final SingleChronicleQueue queue = builder(DirectoryUtils.tempDir("to-be-deleted"), wireType).testBlockSize().build()) {
            ExcerptAppender appender0 = queue.acquireAppender();
            if (!(appender0 instanceof InternalAppender))
                return;
            InternalAppender appender = (InternalAppender) appender0;
            if (!(appender instanceof StoreAppender))
                return;
            List<BytesWithIndex> bytesWithIndies = new ArrayList<>();
            try {
                for (int i = 0; i < 5; i++) {
                    bytesWithIndies.add(bytes(tailer));
                }
                for (int i = 0; i < 4; i++) {
                    BytesWithIndex b = bytesWithIndies.get(i);
                    appender.writeBytes(b.index, b.bytes);
                }
                for (int i = 0; i < 4; i++) {
                    BytesWithIndex b = bytesWithIndies.get(i);
                    appender.writeBytes(b.index, b.bytes);
                }
                BytesWithIndex b = bytesWithIndies.get(4);
                appender.writeBytes(b.index, b.bytes);
                ((StoreAppender) appender).checkWritePositionHeaderNumber();
                appender0.writeText("hello");
            } finally {
                closeQuietly(bytesWithIndies);
            }
            System.out.println(queue.dump());
            Assert.assertTrue(queue.dump().contains("--- !!data #binary\n" + "hello: world0\n" + "# position: 1041, header: 1\n" + "--- !!data #binary\n" + "hello: world1\n" + "# position: 1058, header: 2\n" + "--- !!data #binary\n" + "hello: world2\n" + "# position: 1075, header: 3\n" + "--- !!data #binary\n" + "hello: world3\n" + "# position: 1092, header: 4\n" + "--- !!data #binary\n" + "hello: world4\n" + "# position: 1109, header: 5\n" + "--- !!data\n" + "hello\n"));
        }
    }
}
Also used : StoreAppender(net.openhft.chronicle.queue.impl.single.SingleChronicleQueueExcerpts.StoreAppender) InternalAppender(net.openhft.chronicle.queue.impl.single.SingleChronicleQueueExcerpts.InternalAppender)

Example 2 with InternalAppender

use of net.openhft.chronicle.queue.impl.single.SingleChronicleQueueExcerpts.InternalAppender in project Chronicle-Queue by OpenHFT.

the class SingleChronicleQueueTest method testMoveToWithAppender.

@Test
public void testMoveToWithAppender() {
    try (ChronicleQueue syncQ = builder(getTmpDir(), this.wireType).build()) {
        InternalAppender sync = (InternalAppender) syncQ.acquireAppender();
        File name2 = DirectoryUtils.tempDir(testName.getMethodName());
        try (ChronicleQueue chronicle = builder(name2, this.wireType).build()) {
            ExcerptAppender appender = chronicle.acquireAppender();
            appender.writeDocument(w -> w.writeEventName("hello").text("world0"));
            appender.writeDocument(w -> w.writeEventName("hello").text("world1"));
            appender.writeDocument(w -> w.writeEventName("hello").text("world2"));
            ExcerptTailer tailer = chronicle.createTailer();
            try (DocumentContext documentContext = tailer.readingDocument()) {
                sync.writeBytes(documentContext.index(), documentContext.wire().bytes());
            }
            try (DocumentContext documentContext = tailer.readingDocument()) {
                String text = documentContext.wire().read().text();
                Assert.assertEquals("world1", text);
            }
        }
    }
}
Also used : RollingChronicleQueue(net.openhft.chronicle.queue.impl.RollingChronicleQueue) InternalAppender(net.openhft.chronicle.queue.impl.single.SingleChronicleQueueExcerpts.InternalAppender)

Example 3 with InternalAppender

use of net.openhft.chronicle.queue.impl.single.SingleChronicleQueueExcerpts.InternalAppender in project Chronicle-Queue by OpenHFT.

the class CreateAtIndexTest method testWriteBytesWithIndex.

@Test
public void testWriteBytesWithIndex() throws Exception {
    String tmp = OS.TARGET + "/" + getClass().getSimpleName() + "-" + System.nanoTime();
    try (SingleChronicleQueue queue = ChronicleQueueBuilder.single(tmp).testBlockSize().rollCycle(TEST_DAILY).build()) {
        InternalAppender appender = (InternalAppender) queue.acquireAppender();
        appender.writeBytes(0x421d00000000L, HELLO_WORLD);
        appender.writeBytes(0x421d00000001L, HELLO_WORLD);
    }
    try (SingleChronicleQueue queue = ChronicleQueueBuilder.single(tmp).testBlockSize().build()) {
        InternalAppender appender = (InternalAppender) queue.acquireAppender();
        String before = queue.dump();
        appender.writeBytes(0x421d00000000L, HELLO_WORLD);
        String after = queue.dump();
        assertEquals(before, after);
    }
    boolean runIfAssertsOn = false;
    // assert runIfAssertsOn = true;
    if (runIfAssertsOn) {
        try (SingleChronicleQueue queue = ChronicleQueueBuilder.single(tmp).testBlockSize().build()) {
            InternalAppender appender = (InternalAppender) queue.acquireAppender();
            String before = queue.dump();
            try {
                appender.writeBytes(0x421d00000000L, Bytes.from("hellooooo world"));
                fail();
            } catch (IllegalStateException e) {
            // expected
            }
            String after = queue.dump();
            assertEquals(before, after);
        }
    }
    // try too far
    try (SingleChronicleQueue queue = ChronicleQueueBuilder.single(tmp).testBlockSize().build()) {
        InternalAppender appender = (InternalAppender) queue.acquireAppender();
        try {
            appender.writeBytes(0x421d00000003L, HELLO_WORLD);
            fail();
        } catch (IllegalStateException e) {
            assertEquals("Unable to move to index 421d00000003 beyond the end of the queue", e.getMessage());
        }
    }
    try (SingleChronicleQueue queue = ChronicleQueueBuilder.single(tmp).testBlockSize().build()) {
        InternalAppender appender = (InternalAppender) queue.acquireAppender();
        appender.writeBytes(0x421d00000002L, HELLO_WORLD);
        appender.writeBytes(0x421d00000003L, HELLO_WORLD);
    }
    try {
        IOTools.deleteDirWithFiles(tmp, 2);
    } catch (IORuntimeException ignored) {
    }
}
Also used : IORuntimeException(net.openhft.chronicle.core.io.IORuntimeException) SingleChronicleQueue(net.openhft.chronicle.queue.impl.single.SingleChronicleQueue) InternalAppender(net.openhft.chronicle.queue.impl.single.SingleChronicleQueueExcerpts.InternalAppender) Test(org.junit.Test)

Aggregations

InternalAppender (net.openhft.chronicle.queue.impl.single.SingleChronicleQueueExcerpts.InternalAppender)3 IORuntimeException (net.openhft.chronicle.core.io.IORuntimeException)1 RollingChronicleQueue (net.openhft.chronicle.queue.impl.RollingChronicleQueue)1 SingleChronicleQueue (net.openhft.chronicle.queue.impl.single.SingleChronicleQueue)1 StoreAppender (net.openhft.chronicle.queue.impl.single.SingleChronicleQueueExcerpts.StoreAppender)1 Test (org.junit.Test)1