mirror of
https://github.com/meilisearch/meilisearch.git
synced 2025-10-28 22:46:28 +00:00
Introduce codecs for facet types (string, f64, u64, i64)
This commit is contained in:
15
src/facet/facet_type.rs
Normal file
15
src/facet/facet_type.rs
Normal file
@@ -0,0 +1,15 @@
|
||||
use std::cmp;
|
||||
|
||||
#[derive(Debug, Copy, Clone, PartialOrd, Ord, PartialEq, Eq, Hash)]
|
||||
pub enum FacetType {
|
||||
String,
|
||||
F64,
|
||||
I64,
|
||||
U64,
|
||||
}
|
||||
|
||||
impl FacetType {
|
||||
pub fn merge(a: FacetType, b: FacetType) -> FacetType {
|
||||
cmp::min(a, b)
|
||||
}
|
||||
}
|
||||
4
src/facet/mod.rs
Normal file
4
src/facet/mod.rs
Normal file
@@ -0,0 +1,4 @@
|
||||
mod facet_type;
|
||||
pub mod value_encoding;
|
||||
|
||||
pub use self::facet_type::FacetType;
|
||||
89
src/facet/value_encoding.rs
Normal file
89
src/facet/value_encoding.rs
Normal file
@@ -0,0 +1,89 @@
|
||||
// https://stackoverflow.com/a/43305015/1941280
|
||||
#[inline]
|
||||
pub fn f64_into_bytes(float: f64) -> Option<[u8; 8]> {
|
||||
if float.is_finite() {
|
||||
if float == 0.0 || float == -0.0 {
|
||||
return Some(xor_first_bit(0.0_f64.to_be_bytes()));
|
||||
} else if float.is_sign_negative() {
|
||||
return Some(xor_all_bits(float.to_be_bytes()));
|
||||
} else if float.is_sign_positive() {
|
||||
return Some(xor_first_bit(float.to_be_bytes()));
|
||||
}
|
||||
}
|
||||
None
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub fn u64_into_bytes(int: u64) -> [u8; 8] {
|
||||
int.to_be_bytes()
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub fn u64_from_bytes(bytes: [u8; 8]) -> u64 {
|
||||
u64::from_be_bytes(bytes)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub fn i64_into_bytes(int: i64) -> [u8; 8] {
|
||||
xor_first_bit(int.to_be_bytes())
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub fn i64_from_bytes(bytes: [u8; 8]) -> i64 {
|
||||
i64::from_be_bytes(xor_first_bit(bytes))
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn xor_first_bit(mut x: [u8; 8]) -> [u8; 8] {
|
||||
x[0] ^= 0x80;
|
||||
x
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn xor_all_bits(mut x: [u8; 8]) -> [u8; 8] {
|
||||
x.iter_mut().for_each(|b| *b ^= 0xff);
|
||||
x
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use std::cmp::Ordering::Less;
|
||||
use super::*;
|
||||
|
||||
fn is_sorted<T: Ord>(x: &[T]) -> bool {
|
||||
x.windows(2).map(|x| x[0].cmp(&x[1])).all(|o| o == Less)
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn ordered_f64_bytes() {
|
||||
let a = -13_f64;
|
||||
let b = -10.0;
|
||||
let c = -0.0;
|
||||
let d = 1.0;
|
||||
let e = 43.0;
|
||||
|
||||
let vec: Vec<_> = [a, b, c, d, e].iter().cloned().map(f64_into_bytes).collect();
|
||||
assert!(is_sorted(&vec), "{:?}", vec);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn ordered_u64_bytes() {
|
||||
let a = 0_u64;
|
||||
let b = 1_u64;
|
||||
let c = 43_u64;
|
||||
|
||||
let vec: Vec<_> = [a, b, c].iter().cloned().map(u64_into_bytes).collect();
|
||||
assert!(is_sorted(&vec), "{:?}", vec);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn ordered_i64_bytes() {
|
||||
let a = -10_i64;
|
||||
let b = -0_i64;
|
||||
let c = 1_i64;
|
||||
let d = 43_i64;
|
||||
|
||||
let vec: Vec<_> = [a, b, c, d].iter().cloned().map(i64_into_bytes).collect();
|
||||
assert!(is_sorted(&vec), "{:?}", vec);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user