Package org.apache.cassandra.db.marshal
Interface ValueAccessor<V>
-
- Type Parameters:
V
- the backing type
- All Known Implementing Classes:
ByteArrayAccessor
,ByteBufferAccessor
public interface ValueAccessor<V>
ValueAccessor allows serializers and other code dealing with raw bytes to operate on different backing types (ie: byte arrays, byte buffers, etc) without requiring that the supported backing types share a common type ancestor and without incuring the allocation cost of a wrapper object. A note on byte buffers for implementors: the "value" of a byte buffer is always interpreted as beginning at it'sBuffer.position()
and having a length ofBuffer.remaining()
. ValueAccessor implementations need to maintain this internally. ValueAccessors should also never modify the state of the byte buffers view (ie: offset, limit). This would also apply to value accessors for simlilar types (ie: netty's ByteBuf}.
-
-
Nested Class Summary
Nested Classes Modifier and Type Interface Description static interface
ValueAccessor.ObjectFactory<V>
Creates db objects using the given accessors value type.
-
Method Summary
All Methods Static Methods Instance Methods Abstract Methods Default Methods Modifier and Type Method Description V
allocate(int size)
Allocate and return a {@param} instance of {@param size} bytes on the heap. static <L,R>
intcompare(L left, ValueAccessor<L> leftAccessor, R right, ValueAccessor<R> rightAccessor)
lexicographically compare {@param left} to {@param right}<VR> int
compare(V left, VR right, ValueAccessor<VR> accessorR)
lexicographically compare {@param left} to {@param right}int
compareByteArrayTo(byte[] left, V right)
compare a byte array on the left with a {@param} on the right} int
compareByteBufferTo(java.nio.ByteBuffer left, V right)
compare a byte buffer on the left with a {@param} on the right} <V2> V
convert(V2 src, ValueAccessor<V2> accessor)
Convert the data in {@param src} to {@param} {@param src} and the returned value may share a common byte array instance, so caller should assume that modifying the returned value will also modify the contents of {@param src} int
copyByteArrayTo(byte[] src, int srcOffset, V dst, int dstOffset, int size)
copies a byte array into this accessors value.int
copyByteBufferTo(java.nio.ByteBuffer src, int srcOffset, V dst, int dstOffset, int size)
copies a byte buffer into this accessors value.<V2> int
copyTo(V src, int srcOffset, V2 dst, ValueAccessor<V2> dstAccessor, int dstOffset, int size)
copy the {@param size} bytes from the {@param src} value, starting at the offset {@param srcOffset} into the {@param dst} value, starting at the offset {@param dstOffset}, using the accessor {@param dstAccessor}V[]
createArray(int length)
allocate an instance of the accessors backing typevoid
digest(V value, int offset, int size, Digest digest)
updates {@param digest} with {@param size} bytes from the contents of {@param value} starting at offset {@param offset}default void
digest(V value, Digest digest)
updates {@param digest} with te contents of {@param value}V
empty()
return a value with a length of 0static <L,R>
booleanequals(L left, ValueAccessor<L> leftAccessor, R right, ValueAccessor<R> rightAccessor)
ValueAccessor.ObjectFactory<V>
factory()
returns theValueAccessor.ObjectFactory
for the backing type {@param} default boolean
getBoolean(V value, int offset)
returns a boolean from offset {@param offset}byte
getByte(V value, int offset)
returns a byte from offset {@param offset}int
getInt(V value, int offset)
returns an int from offset {@param offset}long
getLong(V value, int offset)
returns a long from offset {@param offset}short
getShort(V value, int offset)
returns a short from offset {@param offset}int
getUnsignedShort(V value, int offset)
returns an unsigned short from offset {@param offset}default int
hashCode(V value)
default boolean
isEmpty(V value)
default boolean
isEmptyFromOffset(V value, int offset)
int
putInt(V dst, int offset, int value)
writes the int value {@param value} to {@param dst} at offset {@param offset}int
putLong(V dst, int offset, long value)
writes the long value {@param value} to {@param dst} at offset {@param offset}int
putShort(V dst, int offset, short value)
writes the short value {@param value} to {@param dst} at offset {@param offset}V
read(DataInputPlus in, int length)
Reads a value of {@param length} bytes from {@param in}int
size(V value)
default int
sizeFromOffset(V value, int offset)
default int
sizeWithShortLength(V value)
serialized size including a short length prefixdefault int
sizeWithVIntLength(V value)
serializes size including a vint length prefixV
slice(V input, int offset, int length)
Returns a value with the contents of {@param input} from {@param offset} to {@param length}.default V
sliceWithShortLength(V input, int offset)
same asslice(Object, int, int)
, except the length is taken from the first 2 bytes from the given offset (and not included in the return value)byte[]
toArray(V value)
returns a byte[] with the contents of {@param value} Depending on the accessor implementation, this method may: * allocate a new byte[] object and copy data into it * return the value, if the backing type is byte[]byte[]
toArray(V value, int offset, int length)
returns a byte[] with {@param length} bytes copied from the contents of {@param value} starting at offset {@param offset}.java.nio.ByteBuffer
toBuffer(V value)
returns a ByteBuffer with the contents of {@param value} Depending on the accessor implementation, this method may: * allocate a new ByteBuffer and copy data into it * return the value, if the backing type is a bytebufferbyte
toByte(V value)
returns a byte from offset 0double
toDouble(V value)
returns a double from offset 0float
toFloat(V value)
returns a float from offset 0java.lang.String
toHex(V value)
int
toInt(V value)
returns an int from offset 0long
toLong(V value)
returns a long from offset 0short
toShort(V value)
returns a short from offset 0default java.lang.String
toString(V value)
java.lang.String
toString(V value, java.nio.charset.Charset charset)
java.util.UUID
toUUID(V value)
returns a UUID from offset 0V
valueOf(boolean v)
return a value with the bytes from {@param v}V
valueOf(byte v)
return a value with the bytes from {@param v}V
valueOf(byte[] bytes)
return a value containing the {@param bytes} Caller should assume that modifying the returned value will also modify the contents of {@param bytes}V
valueOf(double v)
return a value with the bytes from {@param v}V
valueOf(float v)
return a value with the bytes from {@param v}V
valueOf(int v)
return a value with the bytes from {@param v}V
valueOf(long v)
return a value with the bytes from {@param v}V
valueOf(short v)
return a value with the bytes from {@param v}V
valueOf(java.lang.String s, java.nio.charset.Charset charset)
return a value containing the bytes for the given string and charsetV
valueOf(java.nio.ByteBuffer bytes)
return a value containing the {@param bytes} {@param src} and the returned value may share a common byte array instance, so caller should assume that modifying the returned value will also modify the contents of {@param src}V
valueOf(java.util.UUID v)
return a value with the bytes from {@param v}void
write(V value, java.nio.ByteBuffer out)
Write the contents of the given value into the ByteBuffervoid
write(V value, DataOutputPlus out)
Write the contents of the given value into the a DataOutputPlusdefault void
writeWithVIntLength(V value, DataOutputPlus out)
-
-
-
Method Detail
-
size
int size(V value)
- Returns:
- the size of the given value
-
sizeWithVIntLength
default int sizeWithVIntLength(V value)
serializes size including a vint length prefix
-
sizeWithShortLength
default int sizeWithShortLength(V value)
serialized size including a short length prefix
-
isEmpty
default boolean isEmpty(V value)
- Returns:
- true if the size of the given value is zero, false otherwise
-
sizeFromOffset
default int sizeFromOffset(V value, int offset)
- Returns:
- the number of bytes remaining in the value from the given offset
-
isEmptyFromOffset
default boolean isEmptyFromOffset(V value, int offset)
- Returns:
- true if there are no bytes present after the given offset, false otherwise
-
createArray
V[] createArray(int length)
allocate an instance of the accessors backing type- Parameters:
length
- size of backing typ to allocate
-
write
void write(V value, DataOutputPlus out) throws java.io.IOException
Write the contents of the given value into the a DataOutputPlus- Throws:
java.io.IOException
-
writeWithVIntLength
default void writeWithVIntLength(V value, DataOutputPlus out) throws java.io.IOException
- Throws:
java.io.IOException
-
write
void write(V value, java.nio.ByteBuffer out)
Write the contents of the given value into the ByteBuffer
-
copyTo
<V2> int copyTo(V src, int srcOffset, V2 dst, ValueAccessor<V2> dstAccessor, int dstOffset, int size)
copy the {@param size} bytes from the {@param src} value, starting at the offset {@param srcOffset} into the {@param dst} value, starting at the offset {@param dstOffset}, using the accessor {@param dstAccessor}- Type Parameters:
V2
- the destination value type- Returns:
- the number of bytes copied ({@param size})
-
copyByteArrayTo
int copyByteArrayTo(byte[] src, int srcOffset, V dst, int dstOffset, int size)
copies a byte array into this accessors value.
-
copyByteBufferTo
int copyByteBufferTo(java.nio.ByteBuffer src, int srcOffset, V dst, int dstOffset, int size)
copies a byte buffer into this accessors value.
-
digest
void digest(V value, int offset, int size, Digest digest)
updates {@param digest} with {@param size} bytes from the contents of {@param value} starting at offset {@param offset}
-
digest
default void digest(V value, Digest digest)
updates {@param digest} with te contents of {@param value}
-
read
V read(DataInputPlus in, int length) throws java.io.IOException
Reads a value of {@param length} bytes from {@param in}- Throws:
java.io.IOException
-
slice
V slice(V input, int offset, int length)
Returns a value with the contents of {@param input} from {@param offset} to {@param length}. Depending on the accessor implementation, this method may: * allocate a new {@param} object of {@param length}, and copy data into it * return a view of {@param input} where changes to one will be reflected in the other
-
sliceWithShortLength
default V sliceWithShortLength(V input, int offset)
same asslice(Object, int, int)
, except the length is taken from the first 2 bytes from the given offset (and not included in the return value)
-
compare
<VR> int compare(V left, VR right, ValueAccessor<VR> accessorR)
lexicographically compare {@param left} to {@param right}- Type Parameters:
VR
- backing type of
-
compareByteArrayTo
int compareByteArrayTo(byte[] left, V right)
compare a byte array on the left with a {@param} on the right}
-
compareByteBufferTo
int compareByteBufferTo(java.nio.ByteBuffer left, V right)
compare a byte buffer on the left with a {@param} on the right}
-
hashCode
default int hashCode(V value)
-
toBuffer
java.nio.ByteBuffer toBuffer(V value)
returns a ByteBuffer with the contents of {@param value} Depending on the accessor implementation, this method may: * allocate a new ByteBuffer and copy data into it * return the value, if the backing type is a bytebuffer
-
toArray
byte[] toArray(V value)
returns a byte[] with the contents of {@param value} Depending on the accessor implementation, this method may: * allocate a new byte[] object and copy data into it * return the value, if the backing type is byte[]
-
toArray
byte[] toArray(V value, int offset, int length)
returns a byte[] with {@param length} bytes copied from the contents of {@param value} starting at offset {@param offset}. Depending on the accessor implementation, this method may: * allocate a new byte[] object and copy data into it * return the value, if the backing type is byte[], offset is 0 and {@param length} == size(value)
-
toString
java.lang.String toString(V value, java.nio.charset.Charset charset) throws java.nio.charset.CharacterCodingException
- Throws:
java.nio.charset.CharacterCodingException
-
toString
default java.lang.String toString(V value) throws java.nio.charset.CharacterCodingException
- Throws:
java.nio.charset.CharacterCodingException
-
toHex
java.lang.String toHex(V value)
-
getBoolean
default boolean getBoolean(V value, int offset)
returns a boolean from offset {@param offset}
-
toByte
byte toByte(V value)
returns a byte from offset 0
-
getByte
byte getByte(V value, int offset)
returns a byte from offset {@param offset}
-
toShort
short toShort(V value)
returns a short from offset 0
-
getShort
short getShort(V value, int offset)
returns a short from offset {@param offset}
-
getUnsignedShort
int getUnsignedShort(V value, int offset)
returns an unsigned short from offset {@param offset}
-
toInt
int toInt(V value)
returns an int from offset 0
-
getInt
int getInt(V value, int offset)
returns an int from offset {@param offset}
-
toLong
long toLong(V value)
returns a long from offset 0
-
getLong
long getLong(V value, int offset)
returns a long from offset {@param offset}
-
toFloat
float toFloat(V value)
returns a float from offset 0
-
toDouble
double toDouble(V value)
returns a double from offset 0
-
toUUID
java.util.UUID toUUID(V value)
returns a UUID from offset 0
-
putShort
int putShort(V dst, int offset, short value)
writes the short value {@param value} to {@param dst} at offset {@param offset}- Returns:
- the number of bytes written to {@param value}
-
putInt
int putInt(V dst, int offset, int value)
writes the int value {@param value} to {@param dst} at offset {@param offset}- Returns:
- the number of bytes written to {@param value}
-
putLong
int putLong(V dst, int offset, long value)
writes the long value {@param value} to {@param dst} at offset {@param offset}- Returns:
- the number of bytes written to {@param value}
-
empty
V empty()
return a value with a length of 0
-
valueOf
V valueOf(byte[] bytes)
return a value containing the {@param bytes} Caller should assume that modifying the returned value will also modify the contents of {@param bytes}
-
valueOf
V valueOf(java.nio.ByteBuffer bytes)
return a value containing the {@param bytes} {@param src} and the returned value may share a common byte array instance, so caller should assume that modifying the returned value will also modify the contents of {@param src}
-
valueOf
V valueOf(java.lang.String s, java.nio.charset.Charset charset)
return a value containing the bytes for the given string and charset
-
valueOf
V valueOf(java.util.UUID v)
return a value with the bytes from {@param v}
-
valueOf
V valueOf(boolean v)
return a value with the bytes from {@param v}
-
valueOf
V valueOf(byte v)
return a value with the bytes from {@param v}
-
valueOf
V valueOf(short v)
return a value with the bytes from {@param v}
-
valueOf
V valueOf(int v)
return a value with the bytes from {@param v}
-
valueOf
V valueOf(long v)
return a value with the bytes from {@param v}
-
valueOf
V valueOf(float v)
return a value with the bytes from {@param v}
-
valueOf
V valueOf(double v)
return a value with the bytes from {@param v}
-
convert
<V2> V convert(V2 src, ValueAccessor<V2> accessor)
Convert the data in {@param src} to {@param} {@param src} and the returned value may share a common byte array instance, so caller should assume that modifying the returned value will also modify the contents of {@param src}
-
allocate
V allocate(int size)
Allocate and return a {@param} instance of {@param size} bytes on the heap.
-
factory
ValueAccessor.ObjectFactory<V> factory()
returns theValueAccessor.ObjectFactory
for the backing type {@param}
-
compare
static <L,R> int compare(L left, ValueAccessor<L> leftAccessor, R right, ValueAccessor<R> rightAccessor)
lexicographically compare {@param left} to {@param right}
-
equals
static <L,R> boolean equals(L left, ValueAccessor<L> leftAccessor, R right, ValueAccessor<R> rightAccessor)
-
-