Skip to content
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

Provide StoreReader::enumerate to simplify creation of secondary indexes #2361

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/indexer/merger.rs
Original file line number Diff line number Diff line change
Expand Up @@ -696,7 +696,7 @@ impl IndexMerger {
for old_doc_addr in doc_id_mapping.iter_old_doc_addrs() {
let doc_bytes_it = &mut document_iterators[old_doc_addr.segment_ord as usize];
if let Some(doc_bytes_res) = doc_bytes_it.next() {
let doc_bytes = doc_bytes_res?;
let (_, doc_bytes) = doc_bytes_res?;
store_writer.store_bytes(&doc_bytes)?;
} else {
return Err(DataCorruption::comment_only(format!(
Expand Down Expand Up @@ -728,7 +728,7 @@ impl IndexMerger {
|| store_reader.decompressor() != store_writer.compressor().into()
{
for doc_bytes_res in store_reader.iter_raw(reader.alive_bitset()) {
let doc_bytes = doc_bytes_res?;
let (_, doc_bytes) = doc_bytes_res?;
store_writer.store_bytes(&doc_bytes)?;
}
} else {
Expand Down
23 changes: 17 additions & 6 deletions src/store/reader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -241,12 +241,23 @@ impl StoreReader {
&'b self,
alive_bitset: Option<&'a AliveBitSet>,
) -> impl Iterator<Item = crate::Result<D>> + 'b {
self.enumerate(alive_bitset)
.map(|res| res.map(|(_, doc)| doc))
}

/// A variant of [`iter`][Self::iter] which also yields document ID.
pub fn enumerate<'a: 'b, 'b, D: DocumentDeserialize>(
&'b self,
alive_bitset: Option<&'a AliveBitSet>,
) -> impl Iterator<Item = crate::Result<(DocId, D)>> + 'b {
self.iter_raw(alive_bitset).map(|doc_bytes_res| {
let mut doc_bytes = doc_bytes_res?;
let (doc_id, mut doc_bytes) = doc_bytes_res?;

let deserializer = BinaryDocumentDeserializer::from_reader(&mut doc_bytes)
.map_err(crate::TantivyError::from)?;
D::deserialize(deserializer).map_err(crate::TantivyError::from)
let doc = D::deserialize(deserializer).map_err(crate::TantivyError::from)?;

Ok((doc_id, doc))
})
}

Expand All @@ -256,7 +267,7 @@ impl StoreReader {
pub(crate) fn iter_raw<'a: 'b, 'b>(
&'b self,
alive_bitset: Option<&'a AliveBitSet>,
) -> impl Iterator<Item = crate::Result<OwnedBytes>> + 'b {
) -> impl Iterator<Item = crate::Result<(DocId, OwnedBytes)>> + 'b {
let last_doc_id = self
.block_checkpoints()
.last()
Expand Down Expand Up @@ -284,14 +295,14 @@ impl StoreReader {

let alive = alive_bitset.map_or(true, |bitset| bitset.is_alive(doc_id));
let res = if alive {
Some((curr_block.clone(), doc_pos))
Some((doc_id, curr_block.clone(), doc_pos))
} else {
None
};
doc_pos += 1;
res
})
.map(move |(block, doc_pos)| {
.map(move |(doc_id, block, doc_pos)| {
let block = block
.ok_or_else(|| {
DataCorruption::comment_only(
Expand All @@ -304,7 +315,7 @@ impl StoreReader {
})?;

let range = block_read_index(&block, doc_pos)?;
Ok(block.slice(range))
Ok((doc_id, block.slice(range)))
})
}

Expand Down
Loading