use of net.openhft.chronicle.wire.Wire in project Chronicle-Queue by OpenHFT.
the class PeekDocumentTest method testReadWrite10Backwards.
@Test
public void testReadWrite10Backwards() {
File tempDir = getTmpDir();
try (SingleChronicleQueue queue = SingleChronicleQueueBuilder.binary(tempDir).build()) {
ExcerptAppender appender = queue.acquireAppender();
try (DocumentContext documentContext = appender.writingDocument()) {
documentContext.wire().write("value").text("hello");
}
ExcerptTailer tailer = queue.createTailer();
assertTrue(tailer.peekDocument());
try (DocumentContext documentContext = tailer.readingDocument()) {
assertTrue(documentContext.isPresent());
assertTrue(tailer.peekDocument());
Wire wire = documentContext.wire();
String result = wire.read("value").text();
assertEquals("hello", result);
// System.out.println(result);
}
assertFalse(tailer.peekDocument());
tailer.direction(TailerDirection.BACKWARD);
assertTrue(tailer.peekDocument());
try (DocumentContext documentContext = tailer.readingDocument()) {
}
assertFalse(tailer.peekDocument());
} finally {
tempDir.deleteOnExit();
}
}
use of net.openhft.chronicle.wire.Wire in project Chronicle-Queue by OpenHFT.
the class TestBinarySearch method toWire.
@NotNull
private Wire toWire(int key) {
final MyData myData = new MyData();
myData.key = key;
myData.value = Integer.toString(key);
Wire wire = WireType.BINARY.apply(Bytes.elasticByteBuffer());
wire.usePadding(true);
try (final DocumentContext dc = wire.writingDocument()) {
dc.wire().getValueOut().typedMarshallable(myData);
}
return wire;
}
use of net.openhft.chronicle.wire.Wire in project Chronicle-Queue by OpenHFT.
the class MicroToucher method execute.
public boolean execute() {
final Wire bufferWire = appender.wire();
if (bufferWire == null)
return false;
final long lastPosition = appender.lastPosition;
final long lastPage = lastPosition & ~0xFFF;
final long nextPage = (lastPosition + 0xFFF) & ~0xFFF;
Bytes bytes = bufferWire.bytes();
if (nextPage != lastPageTouched) {
lastPageTouched = nextPage;
try {
// best effort
final BytesStore bs = bytes.bytesStore();
if (bs.inside(nextPage, 8))
touchPage(nextPage, bs);
} catch (Throwable ignored) {
}
return true;
}
lastPageToSync = lastPage;
return false;
}
use of net.openhft.chronicle.wire.Wire in project Chronicle-Queue by OpenHFT.
the class MicroToucher method bgExecute.
public void bgExecute() {
final long lastPage = this.lastPageToSync;
final long start = this.lastPageSynced;
final long length = Math.min(8 << 20, lastPage - start);
// System.out.println("len "+length);
if (length < 8 << 20)
return;
final Wire bufferWire = appender.wire();
if (bufferWire == null)
return;
BytesStore bytes = bufferWire.bytes().bytesStore();
sync(bytes, start, length);
this.lastPageSynced += length;
}
use of net.openhft.chronicle.wire.Wire in project Chronicle-Queue by OpenHFT.
the class SingleTableBuilder method build.
// *************************************************************************
//
// *************************************************************************
@NotNull
public TableStore<T> build() {
if (readOnly) {
if (!file.exists())
throw new IORuntimeException("Metadata file not found in readOnly mode");
// Wait a short time for the file to be initialized
TimingPauser pauser = Pauser.balanced();
try {
while (file.length() < OS.mapAlignment()) {
pauser.pause(1, TimeUnit.SECONDS);
}
} catch (TimeoutException e) {
throw new IORuntimeException("Metadata file found in readOnly mode, but not initialized yet");
}
}
MappedBytes bytes = null;
try {
if (!readOnly && file.createNewFile() && !file.canWrite()) {
throw new IllegalStateException("Cannot write to tablestore file " + file);
}
bytes = MappedBytes.mappedBytes(file, OS.SAFE_PAGE_SIZE, OS.SAFE_PAGE_SIZE, readOnly);
// these MappedBytes are shared, but the assumption is they shouldn't grow. Supports 2K entries.
bytes.disableThreadSafetyCheck(true);
// eagerly initialize backing MappedFile page - otherwise wire.writeFirstHeader() will try to lock the file
// to allocate the first byte store and that will cause lock overlap
bytes.readVolatileInt(0);
Wire wire = wireType.apply(bytes);
if (readOnly)
return SingleTableStore.doWithSharedLock(file, v -> {
try {
return readTableStore(wire);
} catch (IOException ex) {
throw Jvm.rethrow(ex);
}
}, () -> null);
else {
MappedBytes finalBytes = bytes;
return SingleTableStore.doWithExclusiveLock(file, v -> {
try {
if (wire.writeFirstHeader()) {
return writeTableStore(finalBytes, wire);
} else {
return readTableStore(wire);
}
} catch (IOException ex) {
throw Jvm.rethrow(ex);
}
}, () -> null);
}
} catch (IOException e) {
throw new IORuntimeException("file=" + file.getAbsolutePath(), e);
} finally {
if (bytes != null)
bytes.clearUsedByThread();
}
}
Aggregations