Search in sources :

Example 6 with MappedBytes

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

the class SingleCQFormatTest method testTwoMessages.

@Test
public void testTwoMessages() throws FileNotFoundException {
    @NotNull File dir = new File(OS.TARGET + "/deleteme-" + System.nanoTime());
    dir.mkdir();
    @NotNull RollCycles cycle = RollCycles.TEST4_DAILY;
    {
        @NotNull MappedBytes mappedBytes = MappedBytes.mappedBytes(new File(dir, "19700102" + SingleChronicleQueue.SUFFIX), 64 << 10);
        @NotNull Wire wire = new BinaryWire(mappedBytes);
        try (DocumentContext dc = wire.writingDocument(true)) {
            dc.wire().writeEventName(() -> "header").typedMarshallable(new SingleChronicleQueueStore(cycle, WireType.BINARY, mappedBytes, 0, cycle.defaultIndexCount(), cycle.defaultIndexSpacing(), new TimedStoreRecovery(WireType.BINARY), -1, 0));
        }
        try (DocumentContext dc = wire.writingDocument(false)) {
            dc.wire().writeEventName("msg").text("Hello world");
        }
        try (DocumentContext dc = wire.writingDocument(false)) {
            dc.wire().writeEventName("msg").text("Also hello world");
        }
        assertEquals("--- !!meta-data #binary\n" + "header: !SCQStore {\n" + "  wireType: !WireType BINARY,\n" + "  writePosition: [\n" + "    0,\n" + "    0\n" + "  ],\n" + "  roll: !SCQSRoll {\n" + "    length: !int 86400000,\n" + "    format: yyyyMMdd,\n" + "    epoch: 0\n" + "  },\n" + "  indexing: !SCQSIndexing {\n" + "    indexCount: 32,\n" + "    indexSpacing: 4,\n" + "    index2Index: 0,\n" + "    lastIndex: 0\n" + "  },\n" + "  lastAcknowledgedIndexReplicated: -1,\n" + "  recovery: !TimedStoreRecovery {\n" + "    timeStamp: 0\n" + "  },\n" + "  deltaCheckpointInterval: !byte -1,\n" + "  lastIndexReplicated: -1,\n" + "  sourceId: 0\n" + "}\n" + "# position: 442, header: 0\n" + "--- !!data #binary\n" + "msg: Hello world\n" + "# position: 463, header: 1\n" + "--- !!data #binary\n" + "msg: Also hello world\n", Wires.fromSizePrefixedBlobs(mappedBytes.readPosition(0)));
        mappedBytes.release();
    }
    @NotNull SingleChronicleQueue queue = binary(dir).rollCycle(cycle).testBlockSize().build();
    @NotNull ExcerptTailer tailer = queue.createTailer();
    readTwo(tailer);
    tailer.toStart();
    readTwo(tailer);
    // TODO no direction
    tailer.direction(TailerDirection.NONE).toStart();
    long start = queue.firstIndex();
    assertEquals(start, tailer.index());
    expected(tailer, "msg: Hello world\n");
    assertEquals(start, tailer.index());
    expected(tailer, "msg: Hello world\n");
    /* TODO FIX.
        assertEquals(start + 1, queue.lastIndex());

        tailer.direction(TailerDirection.BACKWARD).toEnd();

        assertEquals(start + 1, tailer.index());
        expected(tailer, "msg: Also hello world\n");
        assertEquals(start, tailer.index());
        expected(tailer, "msg: Hello world\n");
        assertEquals(start - 1, tailer.index());
*/
    queue.close();
    try {
        IOTools.shallowDeleteDirWithFiles(dir.getAbsolutePath());
    } catch (Exception e) {
        e.printStackTrace();
    }
}
Also used : NotNull(org.jetbrains.annotations.NotNull) TimeoutException(java.util.concurrent.TimeoutException) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException) File(java.io.File) MappedFile(net.openhft.chronicle.bytes.MappedFile) MappedBytes(net.openhft.chronicle.bytes.MappedBytes) Test(org.junit.Test)

Example 7 with MappedBytes

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

the class SingleCQFormatTest method testIncompleteHeader.

