net.openhft.chronicle.wire.Wires Java Examples

The following examples show how to use net.openhft.chronicle.wire.Wires. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: QueueDumpMain.java    From Chronicle-Queue with Apache License 2.0 6 votes vote down vote up
public static void dump(File filename, PrintWriter pw) throws FileNotFoundException {
    MappedFile mappedFile = MappedFile.mappedFile(filename, 64 << 20, 16 << 20);
    Bytes bytes = mappedFile.bytes();
    pw.print("# Magic: ");
    for (int i = 0; i < 8; i++)
        pw.print((char) bytes.readUnsignedByte());
    pw.println();
    while (true) {
        long spb = bytes.readUnsignedInt();
        if (!Wires.isKnownLength(spb))
            break;
        pw.print("--- !");
        pw.print(SBP_TYPES[((int) (spb >>> 30))]);
        pw.println();
        long start = bytes.position();
        BytesUtil.toString(bytes, pw, start, start, start + Wires.lengthOf(spb));
        pw.println();
        bytes.skip(Wires.lengthOf(spb));
    }
    pw.flush();
}
 
Example #2
Source File: SingleChronicleQueue.java    From Chronicle-Queue with Apache License 2.0 6 votes vote down vote up
@Override
public boolean readDocument(@NotNull AtomicLong offset, @NotNull Bytes buffer) {
    buffer.clear();
    long lastByte = offset.get();
    for (; ; ) {
        int length = bytes.readVolatileInt(lastByte);
        int length2 = length30(length);
        if (Wires.isReady(length)) {
            lastByte += 4;
            buffer.write(bytes, lastByte, length2);
            lastByte += length2;
            offset.set(lastByte);
            return isData(length);
        }

        if (Thread.currentThread().isInterrupted()) {
            return false;
        }
    }
}
 
Example #3
Source File: RollEOFTest.java    From Chronicle-Queue with Apache License 2.0 6 votes vote down vote up
private void removeEOF(Path path) throws IOException {
    long blockSize = 64 << 10;
    long chunkSize = OS.pageAlign(blockSize);
    long overlapSize = OS.pageAlign(blockSize / 4);
    final MappedBytes mappedBytes = MappedBytes.mappedBytes(path.toFile(), chunkSize, overlapSize, false);
    mappedBytes.reserve(test);
    try {
        final Wire wire = WireType.BINARY_LIGHT.apply(mappedBytes);
        final Bytes<?> bytes = wire.bytes();
        bytes.readLimitToCapacity();
        bytes.readSkip(4);
        // move past header
        try (final SingleChronicleQueueStore qs = loadStore(wire)) {
            assertNotNull(qs);
            long l = qs.writePosition();
            long len = Wires.lengthOf(bytes.readVolatileInt(l));
            long eofOffset = l + len + 4L;
            bytes.writePosition(eofOffset);
            bytes.writeInt(0);
        }
    } finally {
        mappedBytes.release(test);
    }
}
 
Example #4
Source File: RollCycleMultiThreadTest.java    From Chronicle-Queue with Apache License 2.0 6 votes vote down vote up
@Override
public synchronized Integer call() {

    try (final DocumentContext dc = tailer.readingDocument()) {

        System.out.println("index=" + Long.toHexString(dc.index()));
        if (!dc.isPresent())
            return documentsRead;

        StringBuilder sb = Wires.acquireStringBuilder();
        dc.wire().bytes().parse8bit(sb, StopCharTesters.ALL);

        String readText = sb.toString();
        if (java.util.Objects.equals(readText, "")) {
            return documentsRead;
        }
        System.out.println("Read a document " + readText);
        documentsRead++;

    }
    return documentsRead;
}
 
Example #5
Source File: VerySimpleClient.java    From Chronicle-Network with Apache License 2.0 5 votes vote down vote up
private void readDocument(Wire inMetaDataWire) throws IOException {
    ByteBuffer inBuff = (ByteBuffer) inMetaDataWire.bytes().underlyingObject();

    // write the data to the socket
    long start = inMetaDataWire.bytes().writePosition();

    while (inBuff.position() < 4 + start)
        client.read(inBuff);

    int len = Wires.lengthOf(inMetaDataWire.bytes().readInt(start));
    inMetaDataWire.bytes().readLimit(start + 4 + len);
    while (inBuff.position() < 4 + len + start)
        client.read(inBuff);
}
 
Example #6
Source File: VerySimpleClientTest.java    From Chronicle-Network with Apache License 2.0 5 votes vote down vote up
private void readDocument(@NotNull Wire inMetaDataWire) throws IOException {
    @Nullable ByteBuffer inBuff = (ByteBuffer) inMetaDataWire.bytes().underlyingObject();

    // write the data to the socket
    long start = inMetaDataWire.bytes().readPosition();
    while (inBuff.position() + start < 4)
        client.read(inBuff);

    inMetaDataWire.bytes().writePosition(inBuff.position());
    int len = Wires.lengthOf(inMetaDataWire.bytes().readInt(start));
    while (inBuff.position() < 4 + len + start)
        client.read(inBuff);
}
 
Example #7
Source File: MarshallableReaderWriter.java    From Chronicle-Map with Apache License 2.0 5 votes vote down vote up
@NotNull
@Override
public V read(Bytes in, long size, @Nullable V using) {
    if (using == null)
        using = createInstance();

    using.readMarshallable(Wires.binaryWireForRead(in, in.readPosition(), size));
    return using;
}
 
