mirror of
https://github.com/quickwit-oss/tantivy.git
synced 2026-11-29 22:14:17 +00:00
Name streamed ranges and candidate dictionary blocks
This commit is contained in:
+11
-27
@@ -40,7 +40,7 @@ pub trait FileHandle: 'static + Send + Sync + HasLen + fmt::Debug {
|
||||
/// without buffering an entire merged read. Dropping the stream cancels further reads.
|
||||
/// The default calls `read_bytes_async` for one range at a time. Invalid ranges may panic.
|
||||
#[cfg(feature = "quickwit")]
|
||||
fn read_bytes_batch_stream(
|
||||
fn stream_ranges(
|
||||
&self,
|
||||
ranges: Box<dyn Iterator<Item = Range<usize>> + Send>,
|
||||
) -> BoxStream<'_, io::Result<OwnedBytes>> {
|
||||
@@ -338,16 +338,15 @@ impl FileHandle for FileSlice {
|
||||
}
|
||||
|
||||
#[cfg(feature = "quickwit")]
|
||||
fn read_bytes_batch_stream(
|
||||
fn stream_ranges(
|
||||
&self,
|
||||
ranges: Box<dyn Iterator<Item = Range<usize>> + Send>,
|
||||
) -> BoxStream<'_, io::Result<OwnedBytes>> {
|
||||
let slice = self.range.clone();
|
||||
self.data
|
||||
.read_bytes_batch_stream(Box::new(ranges.map(move |range| {
|
||||
assert!(range.start <= range.end && range.end <= slice.len());
|
||||
slice.start + range.start..slice.start + range.end
|
||||
})))
|
||||
self.data.stream_ranges(Box::new(ranges.map(move |range| {
|
||||
assert!(range.start <= range.end && range.end <= slice.len());
|
||||
slice.start + range.start..slice.start + range.end
|
||||
})))
|
||||
}
|
||||
}
|
||||
|
||||
@@ -426,7 +425,6 @@ mod tests {
|
||||
|
||||
use super::*;
|
||||
use crate::OwnedBytes;
|
||||
use crate::file_slice::WrapFile;
|
||||
|
||||
#[test]
|
||||
fn nested_slices() {
|
||||
@@ -445,21 +443,21 @@ mod tests {
|
||||
panic!("expected a streaming read");
|
||||
}
|
||||
|
||||
fn read_bytes_batch_stream(
|
||||
fn stream_ranges(
|
||||
&self,
|
||||
ranges: Box<dyn Iterator<Item = Range<usize>> + Send>,
|
||||
) -> BoxStream<'_, io::Result<OwnedBytes>> {
|
||||
let ranges: Vec<_> = ranges.collect();
|
||||
assert_eq!(ranges, [4..6, 2..3]);
|
||||
static BYTES: &[u8] = b"abcdefgh";
|
||||
BYTES.read_bytes_batch_stream(Box::new(ranges.into_iter()))
|
||||
BYTES.stream_ranges(Box::new(ranges.into_iter()))
|
||||
}
|
||||
}
|
||||
|
||||
async {
|
||||
let inner = FileSlice::new(Arc::new(StreamOnlyHandle)).slice(1..8);
|
||||
let outer = FileSlice::new(Arc::new(inner)).slice(1..6);
|
||||
let mut stream = outer.read_bytes_batch_stream(Box::new([2..4, 0..1].into_iter()));
|
||||
let mut stream = outer.stream_ranges(Box::new([2..4, 0..1].into_iter()));
|
||||
assert_eq!(&*stream.try_next().await.unwrap().unwrap(), b"ef");
|
||||
assert_eq!(&*stream.try_next().await.unwrap().unwrap(), b"c");
|
||||
assert!(stream.try_next().await.unwrap().is_none());
|
||||
@@ -477,7 +475,7 @@ mod tests {
|
||||
async {
|
||||
for handle in handles {
|
||||
let ranges = [1..4, 6..6, 1..4];
|
||||
let mut stream = handle.read_bytes_batch_stream(Box::new(ranges.into_iter()));
|
||||
let mut stream = handle.stream_ranges(Box::new(ranges.into_iter()));
|
||||
for expected in [b"bcd".as_slice(), b"", b"bcd"] {
|
||||
assert_eq!(&*stream.try_next().await.unwrap().unwrap(), expected);
|
||||
}
|
||||
@@ -495,27 +493,13 @@ mod tests {
|
||||
for range in [0..4, 4..4, Range { start: 2, end: 1 }] {
|
||||
assert!(
|
||||
catch_unwind(AssertUnwindSafe(|| {
|
||||
let mut stream =
|
||||
outer.read_bytes_batch_stream(Box::new(std::iter::once(range)));
|
||||
let mut stream = outer.stream_ranges(Box::new(std::iter::once(range)));
|
||||
let _ = stream.try_next().now_or_never();
|
||||
}))
|
||||
.is_err()
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn default_unsupported() {
|
||||
let handle =
|
||||
WrapFile::new(std::fs::File::open(std::env::current_exe().unwrap()).unwrap())
|
||||
.unwrap();
|
||||
let result = handle
|
||||
.read_bytes_batch_stream(Box::new(std::iter::once(0..0)))
|
||||
.try_next()
|
||||
.now_or_never()
|
||||
.unwrap();
|
||||
assert_eq!(result.err().unwrap().kind(), io::ErrorKind::Unsupported);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
||||
@@ -521,14 +521,14 @@ impl InvertedIndexReader {
|
||||
A::State: Clone + Send + 'static,
|
||||
{
|
||||
const TARGET_BATCH_BYTES: usize = 1024 * 1024;
|
||||
let (file, blocks) = self.termdict.matching_blocks(&automaton);
|
||||
let (file, blocks) = self.termdict.candidate_blocks(&automaton);
|
||||
let (ranges, block_info): (Vec<_>, Vec<_>) = blocks
|
||||
.map(|block| {
|
||||
let info = (block.byte_range.len(), block.first_ordinal);
|
||||
(block.byte_range, info)
|
||||
let len = block.byte_range.len();
|
||||
(block.byte_range, (len, block.first_ordinal))
|
||||
})
|
||||
.unzip();
|
||||
let mut reads = file.read_bytes_batch_stream(Box::new(ranges.into_iter()));
|
||||
let mut dictionary_blocks = file.stream_ranges(Box::new(ranges.into_iter()));
|
||||
let mut block_info = block_info.into_iter().peekable();
|
||||
let mut encoded_terminfo = Vec::new();
|
||||
let mut previous_end = 0;
|
||||
@@ -537,7 +537,7 @@ impl InvertedIndexReader {
|
||||
let mut batch_bytes = 0;
|
||||
let mut batch = Vec::new();
|
||||
for (len, first_ordinal) in block_info.by_ref() {
|
||||
let bytes = reads
|
||||
let bytes = dictionary_blocks
|
||||
.try_next()
|
||||
.await?
|
||||
.ok_or(io::ErrorKind::UnexpectedEof)?;
|
||||
@@ -557,7 +557,7 @@ impl InvertedIndexReader {
|
||||
.termdict
|
||||
.search(automaton)
|
||||
.without_keys()
|
||||
.into_stream_given_delta_reader(sstable::DeltaReader::from_multiple_blocks(batch))?
|
||||
.into_stream_from_delta_reader(sstable::DeltaReader::from_multiple_blocks(batch))?
|
||||
.into_owned();
|
||||
(automaton, encoded_terminfo, previous_end) = executor
|
||||
.spawn_blocking(move || {
|
||||
@@ -574,13 +574,13 @@ impl InvertedIndexReader {
|
||||
.await
|
||||
.map_err(|_| io::Error::other("automaton term enumeration task panicked"))?;
|
||||
}
|
||||
if reads.try_next().await?.is_some() {
|
||||
if dictionary_blocks.try_next().await?.is_some() {
|
||||
return Err(io::Error::new(
|
||||
io::ErrorKind::InvalidData,
|
||||
"unexpected dictionary block",
|
||||
));
|
||||
}
|
||||
drop(reads);
|
||||
drop(dictionary_blocks);
|
||||
let mut bitset = BitSet::with_max_value(max_doc);
|
||||
if encoded_terminfo.is_empty() {
|
||||
return Ok(bitset);
|
||||
@@ -599,16 +599,14 @@ impl InvertedIndexReader {
|
||||
Some((start..end, next() as u32))
|
||||
});
|
||||
let ranges = terms.clone().map(|(range, _)| range);
|
||||
let mut postings = self
|
||||
.postings_file_slice
|
||||
.read_bytes_batch_stream(Box::new(ranges));
|
||||
let mut posting_lists = self.postings_file_slice.stream_ranges(Box::new(ranges));
|
||||
let mut terms = terms.peekable();
|
||||
let record_option = self.record_option;
|
||||
while terms.peek().is_some() {
|
||||
let mut batch = Vec::new();
|
||||
let mut batch_bytes = 0;
|
||||
for (range, doc_freq) in terms.by_ref() {
|
||||
let bytes = postings
|
||||
let bytes = posting_lists
|
||||
.try_next()
|
||||
.await?
|
||||
.ok_or(io::ErrorKind::UnexpectedEof)?;
|
||||
@@ -624,7 +622,7 @@ impl InvertedIndexReader {
|
||||
break;
|
||||
}
|
||||
}
|
||||
let add_batch = move || -> io::Result<BitSet> {
|
||||
let insert_doc_ids = move || -> io::Result<BitSet> {
|
||||
for (doc_freq, bytes) in batch {
|
||||
let mut postings = BlockSegmentPostings::open_bytes(
|
||||
doc_freq,
|
||||
@@ -642,11 +640,11 @@ impl InvertedIndexReader {
|
||||
Ok(bitset)
|
||||
};
|
||||
bitset = executor
|
||||
.spawn_blocking(add_batch)
|
||||
.spawn_blocking(insert_doc_ids)
|
||||
.await
|
||||
.map_err(|_| io::Error::other("automaton bitset task panicked"))??;
|
||||
}
|
||||
if postings.try_next().await?.is_some() {
|
||||
if posting_lists.try_next().await?.is_some() {
|
||||
return Err(io::Error::new(
|
||||
io::ErrorKind::InvalidData,
|
||||
"unexpected posting list",
|
||||
|
||||
@@ -29,7 +29,7 @@ impl FileHandle for StreamingPostings {
|
||||
panic!("postings must be streamed");
|
||||
}
|
||||
|
||||
fn read_bytes_batch_stream(
|
||||
fn stream_ranges(
|
||||
&self,
|
||||
ranges: Box<dyn Iterator<Item = Range<usize>> + Send>,
|
||||
) -> BoxStream<'_, io::Result<OwnedBytes>> {
|
||||
|
||||
+1
-1
@@ -158,7 +158,7 @@ impl TermDictionary {
|
||||
}
|
||||
|
||||
#[cfg(feature = "quickwit")]
|
||||
pub(crate) fn matching_blocks<'a, 's: 'a>(
|
||||
pub(crate) fn candidate_blocks<'a, 's: 'a>(
|
||||
&'s self,
|
||||
automaton: &'a impl Automaton,
|
||||
) -> (&'s FileSlice, impl Iterator<Item = sstable::BlockAddr> + 'a) {
|
||||
|
||||
@@ -167,7 +167,7 @@ where
|
||||
}
|
||||
|
||||
/// Searches dictionary blocks supplied by the caller instead of reading them from the file.
|
||||
pub fn into_stream_given_delta_reader(
|
||||
pub fn into_stream_from_delta_reader(
|
||||
self,
|
||||
delta_reader: DeltaReader<<TSSTable as SSTable>::ValueReader>,
|
||||
) -> io::Result<Streamer<'a, TSSTable, A, K>> {
|
||||
@@ -217,14 +217,14 @@ where
|
||||
merge_holes_under_bytes: usize,
|
||||
) -> io::Result<Streamer<'a, TSSTable, A, K>> {
|
||||
let delta_reader = self.delta_reader_async(merge_holes_under_bytes).await?;
|
||||
self.into_stream_given_delta_reader(delta_reader)
|
||||
self.into_stream_from_delta_reader(delta_reader)
|
||||
}
|
||||
|
||||
/// Creates the stream corresponding to the range
|
||||
/// of terms defined using the `StreamerBuilder`.
|
||||
pub fn into_stream(self) -> io::Result<Streamer<'a, TSSTable, A, K>> {
|
||||
let delta_reader = self.delta_reader()?;
|
||||
self.into_stream_given_delta_reader(delta_reader)
|
||||
self.into_stream_from_delta_reader(delta_reader)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user