@Test
public void testIncompleteHeader() throws FileNotFoundException {
    @NotNull File dir = new File(OS.TARGET, getClass().getSimpleName() + "-" + System.nanoTime());
    dir.mkdir();
    @NotNull MappedBytes bytes = MappedBytes.mappedBytes(new File(dir, "19700101" + SingleChronicleQueue.SUFFIX), ChronicleQueue.TEST_BLOCK_SIZE);
    @NotNull Wire wire = new BinaryWire(bytes);
    try (DocumentContext dc = wire.writingDocument(true)) {
        dc.wire().writeEventName(() -> "header").typePrefix(SingleChronicleQueueStore.class).marshallable(w -> w.write(() -> "wireType").object(WireType.BINARY));
    }
    bytes.release();
    try (@NotNull SingleChronicleQueue queue = binary(dir).rollCycle(RollCycles.TEST4_DAILY).blockSize(ChronicleQueue.TEST_BLOCK_SIZE).build()) {
        testQueue(queue);
        fail();
    } catch (Exception e) {
        // e.printStackTrace();
        assertEquals("net.openhft.chronicle.core.io.IORuntimeException: net.openhft.chronicle.core.io.IORuntimeException: field writePosition required", e.toString());
    }
    System.gc();
    try {
        IOTools.shallowDeleteDirWithFiles(dir.getAbsolutePath());
    } catch (Exception e) {
        e.printStackTrace();
    }
}
Also used : NotNull(org.jetbrains.annotations.NotNull) File(java.io.File) MappedFile(net.openhft.chronicle.bytes.MappedFile) MappedBytes(net.openhft.chronicle.bytes.MappedBytes) TimeoutException(java.util.concurrent.TimeoutException) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException) Test(org.junit.Test)

Example 8 with MappedBytes

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

the class DumpQueueMain method dumpFile.

private static void dumpFile(@NotNull File file, @NotNull PrintStream out, long upperLimit) {
    if (file.getName().endsWith(SingleChronicleQueue.SUFFIX)) {
        Bytes<ByteBuffer> buffer = Bytes.elasticByteBuffer();
        try {
            MappedBytes bytes = MappedBytes.mappedBytes(file, 4 << 20, OS.pageSize(), !OS.isWindows());
            bytes.readLimit(bytes.realCapacity());
            StringBuilder sb = new StringBuilder();
            WireDumper dumper = WireDumper.of(bytes);
            while (bytes.readRemaining() >= 4) {
                sb.setLength(0);
                boolean last = dumper.dumpOne(sb, buffer);
                if (sb.indexOf("\nindex2index:") != -1 || sb.indexOf("\nindex:") != -1) {
                    // truncate trailing zeros
                    if (sb.indexOf(", 0\n]\n") == sb.length() - 6) {
                        int i = indexOfLastZero(sb);
                        if (i < sb.length())
                            sb.setLength(i - 5);
                        sb.append(" # truncated trailing zeros\n]");
                    }
                }
                out.println(sb);
                if (last)
                    break;
                if (bytes.readPosition() > upperLimit) {
                    out.println("# limit reached.");
                    return;
                }
            }
        } catch (IOException ioe) {
            err.println("Failed to read " + file + " " + ioe);
        } finally {
            buffer.release();
        }
    }
}
Also used : WireDumper(net.openhft.chronicle.wire.WireDumper) IOException(java.io.IOException) ByteBuffer(java.nio.ByteBuffer) MappedBytes(net.openhft.chronicle.bytes.MappedBytes)

Example 9 with MappedBytes

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

the class DetectNotReadyEntriesTest method testDeadEntries.

