public abstract class MappedBytes extends AbstractBytes<Void> implements net.openhft.chronicle.core.io.Closeable, net.openhft.chronicle.core.io.ManagedCloseable, net.openhft.chronicle.core.io.Syncable
AbstractBytes
that wraps memory-mapped data for efficient random file access.
Memory is grouped in chunks of 64 MB by default. The chunk size can be significantly increased if the
OS supports sparse files via the OS.isSparseFileSupported()
method, e.g. blockSize(512 << 30)
.
Only the most recently accessed memory chunk is reserved, and the previous chunk is released. For random access, a chunk can be manually reserved by obtaining the bytesStore() and using reserve(owner) on it. However, it is crucial to call release(owner) on the same BytesStore before closing the file to avoid memory leaks.
Several factory methods are provided to create different types of MappedBytes, including single mapped bytes and chunked mapped bytes, with optional settings for read-only mode and chunk overlap size.
Note: MappedBytes, like all Bytes, are single-threaded. Also, it is recommended to ensure the mapped file is reserved before using MappedBytes.
BytesStore
,
MappedFile
,
AbstractBytes
Modifier and Type | Field and Description |
---|---|
protected static boolean |
TRACE |
bytesStore, isPresent, readPosition, writeLimit
referenceCounted, WARN_COUNT, WARN_NS
DEFAULT_BYTE_BUFFER_CAPACITY, MAX_CAPACITY, MAX_HEAP_CAPACITY
JAVA9_STRING_CODER_LATIN, JAVA9_STRING_CODER_UTF16
Modifier | Constructor and Description |
---|---|
protected |
MappedBytes() |
protected |
MappedBytes(String name) |
Modifier and Type | Method and Description |
---|---|
@NotNull Bytes<Void> |
bytesForRead()
Provides a bytes object for read operations.
|
@NotNull Bytes<Void> |
bytesForWrite()
Provides a bytes object for write operations.
|
abstract void |
chunkCount(long[] chunkCount)
Updates the number of chunks in the mapped file.
|
abstract boolean |
isBackingFileReadOnly()
Checks if the backing file is read-only.
|
static @NotNull MappedBytes |
mappedBytes(@NotNull File file,
long chunkSize)
Creates a MappedBytes instance that wraps a memory-mapped file divided into chunks of a specified size.
|
static @NotNull MappedBytes |
mappedBytes(@NotNull File file,
long chunkSize,
long overlapSize)
Creates a MappedBytes instance that wraps a memory-mapped file divided into chunks of a specified size.
|
static @NotNull MappedBytes |
mappedBytes(@NotNull File file,
long chunkSize,
long overlapSize,
boolean readOnly) |
static @NotNull MappedBytes |
mappedBytes(@NotNull File file,
long chunkSize,
long overlapSize,
int pageSize,
boolean readOnly)
Creates a MappedBytes instance that wraps a memory-mapped file divided into chunks of a specified size.
|
static @NotNull MappedBytes |
mappedBytes(@NotNull MappedFile rw)
Create a MappedBytes for a MappedFile
|
static @NotNull MappedBytes |
mappedBytes(@NotNull String filename,
long chunkSize)
Creates a MappedBytes instance that wraps a memory-mapped file divided into chunks of a specified size.
|
abstract MappedFile |
mappedFile()
Retrieves the mapped file.
|
static @NotNull MappedBytes |
readOnly(@NotNull File file)
Creates a MappedBytes instance that wraps a read-only memory-mapped file.
|
boolean |
sharedMemory()
Checks if the bytes are stored in shared memory.
|
static @NotNull MappedBytes |
singleMappedBytes(@NotNull File file,
long capacity)
Creates a MappedBytes instance that wraps a single memory-mapped file.
|
static @NotNull MappedBytes |
singleMappedBytes(@NotNull File file,
long capacity,
boolean readOnly)
Creates a MappedBytes instance that wraps a single memory-mapped file.
|
static @NotNull MappedBytes |
singleMappedBytes(@NotNull String filename,
long capacity)
Creates a MappedBytes instance that wraps a single memory-mapped file.
|
void |
sync()
Ensures that any modifications to this MappedBytes instance are written to the storage device containing the mapped file.
|
acquireUncheckedInput, addressForRead, addressForWrite, addressForWritePosition, append, append, append, append, append, append, appendAndReturnLength, byteCheckSum, byteCheckSum, byteCheckSum, bytesStore, bytesStore, canReadDirect, canWriteDirect, capacity, clear, clearAndPad, compact, compareAndSwapInt, compareAndSwapLong, copyTo, decimaliser, decimaliser, equals, fpAppend0, fpAppend0, hashCode, isDirectMemory, isElastic, isImmutableEmptyByteStore, lastDecimalPlaces, lastDecimalPlaces, lastNumberHadDigits, lastNumberHadDigits, length, lenient, lenient, move, nativeRead, nativeWrite, peekUnsignedByte, peekUnsignedByte, performRelease, prewrite, prewrite, prewriteByte, prewriteInt, prewriteLong, prewriteOffsetPositionMoved, prewriteShort, read, read, readByte, readByte, readCheckOffset, readDouble, readDouble, readFloat, readFloat, readInt, readInt, readLimit, readLimit, readLong, readLong, readOffsetPositionMoved, readPosition, readPosition, readPositionForHeader, readShort, readShort, readSkip, readUnsignedByte, readUnsignedByte, readVolatileByte, readVolatileInt, readVolatileInt, readVolatileLong, readVolatileLong, readVolatileShort, realCapacity, safeCopySize, start, startsWith, testAndSetInt, toString, uncheckedReadSkipBackOne, uncheckedReadSkipOne, uncheckedReadUnsignedByte, uncheckedWritePosition, underlyingObject, write, write, write, write, write, write, write8bit, write8bit, write8bit, write8bit, writeBoolean, writeByte, writeByte, writeCheckOffset, writeDouble, writeDouble, writeDoubleAndInt, writeFloat, writeFloat, writeInt, writeInt, writeIntAdv, writeLimit, writeLimit, writeLong, writeLong, writeLongAdv, writeOffsetPositionMoved, writeOffsetPositionMoved, writeOrderedInt, writeOrderedInt, writeOrderedLong, writeOrderedLong, writePosition, writePosition, writeShort, writeShort, writeSkip, writeSome, writeVolatileByte, writeVolatileInt, writeVolatileLong, writeVolatileShort
addReferenceChangeListener, assertReferencesReleased, backgroundPerformRelease, canReleaseInBackground, createdHere, disableReferenceTracing, enableReferenceTracing, refCount, referenceCountedUnmonitored, referenceId, release, releaseLast, removeReferenceChangeListener, reserve, reserveTransfer, singleThreadedCheckDisabled, singleThreadedCheckReset, threadSafetyCheck, throwExceptionIfNotReleased, throwExceptionIfReleased, tryReserve, unmonitor, warnAndReleaseIfNotReleased
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
createdHere, throwExceptionIfClosed, warnAndCloseIfNotClosed
close, closeQuietly, closeQuietly
allocateDirect, allocateDirect, allocateElasticDirect, allocateElasticDirect, allocateElasticOnHeap, allocateElasticOnHeap, copy, copyTo, copyTo, directFrom, elasticByteBuffer, elasticByteBuffer, elasticByteBuffer, elasticHeapByteBuffer, elasticHeapByteBuffer, empty, ensureCapacity, forFieldGroup, from, from, fromDirect, fromHexString, indexOf, indexOf, isClear, isEqual, readMarshallableLength16, readWithLength, readWrite, safeLimit, toHexString, toHexString, toHexString, toString, toString, toString, unchecked, unchecked, unwrite, valueOf, wrapForRead, wrapForRead, wrapForWrite, wrapForWrite, write, writeMarshallableLength16
addAndGetDouble, addAndGetDoubleNotAtomic, addAndGetFloat, addAndGetFloatNotAtomic, addAndGetInt, addAndGetIntNotAtomic, addAndGetLong, addAndGetShortNotAtomic, addAndGetUnsignedByteNotAtomic, charAt, cipher, cipher, compareAndSwapDouble, compareAndSwapFloat, contentEquals, elasticByteBuffer, endsWith, equalBytes, follow, forFields, from, hash, inside, inside, isEmpty, isEqual, lazyNativeBytesStoreWithFixedCapacity, nativePointer, nativeStore, nativeStoreFrom, nativeStoreWithFixedCapacity, startsWith, subSequence, to8bitString, toDebugString, toDebugString, wrap, wrap, wrap, writeMaxInt, writeMaxLong, zeroOut
append, append, write, write, writeBoolean, writeByte, writeInt24, writeOrderedDouble, writeOrderedFloat, writeUnsignedByte, writeUnsignedInt, writeUnsignedShort, writeUtf8, writeUtf8Limited, writeVolatileDouble, writeVolatileFloat
chars, codePoints
bytesMethodReader, bytesMethodReaderBuilder, readObject
canReadDirect, compareUtf8, copyTo, fastHash, findByte, parseLong, peekVolatileInt, printable, readBoolean, readIncompleteLong, readUnsignedInt, readUnsignedInt24, readUnsignedShort, readUtf8, readUtf8Limited, readUtf8Limited, readVolatileDouble, readVolatileFloat, subBytes, toByteArray, toTemporaryDirectByteBuffer
parse8bit, parse8bit, parse8bit, parse8bit, parse8bit, parseBigDecimal, parseBoolean, parseBoolean, parseDouble, parseFlexibleLong, parseFloat, parseInt, parseLong, parseLongDecimal, parseUtf8, parseUtf8, parseUtf8, reader, skipTo
inputStream, parseHexLong, parseUtf8, parseUtf8, rawReadByte, rawReadInt, rawReadLong, read, read, read, read, read, read8bit, read8bit, read8bit, readBigDecimal, readBigInteger, readBoolean, readChar, readEnum, readHistogram, readIncompleteLong, readInt24, readLimitToCapacity, readPositionRemaining, readPositionUnlimited, readStopBit, readStopBitChar, readStopBitDecimal, readStopBitDouble, readUnsignedInt, readUnsignedInt24, readUnsignedShort, readUtf8, readUtf8, readUtf8, readUtf8, readWithLength, readWithLength, readWithLength0, unsafeRead, unsafeReadObject, unsafeReadObject
bytesMethodWriter, writeObject
append, append, append, append, append, append8bit, append8bit, append8bit, append8bit, append8bit, appendBase, appendBase16, appendBase16, appendDateMillis, appendDecimal, appendTimeMillis, writer
appendUtf8, appendUtf8, appendUtf8, appendUtf8, appendUtf8, appendUtf8, copyFrom, outputStream, rawWriteByte, rawWriteInt, rawWriteLong, unsafeWrite, unsafeWriteObject, unsafeWriteObject, write, write, write, write, write, write8bit, write8bit, write8bit, writeBigDecimal, writeBigInteger, writeChar, writeEnum, writeHistogram, writeInt24, writePositionForHeader, writePositionRemaining, writeStopBit, writeStopBit, writeStopBit, writeStopBitDecimal, writeUnsignedByte, writeUnsignedInt, writeUnsignedInt24, writeUnsignedShort, writeUtf8, writeUtf8, writeWithLength
prepend
adjustHexDumpIndentation, retainedHexDumpDescription, writeHexDumpDescription
protected MappedBytes() throws net.openhft.chronicle.core.io.ClosedIllegalStateException, net.openhft.chronicle.core.io.ThreadingIllegalStateException
net.openhft.chronicle.core.io.ClosedIllegalStateException
net.openhft.chronicle.core.io.ThreadingIllegalStateException
protected MappedBytes(String name) throws net.openhft.chronicle.core.io.ClosedIllegalStateException, net.openhft.chronicle.core.io.ThreadingIllegalStateException
net.openhft.chronicle.core.io.ClosedIllegalStateException
net.openhft.chronicle.core.io.ThreadingIllegalStateException
@NotNull public static @NotNull MappedBytes singleMappedBytes(@NotNull @NotNull String filename, long capacity) throws FileNotFoundException, IllegalStateException
filename
- The name of the file to be memory-mapped.capacity
- The maximum number of bytes that can be read from or written to the mapped file.FileNotFoundException
- If the file does not exist.net.openhft.chronicle.core.io.ClosedIllegalStateException
- If the resource has been released or closed.net.openhft.chronicle.core.io.ThreadingIllegalStateException
- If this resource was accessed by multiple threads in an unsafe wayIllegalStateException
@NotNull public static @NotNull MappedBytes singleMappedBytes(@NotNull @NotNull File file, long capacity) throws FileNotFoundException
file
- The name of the file to be memory-mapped.capacity
- The maximum number of bytes that can be read from or written to the mapped file.FileNotFoundException
- If the file does not exist.@NotNull public static @NotNull MappedBytes singleMappedBytes(@NotNull @NotNull File file, long capacity, boolean readOnly) throws FileNotFoundException
file
- The name of the file to be memory-mapped.capacity
- The maximum number of bytes that can be read from or written to the mapped file.readOnly
- read only is true, read-write if falseFileNotFoundException
- If the file does not exist.@NotNull public static @NotNull MappedBytes mappedBytes(@NotNull @NotNull String filename, long chunkSize) throws FileNotFoundException, net.openhft.chronicle.core.io.ClosedIllegalStateException
filename
- The name of the file to be memory-mapped.chunkSize
- The size of each chunk in bytes.FileNotFoundException
- If the file does not exist.net.openhft.chronicle.core.io.ClosedIllegalStateException
- If the resource has been released or closed.net.openhft.chronicle.core.io.ThreadingIllegalStateException
- If this resource was accessed by multiple threads in an unsafe way@NotNull public static @NotNull MappedBytes mappedBytes(@NotNull @NotNull File file, long chunkSize) throws FileNotFoundException, net.openhft.chronicle.core.io.ClosedIllegalStateException
file
- The name of the file to be memory-mapped.chunkSize
- The size of each chunk in bytes.FileNotFoundException
- If the file does not exist.net.openhft.chronicle.core.io.ClosedIllegalStateException
- If the resource has been released or closed.net.openhft.chronicle.core.io.ThreadingIllegalStateException
- If this resource was accessed by multiple threads in an unsafe way@NotNull public static @NotNull MappedBytes mappedBytes(@NotNull @NotNull File file, long chunkSize, long overlapSize) throws FileNotFoundException, net.openhft.chronicle.core.io.ClosedIllegalStateException
file
- The name of the file to be memory-mapped.chunkSize
- The size of each chunk in bytes.overlapSize
- The size of overlap of chunks in bytes.FileNotFoundException
- If the file does not exist.net.openhft.chronicle.core.io.ClosedIllegalStateException
- If the resource has been released or closed.net.openhft.chronicle.core.io.ThreadingIllegalStateException
- If this resource was accessed by multiple threads in an unsafe way@NotNull public static @NotNull MappedBytes mappedBytes(@NotNull @NotNull File file, long chunkSize, long overlapSize, int pageSize, boolean readOnly) throws FileNotFoundException, net.openhft.chronicle.core.io.ClosedIllegalStateException
file
- The name of the file to be memory-mapped.chunkSize
- The size of each chunk in bytes.overlapSize
- The size of overlap of chunks in bytes.pageSize
- The custom page size in bytes.readOnly
- read only is true, read-write if falseFileNotFoundException
- If the file does not exist.net.openhft.chronicle.core.io.ClosedIllegalStateException
- If the resource has been released or closed.net.openhft.chronicle.core.io.ThreadingIllegalStateException
- If this resource was accessed by multiple threads in an unsafe way@NotNull public static @NotNull MappedBytes mappedBytes(@NotNull @NotNull File file, long chunkSize, long overlapSize, boolean readOnly) throws FileNotFoundException, net.openhft.chronicle.core.io.ClosedIllegalStateException
FileNotFoundException
net.openhft.chronicle.core.io.ClosedIllegalStateException
mappedBytes(File, long, long, int, boolean)
@NotNull public static @NotNull MappedBytes mappedBytes(@NotNull @NotNull MappedFile rw) throws net.openhft.chronicle.core.io.ClosedIllegalStateException
rw
- MappedFile to usenet.openhft.chronicle.core.io.ClosedIllegalStateException
- If the resource has been released or closed.net.openhft.chronicle.core.io.ThreadingIllegalStateException
- If this resource was accessed by multiple threads in an unsafe way.@NotNull public static @NotNull MappedBytes readOnly(@NotNull @NotNull File file) throws FileNotFoundException, net.openhft.chronicle.core.io.ClosedIllegalStateException, net.openhft.chronicle.core.io.ThreadingIllegalStateException
file
- The file to be memory-mapped in read-only mode.FileNotFoundException
- If the file does not exist.net.openhft.chronicle.core.io.ClosedIllegalStateException
- If the resource has been released or closed.net.openhft.chronicle.core.io.ThreadingIllegalStateException
- If this resource was accessed by multiple threads in an unsafe waypublic abstract boolean isBackingFileReadOnly()
public boolean sharedMemory()
sharedMemory
in interface Bytes<Void>
public abstract void chunkCount(long[] chunkCount)
chunkCount
- The new number of chunks.public abstract MappedFile mappedFile()
public void sync()
sync
in interface net.openhft.chronicle.core.io.Syncable
@NotNull public @NotNull Bytes<Void> bytesForRead() throws net.openhft.chronicle.core.io.ClosedIllegalStateException
bytesForRead
in interface Bytes<Void>
bytesForRead
in interface BytesStore<Bytes<Void>,Void>
net.openhft.chronicle.core.io.ClosedIllegalStateException
- If the resource has been released or closed.net.openhft.chronicle.core.io.ThreadingIllegalStateException
- If this resource was accessed by multiple threads in an unsafe way.@NotNull public @NotNull Bytes<Void> bytesForWrite() throws net.openhft.chronicle.core.io.ClosedIllegalStateException
bytesForWrite
in interface Bytes<Void>
bytesForWrite
in interface BytesStore<Bytes<Void>,Void>
net.openhft.chronicle.core.io.ClosedIllegalStateException
- If the resource has been released or closed.net.openhft.chronicle.core.io.ThreadingIllegalStateException
- If this resource was accessed by multiple threads in an unsafe way.Copyright © 2024. All rights reserved.