Trait q_compress::data_types::NumberLike
source · [−]pub trait NumberLike: Copy + Debug + Display + Default + PartialEq + 'static {
type Signed: SignedLike + NumberLike<Signed = Self::Signed, Unsigned = Self::Unsigned>;
type Unsigned: UnsignedLike;
const HEADER_BYTE: u8;
const PHYSICAL_BITS: usize;
fn to_unsigned(self) -> Self::Unsigned;
fn from_unsigned(off: Self::Unsigned) -> Self;
fn to_signed(self) -> Self::Signed;
fn from_signed(signed: Self::Signed) -> Self;
fn to_bytes(self) -> Vec<u8>;
fn from_bytes(bytes: Vec<u8>) -> QCompressResult<Self>;
fn num_eq(&self, other: &Self) -> bool { ... }
fn read_from(reader: &mut BitReader<'_>) -> QCompressResult<Self> { ... }
fn write_to(self, writer: &mut BitWriter) { ... }
}
Expand description
Trait for data types supported for compression/decompression.
If you have a new data type you would like to add to the library or implement as custom in your own, these are the questions you need to answer:
- What are the corresponding signed integer and unsigned integer types? These are usually the next-larger signed and unsigned integers.
- How can I convert to these signed and unsigned representations and back
in a way that preserves ordering? For instance, converting
f32
toi32
can be done trivially by transmuting the bytes in memory, but converting fromf32
tou32
in an order-preserving way requires flipping the sign bit and, if negative, the rest of the bits. - How can I encode and decode this number in an uncompressed way? This uncompressed representation is used to store metadata in each chunk of the Quantile Compression format.
Associated Types
type Signed: SignedLike + NumberLike<Signed = Self::Signed, Unsigned = Self::Unsigned>
type Signed: SignedLike + NumberLike<Signed = Self::Signed, Unsigned = Self::Unsigned>
The signed integer this type can convert between to do wrapped
subtraction and addition for delta encoding/decoding.
Must be another NumberLike
with the same Signed
and Unsigned
as
this type; in this way, if we take 7th order deltas, they are ensured to
have the same type as 1st order deltas.
type Unsigned: UnsignedLike
type Unsigned: UnsignedLike
The unsigned integer this type can convert between to do bitwise logic and such.
Associated Constants
const HEADER_BYTE: u8
const HEADER_BYTE: u8
A number from 0-255 that corresponds to the number’s data type.
Each NumberLike
implementation should have a different HEADER_BYTE
.
This byte gets written into the file’s header during compression, and
if the wrong header byte shows up during decompression, the decompressor
will return an error.
To choose a header byte for a new data type, review all header bytes in
the library and pick an unused one. For instance, as of writing, bytes
1 through 13 are used, so 14 would be a good choice for another
q_compress
-supported data type, and 255 would be a good choice for a
custom data type.
const PHYSICAL_BITS: usize
const PHYSICAL_BITS: usize
The number of bits in the number’s uncompressed representation.
This must match the number of bytes in the to_bytes
and from_bytes
implementations.
Note that booleans have 8 physical bits (not 1)
and timestamps have 96 (not 128).
Required methods
fn to_unsigned(self) -> Self::Unsigned
fn to_unsigned(self) -> Self::Unsigned
Used during compression to convert to an unsigned integer.
fn from_unsigned(off: Self::Unsigned) -> Self
fn from_unsigned(off: Self::Unsigned) -> Self
Used during decompression to convert back from an unsigned integer.
fn from_signed(signed: Self::Signed) -> Self
fn from_signed(signed: Self::Signed) -> Self
Used during delta decoding to convert back from a signed integer.
fn from_bytes(bytes: Vec<u8>) -> QCompressResult<Self>
fn from_bytes(bytes: Vec<u8>) -> QCompressResult<Self>
Creates a number from an uncompressed representation.
Provided methods
Lossless check for bit-exact equality. This is important because not all data types support full ordering: https://stackoverflow.com/questions/26489701/why-does-rust-not-implement-total-ordering-via-the-ord-trait-for-f64-and-f32.
fn read_from(reader: &mut BitReader<'_>) -> QCompressResult<Self>
fn read_from(reader: &mut BitReader<'_>) -> QCompressResult<Self>
Parses an uncompressed representation of the number from the
BitReader
.