use of net.openhft.chronicle.bytes.Bytes in project Chronicle-Queue by OpenHFT.
the class StoreAppender method prepareAndReturnWriteContext.
private StoreAppender.StoreAppenderContext prepareAndReturnWriteContext(boolean metaData) {
if (count > 1) {
assert metaData == writeContext.metaData;
return writeContext;
}
if (queue.doubleBuffer && writeLock.locked() && !metaData) {
writeContext.isClosed = false;
writeContext.rollbackOnClose = false;
writeContext.buffered = true;
if (bufferWire == null) {
Bytes bufferBytes = Bytes.allocateElasticOnHeap();
bufferWire = queue().wireType().apply(bufferBytes);
}
writeContext.wire = bufferWire;
writeContext.metaData(false);
} else {
writeLock.lock();
int cycle = queue.cycle();
if (wire == null)
setWireIfNull(cycle);
if (this.cycle != cycle)
rollCycleTo(cycle);
long safeLength = queue.overlapSize();
resetPosition();
assert !QueueSystemProperties.CHECK_INDEX || checkWritePositionHeaderNumber();
// sets the writeLimit based on the safeLength
openContext(metaData, safeLength);
// Move readPosition to the start of the context. i.e. readRemaining() == 0
wire.bytes().readPosition(wire.bytes().writePosition());
}
return writeContext;
}
use of net.openhft.chronicle.bytes.Bytes 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.bytes.Bytes 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.bytes.Bytes 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.bytes.Bytes 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