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));
}
}
}
});
}
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();
}
}
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();
}
}
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();
}
}
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();
}
}
Aggregations