Java Code Examples for net.openhft.chronicle.wire.Wire#bytes()

The following examples show how to use net.openhft.chronicle.wire.Wire#bytes() . 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: 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 2
Source File: BenchmarkMain.java    From Chronicle-Queue with Apache License 2.0 5 votes vote down vote up
private static void runInner(Histogram transportTime, Histogram readTime, ExcerptTailer tailer) {
    Jvm.safepoint();
    if (tailer.peekDocument()) {
        if (counter++ < 1000) {
            Jvm.safepoint();
            return;
        }
    }
    if (counter > 0)
        Jvm.safepoint();
    else
        Jvm.safepoint();
    counter = 0;
    try (DocumentContext dc = tailer.readingDocument(false)) {
        Jvm.safepoint();
        if (!dc.isPresent()) {
            return;
        }
        long transport = System.nanoTime();
        Jvm.safepoint();
        Wire wire = dc.wire();
        Bytes<?> bytes = wire.bytes();
        long start = readMessage(bytes);
        long end = System.nanoTime();
        transportTime.sample(transport - start);
        readTime.sample(end - transport);
    }
    Jvm.safepoint();
}
 
Example 3
Source File: BenchmarkMain.java    From Chronicle-Queue with Apache License 2.0 5 votes vote down vote up
private static void writeMessage(Wire wire, int messageSize) {
    Bytes<?> bytes = wire.bytes();
    long wp = bytes.writePosition();
    long addr = bytes.addressForWrite(wp);
    Memory memory = OS.memory();
    for (int i = 0; i < messageSize; i += 16) {
        memory.writeLong(addr + i, 0L);
        memory.writeLong(addr + i + 8, 0L);
    }

    bytes.writeSkip(messageSize);
    bytes.writeLong(wp, System.nanoTime());
}
 
Example 4
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 5
Source File: ThroughputMain.java    From Chronicle-Queue with Apache License 2.0 5 votes vote down vote up
static void addToEndOfCache(Wire wire2) {
    Bytes<?> bytes = wire2.bytes();
    long addr = bytes.addressForWrite(bytes.writePosition());
    int pad = (int) (64 - (addr & 63));
    if (pad < 64)
        wire2.addPadding(pad);
}