T
- type of the accessed objectspublic interface Data<T>
ChronicleMap
) and elements (for ChronicleSet
) throughout the Chronicle Map library.
Bytes access: bytes()
+ offset()
+ size()
.
Object access: get()
.
In most cases, each particular Data
wraps either some object or some bytes. Object
is marshalled to bytes lazily on demand, and bytes are lazily deserialized to object,
accordingly.
Modifier and Type | Method and Description |
---|---|
net.openhft.chronicle.bytes.RandomDataInput |
bytes()
Returns the accessor object to the
Data 's bytes. |
static boolean |
bytesEquivalent(Data<?> d1,
Data<?> d2)
Utility method, compares two
Data instances represent equivalent bytes sequences:
by comparing their sizes, then calling d1.equivalent(d2.bytes(), d2.offset()) . |
default boolean |
dataEquals(Object obj)
Data implementations should override Object.equals(Object) with delegation to
this method. |
default int |
dataHashCode()
Data implementations should override Object.hashCode() with delegation to
this method. |
default String |
dataToString()
Data implementations should override Object.toString() with delegation to
this method. |
default boolean |
equivalent(net.openhft.chronicle.bytes.RandomDataInput source,
long sourceOffset)
Compares bytes of this
Data to the given bytes source , starting from the
given offset. |
T |
get()
Returns object view of this
Data . |
T |
getUsing(T using)
Deserialize and return an object from the
Data 's bytes, trying to reuse the given
using object (might be null ). |
default long |
hash(net.openhft.chronicle.algo.hashing.LongHashFunction f)
Computes hash code on the bytes representation of this
Data , using the given hash
function. |
long |
offset()
Returns the offset to the
Data 's bytes sequence within the RandomDataInput ,
returned from bytes() method. |
long |
size()
Returns the size of this
Data 's bytes sequence. |
default void |
writeTo(net.openhft.chronicle.bytes.RandomDataOutput target,
long targetOffset)
Writes bytes of this
Data to the given target from the given targetOffset . |
static boolean bytesEquivalent(Data<?> d1, Data<?> d2)
Data
instances represent equivalent bytes sequences:
by comparing their sizes, then calling d1.equivalent(d2.bytes(), d2.offset())
.d1
- the first Data
to compared2
- the second Data
to compareData
instances represent equivalent bytes sequencesnet.openhft.chronicle.bytes.RandomDataInput bytes()
Data
's bytes.
If this Data
wraps some bytes, this method just returns a reference to that bytes.
If this Data
wraps an object, this method performs serialization internally and
returns a reference to the output buffer, caching the result for subsequent calls of this
method.
For safety, this interface returns read-only object, because it could expose bytes source
that must be immutable, e. g. a `char[]` array behind a String
. But in cases when the
Data
instance wraps off-heap bytes, e. g. MapEntry.value()
, it is allowed to
cast the object, returned from this method, to BytesStore
, and write into the
off-heap memory. You should only ensure that current context (e. g. MapQueryContext
)
is locked exclusively, in order to avoid data races.
long offset()
Data
's bytes sequence within the RandomDataInput
,
returned from bytes()
method. For example, the first byte of the bytes
representation of some data
is data.bytes().readByte(data.offset())
.long size()
default long hash(net.openhft.chronicle.algo.hashing.LongHashFunction f)
Data
, using the given hash
function.f
- the hash function to compute hash code usingData
's bytes, computed by the given functiondefault boolean equivalent(net.openhft.chronicle.bytes.RandomDataInput source, long sourceOffset)
Data
to the given bytes source
, starting from the
given offset.
Default implementation compares bytes()
of this Data
, but custom
implementation may only check if object of this Data
would
be serialized to the same bytes sequence, if this Data
wraps an object and obtaining
bytes()
requires serialization internally.
source
- the bytes source, to compare this Data
's bytes withsourceOffset
- the offset in the bytes source, the bytes sequence starts fromtrue
if the given bytes sequence is equivalent to this Data
's bytes,
byte-by-bytedefault void writeTo(net.openhft.chronicle.bytes.RandomDataOutput target, long targetOffset)
Data
to the given target
from the given targetOffset
.
Default implementation copies bytes()
of this Data
using standard IO
methods of RandomDataInput
and RandomDataOutput
, but custom implementation
may write directly from object, if this Data
is object-based and
obtaining bytes()
requires serialization internally. This allows to avoid double
copy.
target
- the destination to write this data bytes totargetOffset
- the offset in the target, to write the bytes from.T get()
Data
.
If this Data
wraps some object, this method just returns that object.
If this Data
wraps some bytes, this method performs deserialization internally
and returns the resulting on-heap object, caching it for subsequent calls of this method. The
returned object could be reused, therefore it is generally disallowed to use the
object, returned from this method, outside some context, or a block, synchronized with
locks, or lambda, etc., which provided the access to this Data
instance.
T getUsing(@Nullable T using)
Data
's bytes, trying to reuse the given
using
object (might be null
). This method either returns the given using
object back (if reuse is possible) or creates a new object, rather than some
internally cached and reused one, therefore it is always allowed to use the object,
returned from this method, outside some context, or a block, synchronized with locks,
or lambda, etc., which provided the access to this Data
instance.default int dataHashCode()
Data
implementations should override Object.hashCode()
with delegation to
this method. Computes Data
's hash code by applying a hash function to Data
's
bytes representation.default boolean dataEquals(Object obj)
Data
implementations should override Object.equals(Object)
with delegation to
this method. Compares Data
s' bytes representations.default String dataToString()
Data
implementations should override Object.toString()
with delegation to
this method. Delegates to Data
's object toString()
, i. e. equivalent
to calling get().toString()
on this Data
instance with some fallback, if
get()
method throws an exception.Copyright © 2020. All rights reserved.