Skip to content

Commit

Permalink
remove JsonTermWriter
Browse files Browse the repository at this point in the history
  • Loading branch information
PSeitz committed Nov 3, 2023
1 parent 28dd6b6 commit 143cd70
Show file tree
Hide file tree
Showing 9 changed files with 207 additions and 357 deletions.
32 changes: 32 additions & 0 deletions common/src/json_path_writer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@ pub const JSON_PATH_SEGMENT_SEP: u8 = 1u8;
pub const JSON_PATH_SEGMENT_SEP_STR: &str =
unsafe { std::str::from_utf8_unchecked(&[JSON_PATH_SEGMENT_SEP]) };

/// Separates the json path and the value in
/// a JSON term binary representation.
pub const JSON_END_OF_PATH: u8 = 0u8;
pub const JSON_END_OF_PATH_STR: &str =
unsafe { std::str::from_utf8_unchecked(&[JSON_END_OF_PATH]) };

/// Create a new JsonPathWriter, that creates flattened json paths for tantivy.
#[derive(Clone, Debug, Default)]
pub struct JsonPathWriter {
Expand All @@ -14,6 +20,14 @@ pub struct JsonPathWriter {
}

impl JsonPathWriter {
pub fn with_expand_dots(expand_dots: bool) -> Self {
JsonPathWriter {
path: String::new(),
indices: Vec::new(),
expand_dots,
}
}

pub fn new() -> Self {
JsonPathWriter {
path: String::new(),
Expand Down Expand Up @@ -55,6 +69,12 @@ impl JsonPathWriter {
}
}

/// Set the end of JSON path marker.
#[inline]
pub fn set_end(&mut self) {
self.path.push_str(JSON_END_OF_PATH_STR);
}

/// Remove the last segment. Does nothing if the path is empty.
#[inline]
pub fn pop(&mut self) {
Expand Down Expand Up @@ -91,6 +111,7 @@ mod tests {
#[test]
fn json_path_writer_test() {
let mut writer = JsonPathWriter::new();
writer.set_expand_dots(false);

writer.push("root");
assert_eq!(writer.as_str(), "root");
Expand All @@ -109,4 +130,15 @@ mod tests {
writer.push("k8s.node.id");
assert_eq!(writer.as_str(), "root\u{1}k8s\u{1}node\u{1}id");
}

#[test]
fn test_json_path_expand_dots_enabled_pop_segment() {
let mut json_writer = JsonPathWriter::new();
json_writer.push("hello");
assert_eq!(json_writer.as_str(), "hello");
json_writer.push("color.hue");
assert_eq!(json_writer.as_str(), "hello\x01color\x01hue");
json_writer.pop();
assert_eq!(json_writer.as_str(), "hello");
}
}
2 changes: 1 addition & 1 deletion common/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ mod byte_count;
mod datetime;
pub mod file_slice;
mod group_by;
mod json_path_writer;
pub mod json_path_writer;
mod serialize;
mod vint;
mod writer;
Expand Down
3 changes: 2 additions & 1 deletion src/core/inverted_index_reader.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
use std::io;

use common::json_path_writer::JSON_END_OF_PATH;
use common::BinarySerializable;
use fnv::FnvHashSet;

use crate::directory::FileSlice;
use crate::positions::PositionReader;
use crate::postings::{BlockSegmentPostings, SegmentPostings, TermInfo};
use crate::schema::{IndexRecordOption, Term, Type, JSON_END_OF_PATH};
use crate::schema::{IndexRecordOption, Term, Type};
use crate::termdict::TermDictionary;

/// The inverted index reader is in charge of accessing
Expand Down
Loading

0 comments on commit 143cd70

Please sign in to comment.