use of net.openhft.chronicle.queue.ExcerptTailer in project Chronicle-Queue by OpenHFT.
the class StoreTailerTest method shouldHandleCycleRollWhenInReadOnlyMode.
@Test
public void shouldHandleCycleRollWhenInReadOnlyMode() throws Exception {
final MutableTimeProvider timeProvider = new MutableTimeProvider();
final SingleChronicleQueue queue = build(createQueue(dataDirectory, RollCycles.MINUTELY, 0, "cycleRoll", false).timeProvider(timeProvider));
final StringEvents events = queue.acquireAppender().methodWriterBuilder(StringEvents.class).build();
timeProvider.setTime(System.currentTimeMillis());
events.onEvent("firstEvent");
timeProvider.addTime(2, TimeUnit.MINUTES);
events.onEvent("secondEvent");
final SingleChronicleQueue readerQueue = build(createQueue(dataDirectory, RollCycles.MINUTELY, 0, "cycleRoll", true).timeProvider(timeProvider));
final ExcerptTailer tailer = readerQueue.createTailer();
tailer.toStart();
try (final DocumentContext context = tailer.readingDocument()) {
assertThat(context.isPresent(), is(true));
}
tailer.toEnd();
try (final DocumentContext context = tailer.readingDocument()) {
assertThat(context.isPresent(), is(false));
}
}
use of net.openhft.chronicle.queue.ExcerptTailer in project Chronicle-Queue by OpenHFT.
the class TestBinarySearch method testBinarySearch.
@Test
public void testBinarySearch() throws ParseException {
final SetTimeProvider stp = new SetTimeProvider();
long time = 0;
stp.currentTimeMillis(time);
final File tmpDir = getTmpDir();
try (SingleChronicleQueue queue = SingleChronicleQueueBuilder.binary(getTmpDir()).rollCycle(RollCycles.TEST_SECONDLY).timeProvider(stp).build()) {
final ExcerptAppender appender = queue.acquireAppender();
for (int i = 0; i < MAX_NUMBER_OF_TESTED_MESSAGES; i++) {
try (final DocumentContext dc = appender.writingDocument()) {
final MyData myData = new MyData();
myData.key = i;
myData.value = "some value where the key=" + String.valueOf(i);
dc.wire().getValueOut().typedMarshallable(myData);
time += 300;
stp.currentTimeMillis(time);
}
}
for (int j = 0; j < MAX_NUMBER_OF_TESTED_MESSAGES; j++) {
Wire key = toWire(j);
final Comparator<Wire> comparator = (o1, o2) -> {
final long readPositionO1 = o1.bytes().readPosition();
final long readPositionO2 = o2.bytes().readPosition();
try {
MyData myDataO1 = null;
MyData myDataO2 = null;
try (final DocumentContext dc = o1.readingDocument()) {
myDataO1 = dc.wire().getValueIn().typedMarshallable();
assert myDataO1.value != null;
}
try (final DocumentContext dc = o2.readingDocument()) {
myDataO2 = dc.wire().getValueIn().typedMarshallable();
assert myDataO2.value != null;
}
final int compare = Integer.compare(myDataO1.key, myDataO2.key);
return compare;
} finally {
o1.bytes().readPosition(readPositionO1);
o2.bytes().readPosition(readPositionO2);
}
};
long index = BinarySearch.search(queue, key, comparator);
// assert index != -1 : "i=" + j;
final ExcerptTailer tailer = queue.createTailer();
tailer.moveToIndex(index);
try (final DocumentContext documentContext = tailer.readingDocument()) {
Assert.assertTrue(documentContext.toString().contains("some value where the key=" + j));
}
key.bytes().release();
}
} finally {
System.gc();
deleteDir(tmpDir);
}
}
use of net.openhft.chronicle.queue.ExcerptTailer in project Chronicle-Queue by OpenHFT.
the class TestWriteWhenCurrentCycleGotEOF method createQueueWithOnlyHeaderFile.
private void createQueueWithOnlyHeaderFile(File dir) {
SingleChronicleQueue queue = SingleChronicleQueueBuilder.binary(dir).testBlockSize().build();
queue.storeForCycle(queue.cycle(), queue.epoch(), true);
ExcerptTailer tailer = queue.acquireTailer();
try (DocumentContext dc = tailer.readingDocument()) {
assertFalse(dc.isPresent());
}
Wire wire;
ExcerptAppender excerptAppender = queue.acquireAppender();
try (DocumentContext dc = excerptAppender.writingDocument()) {
wire = dc.wire();
}
// overwrite last record with EOF
Bytes<?> bytes = wire.bytes();
bytes.writeVolatileInt(bytes.writePosition() - 5, Wires.END_OF_DATA);
bytes.writeVolatileInt(bytes.writePosition() - 1, 0);
}
use of net.openhft.chronicle.queue.ExcerptTailer in project Chronicle-Queue by OpenHFT.
the class ToEndTest method toEndTest.
@Test
public void toEndTest() {
File baseDir = DirectoryUtils.tempDir("toEndTest");
List<Integer> results = new ArrayList<>();
final SingleChronicleQueue singleChronicleQueue = null;
try (RollingChronicleQueue queue = SingleChronicleQueueBuilder.binary(baseDir).testBlockSize().rollCycle(RollCycles.TEST_DAILY).indexCount(8).indexSpacing(1).build()) {
checkOneFile(baseDir);
ExcerptAppender appender = queue.acquireAppender();
checkOneFile(baseDir);
for (int i = 0; i < 10; i++) {
final int j = i;
appender.writeDocument(wire -> wire.write(() -> "msg").int32(j));
}
checkOneFile(baseDir);
ExcerptTailer tailer = queue.createTailer();
checkOneFile(baseDir);
ExcerptTailer atEnd = tailer.toEnd();
assertEquals(10, queue.rollCycle().toSequenceNumber(atEnd.index()));
checkOneFile(baseDir);
fillResults(atEnd, results);
checkOneFile(baseDir);
assertEquals(0, results.size());
tailer.toStart();
checkOneFile(baseDir);
fillResults(tailer, results);
assertEquals(10, results.size());
checkOneFile(baseDir);
}
System.gc();
try {
IOTools.shallowDeleteDirWithFiles(baseDir);
} catch (Exception e) {
e.printStackTrace();
}
}
use of net.openhft.chronicle.queue.ExcerptTailer in project Chronicle-Queue by OpenHFT.
the class ToEndTest method toEndAfterWriteTest.
@Test
public void toEndAfterWriteTest() {
File file = DirectoryUtils.tempDir("toEndAfterWriteTest");
IOTools.shallowDeleteDirWithFiles(file);
final SetTimeProvider stp = new SetTimeProvider();
stp.currentTimeMillis(1470757797000L);
try (RollingChronicleQueue wqueue = SingleChronicleQueueBuilder.binary(file).testBlockSize().rollCycle(RollCycles.TEST_SECONDLY).timeProvider(stp).build()) {
ExcerptAppender appender = wqueue.acquireAppender();
for (int i = 0; i < 10; i++) {
try (DocumentContext dc = appender.writingDocument()) {
dc.wire().write().text("hi-" + i);
lastCycle = wqueue.rollCycle().toCycle(dc.index());
}
stp.currentTimeMillis(stp.currentTimeMillis() + 1000);
}
}
try (ChronicleQueue rqueue = SingleChronicleQueueBuilder.binary(file).testBlockSize().rollCycle(RollCycles.TEST_SECONDLY).timeProvider(stp).build()) {
ExcerptTailer tailer = rqueue.createTailer();
stp.currentTimeMillis(stp.currentTimeMillis() + 1000);
// noinspection StatementWithEmptyBody
while (tailer.readText() != null) ;
assertNull(tailer.readText());
stp.currentTimeMillis(stp.currentTimeMillis() + 1000);
ExcerptTailer tailer1 = rqueue.createTailer();
ExcerptTailer excerptTailer = tailer1.toEnd();
assertNull(excerptTailer.readText());
}
System.gc();
IOTools.shallowDeleteDirWithFiles(file);
}
Aggregations