Search in sources :

Example 26 with ExcerptAppender

use of net.openhft.chronicle.queue.ExcerptAppender in project Chronicle-Queue by OpenHFT.

the class QueueWireHandler method onEvent.

@SuppressWarnings("UnusedReturnValue")
void onEvent() throws IOException {
    // Be careful not to use Wires.acquireStringBuilder() as we
    // need to store the value
    inWire.readDocument(w -> {
        w.read(CoreFields.csp).text(cspText).read(CoreFields.tid).int64(x -> tid = x).read(CoreFields.cid).int64(x -> cid = x);
        queue = getQueue(cspText);
    }, dataWireIn -> {
        ValueIn vin = inWire.readEventName(eventName);
        try {
            // writes out the tid
            outWire.writeDocument(true, wire -> outWire.write(CoreFields.tid).int64(tid));
            if (EventId.lastWrittenIndex.contentEquals(eventName)) {
                writeData(wireOut -> wireOut.write(CoreFields.reply).int64(queue.lastWrittenIndex()));
            } else if (EventId.createAppender.contentEquals(eventName)) {
                // only need one appender per queue
                queueToAppender.computeIfAbsent(queue, s -> {
                    try {
                        return queue.createAppender();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                    return null;
                });
                outWire.writeDocument(false, wireOut -> {
                    QueueAppenderResponse qar = new QueueAppenderResponse();
                    qar.setCid(cid);
                    qar.setCsp(cspText);
                    wireOut.write(CoreFields.reply).typedMarshallable(qar);
                });
            } else if (EventId.submit.contentEquals(eventName)) {
                ExcerptAppender appender = queueToAppender.get(queue);
                appender.writeDocument(wo -> wo.bytes().write(vin.bytes()));
                outWire.writeDocument(false, wire -> wire.write(EventId.index).int64(appender.lastWrittenIndex()));
            } else if (EventId.createTailer.contentEquals(eventName)) {
                // only need one appender per queue
                queueToTailer.computeIfAbsent(queue, s -> {
                    try {
                        return queue.createTailer();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                    return null;
                });
                outWire.writeDocument(false, wireOut -> {
                    QueueTailerResponse qar = new QueueTailerResponse();
                    qar.setCid(cid);
                    qar.setCsp(cspText);
                    wireOut.write(CoreFields.reply).typedMarshallable(qar);
                });
            } else if (EventId.hasNext.contentEquals(eventName)) {
                ExcerptTailer tailer = queueToTailer.get(queue);
                vin.marshallable((ReadMarshallable) rm -> {
                    long index = rm.read(() -> "index").int64();
                    sendBackMessage(tailer, index);
                });
            }
        } finally {
            if (EventGroup.IS_DEBUG) {
                long len = outWire.bytes().position() - SIZE_OF_SIZE;
                if (len == 0) {
                    System.out.println("--------------------------------------------\n" + "server writes:\n\n<EMPTY>");
                } else {
                    System.out.println("--------------------------------------------\n" + "server writes:\n\n" + Wires.fromSizePrefixedBlobs(outWire.bytes(), SIZE_OF_SIZE, len));
                }
            }
        }
    });
}
Also used : ExcerptTailer(net.openhft.chronicle.queue.ExcerptTailer) Logger(org.slf4j.Logger) StreamCorruptedException(java.io.StreamCorruptedException) EventGroup(net.openhft.chronicle.network.event.EventGroup) ChronicleQueue(net.openhft.chronicle.queue.ChronicleQueue) ChronicleQueueBuilder(net.openhft.chronicle.queue.ChronicleQueueBuilder) LoggerFactory(org.slf4j.LoggerFactory) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) IOException(java.io.IOException) Consumer(java.util.function.Consumer) Bytes(net.openhft.chronicle.bytes.Bytes) EventId(net.openhft.chronicle.engine.client.internal.ClientWiredChronicleQueueStateless.EventId) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) net.openhft.chronicle.wire(net.openhft.chronicle.wire) Map(java.util.Map) ClientWiredStatelessTcpConnectionHub(net.openhft.chronicle.engine.client.ClientWiredStatelessTcpConnectionHub) ExcerptAppender(net.openhft.chronicle.queue.ExcerptAppender) ExcerptAppender(net.openhft.chronicle.queue.ExcerptAppender) IOException(java.io.IOException) ExcerptTailer(net.openhft.chronicle.queue.ExcerptTailer)

