@Nullable static SingleChronicleQueueStore loadStore(@NotNull Wire wire) { final StringBuilder eventName = new StringBuilder(); wire.readEventName(eventName); if (eventName.toString().equals(MetaDataKeys.header.name())) { final SingleChronicleQueueStore store = wire.read().typedMarshallable(); if (store == null) { throw new IllegalArgumentException("Unable to load wire store"); } return store; } LOGGER.warn("Unable to load store file from input. Queue file may be corrupted."); return null; }
private static MessageHistory readHistoryFromWire(final Wire wire, MessageHistory history) { final StringBuilder sb = SBP.acquireStringBuilder(); ValueIn valueIn = wire.read(sb); if (!MethodReader.HISTORY.contentEquals(sb)) return null; valueIn.object(history, MessageHistory.class); return history; }
@NotNull private List<Integer> fillResults(@NotNull ExcerptTailer tailer, @NotNull List<Integer> results) { for (int i = 0; i < 10; i++) { try (DocumentContext documentContext = tailer.readingDocument()) { if (!documentContext.isPresent()) break; results.add(documentContext.wire().read(() -> "msg").int32()); } } return results; }
private void readDocument(@NotNull ThreadLocal<ExcerptTailer> tlt, String text) { try (DocumentContext dc = tlt.get().readingDocument()) { if (!dc.isPresent()) return; Assert.assertEquals(dc.index(), dc.wire().read(() -> "key").int64()); Assert.assertEquals(text, dc.wire().read(() -> "text").text()); } }
private long action(@NotNull final ExcerptTailer tailer1, @NotNull final RollCycle rollCycle) { // tailer1.direction(direction); long readvalue = 0; long seqNumRead = 0; try (final DocumentContext dc = tailer1.readingDocument()) { readvalue = dc.wire().read("value").int64(); seqNumRead = dc.index(); } final long nextSeq = rollCycle.toSequenceNumber(tailer1.index()); /*System.out.println("Return-value=" + readvalue + ", seq=" + rollCycle.toSequenceNumber(seqNumRead) + ", next-seq=" + nextSeq + "(" + Long.toHexString(nextSeq) + "x0)" + ",direction=" + tailer1.direction());*/ return readvalue; }
@Test public void shouldAllowDirectoryToBeDeletedWhenQueueIsClosed() throws IOException { if (OS.isWindows()) { System.err.println("#460 Cannot test deleting after close on windows"); return; } final File dir = DirectoryUtils.tempDir("to-be-deleted"); try (final ChronicleQueue queue = builder(dir, wireType). testBlockSize().build()) { try (final DocumentContext dc = queue.acquireAppender().writingDocument()) { dc.wire().write().text("foo"); } try (final DocumentContext dc = queue.createTailer().readingDocument()) { assertEquals("foo", dc.wire().read().text()); } } Files.walk(dir.toPath()).forEach(p -> { if (!Files.isDirectory(p)) { assertTrue(p.toString(), p.toFile().delete()); } }); assertTrue(dir.delete()); }
/** * Read next message, forward or backward, depending on the settings of the Tailer * * @param tailer ExcerptTailer * @return entry or null, if no entry available */ private String readNextEntry(@NotNull final ExcerptTailer tailer) { DocumentContext dc = tailer.readingDocument(); try { if (dc.isPresent()) { Object parent = dc.wire().parent(); assert parent == tailer; return dc.wire().read().text(); } return null; } finally { dc.close(); } }
private long doRead(@NotNull ExcerptTailer tailer, int expected) { int[] i = {0}; long t_index = 0; while (true) { try (DocumentContext dc = tailer.readingDocument()) { if (!dc.isPresent()) break; t_index = tailer.index(); dc.wire().read("log").marshallable(m -> { String msg = m.read("msg").text(); assertNotNull(msg); System.out.println("msg:" + msg); i[0]++; }); } } assertEquals(expected, i[0]); return t_index; }
@Test public void testDtoAbstractMarshallable() { File tmp = DirectoryUtils.tempDir("abstractBytesMarshalTest"); DtoAbstractMarshallable dto = new DtoAbstractMarshallable(); dto.age = 45; dto.name.append("rob"); try (ChronicleQueue q = ChronicleQueue.singleBuilder(tmp).build()) { try (DocumentContext dc = q.acquireAppender().writingDocument()) { dc.wire().write("who").object(dto); } try (DocumentContext dc = q.createTailer().readingDocument()) { String yaml = dc.toString(); System.out.println(yaml); DtoAbstractMarshallable who = (DtoAbstractMarshallable) dc.wire().read("who").object(); System.out.println(who); Assert.assertTrue(yaml.contains(who.toString())); } } }
private static long doReadBad(@NotNull ExcerptTailer tailer, int expected, boolean additionalClose) { int[] i = {0}; long t_index = 0; while (true) { try (DocumentContext dc = tailer.readingDocument()) { if (!dc.isPresent()) break; t_index = tailer.index(); dc.wire().read("log").marshallable(m -> { String msg = m.read("msg").text(); assertNotNull(msg); i[0]++; }); if (additionalClose) { dc.close(); } } } assertEquals(expected, i[0]); return t_index; }
@Test public void testMapWrapper() { try (ChronicleQueue syncQ = builder(getTmpDir(), this.wireType) .build()) { File name2 = DirectoryUtils.tempDir(testName.getMethodName()); try (ChronicleQueue chronicle = builder(name2, this.wireType) .build()) { ExcerptAppender appender = chronicle.acquireAppender(); MapWrapper myMap = new MapWrapper(); myMap.map.put("hello", 1.2); appender.writeDocument(w -> w.write().object(myMap)); ExcerptTailer tailer = chronicle.createTailer(); try (DocumentContext documentContext = tailer.readingDocument()) { MapWrapper object = documentContext.wire().read().object(MapWrapper.class); Assert.assertEquals(1.2, object.map.get("hello"), 0.0); } } } }
@Test public void testDtoBytesMarshallable() { File tmp = DirectoryUtils.tempDir("abstractBytesMarshalTest"); DtoBytesMarshallable dto = new DtoBytesMarshallable(); dto.age = 45; dto.name.append("rob"); try (ChronicleQueue q = ChronicleQueue.singleBuilder(tmp).build()) { try (DocumentContext dc = q.acquireAppender().writingDocument()) { dc.wire().write("who").object(dto); } try (DocumentContext dc = q.createTailer().readingDocument()) { DtoBytesMarshallable who = (DtoBytesMarshallable) dc.wire().read("who").object(); Assert.assertEquals("!net.openhft.chronicle.queue.DtoBytesMarshallableTest$DtoBytesMarshallable {\n" + " name: rob,\n" + " age: 45\n" + "}\n", who.toString()); } } }
@Test public void checkReferenceCountingAndCheckFileDeletion() { MappedFile mappedFile; try (ChronicleQueue queue = binary(getTmpDir()) .rollCycle(RollCycles.TEST_SECONDLY) .build()) { ExcerptAppender appender = queue.acquireAppender(); try (DocumentContext documentContext1 = appender.writingDocument()) { documentContext1.wire().write().text("some text"); } try (DocumentContext documentContext = queue.createTailer().readingDocument()) { mappedFile = toMappedFile(documentContext); Assert.assertEquals("some text", documentContext.wire().read().text()); } } waitFor(mappedFile::isClosed, "mappedFile is not closed"); if (OS.isWindows()) { System.err.println("#460 Cannot test delete after close on windows"); return; } // this used to fail on windows Assert.assertTrue(mappedFile.file().delete()); }
@Test public void testReadingSecondDocumentNotExistIncludingMeta() { try (final ChronicleQueue chronicle = builder(getTmpDir(), this.wireType) .build()) { final ExcerptAppender appender = chronicle.acquireAppender(); try (DocumentContext dc = appender.writingDocument()) { dc.wire().write(() -> "FirstName").text("Quartilla"); } final ExcerptTailer tailer = chronicle.createTailer(); StringBuilder event = new StringBuilder(); while (true) { try (DocumentContext dc = tailer.readingDocument(true)) { ValueIn in = dc.wire().read(event); if (!StringUtils.isEqual(event, "FirstName")) continue; in.text("Quartilla", Assert::assertEquals); break; } } try (DocumentContext dc = tailer.readingDocument()) { assertFalse(dc.isPresent()); } } }
@Test public void testReadingWritingMarshallableDocument() { try (final ChronicleQueue chronicle = builder(getTmpDir(), this.wireType) .build()) { MyMarshable myMarshable = new MyMarshable(); final ExcerptAppender appender = chronicle.acquireAppender(); try (DocumentContext dc = appender.writingDocument()) { dc.wire().write("myMarshable").typedMarshallable(myMarshable); } ExcerptTailer tailer = chronicle.createTailer(); try (DocumentContext dc = tailer.readingDocument()) { Assert.assertEquals(myMarshable, dc.wire().read(() -> "myMarshable").typedMarshallable()); } } }
@Test public void testReadWriteHourly() { File tmpDir = getTmpDir(); try (final ChronicleQueue qAppender = builder(tmpDir, wireType).rollCycle(HOURLY).build()) { try (DocumentContext documentContext = qAppender.acquireAppender().writingDocument()) { documentContext.wire().write("somekey").text("somevalue"); } } try (final ChronicleQueue qTailer = builder(tmpDir, wireType).rollCycle(HOURLY).build()) { try (DocumentContext documentContext2 = qTailer.createTailer().readingDocument()) { String str = documentContext2.wire().read("somekey").text(); Assert.assertEquals("somevalue", str); } } }
@Test public void testReadingSecondDocumentNotExist() { try (final ChronicleQueue chronicle = builder(getTmpDir(), this.wireType) .build()) { final ExcerptAppender appender = chronicle.acquireAppender(); try (DocumentContext dc = appender.writingDocument()) { dc.wire().write(() -> "FirstName").text("Quartilla"); } final ExcerptTailer tailer = chronicle.createTailer(); try (DocumentContext dc = tailer.readingDocument()) { String text = dc.wire().read(() -> "FirstName").text(); Assert.assertEquals("Quartilla", text); } try (DocumentContext dc = tailer.readingDocument()) { assertFalse(dc.isPresent()); } } }
@Test public void testReadingDocumentForEmptyQueue() { File dir = getTmpDir(); try (ChronicleQueue chronicle = builder(dir, this.wireType) .rollCycle(RollCycles.HOURLY) .build()) { ExcerptTailer tailer = chronicle.createTailer(); // DocumentContext is empty as we have no queue and don't know what the wire type will be. try (DocumentContext dc = tailer.readingDocument()) { assertFalse(dc.isPresent()); } try (ChronicleQueue chronicle2 = builder(dir, this.wireType) .rollCycle(RollCycles.HOURLY) .build()) { ExcerptAppender appender = chronicle2.acquireAppender(); appender.writeDocument(w -> w.write(() -> "test - message").text("text")); while (tailer.state() == TailerState.UNINITIALISED) tailer.toStart(); // DocumentContext should not be empty as we know what the wire type will be. try (DocumentContext dc = tailer.readingDocument()) { assertTrue(dc.isPresent()); dc.wire().read(() -> "test - message").text("text", Assert::assertEquals); } } } }
@Test public void testMoveToWithAppender() { try (ChronicleQueue syncQ = builder(getTmpDir(), this.wireType) .build()) { InternalAppender sync = (InternalAppender) syncQ.acquireAppender(); File name2 = DirectoryUtils.tempDir(testName.getMethodName()); try (ChronicleQueue chronicle = builder(name2, this.wireType) .build()) { ExcerptAppender appender = chronicle.acquireAppender(); appender.writeDocument(w -> w.writeEventName("hello").text("world0")); appender.writeDocument(w -> w.writeEventName("hello").text("world1")); appender.writeDocument(w -> w.writeEventName("hello").text("world2")); ExcerptTailer tailer = chronicle.createTailer(); try (DocumentContext documentContext = tailer.readingDocument()) { sync.writeBytes(documentContext.index(), documentContext.wire().bytes()); } try (DocumentContext documentContext = tailer.readingDocument()) { String text = documentContext.wire().read().text(); Assert.assertEquals("world1", text); } } } }
@Test public void testAppendedBeforeToEnd() { File dir = getTmpDir(); try (ChronicleQueue chronicle = builder(dir, this.wireType) .rollCycle(RollCycles.TEST_SECONDLY) .build(); ChronicleQueue chronicle2 = builder(dir, this.wireType) .rollCycle(RollCycles.TEST_SECONDLY) .build()) { ExcerptTailer tailer = chronicle.createTailer(); ExcerptAppender append = chronicle2.acquireAppender(); append.writeDocument(w -> w.write(() -> "test").text("text")); while (tailer.state() == TailerState.UNINITIALISED) tailer.toEnd(); try (DocumentContext dc = tailer.readingDocument()) { assertFalse(tailer.index() + " " + tailer.state(), dc.isPresent()); } append.writeDocument(w -> w.write(() -> "test").text("text2")); try (DocumentContext dc = tailer.readingDocument()) { assertTrue(dc.isPresent()); assertEquals("text2", dc.wire().read("test").text()); } } }