Search code examples
chronicle-queue

What is the best way to skip a document in Chronicle Queue?


When reading a document from chronicle queue, what is the best way to skip the current document when I am not interested in it?

I.e. given the following code, what do I put instead of the comment?

try (DocumentContext context = getTailer().readingDocument(false)) {
    if (context.isPresent()) {
        // How do I skip the document here?
    }
}

Solution

  • Well I'd assume you at least need to put some code that will prove that you are indeed not interested in this particular document. Otherwise - you just put nothing, when the document context is closed - the document is skipped.

    However if you know that you just read a document which tells you that next document should be skipped, and you feel adventurous, you can do this:

    @Test
    public void testSkip() {
        final SingleChronicleQueue q = SingleChronicleQueueBuilder.binary("test").build();
    
        final ExcerptAppender appender = q.acquireAppender();
        appender.writeText("Hello");
        appender.writeText("Cruel");
        appender.writeText("World");
    
        final ExcerptTailer tailer = q.createTailer().toStart();
    
        boolean skip = false;
        while (true) {
            if (skip)
                tailer.moveToIndex(tailer.index() + 1);
            final String text = tailer.readText();
            if (text == null)
                break;
            System.err.println(text);
            skip = text.equals("Hello");
        }
    }