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's Buffer.position() and having a length of Buffer.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>
      int
      compare​(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 type
      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}
      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 0
      static <L,​R>
      boolean
      equals​(L left, ValueAccessor<L> leftAccessor, R right, ValueAccessor<R> rightAccessor)  
      ValueAccessor.ObjectFactory<V> factory()
      returns the ValueAccessor.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}
      double getDouble​(V value, int offset)  
      float getFloat​(V value, int 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 long getUnsignedVInt​(V value, int offset)  
      default int getUnsignedVInt32​(V value, int offset)  
      default long getVInt​(V value, int offset)  
      default int getVInt32​(V value, int offset)  
      default int hashCode​(V value)  
      default boolean isEmpty​(V value)  
      default boolean isEmptyFromOffset​(V value, int offset)  
      int putByte​(V dst, int offset, byte value)
      writes the byte value {@param value} to {@param dst} at offset {@param offset}
      default int putBytes​(V dst, int offset, byte[] src)  
      default int putBytes​(V dst, int offset, byte[] src, int srcOffset, int length)  
      int putFloat​(V dst, int offset, float value)
      writes the float value {@param value} to {@param dst} at offset {@param 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}
      default int putUnsignedVInt​(V dst, int offset, long value)  
      default int putUnsignedVInt32​(V dst, int offset, int value)  
      default int putVInt​(V dst, int offset, long value)  
      default int putVInt32​(V dst, int offset, int value)  
      V read​(DataInputPlus in, int length)
      Reads a value of {@param length} bytes from {@param in}
      default int remaining​(V value, int offset)  
      int size​(V value)  
      default int sizeFromOffset​(V value, int offset)  
      default int sizeWithShortLength​(V value)
      serialized size including a short length prefix
      default int sizeWithVIntLength​(V value)
      serializes size including a vint length prefix
      V 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 as slice(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}.
      Ballot toBallot​(V value)
      returns a TimeUUID from offset 0
      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
      byte toByte​(V value)
      returns a byte from offset 0
      double toDouble​(V value)
      returns a double from offset 0
      float toFloat​(V value)
      returns a float from offset 0
      float[] toFloatArray​(V value, int dimension)
      returns a float[] from offset 0
      java.lang.String toHex​(V value)  
      int toInt​(V value)
      returns an int from offset 0
      long toLong​(V value)
      returns a long from offset 0
      short toShort​(V value)
      returns a short from offset 0
      default java.lang.String toString​(V value)  
      java.lang.String toString​(V value, java.nio.charset.Charset charset)  
      TimeUUID toTimeUUID​(V value)
      returns a TimeUUID from offset 0
      java.util.UUID toUUID​(V value)
      returns a UUID from offset 0
      V 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 charset
      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}
      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 ByteBuffer
      void write​(V value, DataOutputPlus out)
      Write the contents of the given value into the a DataOutputPlus
      default 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
      • remaining

        default int remaining​(V value,
                              int offset)
      • 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 as slice(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}
      • getUnsignedVInt

        default long getUnsignedVInt​(V value,
                                     int offset)
      • getUnsignedVInt32

        default int getUnsignedVInt32​(V value,
                                      int offset)
      • getVInt

        default long getVInt​(V value,
                             int offset)
      • getVInt32

        default int getVInt32​(V value,
                              int offset)
      • getFloat

        float getFloat​(V value,
                       int offset)
      • getDouble

        double getDouble​(V value,
                         int 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
      • toTimeUUID

        TimeUUID toTimeUUID​(V value)
        returns a TimeUUID from offset 0
      • toBallot

        Ballot toBallot​(V value)
        returns a TimeUUID from offset 0
      • toFloatArray

        float[] toFloatArray​(V value,
                             int dimension)
        returns a float[] from offset 0
      • putByte

        int putByte​(V dst,
                    int offset,
                    byte value)
        writes the byte value {@param value} to {@param dst} at offset {@param offset}
        Returns:
        the number of bytes written to {@param value}
      • 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}
      • putFloat

        int putFloat​(V dst,
                     int offset,
                     float value)
        writes the float value {@param value} to {@param dst} at offset {@param offset}
        Returns:
        the number of bytes written to {@param value}
      • putBytes

        default int putBytes​(V dst,
                             int offset,
                             byte[] src,
                             int srcOffset,
                             int length)
      • putBytes

        default int putBytes​(V dst,
                             int offset,
                             byte[] src)
      • putUnsignedVInt

        default int putUnsignedVInt​(V dst,
                                    int offset,
                                    long value)
      • putUnsignedVInt32

        default int putUnsignedVInt32​(V dst,
                                      int offset,
                                      int value)
      • putVInt

        default int putVInt​(V dst,
                            int offset,
                            long value)
      • putVInt32

        default int putVInt32​(V dst,
                              int offset,
                              int 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.
      • compare

        static <L,​R> int compare​(L left,
                                       ValueAccessor<L> leftAccessor,
                                       R right,
                                       ValueAccessor<R> rightAccessor)
        lexicographically compare {@param left} to {@param right}