Class Varint

java.lang.Object
org.eclipse.rdf4j.sail.lmdb.Varint

public final class Varint extends Object
Encodes and decodes unsigned values using variable-length encoding.
  • Method Details

    • writeUnsigned

      public static void writeUnsigned(ByteBuffer bb, long value)
      Encodes a value using the variable-length encoding of SQLite.

      The encoding has the following properties:

      1. Smaller (and more common) values use fewer bytes and take up less space than larger (and less common) values.
      2. The length of any varint can be determined by looking at just the first byte of the encoding.
      3. Lexicographical and numeric ordering for varints are the same. Hence if a group of varints are ordered lexicographically (that is to say, if they are ordered by Arrays.compare(byte[], byte[]) with shorter varints coming first) then those varints will also be in numeric order. This property means that varints can be used as keys in the key/value backend storage and the records will occur in numerical order of the keys.

      A0 Value
      0–240 A0
      241–248 240 + 256 × (A0 – 241) + A1
      249 2288 + 256 × A1 + A2
      250 A1…A3 as a 3-byte big-endian integer
      251 A1…A4 as a 4-byte big-endian integer
      252 A1…A5 as a 5-byte big-endian integer
      253 A1…A6 as a 6-byte big-endian integer
      254 A1…A7 as a 7-byte big-endian integer
      255 A1…A8 as a 8-byte big-endian integer

      Parameters:
      bb - buffer for writing bytes
      value - value to encode
    • calcLengthUnsigned

      public static int calcLengthUnsigned(long value)
      Calculates required length in bytes to encode the given long value using variable-length encoding.
      Parameters:
      value - the value value
      Returns:
      length in bytes
    • calcListLengthUnsigned

      public static int calcListLengthUnsigned(long a, long b, long c, long d)
      Calculates required length in bytes to encode a list of four long values using variable-length encoding.
      Parameters:
      a - first value
      b - second value
      c - third value
      d - fourth value
      Returns:
      length in bytes
    • readUnsigned

      public static long readUnsigned(ByteBuffer bb) throws IllegalArgumentException
      Decodes a value using the variable-length encoding of SQLite.
      Parameters:
      bb - buffer for reading bytes
      Returns:
      decoded value
      Throws:
      IllegalArgumentException - if encoded varint is longer than 9 bytes
      See Also:
    • readUnsigned

      public static long readUnsigned(ByteBuffer bb, int pos) throws IllegalArgumentException
      Decodes a value using the variable-length encoding of SQLite.
      Parameters:
      bb - buffer for reading bytes
      Returns:
      decoded value
      Throws:
      IllegalArgumentException - if encoded varint is longer than 9 bytes
      See Also:
    • firstToLength

      public static int firstToLength(byte a0)
      Determines length of an encoded varint value by inspecting the first byte.
      Parameters:
      a0 - first byte of varint value
      Returns:
      decoded value
    • readListElementUnsigned

      public static long readListElementUnsigned(ByteBuffer bb, int index)
      Decodes a single element of a list of variable-length long values from a buffer.
      Parameters:
      bb - buffer for reading bytes
      index - the element's index
      Returns:
      the decoded value
    • writeListUnsigned

      public static void writeListUnsigned(ByteBuffer bb, long[] values)
      Encodes multiple values using variable-length encoding into the given buffer.
      Parameters:
      bb - buffer for writing bytes
      values - array with values to write
    • readListUnsigned

      public static void readListUnsigned(ByteBuffer bb, long[] values)
      Decodes multiple values using variable-length encoding from the given buffer.
      Parameters:
      bb - buffer for writing bytes
      values - array for the result values
    • readListUnsigned

      public static void readListUnsigned(ByteBuffer bb, int[] indexMap, long[] values)
      Decodes multiple values using variable-length encoding from the given buffer.
      Parameters:
      bb - buffer for writing bytes
      indexMap - map for indexes of values within values array
      values - array for the result values