public class MappedBytesStore extends NativeBytesStore<Void>
This class is intended for working with large files that can be read from and written to as if they were a part of the program's memory.
WARNING: This class assumes that the caller will correctly handle bounds checking. Incorrect handling can lead to IndexOutOfBoundsException
.
Misuse of this class can cause hard-to-diagnose memory access errors and data corruption.
Modifier and Type | Field and Description |
---|---|
static @NotNull MappedBytesStoreFactory |
MAPPED_BYTES_STORE_FACTORY
Deprecated.
|
protected Runnable |
writeCheck |
address, limit, maximumLimit, memory
referenceCounted, WARN_COUNT, WARN_NS
charToString
Modifier | Constructor and Description |
---|---|
protected |
MappedBytesStore(net.openhft.chronicle.core.io.ReferenceOwner owner,
MappedFile mappedFile,
long start,
long address,
long capacity,
long safeCapacity)
Creates a new MappedBytesStore with the given parameters.
|
Modifier and Type | Method and Description |
---|---|
default long |
addressForRead(long offset,
int buffer)
Retrieves the underlying memory address for reading.
|
long |
appendUtf8(long pos,
char[] chars,
int offset,
int length) |
default ByteOrder |
byteOrder()
Retrieves the byte order used by the buffer.
|
@NotNull Bytes<Void> |
bytesForRead()
Returns a Bytes that wraps this ByteStore from the
start to the realCapacity . |
@NotNull VanillaBytes<Void> |
bytesForWrite()
Returns a Bytes that wraps this ByteStore from the
start to the realCapacity . |
boolean |
compareAndSwapInt(long offset,
int expected,
int value)
Perform a 32-bit CAS at a given offset.
|
boolean |
compareAndSwapLong(long offset,
long expected,
long value)
Perform a 64-bit CAS at a given offset.
|
static MappedBytesStore |
create(net.openhft.chronicle.core.io.ReferenceOwner owner,
MappedFile mappedFile,
long start,
long address,
long capacity,
long safeCapacity)
Creates a new MappedBytesStore with the given parameters.
|
boolean |
inside(long offset)
Returns if a specified offset is inside this BytesStore limits.
|
boolean |
inside(long offset,
long bufferSize)
Returns if a number of bytes starting from an offset are inside this ByteStore limits.
|
default long |
lengthWritten(long startPosition)
Calculates the length of data written from the given start position.
|
FileLock |
lock(long position,
long size,
boolean shared)
Acquires a lock on a region of the underlying file.
|
void |
nativeWrite(long address,
long position,
long size)
Expert-level method that copies data directly from native memory into this BytesStore.
|
protected void |
performRelease()
Sync the ByteStore if required.
|
byte |
readByte(long offset)
Reads a byte value from a specific offset.
|
default long |
readLimit()
If the resource is closed, the returned value is unspecified.
|
long |
readPosition()
Returns the read position.
|
default long |
realReadRemaining()
Calculates the number of bytes that can be safely read directly.
|
default long |
realWriteRemaining()
Calculates the number of bytes remaining that can be written from the current write position with resizing.
|
long |
safeLimit() |
long |
start() |
SyncMode |
syncMode() |
void |
syncMode(SyncMode syncMode)
Set the sync mode for this ByteStore
|
void |
syncUpTo(long position)
Synchronise from the last complete page up to this position.
|
long |
translate(long offset) |
FileLock |
tryLock(long position,
long size,
boolean shared)
Attempts to acquire a lock on a region of the underlying file.
|
long |
underlyingCapacity()
Fetch the capacity of the underlying file
This can differ from the exposed capacity() of this bytes store (which has been page aligned)
|
@NotNull MappedBytesStore |
write(long offsetInRDO,
byte[] byteArray,
int offset,
int length)
Copies the provided
byteArray to this Bytes object starting at writeOffset taking
content starting at readOffset but copying at most length bytes. |
void |
write(long offsetInRDO,
@NotNull ByteBuffer bytes,
int offset,
int length)
Copies a segment from the provided ByteBuffer into this data output.
|
@NotNull MappedBytesStore |
write(long writeOffset,
@NotNull RandomDataInput bytes,
long readOffset,
long length)
Copies a segment from the provided RandomDataInput into this data output.
|
void |
write0(long offsetInRDO,
@NotNull RandomDataInput bytes,
long offset,
long length) |
@NotNull MappedBytesStore |
writeByte(long offset,
byte i8)
Writes a byte at the specified non-negative offset.
|
@NotNull MappedBytesStore |
writeDouble(long offset,
double d)
Writes a double-precision floating-point value at the specified non-negative offset.
|
@NotNull MappedBytesStore |
writeFloat(long offset,
float f)
Writes a single-precision floating-point value at the specified non-negative offset.
|
@NotNull MappedBytesStore |
writeInt(long offset,
int i32)
Writes an integer value at the specified non-negative offset.
|
default long |
writeLimit()
Retrieves the maximum writable position within the buffer.
|
@NotNull MappedBytesStore |
writeLong(long offset,
long i64)
Writes a long integer value at the specified non-negative offset.
|
@NotNull MappedBytesStore |
writeOrderedInt(long offset,
int i)
Performs a non-blocking write operation with a memory barrier to ensure order of stores.
|
@NotNull MappedBytesStore |
writeOrderedLong(long offset,
long i)
Performs a non-blocking write operation with a memory barrier to ensure order of stores.
|
default long |
writePosition()
Returns the write position.
|
@NotNull MappedBytesStore |
writeShort(long offset,
short i16)
Writes a short integer at the specified non-negative offset.
|
@NotNull MappedBytesStore |
writeVolatileByte(long offset,
byte i8)
Writes a volatile byte at the specified non-negative offset.
|
@NotNull MappedBytesStore |
writeVolatileInt(long offset,
int i32)
Writes a volatile integer at the specified non-negative offset.
|
@NotNull MappedBytesStore |
writeVolatileLong(long offset,
long i64)
Writes a volatile long integer at the specified non-negative offset.
|
@NotNull MappedBytesStore |
writeVolatileShort(long offset,
short i16)
Writes a volatile short at the specified non-negative offset.
|
@NotNull MappedBytesStore |
zeroOut(long start,
long end)
Fills the BytesStore with zeros.
|
addAndGetInt, addAndGetLong, addressForRead, addressForWrite, addressForWritePosition, appendAndReturnLength, backgroundPerformRelease, byteCheckSum, byteCheckSum, canReadDirect, capacity, copy, copyTo, copyToDirect, elasticByteBuffer, elasticByteBuffer, equals, fastHash, follow, from, from, hashCode, init, isDirectMemory, isEqual, lazyNativeBytesStoreWithFixedCapacity, move, nativeRead, nativeStore, nativeStoreWithFixedCapacity, peekUnsignedByte, read, read8bit, readDouble, readFloat, readIncompleteLong, readInt, readLong, readShort, readUnsignedByte, readVolatileByte, readVolatileInt, readVolatileLong, readVolatileShort, realCapacity, reverseBytesFrom, setAddress, sharedMemory, testAndSetInt, toString, toTemporaryDirectByteBuffer, underlyingObject, uninit, uninitialized, wrap, write8bit, write8bit
canReleaseInBackground, internalNumberBuffer, readRemaining, writeRemaining
addReferenceChangeListener, assertReferencesReleased, clearUsedByThread, createdHere, disableReferenceTracing, enableReferenceTracing, refCount, referenceCountedUnmonitored, referenceId, release, releaseLast, removeReferenceChangeListener, reserve, reservedBy, reserveTransfer, singleThreadedCheckDisabled, singleThreadedCheckReset, threadSafetyCheck, throwExceptionIfNotReleased, throwExceptionIfReleased, tryReserve, unmonitor, warnAndReleaseIfNotReleased
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
addAndGetDouble, addAndGetDoubleNotAtomic, addAndGetFloat, addAndGetFloatNotAtomic, addAndGetIntNotAtomic, addAndGetShortNotAtomic, addAndGetUnsignedByteNotAtomic, bytesStore, charAt, cipher, cipher, compareAndSwapDouble, compareAndSwapFloat, contentEquals, copyTo, empty, endsWith, equalBytes, forFields, from, from, hash, isClear, isEmpty, isImmutableEmptyByteStore, length, nativePointer, nativeStoreFrom, readWrite, startsWith, startsWith, subSequence, to8bitString, toDebugString, toDebugString, wrap, wrap, writeMaxInt, writeMaxLong
canReadDirect, compareUtf8, copyTo, copyTo, createCharToString, findByte, parseLong, peekVolatileInt, printable, readBoolean, readUnsignedInt, readUnsignedInt24, readUnsignedShort, readUtf8, readUtf8Limited, readUtf8Limited, readVolatileDouble, readVolatileFloat, subBytes, toByteArray
append, append, write, write, writeBoolean, writeByte, writeInt24, writeOrderedDouble, writeOrderedFloat, writeUnsignedByte, writeUnsignedInt, writeUnsignedShort, writeUtf8, writeUtf8Limited, writeVolatileDouble, writeVolatileFloat
addReferenceChangeListener, refCount, release, releaseLast, releaseLast, removeReferenceChangeListener, reserve, reservedBy, reserveTransfer, tryReserve
referenceId, referenceName, temporary
chars, codePoints
@Deprecated @NotNull public static final @NotNull MappedBytesStoreFactory MAPPED_BYTES_STORE_FACTORY
protected final Runnable writeCheck
protected MappedBytesStore(net.openhft.chronicle.core.io.ReferenceOwner owner, MappedFile mappedFile, long start, long address, long capacity, long safeCapacity) throws IllegalStateException
owner
- The owner of this MappedBytesStore.mappedFile
- The MappedFile to be wrapped by this BytesStore.start
- The start position within the MappedFile.address
- The memory address of the mapped data.capacity
- The capacity of the mapped data.safeCapacity
- The safe capacity of the mapped data. Accessing data beyond the safe capacity might lead to a crash.IllegalStateException
- If the MappedFile has already been released.public static MappedBytesStore create(net.openhft.chronicle.core.io.ReferenceOwner owner, MappedFile mappedFile, long start, long address, long capacity, long safeCapacity) throws IllegalStateException
owner
- The owner of this MappedBytesStore.mappedFile
- The MappedFile to be wrapped by this BytesStore.start
- The start position within the MappedFile.address
- The memory address of the mapped data.capacity
- The capacity of the mapped data.safeCapacity
- The safe capacity of the mapped data. Accessing data beyond the safe capacity might lead to a crash.IllegalStateException
- If the MappedFile has already been released.public long underlyingCapacity()
@NotNull public @NotNull Bytes<Void> bytesForRead() throws IllegalStateException
BytesStore
start
to the realCapacity
.
The returned Bytes is not elastic and can be both read and written using cursors.
net.openhft.chronicle.core.io.ClosedIllegalStateException
- if this Bytes has been releasedIllegalStateException
- if this Bytes is in an unusable state@NotNull public @NotNull VanillaBytes<Void> bytesForWrite() throws IllegalStateException
BytesStore
start
to the realCapacity
.
The returned Bytes is not elastic and can be both read and written using cursors.
bytesForWrite
in interface BytesStore<NativeBytesStore<Void>,Void>
bytesForWrite
in class NativeBytesStore<Void>
net.openhft.chronicle.core.io.ClosedIllegalStateException
- if this Bytes has been releasedIllegalStateException
- if this Bytes is in an unusable statepublic boolean inside(long offset)
BytesStore
Use this test to determine if an offset is considered safe for reading from. Note that it checks we are inside the BytesStore limits *without* including the overlap
offset
- the specified offset to checktrue
if offset is safepublic boolean inside(long offset, long bufferSize)
BytesStore
offset
- the starting index to checkbufferSize
- the number of bytes to be read/writtentrue
if the bytes between the offset and offset+buffer are inside the BytesStorepublic long safeLimit()
safeLimit
in interface BytesStore<NativeBytesStore<Void>,Void>
safeLimit
in class NativeBytesStore<Void>
public byte readByte(long offset)
RandomDataInput
readByte
in interface RandomDataInput
readByte
in class NativeBytesStore<Void>
offset
- the location from where the byte value is read.@NotNull public @NotNull MappedBytesStore writeOrderedInt(long offset, int i) throws IllegalStateException
RandomDataOutput
writeOrderedInt
in interface RandomDataOutput<NativeBytesStore<Void>>
writeOrderedInt
in class NativeBytesStore<Void>
offset
- The non-negative position within the data stream to write the integer to.i
- The integer value to write.IllegalStateException
- if releasedpublic long translate(long offset)
translate
in class NativeBytesStore<Void>
public long start()
start
in class AbstractBytesStore<NativeBytesStore<Void>,Void>
public long readPosition()
The read position is start() <= readPosition() && readPosition() <= readLimit() && readPosition < safeLimit()
If the resource is closed, the returned value is unspecified.
readPosition
in class AbstractBytesStore<NativeBytesStore<Void>,Void>
public FileLock lock(long position, long size, boolean shared) throws IOException
position
- The starting byte position of the region to lock.size
- The number of bytes to lock, starting from the position.shared
- If true
, the lock will be shared; otherwise, it will be exclusive.IOException
- If an I/O error occurs while locking.for details on how the lock is acquired.
public FileLock tryLock(long position, long size, boolean shared) throws IOException
position
- The starting byte position of the region to lock.size
- The number of bytes to lock, starting from the position.shared
- If true
, the lock will be shared; otherwise, it will be exclusive.null
if the lock could not be acquired.IOException
- If an I/O error occurs while trying to lock.for details on how the lock is attempted.
@NotNull public @NotNull MappedBytesStore zeroOut(long start, long end)
BytesStore
zeroOut
in interface BytesStore<NativeBytesStore<Void>,Void>
zeroOut
in interface RandomDataOutput<NativeBytesStore<Void>>
zeroOut
in class NativeBytesStore<Void>
start
- first byte inclusiveend
- last byte exclusivepublic boolean compareAndSwapInt(long offset, int expected, int value) throws IllegalStateException
RandomDataInput
compareAndSwapInt
in interface BytesStore<NativeBytesStore<Void>,Void>
compareAndSwapInt
in interface RandomDataInput
compareAndSwapInt
in interface RandomDataOutput<NativeBytesStore<Void>>
compareAndSwapInt
in class NativeBytesStore<Void>
offset
- to perform CASexpected
- valuevalue
- to setIllegalStateException
- if releasedpublic boolean compareAndSwapLong(long offset, long expected, long value) throws IllegalStateException
RandomDataInput
compareAndSwapLong
in interface BytesStore<NativeBytesStore<Void>,Void>
compareAndSwapLong
in interface RandomDataInput
compareAndSwapLong
in interface RandomDataOutput<NativeBytesStore<Void>>
compareAndSwapLong
in class NativeBytesStore<Void>
offset
- to perform CASexpected
- valuevalue
- to setIllegalStateException
- if released@NotNull public @NotNull MappedBytesStore writeByte(long offset, byte i8) throws IllegalStateException
RandomDataOutput
writeByte
in interface RandomDataOutput<NativeBytesStore<Void>>
writeByte
in class NativeBytesStore<Void>
offset
- The non-negative position within the data stream to write the byte to.i8
- The byte value to write.IllegalStateException
- if released@NotNull public @NotNull MappedBytesStore writeShort(long offset, short i16) throws IllegalStateException
RandomDataOutput
writeShort
in interface RandomDataOutput<NativeBytesStore<Void>>
writeShort
in class NativeBytesStore<Void>
offset
- The non-negative position within the data stream to write the short integer to.i16
- The short integer value to write.IllegalStateException
- if released@NotNull public @NotNull MappedBytesStore writeInt(long offset, int i32) throws IllegalStateException
RandomDataOutput
writeInt
in interface RandomDataOutput<NativeBytesStore<Void>>
writeInt
in class NativeBytesStore<Void>
offset
- The non-negative position within the data stream to write the integer to.i32
- The integer value to write.IllegalStateException
- if released@NotNull public @NotNull MappedBytesStore writeLong(long offset, long i64) throws IllegalStateException
RandomDataOutput
writeLong
in interface RandomDataOutput<NativeBytesStore<Void>>
writeLong
in class NativeBytesStore<Void>
offset
- The non-negative position within the data stream to write the long integer to.i64
- The long integer value to write.IllegalStateException
- if released@NotNull public @NotNull MappedBytesStore writeOrderedLong(long offset, long i) throws IllegalStateException
RandomDataOutput
writeOrderedLong
in interface RandomDataOutput<NativeBytesStore<Void>>
writeOrderedLong
in class NativeBytesStore<Void>
offset
- The non-negative position within the data stream to write the long integer to.i
- The long integer value to write.IllegalStateException
- if released@NotNull public @NotNull MappedBytesStore writeFloat(long offset, float f) throws IllegalStateException
RandomDataOutput
writeFloat
in interface RandomDataOutput<NativeBytesStore<Void>>
writeFloat
in class NativeBytesStore<Void>
offset
- The non-negative position within the data stream to write the float to.f
- The float value to write.IllegalStateException
- if released@NotNull public @NotNull MappedBytesStore writeDouble(long offset, double d) throws IllegalStateException
RandomDataOutput
writeDouble
in interface RandomDataOutput<NativeBytesStore<Void>>
writeDouble
in class NativeBytesStore<Void>
offset
- The non-negative position within the data stream to write the double to.d
- The double value to write.IllegalStateException
- if released@NotNull public @NotNull MappedBytesStore writeVolatileByte(long offset, byte i8) throws IllegalStateException
RandomDataOutput
writeVolatileByte
in interface RandomDataOutput<NativeBytesStore<Void>>
writeVolatileByte
in class NativeBytesStore<Void>
offset
- The non-negative position within the data stream to write the byte to.i8
- The byte value to write.IllegalStateException
- if released@NotNull public @NotNull MappedBytesStore writeVolatileShort(long offset, short i16) throws IllegalStateException
RandomDataOutput
writeVolatileShort
in interface RandomDataOutput<NativeBytesStore<Void>>
writeVolatileShort
in class NativeBytesStore<Void>
offset
- The non-negative position within the data stream to write the short to.i16
- The short value to write.IllegalStateException
- if released@NotNull public @NotNull MappedBytesStore writeVolatileInt(long offset, int i32) throws IllegalStateException
RandomDataOutput
writeVolatileInt
in interface RandomDataOutput<NativeBytesStore<Void>>
writeVolatileInt
in class NativeBytesStore<Void>
offset
- The non-negative position within the data stream to write the integer to.i32
- The integer value to write.IllegalStateException
- if released@NotNull public @NotNull MappedBytesStore writeVolatileLong(long offset, long i64) throws IllegalStateException
RandomDataOutput
writeVolatileLong
in interface RandomDataOutput<NativeBytesStore<Void>>
writeVolatileLong
in class NativeBytesStore<Void>
offset
- The non-negative position within the data stream to write the long integer to.i64
- The long integer value to write.IllegalStateException
- if released@NotNull public @NotNull MappedBytesStore write(long offsetInRDO, byte[] byteArray, int offset, int length) throws IllegalStateException
RandomDataOutput
byteArray
to this Bytes object starting at writeOffset
taking
content starting at readOffset
but copying at most length
bytes.
Does not update cursors e.g. writePosition()
write
in interface RandomDataOutput<NativeBytesStore<Void>>
write
in class NativeBytesStore<Void>
offsetInRDO
- the non-negative offset within the data output where the segment should be written.byteArray
- the byte array containing the segment to be written.offset
- the non-negative offset within the byte array where the segment begins.length
- the non-negative length of the segment.IllegalStateException
- if this data output has been previously released.public void write(long offsetInRDO, @NotNull @NotNull ByteBuffer bytes, int offset, int length) throws IllegalStateException
RandomDataOutput
Does not update cursors e.g. writePosition()
write
in interface RandomDataOutput<NativeBytesStore<Void>>
write
in class NativeBytesStore<Void>
offsetInRDO
- the non-negative offset within the data output where the segment should be written.bytes
- the ByteBuffer containing the segment to be written.offset
- the non-negative offset within the ByteBuffer where the segment begins.length
- the non-negative length of the segment.IllegalStateException
- if this data output has been previously released.@NotNull public @NotNull MappedBytesStore write(long writeOffset, @NotNull @NotNull RandomDataInput bytes, long readOffset, long length) throws BufferOverflowException, BufferUnderflowException, IllegalStateException
RandomDataOutput
writePosition()
of this output nor the RandomCommon.readPosition()
of the input.write
in interface RandomDataOutput<NativeBytesStore<Void>>
write
in class NativeBytesStore<Void>
writeOffset
- the non-negative offset within this data output where the segment should be written.bytes
- the RandomDataInput source containing the segment to be written.readOffset
- the non-negative offset within the source where the segment begins.length
- the non-negative length of the segment.BufferOverflowException
- if the capacity of this data output was exceeded.BufferUnderflowException
- if the source does not have enough data to fill the length.IllegalStateException
- if this data output has been previously released.public void write0(long offsetInRDO, @NotNull @NotNull RandomDataInput bytes, long offset, long length) throws IllegalStateException
write0
in class NativeBytesStore<Void>
IllegalStateException
public void nativeWrite(long address, long position, long size) throws IllegalStateException
RandomDataOutput
nativeWrite
in interface RandomDataOutput<NativeBytesStore<Void>>
nativeWrite
in class NativeBytesStore<Void>
address
- the address in the native memory from where data should be copied.position
- the position in the BytesStore where data should be written.size
- the size of the data, in bytes, to be copied from the native memory.IllegalStateException
- if this BytesStore has been previously released.public long appendUtf8(long pos, char[] chars, int offset, int length) throws IllegalStateException
appendUtf8
in class NativeBytesStore<Void>
IllegalStateException
protected void performRelease()
performRelease
in class NativeBytesStore<Void>
public SyncMode syncMode()
public void syncMode(SyncMode syncMode)
syncMode
- to usepublic void syncUpTo(long position)
position
- to sync with the syncMode()public long writePosition()
The write position is readPosition() <= writePosition() && writePosition() <= writeLimit()
If the resource is closed, the returned value is unspecified.
public long lengthWritten(long startPosition)
Typically this calculates the difference however for HexDumpBytes it's not as simple.
If the resource is closed, the returned value is unspecified.
startPosition
- The position to calculate the length from.public long realReadRemaining()
If the resource is closed, the returned value is unspecified.
public long realWriteRemaining()
If the resource is closed, the returned value is unspecified.
public long readLimit()
If the resource is closed, the returned value is unspecified.
public long writeLimit()
If the resource is closed, the returned value is unspecified.
public long addressForRead(long offset, int buffer) throws UnsupportedOperationException, BufferUnderflowException, IllegalStateException
offset
- The offset within this buffer.buffer
- The buffer index.UnsupportedOperationException
- if the underlying buffer is on the heap.BufferUnderflowException
- if the offset is before the start or after the capacity.IllegalStateException
- if the buffer has been closed.public ByteOrder byteOrder()
Copyright © 2023. All rights reserved.