-
-
Notifications
You must be signed in to change notification settings - Fork 709
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1554 from quickwit-oss/prepare_ip_field
prepare for ip field
- Loading branch information
Showing
7 changed files
with
176 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
use std::net::{IpAddr, Ipv6Addr}; | ||
|
||
pub trait MonotonicallyMappableToU128: 'static + PartialOrd + Copy + Send + Sync { | ||
/// Converts a value to u128. | ||
/// | ||
/// Internally all fast field values are encoded as u64. | ||
fn to_u128(self) -> u128; | ||
|
||
/// Converts a value from u128 | ||
/// | ||
/// Internally all fast field values are encoded as u64. | ||
/// **Note: To be used for converting encoded Term, Posting values.** | ||
fn from_u128(val: u128) -> Self; | ||
} | ||
|
||
impl MonotonicallyMappableToU128 for u128 { | ||
fn to_u128(self) -> u128 { | ||
self | ||
} | ||
|
||
fn from_u128(val: u128) -> Self { | ||
val | ||
} | ||
} | ||
|
||
impl MonotonicallyMappableToU128 for IpAddr { | ||
fn to_u128(self) -> u128 { | ||
ip_to_u128(self) | ||
} | ||
|
||
fn from_u128(val: u128) -> Self { | ||
IpAddr::from(val.to_be_bytes()) | ||
} | ||
} | ||
|
||
fn ip_to_u128(ip_addr: IpAddr) -> u128 { | ||
let ip_addr_v6: Ipv6Addr = match ip_addr { | ||
IpAddr::V4(v4) => v4.to_ipv6_mapped(), | ||
IpAddr::V6(v6) => v6, | ||
}; | ||
u128::from_be_bytes(ip_addr_v6.octets()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
use std::ops::BitOr; | ||
|
||
use serde::{Deserialize, Serialize}; | ||
|
||
use super::flags::{FastFlag, IndexedFlag, SchemaFlagList, StoredFlag}; | ||
use super::Cardinality; | ||
|
||
/// Define how an ip field should be handled by tantivy. | ||
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize, Default)] | ||
pub struct IpOptions { | ||
#[serde(skip_serializing_if = "Option::is_none")] | ||
fast: Option<Cardinality>, | ||
stored: bool, | ||
} | ||
|
||
impl IpOptions { | ||
/// Returns true iff the value is a fast field. | ||
pub fn is_fast(&self) -> bool { | ||
self.fast.is_some() | ||
} | ||
|
||
/// Returns `true` if the json object should be stored. | ||
pub fn is_stored(&self) -> bool { | ||
self.stored | ||
} | ||
|
||
/// Returns the cardinality of the fastfield. | ||
/// | ||
/// If the field has not been declared as a fastfield, then | ||
/// the method returns None. | ||
pub fn get_fastfield_cardinality(&self) -> Option<Cardinality> { | ||
self.fast | ||
} | ||
|
||
/// Sets the field as stored | ||
#[must_use] | ||
pub fn set_stored(mut self) -> Self { | ||
self.stored = true; | ||
self | ||
} | ||
|
||
/// Set the field as a fast field. | ||
/// | ||
/// Fast fields are designed for random access. | ||
/// Access time are similar to a random lookup in an array. | ||
/// If more than one value is associated to a fast field, only the last one is | ||
/// kept. | ||
#[must_use] | ||
pub fn set_fast(mut self, cardinality: Cardinality) -> Self { | ||
self.fast = Some(cardinality); | ||
self | ||
} | ||
} | ||
|
||
impl From<()> for IpOptions { | ||
fn from(_: ()) -> IpOptions { | ||
IpOptions::default() | ||
} | ||
} | ||
|
||
impl From<FastFlag> for IpOptions { | ||
fn from(_: FastFlag) -> Self { | ||
IpOptions { | ||
stored: false, | ||
fast: Some(Cardinality::SingleValue), | ||
} | ||
} | ||
} | ||
|
||
impl From<StoredFlag> for IpOptions { | ||
fn from(_: StoredFlag) -> Self { | ||
IpOptions { | ||
stored: true, | ||
fast: None, | ||
} | ||
} | ||
} | ||
|
||
impl From<IndexedFlag> for IpOptions { | ||
fn from(_: IndexedFlag) -> Self { | ||
IpOptions { | ||
stored: false, | ||
fast: None, | ||
} | ||
} | ||
} | ||
|
||
impl<T: Into<IpOptions>> BitOr<T> for IpOptions { | ||
type Output = IpOptions; | ||
|
||
fn bitor(self, other: T) -> IpOptions { | ||
let other = other.into(); | ||
IpOptions { | ||
stored: self.stored | other.stored, | ||
fast: self.fast.or(other.fast), | ||
} | ||
} | ||
} | ||
|
||
impl<Head, Tail> From<SchemaFlagList<Head, Tail>> for IpOptions | ||
where | ||
Head: Clone, | ||
Tail: Clone, | ||
Self: BitOr<Output = Self> + From<Head> + From<Tail>, | ||
{ | ||
fn from(head_tail: SchemaFlagList<Head, Tail>) -> Self { | ||
Self::from(head_tail.head) | Self::from(head_tail.tail) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters