use of net.openhft.chronicle.core.io.ClosedIllegalStateException in project Chronicle-Queue by OpenHFT.
the class SingleTableStore method acquireValueFor.
/**
* {@inheritDoc}
*/
@Override
public synchronized LongValue acquireValueFor(CharSequence key, final long defaultValue) {
if (mappedBytes.isClosed())
throw new ClosedIllegalStateException("Closed");
final StringBuilder sb = Wires.acquireStringBuilder();
mappedBytes.reserve(this);
try {
mappedBytes.readPosition(0);
mappedBytes.readLimit(mappedBytes.realCapacity());
while (mappedWire.readDataHeader()) {
final int header = mappedBytes.readVolatileInt();
if (Wires.isNotComplete(header))
break;
final long readPosition = mappedBytes.readPosition();
final int length = Wires.lengthOf(header);
final ValueIn valueIn = mappedWire.readEventName(sb);
if (StringUtils.equalsCaseIgnore(key, sb)) {
return valueIn.int64ForBinding(null);
}
mappedBytes.readPosition(readPosition + length);
}
mappedBytes.writeLimit(mappedBytes.realCapacity());
long start = mappedBytes.readPosition();
mappedBytes.writePosition(start);
final long pos = mappedWire.enterHeader(128L);
final LongValue longValue = wireType.newLongReference().get();
mappedWire.writeEventName(key).int64forBinding(defaultValue, longValue);
mappedWire.writeAlignTo(Integer.BYTES, 0);
mappedWire.updateHeader(pos, false, 0);
long end = mappedBytes.writePosition();
long chuckSize = mappedFile.chunkSize();
long overlapSize = mappedFile.overlapSize();
long endOfChunk = (start + chuckSize - 1) / chuckSize * chuckSize;
if (end >= endOfChunk + overlapSize)
throw new IllegalStateException("Misaligned write");
return longValue;
} catch (StreamCorruptedException | EOFException e) {
throw new IORuntimeException(e);
} finally {
mappedBytes.release(this);
}
}
use of net.openhft.chronicle.core.io.ClosedIllegalStateException in project Chronicle-Queue by OpenHFT.
the class MicroToucherTest method touchPage.
public void touchPage(Consumer<SingleChronicleQueueBuilder> configure, int pagesExpected) {
long start = System.nanoTime();
String path = OS.getTarget() + "/touchPage-" + System.nanoTime();
int pages = 0;
final SingleChronicleQueueBuilder builder = ChronicleQueue.singleBuilder(path);
configure.accept(builder);
try (ChronicleQueue q = builder.build();
final StoreAppender appender = (StoreAppender) q.acquireAppender()) {
Thread msync = new Thread(() -> {
try {
while (true) {
appender.bgMicroTouch();
Jvm.pause(25);
}
} catch (ClosedIllegalStateException expected) {
}
});
msync.setDaemon(true);
msync.start();
long lastPage = 0;
for (int i = 0; i < (1 << 20); i++) {
try (DocumentContext dc = appender.writingDocument()) {
dc.wire().bytes().writeSkip(256);
}
long page = (appender.lastPosition + 0xFFF) & ~0xFFF;
boolean touch = page != lastPage && appender.wire().bytes().bytesStore().inside(page, 8);
lastPage = page;
if (touch != appender.microTouch())
assertEquals("i: " + i, touch, appender.microTouch());
if (touch)
pages++;
}
}
System.out.println("pages = " + pages);
// assertEquals(pagesExpected, pages);
System.out.println("Time = " + (System.nanoTime() - start) / 1000000 / 1e3);
IOTools.deleteDirWithFiles(path);
}
Aggregations