kv_index/common.rs
1//! Common types shared between radix tree implementations.
2
3use std::sync::Arc;
4
5/// Interned tenant identifier for efficient comparison and storage.
6///
7/// Using `Arc<str>` allows O(1) cloning and pointer-based equality checks
8/// for frequently accessed tenant identifiers.
9pub type TenantId = Arc<str>;
10
11/// Trait for prefix match results.
12///
13/// This trait provides a common interface for accessing match results
14/// from both string-based and token-based radix trees.
15pub trait MatchResult {
16 /// Get the tenant that owns the matched prefix.
17 fn tenant(&self) -> &TenantId;
18
19 /// Get the number of units (chars or tokens) that matched.
20 fn matched_count(&self) -> usize;
21
22 /// Get the total number of units in the input.
23 fn input_count(&self) -> usize;
24
25 /// Calculate the cache hit ratio (matched / input).
26 ///
27 /// Returns 0.0 if input is empty, otherwise returns a value in [0.0, 1.0].
28 fn hit_ratio(&self) -> f64 {
29 let input = self.input_count();
30 if input == 0 {
31 0.0
32 } else {
33 self.matched_count() as f64 / input as f64
34 }
35 }
36}