mirror of
https://github.com/meilisearch/MeiliSearch
synced 2024-11-27 07:14:26 +01:00
Simplify the the facet types
This commit is contained in:
parent
466fb601d6
commit
cf9ddd293d
@ -1,17 +1,50 @@
|
||||
use std::cmp;
|
||||
use std::error::Error;
|
||||
use std::fmt;
|
||||
use std::str::FromStr;
|
||||
|
||||
use serde::{Serialize, Deserialize};
|
||||
|
||||
#[derive(Debug, Copy, Clone, PartialOrd, Ord, PartialEq, Eq, Hash)]
|
||||
#[derive(Serialize, Deserialize)]
|
||||
pub enum FacetType {
|
||||
String,
|
||||
F64,
|
||||
I64,
|
||||
U64,
|
||||
Float,
|
||||
Integer,
|
||||
}
|
||||
|
||||
impl FacetType {
|
||||
pub fn merge(a: FacetType, b: FacetType) -> FacetType {
|
||||
cmp::min(a, b)
|
||||
impl fmt::Display for FacetType {
|
||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||
match self {
|
||||
FacetType::String => f.write_str("string"),
|
||||
FacetType::Float => f.write_str("float"),
|
||||
FacetType::Integer => f.write_str("integer"),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl FromStr for FacetType {
|
||||
type Err = InvalidFacetType;
|
||||
|
||||
fn from_str(s: &str) -> Result<Self, Self::Err> {
|
||||
if s.eq_ignore_ascii_case("string") {
|
||||
Ok(FacetType::String)
|
||||
} else if s.eq_ignore_ascii_case("float") {
|
||||
Ok(FacetType::Float)
|
||||
} else if s.eq_ignore_ascii_case("integer") {
|
||||
Ok(FacetType::Integer)
|
||||
} else {
|
||||
Err(InvalidFacetType)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Copy, Clone, PartialOrd, Ord, PartialEq, Eq, Hash)]
|
||||
pub struct InvalidFacetType;
|
||||
|
||||
impl fmt::Display for InvalidFacetType {
|
||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||
f.write_str(r#"Invalid facet type, must be "string", "float" or "integer""#)
|
||||
}
|
||||
}
|
||||
|
||||
impl Error for InvalidFacetType { }
|
||||
|
@ -13,16 +13,6 @@ pub fn f64_into_bytes(float: f64) -> Option<[u8; 8]> {
|
||||
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())
|
||||
@ -66,16 +56,6 @@ mod tests {
|
||||
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;
|
||||
|
@ -1,28 +0,0 @@
|
||||
use std::borrow::Cow;
|
||||
use std::convert::TryInto;
|
||||
use std::str;
|
||||
|
||||
use crate::heed_codec::StrBytesCodec;
|
||||
use crate::facet::value_encoding::{u64_from_bytes, u64_into_bytes};
|
||||
|
||||
pub struct FacetValueU64Codec;
|
||||
|
||||
impl<'a> heed::BytesDecode<'a> for FacetValueU64Codec {
|
||||
type DItem = (&'a str, u64);
|
||||
|
||||
fn bytes_decode(bytes: &'a [u8]) -> Option<Self::DItem> {
|
||||
let (name, bytes) = StrBytesCodec::bytes_decode(bytes)?;
|
||||
let value = bytes.try_into().map(u64_from_bytes).ok()?;
|
||||
Some((name, value))
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a> heed::BytesEncode<'a> for FacetValueU64Codec {
|
||||
type EItem = (&'a str, u64);
|
||||
|
||||
fn bytes_encode((name, value): &Self::EItem) -> Option<Cow<[u8]>> {
|
||||
let value = u64_into_bytes(*value);
|
||||
let tuple = (*name, &value[..]);
|
||||
StrBytesCodec::bytes_encode(&tuple).map(Cow::into_owned).map(Cow::Owned)
|
||||
}
|
||||
}
|
@ -1,9 +1,7 @@
|
||||
mod facet_value_f64_codec;
|
||||
mod facet_value_i64_codec;
|
||||
mod facet_value_string_codec;
|
||||
mod facet_value_u64_codec;
|
||||
|
||||
pub use self::facet_value_f64_codec::FacetValueF64Codec;
|
||||
pub use self::facet_value_i64_codec::FacetValueI64Codec;
|
||||
pub use self::facet_value_string_codec::FacetValueStringCodec;
|
||||
pub use self::facet_value_u64_codec::FacetValueU64Codec;
|
||||
|
Loading…
Reference in New Issue
Block a user