Package org.eclipse.rdf4j.sail.lmdb
Class Varint
java.lang.Object
org.eclipse.rdf4j.sail.lmdb.Varint
Encodes and decodes unsigned values using variable-length encoding.
-
Nested Class Summary
Modifier and TypeClassDescriptionstatic class
A matcher for partial equality tests of varint lists. -
Method Summary
Modifier and TypeMethodDescriptionstatic int
calcLengthUnsigned
(long value) Calculates required length in bytes to encode the given long value using variable-length encoding.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.static int
firstToLength
(byte a0) Determines length of an encoded varint value by inspecting the first byte.static long
readListElementUnsigned
(ByteBuffer bb, int index) Decodes a single element of a list of variable-length long values from a buffer.static void
readListUnsigned
(ByteBuffer bb, int[] indexMap, long[] values) Decodes multiple values using variable-length encoding from the given buffer.static void
readListUnsigned
(ByteBuffer bb, long[] values) Decodes multiple values using variable-length encoding from the given buffer.static long
Decodes a value using the variable-length encoding of SQLite.static long
readUnsigned
(ByteBuffer bb, int pos) Decodes a value using the variable-length encoding of SQLite.static void
writeListUnsigned
(ByteBuffer bb, long[] values) Encodes multiple values using variable-length encoding into the given buffer.static void
writeUnsigned
(ByteBuffer bb, long value) Encodes a value using the variable-length encoding of SQLite.
-
Method Details
-
writeUnsigned
Encodes a value using the variable-length encoding of SQLite.The encoding has the following properties:
- Smaller (and more common) values use fewer bytes and take up less space than larger (and less common) values.
- The length of any varint can be determined by looking at just the first byte of the encoding.
- 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 bytesvalue
- 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 valueb
- second valuec
- third valued
- fourth value- Returns:
- length in bytes
-
readUnsigned
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
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
Decodes a single element of a list of variable-length long values from a buffer.- Parameters:
bb
- buffer for reading bytesindex
- the element's index- Returns:
- the decoded value
-
writeListUnsigned
Encodes multiple values using variable-length encoding into the given buffer.- Parameters:
bb
- buffer for writing bytesvalues
- array with values to write
-
readListUnsigned
Decodes multiple values using variable-length encoding from the given buffer.- Parameters:
bb
- buffer for writing bytesvalues
- array for the result values
-
readListUnsigned
Decodes multiple values using variable-length encoding from the given buffer.- Parameters:
bb
- buffer for writing bytesindexMap
- map for indexes of values within values arrayvalues
- array for the result values
-