Package org.apache.cassandra.utils.vint
Class VIntCoding
java.lang.Object
org.apache.cassandra.utils.vint.VIntCoding
Borrows idea from
https://developers.google.com/protocol-buffers/docs/encoding#varints
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic classThrow when attempting to decode a vint and the output type doesn't have enough space to fit the value that was decoded -
Field Summary
FieldsModifier and TypeFieldDescriptionprotected static final io.netty.util.concurrent.FastThreadLocal<byte[]>static final int -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic intcheckedCast(long value) static intcomputeUnsignedVIntSize(long value) Compute the number of bytes that would be needed to encode an unsigned varint.static intcomputeUnsignedVIntSize(ByteBuffer input, int readerIndex) Computes size of an unsigned vint that starts at readerIndex of the provided ByteBuf.static intcomputeUnsignedVIntSize(ByteBuffer input, int readerIndex, int readerLimit) static intcomputeVIntSize(long param) Compute the number of bytes that would be needed to encode a varint.static longdecodeZigZag64(long n) Decode a ZigZag-encoded 64-bit value.static intencodeExtraBytesToRead(int extraBytesToRead) static longencodeZigZag64(long n) Encode a ZigZag-encoded 64-bit value.static intfirstByteValueMask(int extraBytesToRead) static longgetUnsignedVInt(ByteBuffer input, int readerIndex) static longgetUnsignedVInt(ByteBuffer input, int readerIndex, int readerLimit) static <V> longgetUnsignedVInt(V input, ValueAccessor<V> accessor, int readerIndex) static <V> longgetUnsignedVInt(V input, ValueAccessor<V> accessor, int readerIndex, int readerLimit) static intgetUnsignedVInt32(ByteBuffer input, int readerIndex) Read up to a 32-bit integer back, using the unsigned (no zigzag) encoding.static <V> intgetUnsignedVInt32(V input, ValueAccessor<V> accessor, int readerIndex) static longgetVInt(ByteBuffer input, int readerIndex) static <V> longgetVInt(V input, ValueAccessor<V> accessor, int readerIndex) static intgetVInt32(ByteBuffer input, int readerIndex) static <V> intgetVInt32(V input, ValueAccessor<V> accessor, int readerIndex) static intnumberOfExtraBytesToRead(int firstByte) static longreadUnsignedVInt(DataInput input) static intreadUnsignedVInt32(DataInput input) Read up to a 32-bit integer.static longstatic intreadVInt32(DataInput input) Read up to a signed 32-bit integer back.static voidskipUnsignedVInt(DataInputPlus input) static voidwriteUnsignedVInt(int value, ByteBuffer output) Deprecated.See CASSANDRA-18099static voidwriteUnsignedVInt(int value, DataOutputPlus output) Deprecated.See CASSANDRA-18099static voidwriteUnsignedVInt(long value, ByteBuffer output) static voidwriteUnsignedVInt(long value, DataOutputPlus output) static <V> intwriteUnsignedVInt(long value, V output, int offset, ValueAccessor<V> accessor) static voidwriteUnsignedVInt32(int value, ByteBuffer output) static voidwriteUnsignedVInt32(int value, DataOutputPlus output) static <V> intwriteUnsignedVInt32(int value, V output, int offset, ValueAccessor<V> accessor) static voidwriteVInt(int value, ByteBuffer output) Deprecated.See CASSANDRA-18099static voidwriteVInt(int value, DataOutputPlus output) Deprecated.See CASSANDRA-18099static voidwriteVInt(long value, ByteBuffer output) static voidwriteVInt(long value, DataOutputPlus output) static <V> intwriteVInt(long value, V output, int offset, ValueAccessor<V> accessor) static voidwriteVInt32(int value, ByteBuffer output) static voidwriteVInt32(int value, DataOutputPlus output) static <V> intwriteVInt32(int value, V output, int offset, ValueAccessor<V> accessor)
-
Field Details
-
encodingBuffer
protected static final io.netty.util.concurrent.FastThreadLocal<byte[]> encodingBuffer -
MAX_SIZE
public static final int MAX_SIZE- See Also:
-
-
Constructor Details
-
VIntCoding
public VIntCoding()
-
-
Method Details
-
readUnsignedVInt
- Throws:
IOException
-
skipUnsignedVInt
- Throws:
IOException
-
getUnsignedVInt32
Read up to a 32-bit integer back, using the unsigned (no zigzag) encoding. Note this method is the same asreadUnsignedVInt(DataInput), except that we do *not* block if there are not enough bytes in the buffer to reconstruct the value.- Throws:
VIntCoding.VIntOutOfRangeException- If the vint doesn't fit into a 32-bit integer
-
getVInt32
-
getVInt
-
getUnsignedVInt
-
getUnsignedVInt
-
getUnsignedVInt32
-
getVInt32
-
getVInt
-
getUnsignedVInt
-
getUnsignedVInt
public static <V> long getUnsignedVInt(V input, ValueAccessor<V> accessor, int readerIndex, int readerLimit) -
computeUnsignedVIntSize
Computes size of an unsigned vint that starts at readerIndex of the provided ByteBuf.- Returns:
- -1 if there are not enough bytes in the input to calculate the size; else, the vint unsigned value size in bytes.
-
computeUnsignedVIntSize
-
readVInt
- Throws:
IOException
-
readVInt32
Read up to a signed 32-bit integer back. Assumes the vint was written usingwriteVInt32(int, DataOutputPlus)or similar that zigzag encodes the integer.- Throws:
VIntCoding.VIntOutOfRangeException- If the vint doesn't fit into a 32-bit integerIOException
-
readUnsignedVInt32
Read up to a 32-bit integer. This method assumes the original integer was written usingwriteUnsignedVInt32(int, DataOutputPlus)or similar that doesn't zigzag encodes the vint.- Throws:
VIntCoding.VIntOutOfRangeException- If the vint doesn't fit into a 32-bit integerIOException
-
firstByteValueMask
public static int firstByteValueMask(int extraBytesToRead) -
encodeExtraBytesToRead
public static int encodeExtraBytesToRead(int extraBytesToRead) -
numberOfExtraBytesToRead
public static int numberOfExtraBytesToRead(int firstByte) -
writeUnsignedVInt
@Deprecated(since="5.0") public static void writeUnsignedVInt(int value, DataOutputPlus output) throws IOException Deprecated.See CASSANDRA-18099- Throws:
IOException
-
writeUnsignedVInt
- Throws:
IOException
-
writeUnsignedVInt32
- Throws:
IOException
-
writeUnsignedVInt
@Deprecated(since="5.0") public static void writeUnsignedVInt(int value, ByteBuffer output) throws IOException Deprecated.See CASSANDRA-18099- Throws:
IOException
-
writeUnsignedVInt
-
writeVInt
@Inline public static <V> int writeVInt(long value, V output, int offset, ValueAccessor<V> accessor) -
writeVInt32
@Inline public static <V> int writeVInt32(int value, V output, int offset, ValueAccessor<V> accessor) -
writeUnsignedVInt32
@Inline public static <V> int writeUnsignedVInt32(int value, V output, int offset, ValueAccessor<V> accessor) -
writeUnsignedVInt
@Inline public static <V> int writeUnsignedVInt(long value, V output, int offset, ValueAccessor<V> accessor) -
writeUnsignedVInt32
-
writeVInt
@Deprecated(since="5.0") public static void writeVInt(int value, DataOutputPlus output) throws IOException Deprecated.See CASSANDRA-18099- Throws:
IOException
-
writeVInt
- Throws:
IOException
-
writeVInt32
- Throws:
IOException
-
writeVInt
Deprecated.See CASSANDRA-18099 -
writeVInt
-
writeVInt32
-
decodeZigZag64
public static long decodeZigZag64(long n) Decode a ZigZag-encoded 64-bit value. ZigZag encodes signed integers into values that can be efficiently encoded with varint. (Otherwise, negative values must be sign-extended to 64 bits to be varint encoded, thus always taking 10 bytes on the wire.)- Parameters:
n- An unsigned 64-bit integer, stored in a signed int because Java has no explicit unsigned support.- Returns:
- A signed 64-bit integer.
-
encodeZigZag64
public static long encodeZigZag64(long n) Encode a ZigZag-encoded 64-bit value. ZigZag encodes signed integers into values that can be efficiently encoded with varint. (Otherwise, negative values must be sign-extended to 64 bits to be varint encoded, thus always taking 10 bytes on the wire.)- Parameters:
n- A signed 64-bit integer.- Returns:
- An unsigned 64-bit integer, stored in a signed int because Java has no explicit unsigned support.
-
computeVIntSize
public static int computeVIntSize(long param) Compute the number of bytes that would be needed to encode a varint. -
computeUnsignedVIntSize
public static int computeUnsignedVIntSize(long value) Compute the number of bytes that would be needed to encode an unsigned varint. -
checkedCast
public static int checkedCast(long value)
-