public class ZOrderByteUtils
extends java.lang.Object
All types except for String are stored within an 8 Byte Buffer
Most of these techniques are derived from https://aws.amazon.com/blogs/database/z-order-indexing-for-multifaceted-queries-in-amazon-dynamodb-part-2/
Some implementation is taken from https://github.com/apache/hbase/blob/master/hbase-common/src/main/java/org/apache/hadoop/hbase/util/OrderedBytes.java
Modifier and Type | Field and Description |
---|---|
static int |
PRIMITIVE_BUFFER_SIZE |
Modifier and Type | Method and Description |
---|---|
static java.nio.ByteBuffer |
byteTruncateOrFill(byte[] val,
int length,
java.nio.ByteBuffer reuse)
Return a bytebuffer with the given bytes truncated to length, or filled with 0's to length
depending on whether the given bytes are larger or smaller than the given length.
|
static java.nio.ByteBuffer |
doubleToOrderedBytes(double val,
java.nio.ByteBuffer reuse)
Doubles are treated the same as floats in
floatToOrderedBytes(float, ByteBuffer) |
static java.nio.ByteBuffer |
floatToOrderedBytes(float val,
java.nio.ByteBuffer reuse)
IEEE 754 : “If two floating-point numbers in the same format are ordered (say, x <
y), they are ordered the same way when their bits are reinterpreted as sign-magnitude
integers.”
|
static byte[] |
interleaveBits(byte[][] columnsBinary,
int interleavedSize,
java.nio.ByteBuffer reuse)
Interleave bits using a naive loop.
|
static java.nio.ByteBuffer |
intToOrderedBytes(int val,
java.nio.ByteBuffer reuse)
Signed ints do not have their bytes in magnitude order because of the sign bit.
|
static java.nio.ByteBuffer |
longToOrderedBytes(long val,
java.nio.ByteBuffer reuse)
Signed longs are treated the same as the signed ints in
intToOrderedBytes(int,
ByteBuffer) |
static java.nio.ByteBuffer |
shortToOrderedBytes(short val,
java.nio.ByteBuffer reuse)
Signed shorts are treated the same as the signed ints in
intToOrderedBytes(int,
ByteBuffer) |
static java.nio.ByteBuffer |
stringToOrderedBytes(java.lang.String val,
int length,
java.nio.ByteBuffer reuse,
java.nio.charset.CharsetEncoder encoder)
Strings are lexicographically sortable BUT if different byte array lengths will ruin the
Z-Ordering.
|
static java.nio.ByteBuffer |
tinyintToOrderedBytes(byte val,
java.nio.ByteBuffer reuse)
Signed tiny ints are treated the same as the signed ints in
intToOrderedBytes(int,
ByteBuffer) |
public static final int PRIMITIVE_BUFFER_SIZE
public static java.nio.ByteBuffer intToOrderedBytes(int val, java.nio.ByteBuffer reuse)
public static java.nio.ByteBuffer longToOrderedBytes(long val, java.nio.ByteBuffer reuse)
intToOrderedBytes(int,
ByteBuffer)
public static java.nio.ByteBuffer shortToOrderedBytes(short val, java.nio.ByteBuffer reuse)
intToOrderedBytes(int,
ByteBuffer)
public static java.nio.ByteBuffer tinyintToOrderedBytes(byte val, java.nio.ByteBuffer reuse)
intToOrderedBytes(int,
ByteBuffer)
public static java.nio.ByteBuffer floatToOrderedBytes(float val, java.nio.ByteBuffer reuse)
Which means floats can be treated as sign magnitude integers which can then be converted into lexicographically comparable bytes
public static java.nio.ByteBuffer doubleToOrderedBytes(double val, java.nio.ByteBuffer reuse)
floatToOrderedBytes(float, ByteBuffer)
public static java.nio.ByteBuffer stringToOrderedBytes(java.lang.String val, int length, java.nio.ByteBuffer reuse, java.nio.charset.CharsetEncoder encoder)
public static java.nio.ByteBuffer byteTruncateOrFill(byte[] val, int length, java.nio.ByteBuffer reuse)
public static byte[] interleaveBits(byte[][] columnsBinary, int interleavedSize, java.nio.ByteBuffer reuse)
columnsBinary
- an array of ordered byte representations of the columns being ZOrderedinterleavedSize
- the number of bytes to use in the output