Search in sources :

Example 21 with Wire

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

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

the class ChronicleQueueIndexTest method readKeyed.

@NotNull
private List<String> readKeyed(ChronicleQueue queue, boolean includeMetaData) {
    try (ExcerptTailer tailer = queue.createTailer()) {
        List<String> allReads = new ArrayList<>();
        for (; ; ) {
            try (DocumentContext dc = tailer.readingDocument(includeMetaData)) {
                if (!dc.isPresent())
                    return allReads;
                final Wire wire = dc.wire();
                final String key = wire.readEvent(String.class);
                if (!key.equals("key"))
                    continue;
                String str = wire.getValueIn().text();
                allReads.add(str);
            }
        }
    }
}
Also used : ArrayList(java.util.ArrayList) DocumentContext(net.openhft.chronicle.wire.DocumentContext) Wire(net.openhft.chronicle.wire.Wire) NotNull(org.jetbrains.annotations.NotNull)

Example 23 with Wire

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

the class Stackoveflow52274284Test method fails.

@Test
public void fails() throws IOException {
    String basePath = OS.getTarget();
    String path = Files.createTempDirectory(Paths.get(basePath), "chronicle-").toAbsolutePath().toString();
    try (ChronicleQueue chronicleQueue = ChronicleQueue.singleBuilder(path).testBlockSize().build()) {
        // Create Appender
        ExcerptAppender appender = chronicleQueue.acquireAppender();
        // Create Tailer
        ExcerptTailer tailer = chronicleQueue.createTailer();
        tailer.toStart();
        int numberOfRecords = 10;
        // Write
        for (int i = 0; i <= numberOfRecords; i++) {
            // System.out.println("Writing " + i);
            try (final DocumentContext dc = appender.writingDocument()) {
                dc.wire().write("msg").text("Hello World!");
            // System.out.println("your data was store to index=" + dc.index());
            } catch (Exception e) {
                System.err.println("Unable to store value to chronicle");
                e.printStackTrace();
            }
        }
        // Read
        for (int i = 0; i <= numberOfRecords; i++) {
            // System.out.println("Reading " + i);
            try (DocumentContext documentContext = tailer.readingDocument()) {
                long currentOffset = documentContext.index();
                // System.out.println("Current offset: " + currentOffset);
                Wire wire = documentContext.wire();
                if (wire != null) {
                    String msg = wire.read("msg").text();
                // System.out.println(msg);
                }
            }
        }
    }
}
Also used : DocumentContext(net.openhft.chronicle.wire.DocumentContext) Wire(net.openhft.chronicle.wire.Wire) IOException(java.io.IOException) Test(org.junit.Test)

Example 24 with Wire

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

the class IndexForIDTest method applyFlyweight.

private static void applyFlyweight(Facade datum, long datumSize, DocumentContext dc) {
    Wire wire = dc.wire();
    Bytes<?> bytes = wire.bytes();
    datum.bytesStore(bytes, bytes.readPosition(), datumSize);
}
Also used : Wire(net.openhft.chronicle.wire.Wire)

Example 25 with Wire

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

the class PeekDocumentTest method testReadWrite10.

@Test
public void testReadWrite10() {
    File tempDir = getTmpDir();
    try {
        try (SingleChronicleQueue queue = SingleChronicleQueueBuilder.binary(tempDir).build()) {
            ExcerptAppender appender = queue.acquireAppender();
            for (int i = 0; i < 1024; i++) {
                try (DocumentContext documentContext = appender.writingDocument()) {
                    documentContext.wire().write("value").text("hello" + i);
                }
            }
            ExcerptTailer tailer = queue.createTailer();
            for (int i = 0; i < 1024; i++) {
                assertTrue(tailer.peekDocument());
                try (DocumentContext documentContext = tailer.readingDocument()) {
                    assertTrue(documentContext.isPresent());
                    assertTrue(tailer.peekDocument());
                    Wire wire = documentContext.wire();
                    long l = wire.bytes().readPosition();
                    try {
                        assertEquals("hello" + i, wire.read("value").text());
                    } finally {
                        // simulate if the message was read
                        if (l % 2 == 0)
                            wire.bytes().readPosition(l);
                    }
                }
            }
            assertFalse(tailer.peekDocument());
            try (DocumentContext documentContext = appender.writingDocument()) {
                documentContext.wire().write("value").text("hello" + 10);
            }
            assertTrue(tailer.peekDocument());
        }
    } finally {
        tempDir.deleteOnExit();
    }
}
Also used : SingleChronicleQueue(net.openhft.chronicle.queue.impl.single.SingleChronicleQueue) DocumentContext(net.openhft.chronicle.wire.DocumentContext) Wire(net.openhft.chronicle.wire.Wire) File(java.io.File) Test(org.junit.Test)

Aggregations

Wire (net.openhft.chronicle.wire.Wire)33 DocumentContext (net.openhft.chronicle.wire.DocumentContext)22 Test (org.junit.Test)11 File (java.io.File)9 ExcerptAppender (net.openhft.chronicle.queue.ExcerptAppender)8 NotNull (org.jetbrains.annotations.NotNull)8 Bytes (net.openhft.chronicle.bytes.Bytes)7 MappedBytes (net.openhft.chronicle.bytes.MappedBytes)7 ExcerptTailer (net.openhft.chronicle.queue.ExcerptTailer)6 IOException (java.io.IOException)5 ChronicleQueue (net.openhft.chronicle.queue.ChronicleQueue)5 BytesStore (net.openhft.chronicle.bytes.BytesStore)3 SingleChronicleQueue (net.openhft.chronicle.queue.impl.single.SingleChronicleQueue)3 Assert (org.junit.Assert)3 Ignore (org.junit.Ignore)3 LocalDateTime (java.time.LocalDateTime)2 Arrays (java.util.Arrays)2 Collection (java.util.Collection)2 Jvm (net.openhft.chronicle.core.Jvm)2 OS (net.openhft.chronicle.core.OS)2