Skip to content

Commit a789ad9

Browse files
authoredMay 23, 2023
Rename DatePrecision to DateTimePrecision (quickwit-oss#2051)
1 parent 8cf26da commit a789ad9

File tree

10 files changed

+60
-31
lines changed

10 files changed

+60
-31
lines changed
 

‎Makefile

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
test:
2-
echo "Run test only... No examples."
2+
@echo "Run test only... No examples."
33
cargo test --tests --lib
44

55
fmt:

‎common/src/datetime.rs

+32-11
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,41 @@
1+
#![allow(deprecated)]
2+
13
use std::fmt;
24

35
use serde::{Deserialize, Serialize};
46
use time::format_description::well_known::Rfc3339;
57
use time::{OffsetDateTime, PrimitiveDateTime, UtcOffset};
68

7-
/// DateTime Precision
9+
/// Precision with which datetimes are truncated when stored in fast fields. This setting is only
10+
/// relevant for fast fields. In the docstore, datetimes are always saved with nanosecond precision.
811
#[derive(
912
Clone, Copy, Debug, Hash, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize, Default,
1013
)]
1114
#[serde(rename_all = "lowercase")]
12-
pub enum DatePrecision {
13-
/// Seconds precision
15+
pub enum DateTimePrecision {
16+
/// Second precision.
1417
#[default]
18+
Second,
19+
/// Millisecond precision.
20+
Millisecond,
21+
/// Microsecond precision.
22+
Microsecond,
23+
/// Nanosecond precision.
24+
Nanosecond,
25+
// TODO: Remove deprecated variants after 2 releases.
26+
#[deprecated(since = "0.20.0", note = "Use `Second` instead")]
1527
Seconds,
16-
/// Milli-seconds precision.
28+
#[deprecated(since = "0.20.0", note = "Use `Millisecond` instead")]
1729
Milliseconds,
18-
/// Micro-seconds precision.
30+
#[deprecated(since = "0.20.0", note = "Use `Microsecond` instead")]
1931
Microseconds,
20-
/// Nano-seconds precision.
32+
#[deprecated(since = "0.20.0", note = "Use `Nanosecond` instead")]
2133
Nanoseconds,
2234
}
2335

36+
#[deprecated(since = "0.20.0", note = "Use `DateTimePrecision` instead")]
37+
pub type DatePrecision = DateTimePrecision;
38+
2439
/// A date/time value with nanoseconds precision.
2540
///
2641
/// This timestamp does not carry any explicit time zone information.
@@ -139,12 +154,18 @@ impl DateTime {
139154
}
140155

141156
/// Truncates the microseconds value to the corresponding precision.
142-
pub fn truncate(self, precision: DatePrecision) -> Self {
157+
pub fn truncate(self, precision: DateTimePrecision) -> Self {
143158
let truncated_timestamp_micros = match precision {
144-
DatePrecision::Seconds => (self.timestamp_nanos / 1_000_000_000) * 1_000_000_000,
145-
DatePrecision::Milliseconds => (self.timestamp_nanos / 1_000_000) * 1_000_000,
146-
DatePrecision::Microseconds => (self.timestamp_nanos / 1_000) * 1_000,
147-
DatePrecision::Nanoseconds => self.timestamp_nanos,
159+
DateTimePrecision::Second | DateTimePrecision::Seconds => {
160+
(self.timestamp_nanos / 1_000_000_000) * 1_000_000_000
161+
}
162+
DateTimePrecision::Millisecond | DateTimePrecision::Milliseconds => {
163+
(self.timestamp_nanos / 1_000_000) * 1_000_000
164+
}
165+
DateTimePrecision::Microsecond | DateTimePrecision::Microseconds => {
166+
(self.timestamp_nanos / 1_000) * 1_000
167+
}
168+
DateTimePrecision::Nanosecond | DateTimePrecision::Nanoseconds => self.timestamp_nanos,
148169
};
149170
Self {
150171
timestamp_nanos: truncated_timestamp_micros,

‎common/src/lib.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,9 @@ mod vint;
1414
mod writer;
1515
pub use bitset::*;
1616
pub use byte_count::ByteCount;
17-
pub use datetime::{DatePrecision, DateTime};
17+
#[allow(deprecated)]
18+
pub use datetime::DatePrecision;
19+
pub use datetime::{DateTime, DateTimePrecision};
1820
pub use group_by::GroupByIteratorExtended;
1921
pub use ownedbytes::{OwnedBytes, StableDeref};
2022
pub use serialize::{BinarySerializable, DeserializeFrom, FixedSize};

‎examples/date_time_field.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ fn main() -> tantivy::Result<()> {
1313
let opts = DateOptions::from(INDEXED)
1414
.set_stored()
1515
.set_fast()
16-
.set_precision(tantivy::DatePrecision::Seconds);
16+
.set_precision(tantivy::DateTimePrecision::Second);
1717
// Add `occurred_at` date field type
1818
let occurred_at = schema_builder.add_date_field("occurred_at", opts);
1919
let event_type = schema_builder.add_text_field("event", STRING | STORED);

‎src/fastfield/mod.rs

+6-6
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ mod tests {
9595
};
9696
use crate::time::OffsetDateTime;
9797
use crate::tokenizer::{LowerCaser, RawTokenizer, TextAnalyzer, TokenizerManager};
98-
use crate::{DateOptions, DatePrecision, Index, SegmentId, SegmentReader};
98+
use crate::{DateOptions, DateTimePrecision, Index, SegmentId, SegmentReader};
9999

100100
pub static SCHEMA: Lazy<Schema> = Lazy::new(|| {
101101
let mut schema_builder = Schema::builder();
@@ -686,12 +686,12 @@ mod tests {
686686
let mut schema_builder = Schema::builder();
687687
let date_field = schema_builder.add_date_field(
688688
"date",
689-
DateOptions::from(FAST).set_precision(DatePrecision::Nanoseconds),
689+
DateOptions::from(FAST).set_precision(DateTimePrecision::Nanosecond),
690690
);
691691
let multi_date_field = schema_builder.add_date_field(
692692
"multi_date",
693693
DateOptions::default()
694-
.set_precision(DatePrecision::Nanoseconds)
694+
.set_precision(DateTimePrecision::Nanosecond)
695695
.set_fast(),
696696
);
697697
let schema = schema_builder.build();
@@ -862,17 +862,17 @@ mod tests {
862862

863863
#[test]
864864
pub fn test_gcd_date() {
865-
let size_prec_sec = test_gcd_date_with_codec(DatePrecision::Seconds);
865+
let size_prec_sec = test_gcd_date_with_codec(DateTimePrecision::Second);
866866
assert!((1000 * 13 / 8..100 + 1000 * 13 / 8).contains(&size_prec_sec.get_bytes())); // 13 bits per val = ceil(log_2(number of seconds in 2hours);
867-
let size_prec_micros = test_gcd_date_with_codec(DatePrecision::Microseconds);
867+
let size_prec_micros = test_gcd_date_with_codec(DateTimePrecision::Microsecond);
868868
assert!((1000 * 33 / 8..100 + 1000 * 33 / 8).contains(&size_prec_micros.get_bytes()));
869869
// 33 bits per
870870
// val = ceil(log_2(number
871871
// of microsecsseconds
872872
// in 2hours);
873873
}
874874

875-
fn test_gcd_date_with_codec(precision: DatePrecision) -> ByteCount {
875+
fn test_gcd_date_with_codec(precision: DateTimePrecision) -> ByteCount {
876876
let mut rng = StdRng::seed_from_u64(2u64);
877877
const T0: i64 = 1_662_345_825_012_529i64;
878878
const ONE_HOUR_IN_MICROSECS: i64 = 3_600 * 1_000_000;

‎src/fastfield/writer.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use crate::indexer::doc_id_mapping::DocIdMapping;
88
use crate::schema::term::{JSON_PATH_SEGMENT_SEP, JSON_PATH_SEGMENT_SEP_STR};
99
use crate::schema::{value_type_to_column_type, Document, FieldType, Schema, Type, Value};
1010
use crate::tokenizer::{TextAnalyzer, TokenizerManager};
11-
use crate::{DatePrecision, DocId, TantivyError};
11+
use crate::{DateTimePrecision, DocId, TantivyError};
1212

1313
/// Only index JSON down to a depth of 20.
1414
/// This is mostly to guard us from a stack overflow triggered by malicious input.
@@ -19,7 +19,7 @@ pub struct FastFieldsWriter {
1919
columnar_writer: ColumnarWriter,
2020
fast_field_names: Vec<Option<String>>, //< TODO see if we can hash the field name hash too.
2121
per_field_tokenizer: Vec<Option<TextAnalyzer>>,
22-
date_precisions: Vec<DatePrecision>,
22+
date_precisions: Vec<DateTimePrecision>,
2323
expand_dots: Vec<bool>,
2424
num_docs: DocId,
2525
// Buffer that we recycle to avoid allocation.
@@ -41,8 +41,8 @@ impl FastFieldsWriter {
4141
let mut columnar_writer = ColumnarWriter::default();
4242

4343
let mut fast_field_names: Vec<Option<String>> = vec![None; schema.num_fields()];
44-
let mut date_precisions: Vec<DatePrecision> =
45-
std::iter::repeat_with(DatePrecision::default)
44+
let mut date_precisions: Vec<DateTimePrecision> =
45+
std::iter::repeat_with(DateTimePrecision::default)
4646
.take(schema.num_fields())
4747
.collect();
4848
let mut expand_dots = vec![false; schema.num_fields()];

‎src/lib.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,9 @@ pub use crate::directory::Directory;
184184
pub use crate::indexer::operation::UserOperation;
185185
pub use crate::indexer::{merge_filtered_segments, merge_indices, IndexWriter, PreparedCommit};
186186
pub use crate::postings::Postings;
187-
pub use crate::schema::{DateOptions, DatePrecision, Document, Term};
187+
#[allow(deprecated)]
188+
pub use crate::schema::DatePrecision;
189+
pub use crate::schema::{DateOptions, DateTimePrecision, Document, Term};
188190

189191
/// Index format version.
190192
const INDEX_FORMAT_VERSION: u32 = 5;

‎src/schema/date_time_options.rs

+6-4
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
use std::ops::BitOr;
22

3+
#[allow(deprecated)]
34
pub use common::DatePrecision;
5+
pub use common::DateTimePrecision;
46
use serde::{Deserialize, Serialize};
57

68
use crate::schema::flags::{FastFlag, IndexedFlag, SchemaFlagList, StoredFlag};
79

810
/// The precision of the indexed date/time values in the inverted index.
9-
pub const DATE_TIME_PRECISION_INDEXED: DatePrecision = DatePrecision::Seconds;
11+
pub const DATE_TIME_PRECISION_INDEXED: DateTimePrecision = DateTimePrecision::Second;
1012

1113
/// Defines how DateTime field should be handled by tantivy.
1214
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize, Default)]
@@ -20,7 +22,7 @@ pub struct DateOptions {
2022
// Internal storage precision, used to optimize storage
2123
// compression on fast fields.
2224
#[serde(default)]
23-
precision: DatePrecision,
25+
precision: DateTimePrecision,
2426
}
2527

2628
impl DateOptions {
@@ -93,7 +95,7 @@ impl DateOptions {
9395
///
9496
/// Internal storage precision, used to optimize storage
9597
/// compression on fast fields.
96-
pub fn set_precision(mut self, precision: DatePrecision) -> DateOptions {
98+
pub fn set_precision(mut self, precision: DateTimePrecision) -> DateOptions {
9799
self.precision = precision;
98100
self
99101
}
@@ -102,7 +104,7 @@ impl DateOptions {
102104
///
103105
/// Internal storage precision, used to optimize storage
104106
/// compression on fast fields.
105-
pub fn get_precision(&self) -> DatePrecision {
107+
pub fn get_precision(&self) -> DateTimePrecision {
106108
self.precision
107109
}
108110
}

‎src/schema/mod.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,9 @@ mod value;
129129
use columnar::ColumnType;
130130

131131
pub use self::bytes_options::BytesOptions;
132-
pub use self::date_time_options::{DateOptions, DatePrecision, DATE_TIME_PRECISION_INDEXED};
132+
#[allow(deprecated)]
133+
pub use self::date_time_options::DatePrecision;
134+
pub use self::date_time_options::{DateOptions, DateTimePrecision, DATE_TIME_PRECISION_INDEXED};
133135
pub use self::document::Document;
134136
pub(crate) use self::facet::FACET_SEP_BYTE;
135137
pub use self::facet::{Facet, FacetParseError};

‎src/schema/schema.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -973,7 +973,7 @@ mod tests {
973973
"fieldnorms": true,
974974
"fast": true,
975975
"stored": true,
976-
"precision": "seconds"
976+
"precision": "second"
977977
}
978978
},
979979
{

0 commit comments

Comments
 (0)
Please sign in to comment.