final class ScalarEncoding
extends java.lang.Object
Constructor and Description 

ScalarEncoding() 
Modifier and Type  Method and Description 

static int 
encodedLength(long x)
Get the number of bytes occupied by x when encoded.

static boolean 
readBoolean(byte[] data,
boolean desc)
decode the data as a boolean.

static boolean 
readBoolean(byte[] data,
int offset,
boolean desc)
Decode the data in the array as a boolean, starting the read at the
offset position. 
static int 
readInt(byte[] data,
boolean desc)
Decode the int encoded in the byte array.

static int 
readLength(byte[] data,
int byteOffset,
boolean desc)
Read the length of the encoded long in the array, without reading the entire long.

static long 
readLong(byte[] data,
boolean desc)
Decode the data as an encoded long.

static long 
readLong(byte[] data,
int offset,
boolean desc)
Decode the data as an encoded long.

static void 
readLong(byte[] data,
int byteOffset,
boolean desc,
long[] valueAndLength) 
static long[] 
readLong(byte[] data,
int byteOffset,
int reservedBits)
Decode a long, similar to
readLong(byte[], int,boolean) , but which returns an array. 
static byte[] 
writeBoolean(boolean value,
boolean desc)
Serializes a boolean into a 1byte byte[].

static byte[] 
writeLong(long x,
boolean desc) 
static int 
writeLong(long x,
byte[] data,
int offset,
boolean desc)
Encode a long into the specified byte array, at the specified offset.

(package private) static byte[] 
writeLong(long x,
byte extraHeader,
int extraHeaderSize) 
public static byte[] writeLong(long x, boolean desc)
public static int writeLong(long x, byte[] data, int offset, boolean desc)
The first bit is a sign bit. When it is set to 1, the number is positive, otherwise it is negative. The second,third, and 4th are a packed length delimiter, which works as follows:
if x requires only 6 bits, then we use only the second bit as a length field, and pack the value into the remaining 6 bits, for a total of 1 full byte.
if x takes between 7 and 13 bits, then we need 2 bits to represent the length, so we use the second and third bit positions as a length header. Then we use the remaining 5 bits from the header, plus an additional full byte to store x.
if x takes more than 13 bits, then we encode the second and third bit (just as for a 2byte number),
then encode the length in the next 4 bits(Since 9 is the maximum number of bytes that a number
requires including the header byte, and 9 requires 4 bits to encode). This gives us 2 bits left in the header.
We can't leave those bits empty, because otherwise the sort order would be incorrect with respect
to shorterencoded numbers. To avoid this, we encode the remaining 2 bits with the most significant bits
from the base number. The length is encoded using the formula encodedLength = ((length3)^~sign)<<31
After the header byte is written, we pack the most significant bits into the remaining bytes, using bigendian encoding (which preserves the natural sort order of the data).
Example: Consider encoding the number 18278
. In this case, we know that the sign is positive, so the first
bit in the header is 1. We also know that 18278 = 0x00 0x00 0x47 0x66
in hex. Thus, we see that 18278 has
15 bits to encode. Since that is > 13, the header looks like 111x xxxx
(the x denotes an asyet undetermined bit).
We then encode the length as eLength = ((33)^~sign)<<31 = 0
, so we have 1110 00xx
. Finally,
we stuff the 2 most significant bits of the number in the remaining bytes of the header, giving 1110 0000
.
The body is encoded as the bigendian bytes of the data itself, which is 0x47 0x66
; thus, the final
encoded value is 0xE0 0x47 0x66
x
 the value to encodedata
 the array to encode into. This array must be at least as large as the number of bytes
required to encode x
, or an exception will be thrownoffset
 the location inside of data
at which to start writing the encoded contents of x
.
if offset + encodedLength(x)>=data.length
, and exception will be thrown.desc
 true
if bytes are to be sorted in descending order, false
otherwisex
java.lang.AssertionError
 if assertions are enabled and the length of data
is insufficient to hold the
encoded contents of x
java.lang.ArrayIndexOutOfBoundsException
 if assertions are disabled and the length of data
is insufficient
to hold the encoded contents of x
public static int encodedLength(long x)
x
 the value whose encoded size is to be computedpublic static long readLong(byte[] data, boolean desc)
readLong(byte[], int, boolean)
,with offset==0
.data
 the array containing the long to be decoded.desc
 when set to true
, the data is to be treated as encoded
in descending order.public static long readLong(byte[] data, int offset, boolean desc)
data
 the data to decode asoffset
 the position in the byte array to begin decoding at.desc
 when set to true
, the data is to be treated as encoded
in descending order.offset
public static int readInt(byte[] data, boolean desc)
readLong(byte[], boolean)
,
but returning an int for convenience.data
 the byte array containing the int to be decoded.desc
 when true
, the data is treated as encoded in descending order, otherwise
it's treated as encoded in ascending order.public static long[] readLong(byte[] data, int byteOffset, int reservedBits)
readLong(byte[], int,boolean)
, but which returns an array. The
first element in the array is the decoded long, and the second element is the number of bytes that were
used to store the encoded data.data
 the data storing the encoded longbyteOffset
 the offset at which to start readingreservedBits
 the number of bits in the header which are "reserved"i.e. used by something else.(x,length(x))
public static byte[] writeBoolean(boolean value, boolean desc)
When sorted in ascending order,true
is encoded as 1, and false
is encoded as 2.
When sorted in descending order, true
is encoded as 2, and false
is encoded as 1.
value
 the value to serializedesc
 if it should be sorted in descending order.value
is true, [2] otherwisepublic static boolean readBoolean(byte[] data, boolean desc)
readBoolean(byte[],int, boolean)
.data
 the data to be decodeddesc
 when true
, the data is encoded in descending order, otherwise encoded in ascending
order.public static boolean readBoolean(byte[] data, int offset, boolean desc)
offset
position.data
 the data to be decodedoffset
 the position in the array to start reading fromdesc
 when true
, the data is encoded in descending order, otherwise encoded in ascending order.public static int readLength(byte[] data, int byteOffset, boolean desc)
data
 the data to read frombyteOffset
 the offset in the array to begin reading fromdesc
 when true
, the data is encoded in descending order, otherwise encoded in ascending order.public static void readLong(byte[] data, int byteOffset, boolean desc, long[] valueAndLength)
static byte[] writeLong(long x, byte extraHeader, int extraHeaderSize)