Example #8
Source File: SingleTableBuilder.java    From Chronicle-Queue with Apache License 2.0 5 votes vote down vote up
@NotNull
private TableStore<T> readTableStore(Wire wire) throws StreamCorruptedException {
    wire.readFirstHeader();

    StringBuilder name = Wires.acquireStringBuilder();
    ValueIn valueIn = wire.readEventName(name);
    if (StringUtils.isEqual(name, MetaDataKeys.header.name())) {
        @NotNull TableStore<T> existing = Objects.requireNonNull(valueIn.typedMarshallable());
        metadata.overrideFrom(existing.metadata());
        return existing;
    } else {
        throw new StreamCorruptedException("The first message should be the header, was " + name);
    }
}
 
Example #9
Source File: SingleChronicleQueueBuilderTest.java    From Chronicle-Queue with Apache License 2.0 5 votes vote down vote up
@Test(expected = IllegalArgumentException.class)
public void setAllNullFieldsShouldFailWithDifferentHierarchy() {
    SingleChronicleQueueBuilder b1 = Wires.tupleFor(SingleChronicleQueueBuilder.class, "ChronicleQueueBuilder");
    SingleChronicleQueueBuilder b2 = SingleChronicleQueueBuilder.builder();
    b2.bufferCapacity(98765);
    b1.blockSize(1234567);
    b2.setAllNullFields(b1);
}
 
Example #10
Source File: SingleChronicleQueueBuilderTest.java    From Chronicle-Queue with Apache License 2.0 5 votes vote down vote up
@Test
@Ignore("https://github.com/OpenHFT/Chronicle-Wire/issues/165")
public void testWriteMarshallableBinary() {
    final SingleChronicleQueueBuilder builder = SingleChronicleQueueBuilder.single("test").rollCycle(HOURLY);

    builder.build().close();
    final Wire wire = Wires.acquireBinaryWire();
    wire.write().typedMarshallable(builder);

    System.err.println(wire.bytes().toHexString());

    SingleChronicleQueueBuilder builder2 = wire.read().typedMarshallable();
    builder2.build().close();
}
 
Example #11
Source File: TestTailAfterRoll.java    From Chronicle-Queue with Apache License 2.0 5 votes vote down vote up
/**
 * the following steps
 * <p>
 * (1) write to a queue
 * (2) force and end for file marker
 * (3) write to the queue again, this will cause it to be written to tomorrows .cq4 file
 * (4) delete todays queue files, that was created in step (1)
 * (5) create a new instance of chronicle queue ( same directory ) as step 1
 * (6) create a tailer toEnd
 * (6) write to this queue created in (5)
 * (7) when you now try to read from this queue you will not be able to read back what you have just written in (6)
 */
@Test
public void test() {
    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(files.length, 1);
    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());
    }
}
 
Example #12
Source File: MarshallableReaderWriter.java    From Chronicle-Map with Apache License 2.0 4 votes vote down vote up
@Override
public long size(@NotNull V toWrite) {
    Wire wire = Wires.acquireBinaryWire();
    toWrite.writeMarshallable(wire);
    return wire.bytes().readRemaining();
}
 
Example #13
Source File: MarshallableReaderWriter.java    From Chronicle-Map with Apache License 2.0 4 votes vote down vote up
@Override
public void write(Bytes out, long size, @NotNull V toWrite) {
    BinaryWire wire = Wires.binaryWireForWrite(out, out.writePosition(), size);
    toWrite.writeMarshallable(wire);
}
 
Example #14
Source File: BytesMarshallableReaderWriter.java    From Chronicle-Map with Apache License 2.0 4 votes vote down vote up
@Override
public long size(@NotNull V toWrite) {
    Bytes<?> bytes = Wires.acquireBytes();
    toWrite.writeMarshallable(bytes);
    return bytes.readRemaining();
}
 
Example #15
Source File: RawAccessJava.java    From Chronicle-Queue with Apache License 2.0 4 votes vote down vote up
@Test
public void Tailer() {
    if (!assert_from_cpp())
        return;

    String tmp = "/dev/shm/RawAccessCtoJ";
    System.out.println(tmp); // so C++ knows this ran rather than skipped

    try (ChronicleQueue cq = SingleChronicleQueueBuilder.binary(tmp).build()) {

        ExcerptTailer tailer = cq.createTailer();

        for (int i = 0; i < COUNT; ++i) {
            try (DocumentContext dc = tailer.readingDocument()) {

                Bytes bytes = dc.wire().bytes();

                bytes.readSkip(-QUEUE_HEADER_SIZE);
                int header = bytes.readInt();

                // document length, inc 4-byte length
                int length = Wires.lengthOf(header);

                // actual length of data
                int data_length = bytes.readInt();

                assertEquals(bytes.readByte(), (byte) 0xab);
                assertEquals(bytes.readShort(), (short) 12);
                assertEquals(bytes.readInt(), 123);
                assertEquals(bytes.readLong(), 123456789L);
                assertEquals(bytes.readFloat(), 1.234f, 1.0e-7);
                assertEquals(bytes.readDouble(), 123.456, 1.0e-7);
                assertEquals(bytes.readChar(), 'a');

                StringBuilder sb = new StringBuilder();
                bytes.read8bit(sb);
                assertEquals(sb.toString(), "Hello World");
            }
        }
    }
}