Skip to content

Commit

Permalink
fixed comment from PR and trucated during indexing to secs
Browse files Browse the repository at this point in the history
  • Loading branch information
evanxg852000 committed Jul 10, 2022
1 parent 32d1dbc commit 22df8d6
Show file tree
Hide file tree
Showing 15 changed files with 159 additions and 128 deletions.
32 changes: 16 additions & 16 deletions src/aggregation/bucket/range.rs
Original file line number Diff line number Diff line change
Expand Up @@ -461,14 +461,14 @@ mod tests {

let buckets = collector.buckets;
assert_eq!(buckets[0].range.start, u64::MIN);
assert_eq!(buckets[0].range.end, 10f64.to_u64(None));
assert_eq!(buckets[1].range.start, 10f64.to_u64(None));
assert_eq!(buckets[1].range.end, 20f64.to_u64(None));
assert_eq!(buckets[0].range.end, 10f64.to_u64());
assert_eq!(buckets[1].range.start, 10f64.to_u64());
assert_eq!(buckets[1].range.end, 20f64.to_u64());
// Added bucket to fill hole
assert_eq!(buckets[2].range.start, 20f64.to_u64(None));
assert_eq!(buckets[2].range.end, 30f64.to_u64(None));
assert_eq!(buckets[3].range.start, 30f64.to_u64(None));
assert_eq!(buckets[3].range.end, 40f64.to_u64(None));
assert_eq!(buckets[2].range.start, 20f64.to_u64());
assert_eq!(buckets[2].range.end, 30f64.to_u64());
assert_eq!(buckets[3].range.start, 30f64.to_u64());
assert_eq!(buckets[3].range.end, 40f64.to_u64());
}