Example 27 with ExcerptAppender

use of net.openhft.chronicle.queue.ExcerptAppender in project Chronicle-Queue by OpenHFT.

the class SingleChronicleQueueTest method testScanFromLastKnownIndex.

@Test
public void testScanFromLastKnownIndex() {
    final File file = createTempFile("testScanFromLastKnownIndex");
    try {
        final SingleChronicleQueue chronicle = (SingleChronicleQueue) createQueue(file);
        final ExcerptAppender appender = chronicle.acquireAppender();
        // create 100 documents
        for (int i = 0; i < 65; i++) {
            final int j = i;
            appender.writeDocument(wire -> wire.write(() -> "key").text("value=" + j));
        }
        // creates the indexes - index's 1 and 2 are created by the indexer
        new Indexer(chronicle).index();
        // create 100 documents
        for (long i = chronicle.lastIndex() + 1; i < 200; i++) {
            final long j = i;
            appender.writeDocument(wire -> wire.write(() -> "key").text("value=" + j));
        }
        final ExcerptTailer tailer = chronicle.createTailer();
        {
            int expected = 150;
            tailer.index(expected);
            StringBuilder sb = new StringBuilder();
            tailer.readDocument(wire -> wire.read(() -> "key").text(sb));
            Assert.assertEquals("value=" + expected, sb.toString());
        }
        // read back earlier
        {
            int expected = 167;
            tailer.index(expected);
            StringBuilder sb = new StringBuilder();
            tailer.readDocument(wire -> wire.read(() -> "key").text(sb));
            Assert.assertEquals("value=" + expected, sb.toString());
        }
    } finally {
        file.delete();
    }
}
Also used : BinaryWire(net.openhft.chronicle.wire.BinaryWire) ExcerptTailer(net.openhft.chronicle.queue.ExcerptTailer) Arrays(java.util.Arrays) ChronicleQueue(net.openhft.chronicle.queue.ChronicleQueue) ChronicleQueueBuilder(net.openhft.chronicle.queue.ChronicleQueueBuilder) Collection(java.util.Collection) RunWith(org.junit.runner.RunWith) Test(org.junit.Test) IOException(java.io.IOException) Wire(net.openhft.chronicle.wire.Wire) File(java.io.File) TextWire(net.openhft.chronicle.wire.TextWire) ExcerptAppender(net.openhft.chronicle.queue.ExcerptAppender) Assert(org.junit.Assert) Parameterized(org.junit.runners.Parameterized) ExcerptAppender(net.openhft.chronicle.queue.ExcerptAppender) File(java.io.File) ExcerptTailer(net.openhft.chronicle.queue.ExcerptTailer) Test(org.junit.Test)

Example 28 with ExcerptAppender

use of net.openhft.chronicle.queue.ExcerptAppender in project Chronicle-Queue by OpenHFT.

the class SingleChronicleQueueTest method testReadAtIndexWithIndexesAtStart.

@Test
public void testReadAtIndexWithIndexesAtStart() {
    final File file = createTempFile("testReadAtIndexWithIndexesAtStart");
    try {
        final SingleChronicleQueue chronicle = (SingleChronicleQueue) createQueue(file);
        final ExcerptAppender appender = chronicle.acquireAppender();
        // create 100 documents
        for (int i = 0; i < 100; i++) {
            final int j = i;
            appender.writeDocument(wire -> wire.write(() -> "key").text("value=" + j));
        }
        new Indexer(chronicle).index();
        long index = 67;
        final ExcerptTailer tailer = chronicle.createTailer();
        tailer.index(index);
        StringBuilder sb = new StringBuilder();
        tailer.readDocument(wire -> wire.read(() -> "key").text(sb));
        Assert.assertEquals("value=" + index, sb.toString());
    } finally {
        file.delete();
    }
}
Also used : ExcerptAppender(net.openhft.chronicle.queue.ExcerptAppender) File(java.io.File) ExcerptTailer(net.openhft.chronicle.queue.ExcerptTailer) Test(org.junit.Test)

