use of net.openhft.chronicle.queue.impl.single.InternalAppender in project Chronicle-Queue by OpenHFT.
the class CreateAtIndexTest method testWriteBytesWithIndex.
@Test
public void testWriteBytesWithIndex() {
final Bytes HELLO_WORLD = Bytes.from("hello world");
File tmp = getTmpDir();
try (ChronicleQueue queue = single(tmp).testBlockSize().rollCycle(TEST_DAILY).build()) {
InternalAppender appender = (InternalAppender) queue.acquireAppender();
appender.writeBytes(0x421d00000000L, HELLO_WORLD);
appender.writeBytes(0x421d00000001L, HELLO_WORLD);
}
try (ChronicleQueue queue = single(tmp).testBlockSize().build()) {
InternalAppender appender = (InternalAppender) queue.acquireAppender();
String before = queue.dump();
appender.writeBytes(0x421d00000000L, HELLO_WORLD);
String after = queue.dump();
assertEquals(before, after);
}
// try too far
try (ChronicleQueue queue = single(tmp).testBlockSize().build()) {
InternalAppender appender = (InternalAppender) queue.acquireAppender();
try {
appender.writeBytes(0x421d00000003L, HELLO_WORLD);
fail();
} catch (IllegalStateException e) {
assertTrue(e.getMessage().startsWith("Unable to move to index 421d00000003 beyond the end of the queue"));
}
}
try (ChronicleQueue queue = single(tmp).testBlockSize().build()) {
InternalAppender appender = (InternalAppender) queue.acquireAppender();
appender.writeBytes(0x421d00000002L, HELLO_WORLD);
appender.writeBytes(0x421d00000003L, HELLO_WORLD);
}
try {
IOTools.deleteDirWithFiles(tmp, 2);
} catch (IORuntimeException ignored) {
}
}
use of net.openhft.chronicle.queue.impl.single.InternalAppender in project Chronicle-Queue by OpenHFT.
the class ChronicleQueueIndexTest method testIndexQueue.
@Test
public void testIndexQueue() {
File file1 = getTmpDir();
file1.deleteOnExit();
try (ChronicleQueue queue = SingleChronicleQueueBuilder.builder().path(file1).rollCycle(RollCycles.DEFAULT).build()) {
InternalAppender appender = (InternalAppender) queue.acquireAppender();
Bytes<byte[]> hello_world = Bytes.from("Hello World 1");
appender.writeBytes(RollCycles.DEFAULT.toIndex(18264, 0L), hello_world);
hello_world.releaseLast();
hello_world = Bytes.from("Hello World 2");
appender.writeBytes(RollCycles.DEFAULT.toIndex(18264, 1L), hello_world);
hello_world.releaseLast();
// Simulate the end of the day i.e the queue closes the day rolls
// (note the change of index from 18264 to 18265)
}
try (ChronicleQueue queue = SingleChronicleQueueBuilder.builder().path(file1).rollCycle(RollCycles.DEFAULT).build()) {
InternalAppender appender = (InternalAppender) queue.acquireAppender();
// add a message for the new day
Bytes<byte[]> hello_world = Bytes.from("Hello World 3");
appender.writeBytes(RollCycles.DEFAULT.toIndex(18265, 0L), hello_world);
hello_world.releaseLast();
final ExcerptTailer tailer = queue.createTailer();
final Bytes<?> forRead = Bytes.elasticByteBuffer();
try {
final List<String> results = new ArrayList<>();
while (tailer.readBytes(forRead)) {
results.add(forRead.to8bitString());
forRead.clear();
}
assertTrue(results.toString(), results.contains("Hello World 1"));
assertTrue(results.contains("Hello World 2"));
// The reader fails to read the third message. The reason for this is
// that there was no EOF marker placed at end of the 18264 indexed file
// so when the reader started reading through the queues it got stuck on
// that file and never progressed to the latest queue file.
assertTrue(results.contains("Hello World 3"));
} finally {
forRead.releaseLast();
}
}
}
use of net.openhft.chronicle.queue.impl.single.InternalAppender in project Chronicle-Queue by OpenHFT.
the class InternalAppenderWriteBytesTest method test3.
@Test
public void test3() {
@NotNull Bytes<byte[]> test = Bytes.from("hello world");
Bytes result = Bytes.elasticHeapByteBuffer();
try (SingleChronicleQueue q = SingleChronicleQueueBuilder.binary(getTmpDir()).timeProvider(() -> 0).build()) {
ExcerptAppender appender = q.acquireAppender();
appender.writeBytes(test);
ExcerptTailer tailer = q.createTailer();
expectException("Trying to overwrite index 0 which is before the end of the queue");
((InternalAppender) appender).writeBytes(0, test);
try (DocumentContext documentContext = tailer.readingDocument()) {
result.write(documentContext.wire().bytes());
}
Assert.assertTrue("hello world".contentEquals(result));
assertEquals(1, tailer.index());
result.clear();
((InternalAppender) appender).writeBytes(1, test);
try (DocumentContext dc = tailer.readingDocument()) {
dc.rollbackOnClose();
}
assertEquals(1, tailer.index());
((InternalAppender) appender).writeBytes(2, test);
}
}
use of net.openhft.chronicle.queue.impl.single.InternalAppender in project Chronicle-Queue by OpenHFT.
the class InternalAppenderWriteBytesTest method dontOverwriteExisting.
@Test
public void dontOverwriteExisting() {
@NotNull Bytes<byte[]> test = Bytes.from("hello world");
Bytes result = Bytes.elasticHeapByteBuffer();
try (SingleChronicleQueue q = SingleChronicleQueueBuilder.binary(getTmpDir()).timeProvider(() -> 0).build()) {
ExcerptAppender appender = q.acquireAppender();
appender.writeBytes(test);
expectException("Trying to overwrite index 0 which is before the end of the queue");
// try to overwrite - will not overwrite
((InternalAppender) appender).writeBytes(0, Bytes.from("HELLO WORLD"));
ExcerptTailer tailer = q.createTailer();
tailer.readBytes(result);
assertEquals(test, result);
assertEquals(1, tailer.index());
}
}
use of net.openhft.chronicle.queue.impl.single.InternalAppender in project Chronicle-Queue by OpenHFT.
the class InternalAppenderWriteBytesTest method writeJustAfterLastIndex.
@Test
public void writeJustAfterLastIndex() {
@NotNull Bytes<byte[]> test = Bytes.from("hello world");
@NotNull Bytes<byte[]> test2 = Bytes.from("hello world again");
Bytes result = Bytes.elasticHeapByteBuffer();
try (SingleChronicleQueue q = SingleChronicleQueueBuilder.binary(getTmpDir()).timeProvider(() -> 0).build()) {
ExcerptAppender appender = q.acquireAppender();
// write at index 0
appender.writeBytes(test);
// append at index 1
((InternalAppender) appender).writeBytes(1, test2);
ExcerptTailer tailer = q.createTailer();
tailer.readBytes(result);
assertEquals(test, result);
result.clear();
tailer.readBytes(result);
assertEquals(test2, result);
result.clear();
}
}
Aggregations