@Test
public void testDeadEntries() throws FileNotFoundException {
    // TODO FIX.
    if (OS.isWindows())
        return;
    File dir = new File(OS.TARGET, getClass().getSimpleName() + "-" + System.nanoTime());
    dir.mkdir();
    MappedBytes bytes = MappedBytes.mappedBytes(new File(dir, "19700101" + SingleChronicleQueue.SUFFIX), 64 << 10);
    Wire wire = new BinaryWire(bytes);
    try (DocumentContext dc = wire.writingDocument(true)) {
        dc.wire().writeEventName(() -> "header").typePrefix(SingleChronicleQueueStore.class).marshallable(w -> {
            w.write(() -> "wireType").object(WireType.BINARY);
            w.write(() -> "writePosition").int64forBinding(288 + 4 + 17);
            w.write(() -> "roll").typedMarshallable(new SCQRoll(RollCycles.DAILY, 0));
            w.write(() -> "indexing").typedMarshallable(new SCQIndexing(WireType.BINARY, 32 << 10, 32));
            w.write(() -> "lastAcknowledgedIndexReplicated").int64forBinding(0);
        });
    }
    long pos = wire.bytes().writePosition();
    try (DocumentContext dc = wire.writingDocument(false)) {
        dc.wire().write("test").text("Hello World");
    }
    assertEquals(17, wire.bytes().readInt(pos));
    // make it incomplete, note that the length is removed,
    // since writing a length into an incomplete excerpt is not allowed
    wire.bytes().writeInt(pos, Wires.NOT_COMPLETE);
    assertEquals("--- !!meta-data #binary\n" + "header: !SCQStore {\n" + "  wireType: !WireType BINARY,\n" + "  writePosition: 309,\n" + "  roll: !SCQSRoll {\n" + "    length: !int 86400000,\n" + "    format: yyyyMMdd,\n" + "    epoch: 0\n" + "  },\n" + "  indexing: !SCQSIndexing {\n" + "    indexCount: !int 32768,\n" + "    indexSpacing: 32,\n" + "    index2Index: 0,\n" + "    lastIndex: 0\n" + "  },\n" + "  lastAcknowledgedIndexReplicated: 0\n" + "}\n" + "# position: 288, header: -1 or 0\n" + "--- !!not-ready-data! #binary\n" + "...\n" + "# 17 bytes remaining\n", Wires.fromSizePrefixedBlobs(bytes.readPosition(0)));
    bytes.release();
    try (SingleChronicleQueue queue = SingleChronicleQueueBuilder.binary(dir).testBlockSize().build()) {
        queue.acquireAppender().writeText("Bye for now");
    }
    try {
        IOTools.shallowDeleteDirWithFiles(dir.getAbsolutePath());
    } catch (Exception e) {
        e.printStackTrace();
    }
}
Also used : BinaryWire(net.openhft.chronicle.wire.BinaryWire) Wire(net.openhft.chronicle.wire.Wire) DocumentContext(net.openhft.chronicle.wire.DocumentContext) File(java.io.File) MappedBytes(net.openhft.chronicle.bytes.MappedBytes) FileNotFoundException(java.io.FileNotFoundException) BinaryWire(net.openhft.chronicle.wire.BinaryWire) Test(org.junit.Test)

Example 10 with MappedBytes

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

the class SingleCQFormat2Test method checkFileContents.

public void checkFileContents(@NotNull File file, String expected) throws FileNotFoundException {
    @NotNull MappedBytes bytes = MappedBytes.mappedBytes(file, ChronicleQueue.TEST_BLOCK_SIZE);
    bytes.readLimit(bytes.realCapacity());
    assertEquals(expected, Wires.fromSizePrefixedBlobs(bytes));
    bytes.release();
}
Also used : NotNull(org.jetbrains.annotations.NotNull) MappedBytes(net.openhft.chronicle.bytes.MappedBytes)

Aggregations

MappedBytes (net.openhft.chronicle.bytes.MappedBytes)14 File (java.io.File)9 IOException (java.io.IOException)8 NotNull (org.jetbrains.annotations.NotNull)8 Test (org.junit.Test)8 TimeoutException (java.util.concurrent.TimeoutException)7 FileNotFoundException (java.io.FileNotFoundException)6 MappedFile (net.openhft.chronicle.bytes.MappedFile)6 Wire (net.openhft.chronicle.wire.Wire)4 ByteBuffer (java.nio.ByteBuffer)2 DocumentContext (net.openhft.chronicle.wire.DocumentContext)2 WireDumper (net.openhft.chronicle.wire.WireDumper)2 Ignore (org.junit.Ignore)2 EOFException (java.io.EOFException)1 PrintWriter (java.io.PrintWriter)1 StreamCorruptedException (java.io.StreamCorruptedException)1 StringWriter (java.io.StringWriter)1 LocalDateTime (java.time.LocalDateTime)1 IORuntimeException (net.openhft.chronicle.core.io.IORuntimeException)1 CommonStore (net.openhft.chronicle.queue.impl.CommonStore)1