Example 29 with ExcerptAppender

use of net.openhft.chronicle.queue.ExcerptAppender in project Chronicle-Queue by OpenHFT.

the class SingleChronicleQueueTest method testSingleDirect.

@Test
public void testSingleDirect() {
    final File file = createTempFile("testSingleDirect");
    try {
        final DirectChronicleQueue chronicle = createQueue(file);
        final ExcerptAppender appender = chronicle.acquireAppender();
        // create 100 documents
        for (int i = 0; i < 100; i++) {
            final int j = i;
            appender.writeDocument(wire -> wire.write(() -> "key").text("value=" + j));
        }
        final ExcerptTailer tailer = chronicle.createTailer();
        final StringBuilder sb = new StringBuilder();
        for (int j = 0; j < chronicle.lastIndex(); j++) {
            sb.setLength(0);
            tailer.readDocument(wire -> wire.read(() -> "key").text(sb));
            Assert.assertEquals("value=" + j, sb.toString());
        }
    } finally {
        file.delete();
    }
}
Also used : ExcerptAppender(net.openhft.chronicle.queue.ExcerptAppender) File(java.io.File) ExcerptTailer(net.openhft.chronicle.queue.ExcerptTailer) Test(org.junit.Test)

Example 30 with ExcerptAppender

use of net.openhft.chronicle.queue.ExcerptAppender in project Chronicle-Queue by OpenHFT.

the class SingleChronicleQueueTest method testLastWrittenIndexPerAppender.

@Test
public void testLastWrittenIndexPerAppender() {
    final File file = createTempFile("testLastWrittenIndexPerAppender");
    try {
        final ChronicleQueue chronicle = createQueue(file);
        final ExcerptAppender appender = chronicle.acquireAppender();
        appender.writeDocument(wire -> wire.write(() -> "key").text("test"));
        Assert.assertEquals(0, appender.lastWrittenIndex());
    } finally {
        file.delete();
    }
}
Also used : ChronicleQueue(net.openhft.chronicle.queue.ChronicleQueue) ExcerptAppender(net.openhft.chronicle.queue.ExcerptAppender) File(java.io.File) Test(org.junit.Test)

Aggregations

ExcerptAppender (net.openhft.chronicle.queue.ExcerptAppender)63 Test (org.junit.Test)55 ExcerptTailer (net.openhft.chronicle.queue.ExcerptTailer)34 File (java.io.File)33 DocumentContext (net.openhft.chronicle.wire.DocumentContext)28 ChronicleQueue (net.openhft.chronicle.queue.ChronicleQueue)21 MappedFile (net.openhft.chronicle.bytes.MappedFile)15 SingleChronicleQueue (net.openhft.chronicle.queue.impl.single.SingleChronicleQueue)6 SetTimeProvider (net.openhft.chronicle.core.time.SetTimeProvider)5 RollingChronicleQueue (net.openhft.chronicle.queue.impl.RollingChronicleQueue)5 Ignore (org.junit.Ignore)5 IOException (java.io.IOException)4 ArrayList (java.util.ArrayList)4 ValueOut (net.openhft.chronicle.wire.ValueOut)4 Wire (net.openhft.chronicle.wire.Wire)4 Arrays (java.util.Arrays)3 Collection (java.util.Collection)3 ExecutorService (java.util.concurrent.ExecutorService)3 Bytes (net.openhft.chronicle.bytes.Bytes)3 ChronicleQueueTestBase (net.openhft.chronicle.queue.ChronicleQueueTestBase)3