use of net.openhft.chronicle.bytes.Bytes 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);
try (SingleChronicleQueue queue = ChronicleQueue.singleBuilder(getTmpDir()).rollCycle(RollCycles.TEST_SECONDLY).timeProvider(stp).build()) {
final ExcerptAppender appender = queue.acquireAppender();
for (int i = 0; i < numberOfMessages; i++) {
try (final DocumentContext dc = appender.writingDocument()) {
final MyData myData = new MyData();
myData.key = i;
myData.value = "some value where the key=" + i;
dc.wire().getValueOut().typedMarshallable(myData);
time += 300;
stp.currentTimeMillis(time);
}
}
final Comparator<Wire> comparator = (o1, o2) -> {
final long readPositionO1 = o1.bytes().readPosition();
final long readPositionO2 = o2.bytes().readPosition();
try {
final MyData myDataO1;
try (final DocumentContext dc = o1.readingDocument()) {
myDataO1 = dc.wire().getValueIn().typedMarshallable();
}
final MyData myDataO2;
try (final DocumentContext dc = o2.readingDocument()) {
myDataO2 = dc.wire().getValueIn().typedMarshallable();
}
final int compare = Integer.compare(myDataO1.key, myDataO2.key);
return compare;
} finally {
o1.bytes().readPosition(readPositionO1);
o2.bytes().readPosition(readPositionO2);
}
};
try (final ExcerptTailer tailer = queue.createTailer()) {
for (int j = 0; j < numberOfMessages; j++) {
try (DocumentContext ignored = tailer.readingDocument()) {
Wire key = toWire(j);
long index = BinarySearch.search(queue, key, comparator);
Assert.assertEquals(tailer.index(), index);
key.bytes().releaseLast();
}
}
}
Wire key = toWire(numberOfMessages);
Assert.assertTrue("Should not find non-existent", BinarySearch.search(queue, key, comparator) < 0);
}
}
use of net.openhft.chronicle.bytes.Bytes in project Chronicle-Queue by OpenHFT.
the class WriteBytesIndexTest method writeMultipleAppenders.
@Test
public void writeMultipleAppenders() {
File path = IOTools.createTempFile("writeMultipleAppenders");
try (ChronicleQueue q0 = createQueue(path);
ExcerptAppender a0 = q0.acquireAppender();
ExcerptTailer t0 = q0.createTailer();
ChronicleQueue q1 = createQueue(path);
ExcerptAppender a1 = q1.acquireAppender();
ExcerptTailer t1 = q1.createTailer();
ChronicleQueue q2 = createQueue(path)) {
Bytes bytes = Bytes.allocateElasticOnHeap();
Bytes bytes2 = Bytes.allocateElasticOnHeap();
for (int cycle = 1; cycle < 10; cycle++) {
for (int seq = 0; seq < cycle; seq++) {
bytes.clear().append("Msg ").append(String.valueOf(cycle)).append(" ").append(String.valueOf(seq));
long index = q0.rollCycle().toIndex(cycle, seq);
if ((cycle + seq) % 5 < 2) {
((InternalAppender) a0).writeBytes(index, bytes);
}
// try a1
((InternalAppender) a1).writeBytes(index, bytes);
assertTrue(t1.readBytes(bytes2.clear()));
if (!bytes.contentEquals(bytes2)) {
System.out.println(q2.dump());
assertEquals(bytes.toString(), bytes2.toString());
}
assertFalse(t1.readBytes(bytes2.clear()));
assertTrue(t0.readBytes(bytes2.clear()));
if (!bytes.contentEquals(bytes2)) {
System.out.println(q2.dump());
assertEquals(bytes.toString(), bytes2.toString());
}
assertFalse(t0.readBytes(bytes2.clear()));
}
}
}
IOTools.deleteDirWithFiles(path);
}
use of net.openhft.chronicle.bytes.Bytes in project Chronicle-Queue by OpenHFT.
the class TestTailAfterRoll method doTest.
public void doTest() {
File tmpDir = getTmpDir();
File[] files;
try (ChronicleQueue writeQ = ChronicleQueue.singleBuilder(tmpDir).build()) {
ExcerptAppender appender = writeQ.acquireAppender();
long wp;
Wire wire;
try (DocumentContext dc = appender.writingDocument()) {
wire = dc.wire();
wire.write().text("hello world");
Bytes<?> bytes = wire.bytes();
wp = bytes.writePosition();
}
File dir = new File(appender.queue().fileAbsolutePath());
files = dir.listFiles(pathname -> pathname.getAbsolutePath().endsWith(".cq4"));
wire.bytes().writeInt(wp, Wires.END_OF_DATA);
appender.writeText("hello world 2");
}
Assert.assertEquals(1, files.length);
File file = files[0];
file.delete();
try (ChronicleQueue q = ChronicleQueue.singleBuilder(tmpDir).build()) {
ExcerptTailer excerptTailer = q.createTailer().toEnd();
q.acquireAppender().writeText(EXPECTED);
Assert.assertEquals(EXPECTED, excerptTailer.readText());
}
}
use of net.openhft.chronicle.bytes.Bytes in project Chronicle-Queue by OpenHFT.
the class RollCycleTest method testWriteToCorruptedFile.
@Test
public void testWriteToCorruptedFile() {
File dir = DirectoryUtils.tempDir("testWriteToCorruptedFile");
try (SingleChronicleQueue queue = SingleChronicleQueueBuilder.binary(dir).testBlockSize().rollCycle(RollCycles.TEST_DAILY).build()) {
ExcerptAppender appender = queue.acquireAppender();
try (DocumentContext dc = appender.writingDocument()) {
dc.wire().write().text("hello world");
}
Bytes bytes;
long pos;
try (DocumentContext dc = appender.writingDocument()) {
bytes = dc.wire().bytes();
pos = bytes.writePosition() - 4;
}
// write as not complete.
bytes.writeInt(pos, Wires.NOT_COMPLETE_UNKNOWN_LENGTH);
try (DocumentContext dc = appender.writingDocument()) {
dc.wire().write().text("hello world 2");
}
try (DocumentContext dc = appender.writingDocument()) {
dc.wire().write().text("hello world 3");
}
}
}
use of net.openhft.chronicle.bytes.Bytes in project Chronicle-Queue by OpenHFT.
the class SingleCQFormat2Test method appendMessage.
public void appendMessage(@NotNull SingleChronicleQueue queue, long expectedIndex, String msg) {
@NotNull ExcerptAppender appender = queue.acquireAppender().lazyIndexing(lazyIndexing);
switch(appendMode) {
case 1:
appender.writeDocument(w -> w.write(() -> "msg").text(msg));
break;
case 2:
Bytes bytes = Bytes.elasticByteBuffer();
new BinaryWire(bytes).write(() -> "msg").text(msg);
appender.writeBytes(bytes);
bytes.release();
break;
default:
try (DocumentContext dc = appender.writingDocument()) {
Wire wire = dc.wire();
wire.write(() -> "msg").text(msg);
}
break;
}
long index = appender.lastIndexAppended();
assertHexEquals(expectedIndex, index);
}
Aggregations