Search in sources :

Example 21 with MappedBytes

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);
    }
}
Also used : MappedBytes(net.openhft.chronicle.bytes.MappedBytes)

Example 22 with MappedBytes

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);
    }
}
Also used : MappedBytes(net.openhft.chronicle.bytes.MappedBytes)

Example 23 with MappedBytes

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();
    }
}
Also used : IORuntimeException(net.openhft.chronicle.core.io.IORuntimeException) MappedBytes(net.openhft.chronicle.bytes.MappedBytes) StreamCorruptedException(java.io.StreamCorruptedException) TimingPauser(net.openhft.chronicle.threads.TimingPauser) TimeoutException(java.util.concurrent.TimeoutException) IOException(java.io.IOException) Builder(net.openhft.chronicle.core.util.Builder) Wire(net.openhft.chronicle.wire.Wire) WireType(net.openhft.chronicle.wire.WireType) Jvm(net.openhft.chronicle.core.Jvm) File(java.io.File) Objects(java.util.Objects) TimeUnit(java.util.concurrent.TimeUnit) ValueIn(net.openhft.chronicle.wire.ValueIn) StringUtils(net.openhft.chronicle.core.util.StringUtils) CLASS_ALIASES(net.openhft.chronicle.core.pool.ClassAliasPool.CLASS_ALIASES) OS(net.openhft.chronicle.core.OS) NotNull(org.jetbrains.annotations.NotNull) MetaDataKeys(net.openhft.chronicle.queue.impl.single.MetaDataKeys) Path(java.nio.file.Path) Pauser(net.openhft.chronicle.threads.Pauser) Wires(net.openhft.chronicle.wire.Wires) TableStore(net.openhft.chronicle.queue.impl.TableStore) IORuntimeException(net.openhft.chronicle.core.io.IORuntimeException) TimingPauser(net.openhft.chronicle.threads.TimingPauser) IOException(java.io.IOException) Wire(net.openhft.chronicle.wire.Wire) MappedBytes(net.openhft.chronicle.bytes.MappedBytes) TimeoutException(java.util.concurrent.TimeoutException) NotNull(org.jetbrains.annotations.NotNull)

Aggregations

MappedBytes (net.openhft.chronicle.bytes.MappedBytes)23 File (java.io.File)10 IOException (java.io.IOException)8 Test (org.junit.Test)8 Wire (net.openhft.chronicle.wire.Wire)7 FileNotFoundException (java.io.FileNotFoundException)6 ChronicleQueue (net.openhft.chronicle.queue.ChronicleQueue)5 RollingChronicleQueue (net.openhft.chronicle.queue.impl.RollingChronicleQueue)5 DocumentContext (net.openhft.chronicle.wire.DocumentContext)4 NotNull (org.jetbrains.annotations.NotNull)4 ByteBuffer (java.nio.ByteBuffer)3 TimeoutException (java.util.concurrent.TimeoutException)3 ExcerptTailer (net.openhft.chronicle.queue.ExcerptTailer)3 WireDumper (net.openhft.chronicle.wire.WireDumper)3 Bytes (net.openhft.chronicle.bytes.Bytes)2 ExcerptAppender (net.openhft.chronicle.queue.ExcerptAppender)2 SingleChronicleQueueStore (net.openhft.chronicle.queue.impl.single.SingleChronicleQueueStore)2 EOFException (java.io.EOFException)1 PrintWriter (java.io.PrintWriter)1 StreamCorruptedException (java.io.StreamCorruptedException)1