Search in sources :

Example 1 with BinaryWire

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

the class MethodReaderQueueEntryHandler method accept.

@Override
public void accept(final WireIn wireIn, final Consumer<String> messageHandler) {
    long elementCount = 0;
    while (wireIn.hasMore()) {
        new BinaryWire(wireIn.bytes()).copyOne(wireType.apply(textConversionTarget));
        elementCount++;
        if ((elementCount & 1) == 0) {
            messageHandler.accept(textConversionTarget.toString());
            textConversionTarget.clear();
        }
    }
}
Also used : BinaryWire(net.openhft.chronicle.wire.BinaryWire)

Example 2 with BinaryWire

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

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

the class MessageToTextQueueEntryHandler method accept.

@Override
public void accept(final WireIn wireIn, final Consumer<String> messageHandler) {
    final Bytes<?> serialisedMessage = wireIn.bytes();
    final byte dataFormatIndicator = serialisedMessage.readByte(serialisedMessage.readPosition());
    String text;
    if (isBinaryFormat(dataFormatIndicator)) {
        textConversionTarget.clear();
        final BinaryWire binaryWire = new BinaryWire(serialisedMessage);
        binaryWire.copyTo(wireType.apply(textConversionTarget));
        text = textConversionTarget.toString();
    } else {
        text = serialisedMessage.toString();
    }
    messageHandler.accept(text);
}
Also used : BinaryWire(net.openhft.chronicle.wire.BinaryWire)

Aggregations

BinaryWire (net.openhft.chronicle.wire.BinaryWire)3 File (java.io.File)1 FileNotFoundException (java.io.FileNotFoundException)1 MappedBytes (net.openhft.chronicle.bytes.MappedBytes)1 DocumentContext (net.openhft.chronicle.wire.DocumentContext)1 Wire (net.openhft.chronicle.wire.Wire)1 Test (org.junit.Test)1