Skip to main content

Key

Trait Key 

Source
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_input must be a monotonic function: if a < b then a.to_model_input() <= b.to_model_input(). Note: the mapping is non-strict: distinct keys may produce the same f64 due to precision loss (e.g., u64 keys above 2^53). The index handles this correctly via the to_exact_ordinal fallback.
  • The returned f64 must 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_ordinal must be a strictly monotonic, injective function: if a < b then a.to_exact_ordinal() < b.to_exact_ordinal().

Required Methods§

Source

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).

Source

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

Source§

impl Key for i16

Source§

impl Key for i32

Source§

impl Key for i64

Source§

impl Key for i128

Source§

impl Key for u8

Source§

impl Key for u16

Source§

impl Key for u32

Source§

impl Key for u64

Source§

impl Key for u128

Source§

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-endian u64f64. 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-endian u128 with sign-flip. Non-injective for strings sharing a 16-byte prefix; the index uses Ord-based node splits to handle these collisions.
Source§

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-endian u64f64.
  • to_exact_ordinal: first 16 bytes as i128 with sign-flip. Non-injective for vectors sharing a 16-byte prefix.
Source§

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-endian u64 and casts to f64. 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-endian u128 (with the same sign-flip trick as u128). This is injective for N <= 16. For N > 16, it is a best-effort prefix: distinct keys that share their first 16 bytes will collide. The index resolves these via Ord-based splits in the node.

Implementors§