-
-
Notifications
You must be signed in to change notification settings - Fork 710
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
prepare for ip field #1554
prepare for ip field #1554
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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()) | ||
} |
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -1,3 +1,4 @@ | ||||||
use std::ops::Range; | ||||||
use std::sync::Arc; | ||||||
|
||||||
use fastfield_codecs::Column; | ||||||
|
@@ -31,36 +32,39 @@ impl BytesFastFieldReader { | |||||
Ok(BytesFastFieldReader { idx_reader, values }) | ||||||
} | ||||||
|
||||||
fn range(&self, doc: DocId) -> (usize, usize) { | ||||||
fn range(&self, doc: DocId) -> Range<u64> { | ||||||
let idx = doc as u64; | ||||||
let start = self.idx_reader.get_val(idx) as usize; | ||||||
let stop = self.idx_reader.get_val(idx + 1) as usize; | ||||||
(start, stop) | ||||||
let start = self.idx_reader.get_val(idx); | ||||||
let end = self.idx_reader.get_val(idx + 1); | ||||||
start..end | ||||||
} | ||||||
|
||||||
/// Returns the bytes associated to the given `doc` | ||||||
pub fn get_bytes(&self, doc: DocId) -> &[u8] { | ||||||
let (start, stop) = self.range(doc); | ||||||
&self.values.as_slice()[start..stop] | ||||||
let range = self.range(doc); | ||||||
&self.values.as_slice()[range.start as usize..range.end as usize] | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
} | ||||||
|
||||||
/// Returns the length of the bytes associated to the given `doc` | ||||||
pub fn num_bytes(&self, doc: DocId) -> usize { | ||||||
let (start, stop) = self.range(doc); | ||||||
stop - start | ||||||
pub fn num_bytes(&self, doc: DocId) -> u64 { | ||||||
let range = self.range(doc); | ||||||
range.end - range.start | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You might need to use std::iter::ExactSizeIterator for that one. |
||||||
} | ||||||
|
||||||
/// Returns the overall number of bytes in this bytes fast field. | ||||||
pub fn total_num_bytes(&self) -> usize { | ||||||
self.values.len() | ||||||
pub fn total_num_bytes(&self) -> u64 { | ||||||
self.values.len() as u64 | ||||||
} | ||||||
} | ||||||
|
||||||
impl MultiValueLength for BytesFastFieldReader { | ||||||
fn get_range(&self, doc_id: DocId) -> std::ops::Range<u64> { | ||||||
self.range(doc_id) | ||||||
} | ||||||
fn get_len(&self, doc_id: DocId) -> u64 { | ||||||
self.num_bytes(doc_id) as u64 | ||||||
self.num_bytes(doc_id) | ||||||
} | ||||||
fn get_total_len(&self) -> u64 { | ||||||
self.total_num_bytes() as u64 | ||||||
self.total_num_bytes() | ||||||
} | ||||||
} |
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) | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.