use of net.openhft.chronicle.bytes.MappedBytes in project Chronicle-Queue by OpenHFT.
the class SingleChronicleQueueStore method dumpHeader.
@Override
public String dumpHeader() {
try (MappedBytes bytes = MappedBytes.mappedBytes(mappedFile)) {
int size = bytes.readInt(0);
if (!Wires.isReady(size))
return "not ready";
bytes.readLimit(Wires.lengthOf(size) + 4L);
return Wires.fromSizePrefixedBlobs(bytes);
}
}
use of net.openhft.chronicle.bytes.MappedBytes in project Chronicle-Queue by OpenHFT.
the class SingleChronicleQueueStore method dump.
private String dump(boolean abbrev) {
try (MappedBytes bytes = MappedBytes.mappedBytes(mappedFile)) {
bytes.readLimit(bytes.realCapacity());
final Wire w = WireType.BINARY.apply(bytes);
w.usePadding(dataVersion > 0);
return Wires.fromSizePrefixedBlobs(w, abbrev);
}
}
use of net.openhft.chronicle.bytes.MappedBytes 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