Search in sources :

Example 16 with MappedBytes

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

the class RollEOFTest method removeEOF.

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();
    try {
        final Wire wire = WireType.BINARY_LIGHT.apply(mappedBytes);
        final Bytes<?> bytes = wire.bytes();
        bytes.readLimit(bytes.capacity());
        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();
    }
}
Also used : SingleChronicleQueueStore(net.openhft.chronicle.queue.impl.single.SingleChronicleQueueStore) Wire(net.openhft.chronicle.wire.Wire) MappedBytes(net.openhft.chronicle.bytes.MappedBytes)

Example 17 with MappedBytes

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

the class RollEOFTest method removeEOF.

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);
    }
}
Also used : SingleChronicleQueueStore(net.openhft.chronicle.queue.impl.single.SingleChronicleQueueStore) Wire(net.openhft.chronicle.wire.Wire) MappedBytes(net.openhft.chronicle.bytes.MappedBytes)

Example 18 with MappedBytes

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

the class SingleCQFormatTest method testCompleteHeader2.

@Test
public void testCompleteHeader2() throws FileNotFoundException {
    final File dir = new File(OS.getTarget(), getClass().getSimpleName() + "-" + Time.uniqueId());
    dir.mkdir();
    final MappedBytes bytes = MappedBytes.mappedBytes(new File(dir, "19700101-02" + SingleChronicleQueue.SUFFIX), QueueUtil.testBlockSize() * 2L);
    final Wire wire = new BinaryWire(bytes);
    wire.usePadding(true);
    try (final SingleChronicleQueueStore store = new SingleChronicleQueueStore(RollCycles.HOURLY, WireType.BINARY, bytes, 4 << 10, 4)) {
        try (DocumentContext dc = wire.writingDocument(true)) {
            dc.wire().write("header").typedMarshallable(store);
        }
        assertEquals("--- !!meta-data #binary\n" + "header: !SCQStore {\n" + "  writePosition: [\n" + "    0,\n" + "    0\n" + "  ],\n" + "  indexing: !SCQSIndexing {\n" + "    indexCount: !short 4096,\n" + "    indexSpacing: 4,\n" + "    index2Index: 0,\n" + "    lastIndex: 0\n" + "  },\n" + "  dataFormat: 1\n" + "}\n", Wires.fromSizePrefixedBlobs(bytes.readPosition(0)));
    }
    try (RollingChronicleQueue queue = binary(dir).testBlockSize().rollCycle(RollCycles.HOURLY).build()) {
        testQueue(queue);
        assertEquals(2, queue.firstCycle());
    }
    try {
        IOTools.shallowDeleteDirWithFiles(dir.getAbsolutePath());
    } catch (Exception e) {
        e.printStackTrace();
    }
}
Also used : RollingChronicleQueue(net.openhft.chronicle.queue.impl.RollingChronicleQueue) File(java.io.File) MappedBytes(net.openhft.chronicle.bytes.MappedBytes) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException) Test(org.junit.Test)

Example 19 with MappedBytes

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

the class MultiQueueStressMain method main.

public static void main(String[] args) throws FileNotFoundException {
    for (int t = 0; t < runs; t++) {
        long start0 = System.currentTimeMillis();
        int count = 0;
        String baseDir = target + "/deleteme/" + Time.uniqueId();
        new File(baseDir).mkdirs();
        MappedBytes[] queues = new MappedBytes[queueCount];
        int pagesPer10Second = (int) (10L * (throughput << 20) / queueCount / (4 << 10));
        for (int i = 0; i < queueCount; i++) {
            long start1 = System.currentTimeMillis();
            String filename = baseDir + "/" + count++;
            queues[i] = MappedBytes.mappedBytes(filename, pagesPer10Second * (4 << 10));
            long time1 = System.currentTimeMillis() - start1;
        // if (time1 > 20)
        // System.out.printf("Creating %s took %.3f seconds%n", filename, time1 / 1e3);
        }
        long mid1 = System.currentTimeMillis();
        for (int i = 0; i < pagesPer10Second; i++) {
            for (MappedBytes bytes : queues) {
                bytes.writeLong(i * (4 << 10), i);
            }
        }
        long mid2 = System.currentTimeMillis();
        for (MappedBytes bytes : queues) {
            bytes.releaseLast();
        }
        long end0 = System.currentTimeMillis();
        long time0 = end0 - start0;
        System.out.printf("Took %.3f seconds to write %,d MB, create: %.3f, write: %.3f, close: %.3f%n", time0 / 1e3, throughput * 10, (mid1 - start0) / 1e3, (mid2 - mid1) / 1e3, (end0 - mid2) / 1e3);
        long delay = 10000 - time0;
        if (delay > 0)
            Jvm.pause(delay);
    }
}
Also used : File(java.io.File) MappedBytes(net.openhft.chronicle.bytes.MappedBytes)

Example 20 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, QueueUtil.testBlockSize());
    bytes.readLimit(bytes.realCapacity());
    assertEquals(expected, Wires.fromAlignedSizePrefixedBlobs(bytes).replaceAll("(?m)^#.+$\\n", ""));
    bytes.releaseLast();
}
Also used : NotNull(org.jetbrains.annotations.NotNull) MappedBytes(net.openhft.chronicle.bytes.MappedBytes)

Aggregations

MappedBytes (net.openhft.chronicle.bytes.MappedBytes)23 File (java.io.File)10 IOException (java.io.IOException)8 Test (org.junit.Test)8 Wire (net.openhft.chronicle.wire.Wire)7 FileNotFoundException (java.io.FileNotFoundException)6 ChronicleQueue (net.openhft.chronicle.queue.ChronicleQueue)5 RollingChronicleQueue (net.openhft.chronicle.queue.impl.RollingChronicleQueue)5 DocumentContext (net.openhft.chronicle.wire.DocumentContext)4 NotNull (org.jetbrains.annotations.NotNull)4 ByteBuffer (java.nio.ByteBuffer)3 TimeoutException (java.util.concurrent.TimeoutException)3 ExcerptTailer (net.openhft.chronicle.queue.ExcerptTailer)3 WireDumper (net.openhft.chronicle.wire.WireDumper)3 Bytes (net.openhft.chronicle.bytes.Bytes)2 ExcerptAppender (net.openhft.chronicle.queue.ExcerptAppender)2 SingleChronicleQueueStore (net.openhft.chronicle.queue.impl.single.SingleChronicleQueueStore)2 EOFException (java.io.EOFException)1 PrintWriter (java.io.PrintWriter)1 StreamCorruptedException (java.io.StreamCorruptedException)1