Interface Resource
- All Superinterfaces:
AutoCloseable
- All Known Subinterfaces:
Buffer,Memory,Positional,WritableBuffer,WritableMemory
- Author:
- Lee Rhodes
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final MemoryRequestServerThe default MemoryRequestServer is used if not specified by the user. -
Method Summary
Modifier and TypeMethodDescriptionvoidclose()Closes this resource if this can be closed via AutoCloseable.booleanReturns true if the given Resource has equal contents to this object in the given range of bytes.default booleanReturns true if the given object (that) is an instance of this class and has contents equal to this object.longGets the capacity of this object in bytesdefault longGets the cumulative offset in bytes of this object from the backing resource.longgetCumulativeOffset(long offsetBytes) Gets the cumulative offset in bytes of this object from the backing resource including the given offsetBytes.Gets the MemoryRequestServer object, if set, for the below resources to request additional memory.longReturns the offset of address zero of this object relative to the base address of the backing resource.Gets the current ByteOrder.booleanReturns true if this Memory is backed by a ByteBuffer.booleanReturns true if the MemoryRequestServer has been configured by the user.booleanisAlive()Returns true if this object is alive and has not been closed.booleanisByteOrderCompatible(ByteOrder byteOrder) Returns true if the Native ByteOrder is the same as the ByteOrder of the current Buffer or Memory and the same ByteOrder as the given byteOrder.booleanReturn true if this resource is closeable.booleanisDirect()If true, the backing resource is off-heap memory.booleanReturns true if this instance is a duplicate of a Buffer instance.booleanisHeap()Returns true if this object is backed by an on-heap primitive array or an on-heap ByteBuffer.booleanisMemory()Iftrue, this is a Memory or WritableMemory instance, which provides the Memory API.booleanIf true, all put and get operations will assume the non-native ByteOrder.booleanReturns true if this object or the backing resource is read-only.booleanReturns true if this instance is a region view of another Memory or BufferbooleanisSameResource(Resource that) Returns true if the backing resource of this is identical with the backing resource of that.voidsetMemoryRequestServer(MemoryRequestServer memReqSvr) Sets the Default MemoryRequestServertoString()Returns a brief description of this object.Returns a description of this object with an optional formatted hex string of the data for the specified a range.longxxHash64(long in, long seed) Returns a 64-bit hash from a single long.longxxHash64(long offsetBytes, long lengthBytes, long seed) Returns the 64-bit hash of the sequence of bytes in this object specified by offsetBytes, lengthBytes and a seed.
-
Field Details
-
defaultMemReqSvr
The default MemoryRequestServer is used if not specified by the user.
-
-
Method Details
-
getMemoryRequestServer
MemoryRequestServer getMemoryRequestServer()Gets the MemoryRequestServer object, if set, for the below resources to request additional memory.WritableMemory enables this for ByteBuffer, Heap and Off-heap Memory backed resources.
WritableBuffer enables this for ByteBuffer backed resources. However, the object returned is in the form of a WritableMemory. To convert to WritableBuffer use asWritableBuffer(). To enable for Heap and Off-heap Buffer resources, use the WritableMemory to configure and then call asWritableBuffer().
Map backed resources will always return null.
The user must customize the actions of the MemoryRequestServer by implementing the MemoryRequestServer interface.
For WritableMemory, to enable at runtime set your custom MemoryRequestServer using one of these methods:
WritableMemory.allocateDirect(long, ByteOrder, MemoryRequestServer)WritableMemory.allocate(int, ByteOrder, MemoryRequestServer)WritableMemory.writableWrap(ByteBuffer, ByteOrder, MemoryRequestServer)
ForWritableBuffer, to enable at runtime set your custom MemoryRequestServer using the following method:
Simple implementation examples include the DefaultMemoryRequestServer in the main source tree, as well as the ExampleMemoryRequestServerTest and the use with ByteBuffer documented in the DruidIssue11544Test in the test source tree.
- Returns:
- the MemoryRequestServer object or null.
-
hasMemoryRequestServer
boolean hasMemoryRequestServer()Returns true if the MemoryRequestServer has been configured by the user.- Returns:
- true if the MemoryRequestServer has been configured by the user..
-
setMemoryRequestServer
Sets the Default MemoryRequestServer- Parameters:
memReqSvr- the given MemoryRequestServer.
-
close
void close()Closes this resource if this can be closed via AutoCloseable. If this operation completes without exceptions, this resource will be marked as not alive, and subsequent operations on this resource will fail withIllegalStateException.This operation is not idempotent; that is, closing an already closed resource always results in an exception being thrown. This reflects a deliberate design choice: resource state transitions should be manifest in the client code; a failure in any of these transitions reveals a bug in the underlying application logic.
- Specified by:
closein interfaceAutoCloseable- Throws:
IllegalStateException- if this Resource is not alive.IllegalStateException- if this method is not accessed from the owning thread.UnsupportedOperationException- if this resource is notAutoCloseable.
-
isCloseable
boolean isCloseable()Return true if this resource is closeable.- Returns:
- true if this resource is closeable.
-
equalTo
Returns true if the given object (that) is an instance of this class and has contents equal to this object.- Parameters:
that- the given Resource object- Returns:
- true if the given object has equal contents to this object.
- See Also:
-
equalTo
Returns true if the given Resource has equal contents to this object in the given range of bytes. This will also check two distinct ranges within the same object for equals.- Parameters:
thisOffsetBytes- the starting offset in bytes for this object.that- the given ResourcethatOffsetBytes- the starting offset in bytes for the given Resource objectlengthBytes- the size of the range in bytes- Returns:
- true if the given Resource object has equal contents to this object in the given range of bytes.
- Throws:
IllegalStateException- if either resource is not alive.MemoryBoundsException- if there is a bounds violation.
-
getCapacity
long getCapacity()Gets the capacity of this object in bytes- Returns:
- the capacity of this object in bytes
-
getCumulativeOffset
default long getCumulativeOffset()Gets the cumulative offset in bytes of this object from the backing resource. This offset may also include other offset components such as the native off-heap memory address, DirectByteBuffer split offsets, region offsets, and unsafe arrayBaseOffsets.- Returns:
- the cumulative offset in bytes of this object from the backing resource.
-
getCumulativeOffset
long getCumulativeOffset(long offsetBytes) Gets the cumulative offset in bytes of this object from the backing resource including the given offsetBytes. This offset may also include other offset components such as the native off-heap memory address, DirectByteBuffer split offsets, region offsets, and unsafe arrayBaseOffsets.- Parameters:
offsetBytes- offset to be added to the cumulative offset.- Returns:
- the cumulative offset in bytes of this object from the backing resource including the given offsetBytes.
-
getRelativeOffset
long getRelativeOffset()Returns the offset of address zero of this object relative to the base address of the backing resource. This does not include the object header for heap arrays nor the initial offset of a memory-mapped file.- Returns:
- the offset of address zero of this object relative to the base address of the backing resource.
-
getTypeByteOrder
ByteOrder getTypeByteOrder()Gets the current ByteOrder. This may be different from the ByteOrder of the backing resource andByteOrder.nativeOrder()- Returns:
- the current ByteOrder.
-
hasByteBuffer
boolean hasByteBuffer()Returns true if this Memory is backed by a ByteBuffer.- Returns:
- true if this Memory is backed by a ByteBuffer.
-
isByteOrderCompatible
Returns true if the Native ByteOrder is the same as the ByteOrder of the current Buffer or Memory and the same ByteOrder as the given byteOrder.- Parameters:
byteOrder- the given ByteOrder- Returns:
- true if the Native ByteOrder is the same as the ByteOrder of the current Buffer or Memory and the same ByteOrder as the given byteOrder.
-
isDirect
boolean isDirect()If true, the backing resource is off-heap memory. This is the case for allocated off-heap memory, memory-mapped files, or from a wrapped ByteBuffer that was allocated off-heap. If false, the backing resource is the Java heap.- Returns:
- true if the backing resource is off-heap memory.
-
isDuplicate
boolean isDuplicate()Returns true if this instance is a duplicate of a Buffer instance.- Returns:
- true if this instance is a duplicate of a Buffer instance.
-
isHeap
boolean isHeap()Returns true if this object is backed by an on-heap primitive array or an on-heap ByteBuffer.- Returns:
- true if this object is backed by an on-heap primitive array or an on-heap ByteBuffer.
-
isMemory
boolean isMemory()Iftrue, this is a Memory or WritableMemory instance, which provides the Memory API. The Memory API is the principal API for this Memory Component. It provides a rich variety of direct manipulations of four types of resources: On-heap memory, off-heap memory, memory-mapped files, and ByteBuffers. If false, this is a Buffer or WritableBuffer instance, which provides the Buffer API.The Buffer API is largely parallel to the Memory API except that it adds a positional API similar to that in ByteBuffer. The positional API is a convenience when iterating over structured arrays, or buffering input or output streams (thus the name).
- Returns:
trueif this is a Memory or WritableMemory instance, which provides the Memory API, otherwise this is a Buffer or WritableBuffer instance, which provides the Buffer API.
-
isNonNativeOrder
boolean isNonNativeOrder()If true, all put and get operations will assume the non-native ByteOrder. Otherwise, all put and get operations will assume the native ByteOrder.- Returns:
- true, if all put and get operations will assume the non-native ByteOrder.
-
isReadOnly
boolean isReadOnly()Returns true if this object or the backing resource is read-only.- Returns:
- true if this object or the backing resource is read-only.
-
isRegionView
boolean isRegionView()Returns true if this instance is a region view of another Memory or Buffer- Returns:
- true if this instance is a region view of another Memory or Buffer
-
isSameResource
Returns true if the backing resource of this is identical with the backing resource of that. The capacities must be the same. If this is a region, the region offset must also be the same.- Parameters:
that- A different non-null Resource- Returns:
- true if the backing resource of this is the same as the backing resource of that.
-
isAlive
boolean isAlive()Returns true if this object is alive and has not been closed. This is relevant only for off-heap memory and memory-mapped Files.- Returns:
- true if this object is alive and has not been closed.
-
toString
Returns a description of this object with an optional formatted hex string of the data for the specified a range. Used primarily for testing.- Parameters:
header- a descriptive headeroffsetBytes- offset bytes relative to this object startlengthBytes- number of bytes to convert to a hex stringwithData- include output listing of byte data in the given range- Returns:
- a formatted hex string in a human readable array
-
toString
String toString()Returns a brief description of this object. -
xxHash64
long xxHash64(long offsetBytes, long lengthBytes, long seed) Returns the 64-bit hash of the sequence of bytes in this object specified by offsetBytes, lengthBytes and a seed. Note that the sequence of bytes is always processed in the same order independent of endianness.- Parameters:
offsetBytes- the given offset in bytes to the first byte of the byte sequence.lengthBytes- the given length in bytes of the byte sequence.seed- the given long seed.- Returns:
- the 64-bit hash of the sequence of bytes in this object specified by offsetBytes and lengthBytes.
-
xxHash64
long xxHash64(long in, long seed) Returns a 64-bit hash from a single long. This method has been optimized for speed when only a single hash of a long is required.- Parameters:
in- A long.seed- A long valued seed.- Returns:
- the hash.
-