pub trait Key:
Clone
+ Ord
+ Send
+ Sync
+ Debug
+ 'static {
// Required methods
fn to_model_input(&self) -> f64;
fn to_exact_ordinal(&self) -> i128;
}Expand description
A key type usable in a learned index.
§Contract
to_model_inputmust be a monotonic function: ifa < bthena.to_model_input() <= b.to_model_input(). Note: the mapping is non-strict: distinct keys may produce the samef64due to precision loss (e.g.,u64keys above 2^53). The index handles this correctly via theto_exact_ordinalfallback.- The returned
f64must be finite (not NaN or infinity). - The mapping should preserve relative distances where possible, so that linear models can fit the key distribution effectively.
to_exact_ordinalmust be a strictly monotonic, injective function: ifa < bthena.to_exact_ordinal() < b.to_exact_ordinal().
Required Methods§
Sourcefn to_model_input(&self) -> f64
fn to_model_input(&self) -> f64
Convert this key to a f64 value for model prediction.
The conversion must be monotonic and return a finite value. It may be non-injective for large integer keys (precision loss above 2^53).
Sourcefn to_exact_ordinal(&self) -> i128
fn to_exact_ordinal(&self) -> i128
Convert this key to a lossless i128 for exact comparison.
This must be strictly monotonic and injective: if a < b then
a.to_exact_ordinal() < b.to_exact_ordinal(). Used as a fallback
for conflict resolution when to_model_input cannot distinguish keys.
Dyn Compatibility§
This trait is not dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.
Implementations on Foreign Types§
Source§impl Key for i8
impl Key for i8
fn to_model_input(&self) -> f64
fn to_exact_ordinal(&self) -> i128
Source§impl Key for i16
impl Key for i16
fn to_model_input(&self) -> f64
fn to_exact_ordinal(&self) -> i128
Source§impl Key for i32
impl Key for i32
fn to_model_input(&self) -> f64
fn to_exact_ordinal(&self) -> i128
Source§impl Key for i64
impl Key for i64
fn to_model_input(&self) -> f64
fn to_exact_ordinal(&self) -> i128
Source§impl Key for i128
impl Key for i128
fn to_model_input(&self) -> f64
fn to_exact_ordinal(&self) -> i128
Source§impl Key for u8
impl Key for u8
fn to_model_input(&self) -> f64
fn to_exact_ordinal(&self) -> i128
Source§impl Key for u16
impl Key for u16
fn to_model_input(&self) -> f64
fn to_exact_ordinal(&self) -> i128
Source§impl Key for u32
impl Key for u32
fn to_model_input(&self) -> f64
fn to_exact_ordinal(&self) -> i128
Source§impl Key for u64
impl Key for u64
fn to_model_input(&self) -> f64
fn to_exact_ordinal(&self) -> i128
Source§impl Key for u128
impl Key for u128
fn to_model_input(&self) -> f64
fn to_exact_ordinal(&self) -> i128
Source§impl Key for String
Key impl for String.
impl Key for String
Key impl for String.
Enables heap-allocated string keys. Uses the UTF-8 byte representation for model input and ordinal computation.
to_model_input: interprets the first 8 bytes as a big-endianu64→f64. Strings shorter than 8 bytes are zero-padded. Monotonic with respect to lexicographic (byte) order.to_exact_ordinal: interprets the first 16 bytes as a big-endianu128with sign-flip. Non-injective for strings sharing a 16-byte prefix; the index usesOrd-based node splits to handle these collisions.
fn to_model_input(&self) -> f64
fn to_exact_ordinal(&self) -> i128
Source§impl Key for Vec<u8>
Key impl for Vec<u8>.
impl Key for Vec<u8>
Key impl for Vec<u8>.
Enables heap-allocated byte-vector keys. Semantics are identical to the
fixed-size [u8; N] implementation but for dynamically sized vectors.
to_model_input: first 8 bytes as big-endianu64→f64.to_exact_ordinal: first 16 bytes asi128with sign-flip. Non-injective for vectors sharing a 16-byte prefix.
fn to_model_input(&self) -> f64
fn to_exact_ordinal(&self) -> i128
Source§impl<const N: usize> Key for [u8; N]
Key impl for fixed-size byte arrays.
impl<const N: usize> Key for [u8; N]
Key impl for fixed-size byte arrays.
Enables byte-array keys like [u8; 16] (UUIDs), [u8; 32] (hashes), etc.
to_model_input: interprets the first 8 bytes as a big-endianu64and casts tof64. Shorter arrays are zero-padded on the right. This provides a monotonic mapping with respect to lexicographic order.to_exact_ordinal: interprets the first 16 bytes as a big-endianu128(with the same sign-flip trick asu128). This is injective forN <= 16. ForN > 16, it is a best-effort prefix: distinct keys that share their first 16 bytes will collide. The index resolves these viaOrd-based splits in the node.