#[test]
Expand All @@ -484,10 +484,10 @@ mod tests {

let buckets = collector.buckets;
assert_eq!(buckets[0].range.start, u64::MIN);
assert_eq!(buckets[0].range.end, 10f64.to_u64(None));
assert_eq!(buckets[1].range.start, 10f64.to_u64(None));
assert_eq!(buckets[1].range.end, 20f64.to_u64(None));
assert_eq!(buckets[2].range.start, 20f64.to_u64(None));
assert_eq!(buckets[0].range.end, 10f64.to_u64());
assert_eq!(buckets[1].range.start, 10f64.to_u64());
assert_eq!(buckets[1].range.end, 20f64.to_u64());
assert_eq!(buckets[2].range.start, 20f64.to_u64());
assert_eq!(buckets[2].range.end, u64::MAX);
assert_eq!(buckets.len(), 3);
}
Expand Down Expand Up @@ -561,11 +561,11 @@ mod tests {
let search = |val: u64| collector.get_bucket_pos(val);

assert_eq!(search(u64::MIN), 0);
assert_eq!(search(9f64.to_u64(None)), 0);
assert_eq!(search(10f64.to_u64(None)), 1);
assert_eq!(search(11f64.to_u64(None)), 1);
assert_eq!(search(99f64.to_u64(None)), 1);
assert_eq!(search(100f64.to_u64(None)), 2);
assert_eq!(search(9f64.to_u64()), 0);
assert_eq!(search(10f64.to_u64()), 1);
assert_eq!(search(11f64.to_u64()), 1);
assert_eq!(search(99f64.to_u64()), 1);
assert_eq!(search(100f64.to_u64()), 2);
assert_eq!(search(u64::MAX - 1), 2); // Since the end range is never included,
// the max value
}
Expand Down
4 changes: 2 additions & 2 deletions src/aggregation/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -308,8 +308,8 @@ pub(crate) fn f64_from_fastfield_u64(val: u64, field_type: &Type) -> f64 {
pub(crate) fn f64_to_fastfield_u64(val: f64, field_type: &Type) -> Option<u64> {
match field_type {
Type::U64 => Some(val as u64),
Type::I64 => Some((val as i64).to_u64(None)),
Type::F64 => Some(val.to_u64(None)),
Type::I64 => Some((val as i64).to_u64()),
Type::F64 => Some(val.to_u64()),
_ => None,
}
}
Expand Down
5 changes: 2 additions & 3 deletions src/collector/histogram_collector.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ impl HistogramCollector {
let fast_type = TFastValue::to_type();
assert!(fast_type == Type::U64 || fast_type == Type::I64 || fast_type == Type::Date);
HistogramCollector {
min_value: min_value.to_u64(None),
min_value: min_value.to_u64(),
num_buckets,
field,
divider: DividerU64::divide_by(bucket_width),
Expand All @@ -72,8 +72,7 @@ impl HistogramComputer {
return;
}
let delta = value - self.min_value;
let delta_u64 = delta.to_u64(None);
let bucket_id: usize = self.divider.divide(delta_u64) as usize;
let bucket_id: usize = self.divider.divide(delta) as usize;
if bucket_id < self.counts.len() {
self.counts[bucket_id] += 1;
}
Expand Down
58 changes: 24 additions & 34 deletions src/fastfield/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ pub(crate) use self::readers::{type_and_cardinality, FastType};
pub use self::serializer::{CompositeFastFieldSerializer, FastFieldDataAccess, FastFieldStats};
pub use self::writer::{FastFieldsWriter, IntFastFieldWriter};
use crate::schema::{Cardinality, FieldType, Type, Value};
use crate::{DatePrecision, DateTime, DocId};
use crate::{DateTime, DocId};

mod alive_bitset;
mod bytes;
Expand All @@ -58,14 +58,13 @@ pub trait FastValue: Clone + Copy + Send + Sync + PartialOrd + 'static {
/// Converts a value from u64
///
/// Internally all fast field values are encoded as u64.
/// **Note: To be used for converting encoded Term, Posting values.**
fn from_u64(val: u64) -> Self;

/// Converts a value to u64.
///
/// Internally all fast field values are encoded as u64.
// Optionally accepting field_type param because some field options
// could carry info on how the value should be internally treated.
fn to_u64(&self, field_type: Option<&FieldType>) -> u64;
fn to_u64(&self) -> u64;

/// Returns the fast field cardinality that can be extracted from the given
/// `FieldType`.
Expand All @@ -80,7 +79,7 @@ pub trait FastValue: Clone + Copy + Send + Sync + PartialOrd + 'static {
/// Build a default value. This default value is never used, so the value does not
/// really matter.
fn make_zero() -> Self {
Self::from_u64(0i64.to_u64(None))
Self::from_u64(0i64.to_u64())
}

/// Returns the `schema::Type` for this FastValue.
Expand All @@ -92,7 +91,7 @@ impl FastValue for u64 {
val
}

fn to_u64(&self, _: Option<&FieldType>) -> u64 {
fn to_u64(&self) -> u64 {
*self
}

Expand All @@ -118,7 +117,7 @@ impl FastValue for i64 {
common::u64_to_i64(val)
}

fn to_u64(&self, _: Option<&FieldType>) -> u64 {
fn to_u64(&self) -> u64 {
common::i64_to_u64(*self)
}

Expand All @@ -143,7 +142,7 @@ impl FastValue for f64 {
common::u64_to_f64(val)
}

fn to_u64(&self, _: Option<&FieldType>) -> u64 {
fn to_u64(&self) -> u64 {
common::f64_to_u64(*self)
}

Expand All @@ -168,7 +167,7 @@ impl FastValue for bool {
val != 0u64
}

fn to_u64(&self, _: Option<&FieldType>) -> u64 {
fn to_u64(&self) -> u64 {
match self {
false => 0,
true => 1,
Expand All @@ -192,25 +191,16 @@ impl FastValue for bool {
}

impl FastValue for DateTime {
/// Converts a timestamp microsecond into DateTime.
/// Converts a timestamp microseconds into DateTime.
///
/// **Note the timestamps is expected to be in microseconds.**
fn from_u64(timestamp_micros_u64: u64) -> Self {
let timestamp_micros = i64::from_u64(timestamp_micros_u64);
Self::from_timestamp_micros(timestamp_micros)
}

fn to_u64(&self, field_type: Option<&FieldType>) -> u64 {
let timestamp_micros = self.into_timestamp_micros();
let value = match field_type {
Some(FieldType::Date(options)) => match options.get_precision() {
DatePrecision::Seconds => (timestamp_micros / 1_000_000) * 1_000_000,
DatePrecision::Milliseconds => (timestamp_micros / 1_000) * 1_000,
DatePrecision::Microseconds => timestamp_micros,
},
_ => timestamp_micros,
};
common::i64_to_u64(value)
fn to_u64(&self) -> u64 {
common::i64_to_u64(self.into_timestamp_micros())
}

fn fast_field_cardinality(field_type: &FieldType) -> Option<Cardinality> {
Expand All @@ -229,13 +219,13 @@ impl FastValue for DateTime {
}
}

fn value_to_u64(value: &Value, field_type: &FieldType) -> u64 {
fn value_to_u64(value: &Value) -> u64 {
match value {
Value::U64(val) => val.to_u64(None),
Value::I64(val) => val.to_u64(None),
Value::F64(val) => val.to_u64(None),
Value::Bool(val) => val.to_u64(None),
Value::Date(val) => val.to_u64(Some(field_type)),
Value::U64(val) => val.to_u64(),
Value::I64(val) => val.to_u64(),
Value::F64(val) => val.to_u64(),
Value::Bool(val) => val.to_u64(),
Value::Date(val) => val.to_u64(),
_ => panic!("Expected a u64/i64/f64/bool/date field, got {:?} ", value),
}
}
Expand Down Expand Up @@ -299,7 +289,7 @@ mod tests {
#[test]
pub fn test_fastfield_i64_u64() {
let datetime = DateTime::from_utc(OffsetDateTime::UNIX_EPOCH);
assert_eq!(i64::from_u64(datetime.to_u64(None)), 0i64);
assert_eq!(i64::from_u64(datetime.to_u64()), 0i64);
}

#[test]
Expand Down Expand Up @@ -796,16 +786,16 @@ mod tests {
let mut index_writer = index.writer_for_tests()?;
index_writer.set_merge_policy(Box::new(NoMergePolicy));
index_writer.add_document(doc!(
date_field => DateTime::from_u64(1i64.to_u64(None)),
multi_date_field => DateTime::from_u64(2i64.to_u64(None)),
multi_date_field => DateTime::from_u64(3i64.to_u64(None))
date_field => DateTime::from_u64(1i64.to_u64()),
multi_date_field => DateTime::from_u64(2i64.to_u64()),
multi_date_field => DateTime::from_u64(3i64.to_u64())
))?;
index_writer.add_document(doc!(
date_field => DateTime::from_u64(4i64.to_u64(None))
date_field => DateTime::from_u64(4i64.to_u64())
))?;
index_writer.add_document(doc!(
multi_date_field => DateTime::from_u64(5i64.to_u64(None)),
multi_date_field => DateTime::from_u64(6i64.to_u64(None))
multi_date_field => DateTime::from_u64(5i64.to_u64()),
multi_date_field => DateTime::from_u64(6i64.to_u64())
))?;
index_writer.commit()?;
let reader = index.reader()?;
Expand Down
25 changes: 18 additions & 7 deletions src/fastfield/multivalued/writer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@ use fnv::FnvHashMap;
use tantivy_bitpacker::minmax;

use crate::fastfield::serializer::BitpackedFastFieldSerializerLegacy;
use crate::fastfield::{value_to_u64, CompositeFastFieldSerializer, FastFieldType};
use crate::fastfield::{value_to_u64, CompositeFastFieldSerializer, FastFieldType, FastValue};
use crate::indexer::doc_id_mapping::DocIdMapping;
use crate::postings::UnorderedTermId;
use crate::schema::{Document, Field, FieldType};
use crate::schema::{Document, Field, Value};
use crate::termdict::TermOrdinal;
use crate::DocId;
use crate::{DatePrecision, DocId};

/// Writer for multi-valued (as in, more than one value per document)
/// int fast field.
Expand All @@ -36,18 +36,22 @@ use crate::DocId;
/// term ids when the segment is getting serialized.
pub struct MultiValuedFastFieldWriter {
field: Field,
field_type: FieldType,
precision_opt: Option<DatePrecision>,
vals: Vec<UnorderedTermId>,
doc_index: Vec<u64>,
fast_field_type: FastFieldType,
}

impl MultiValuedFastFieldWriter {
/// Creates a new `MultiValuedFastFieldWriter`
pub(crate) fn new(field: Field, fast_field_type: FastFieldType, field_type: FieldType) -> Self {
pub(crate) fn new(
field: Field,
fast_field_type: FastFieldType,
precision_opt: Option<DatePrecision>,
) -> Self {
MultiValuedFastFieldWriter {
field,
field_type,
precision_opt,
vals: Vec::new(),
doc_index: Vec::new(),
fast_field_type,
Expand Down Expand Up @@ -85,7 +89,14 @@ impl MultiValuedFastFieldWriter {
}
for field_value in doc.field_values() {
if field_value.field == self.field {
self.add_val(value_to_u64(field_value.value(), &self.field_type));
let value = field_value.value();
let value_u64 = match (self.precision_opt, value) {
(Some(precision), Value::Date(date_val)) => {
date_val.truncate(precision).to_u64()
}
_ => value_to_u64(value),
};
self.add_val(value_u64);
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/fastfield/reader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,7 @@ impl<Item: FastValue> From<Vec<Item>> for DynamicFastFieldReader<Item> {
.get_field_writer_mut(field)
.expect("With a RamDirectory, this should never fail.");
for val in vals {
fast_field_writer.add_val(val.to_u64(None));
fast_field_writer.add_val(val.to_u64());
}
}
fast_field_writers
Expand Down
Loading

0 comments on commit 22df8d6

Please sign in to comment.