2022-09-05 17:31:26 +02:00
|
|
|
use std::borrow::Cow;
|
|
|
|
use std::cmp;
|
|
|
|
use std::fs::File;
|
|
|
|
|
2022-09-05 13:49:52 +02:00
|
|
|
use grenad::CompressionType;
|
|
|
|
use heed::types::ByteSlice;
|
|
|
|
use heed::{BytesEncode, Error, RoTxn, RwTxn};
|
|
|
|
use log::debug;
|
|
|
|
use roaring::RoaringBitmap;
|
|
|
|
use time::OffsetDateTime;
|
2020-11-17 21:19:25 +01:00
|
|
|
|
2022-09-05 17:31:26 +02:00
|
|
|
use crate::facet::FacetType;
|
|
|
|
use crate::heed_codec::facet::{
|
|
|
|
ByteSliceRef, FacetGroupKey, FacetGroupKeyCodec, FacetGroupValue, FacetGroupValueCodec,
|
|
|
|
};
|
|
|
|
use crate::update::index_documents::{create_writer, writer_into_reader};
|
|
|
|
use crate::{CboRoaringBitmapCodec, FieldId, Index, Result};
|
|
|
|
|
2022-08-31 13:03:36 +02:00
|
|
|
pub struct FacetsUpdateBulk<'i> {
|
2020-11-17 21:19:25 +01:00
|
|
|
index: &'i Index,
|
2022-09-05 13:01:36 +02:00
|
|
|
database: heed::Database<FacetGroupKeyCodec<ByteSliceRef>, FacetGroupValueCodec>,
|
2022-09-06 11:52:57 +02:00
|
|
|
group_size: u8,
|
2022-09-05 13:49:52 +02:00
|
|
|
min_level_size: u8,
|
2022-09-01 08:34:26 +02:00
|
|
|
facet_type: FacetType,
|
2022-09-05 12:52:05 +02:00
|
|
|
// None if level 0 does not need to be updated
|
|
|
|
new_data: Option<grenad::Reader<File>>,
|
2020-11-17 21:19:25 +01:00
|
|
|
}
|
|
|
|
|
2022-08-31 13:03:36 +02:00
|
|
|
impl<'i> FacetsUpdateBulk<'i> {
|
2022-09-05 12:52:05 +02:00
|
|
|
pub fn new(
|
|
|
|
index: &'i Index,
|
|
|
|
facet_type: FacetType,
|
|
|
|
new_data: grenad::Reader<File>,
|
|
|
|
) -> FacetsUpdateBulk<'i> {
|
2022-08-31 13:03:36 +02:00
|
|
|
FacetsUpdateBulk {
|
2020-11-17 21:19:25 +01:00
|
|
|
index,
|
2022-09-01 08:34:26 +02:00
|
|
|
database: match facet_type {
|
2022-09-05 13:49:52 +02:00
|
|
|
FacetType::String => index
|
|
|
|
.facet_id_string_docids
|
|
|
|
.remap_key_type::<FacetGroupKeyCodec<ByteSliceRef>>(),
|
2022-09-01 08:34:26 +02:00
|
|
|
FacetType::Number => {
|
2022-09-05 13:01:36 +02:00
|
|
|
index.facet_id_f64_docids.remap_key_type::<FacetGroupKeyCodec<ByteSliceRef>>()
|
2022-09-01 08:34:26 +02:00
|
|
|
}
|
|
|
|
},
|
2022-09-06 11:52:57 +02:00
|
|
|
group_size: 4,
|
2022-08-30 14:03:18 +02:00
|
|
|
min_level_size: 5,
|
2022-09-01 08:34:26 +02:00
|
|
|
facet_type,
|
2022-09-05 12:52:05 +02:00
|
|
|
new_data: Some(new_data),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn new_not_updating_level_0(
|
|
|
|
index: &'i Index,
|
|
|
|
facet_type: FacetType,
|
|
|
|
) -> FacetsUpdateBulk<'i> {
|
|
|
|
FacetsUpdateBulk {
|
|
|
|
index,
|
|
|
|
database: match facet_type {
|
2022-09-05 13:49:52 +02:00
|
|
|
FacetType::String => index
|
|
|
|
.facet_id_string_docids
|
|
|
|
.remap_key_type::<FacetGroupKeyCodec<ByteSliceRef>>(),
|
2022-09-05 12:52:05 +02:00
|
|
|
FacetType::Number => {
|
2022-09-05 13:01:36 +02:00
|
|
|
index.facet_id_f64_docids.remap_key_type::<FacetGroupKeyCodec<ByteSliceRef>>()
|
2022-09-05 12:52:05 +02:00
|
|
|
}
|
|
|
|
},
|
2022-09-06 11:52:57 +02:00
|
|
|
group_size: 4,
|
2022-09-05 12:52:05 +02:00
|
|
|
min_level_size: 5,
|
|
|
|
facet_type,
|
|
|
|
new_data: None,
|
2020-11-17 21:19:25 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-17 12:58:11 +02:00
|
|
|
/// The number of elements from the level below that are represented by a single element in the level above
|
|
|
|
///
|
|
|
|
/// This setting is always greater than or equal to 2.
|
2022-09-05 13:49:52 +02:00
|
|
|
pub fn level_group_size(mut self, value: u8) -> Self {
|
2022-09-06 11:52:57 +02:00
|
|
|
self.group_size = cmp::max(value, 2);
|
2020-11-17 21:19:25 +01:00
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2022-08-17 12:58:11 +02:00
|
|
|
/// The minimum number of elements that a level is allowed to have.
|
2022-09-05 13:49:52 +02:00
|
|
|
pub fn min_level_size(mut self, value: u8) -> Self {
|
2022-09-06 11:52:57 +02:00
|
|
|
self.min_level_size = cmp::max(value, 2);
|
2020-11-17 21:19:25 +01:00
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2022-08-31 13:03:36 +02:00
|
|
|
#[logging_timer::time("FacetsUpdateBulk::{}")]
|
2022-09-06 11:52:57 +02:00
|
|
|
pub fn execute(self, wtxn: &mut heed::RwTxn) -> Result<()> {
|
2022-09-05 12:52:05 +02:00
|
|
|
debug!("Computing and writing the facet values levels docids into LMDB on disk...");
|
|
|
|
|
2022-09-06 11:52:57 +02:00
|
|
|
let Self { index, database, group_size, min_level_size, facet_type, new_data } = self;
|
|
|
|
|
|
|
|
index.set_updated_at(wtxn, &OffsetDateTime::now_utc())?;
|
|
|
|
|
|
|
|
let inner = FacetsUpdateBulkInner { db: database, new_data, group_size, min_level_size };
|
|
|
|
|
|
|
|
let field_ids = index.faceted_fields_ids(wtxn)?.iter().copied().collect::<Box<[_]>>();
|
2020-11-17 21:19:25 +01:00
|
|
|
|
2022-09-06 11:52:57 +02:00
|
|
|
inner.update(wtxn, &field_ids, |wtxn, field_id, all_docids| {
|
|
|
|
index.put_faceted_documents_ids(wtxn, field_id, facet_type, &all_docids)?;
|
|
|
|
Ok(())
|
|
|
|
})?;
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub(crate) struct FacetsUpdateBulkInner<R: std::io::Read + std::io::Seek> {
|
|
|
|
pub db: heed::Database<FacetGroupKeyCodec<ByteSliceRef>, FacetGroupValueCodec>,
|
|
|
|
pub new_data: Option<grenad::Reader<R>>,
|
|
|
|
pub group_size: u8,
|
|
|
|
pub min_level_size: u8,
|
|
|
|
}
|
|
|
|
impl<R: std::io::Read + std::io::Seek> FacetsUpdateBulkInner<R> {
|
|
|
|
pub fn update(
|
|
|
|
mut self,
|
|
|
|
wtxn: &mut RwTxn,
|
|
|
|
field_ids: &[u16],
|
|
|
|
mut handle_all_docids: impl FnMut(&mut RwTxn, FieldId, RoaringBitmap) -> Result<()>,
|
|
|
|
) -> Result<()> {
|
|
|
|
self.update_level0(wtxn)?;
|
|
|
|
for &field_id in field_ids.iter() {
|
2022-08-30 14:03:18 +02:00
|
|
|
self.clear_levels(wtxn, field_id)?;
|
|
|
|
}
|
2022-07-18 09:59:08 +02:00
|
|
|
|
2022-09-06 11:52:57 +02:00
|
|
|
for &field_id in field_ids.iter() {
|
2022-09-05 12:52:05 +02:00
|
|
|
let (level_readers, all_docids) = self.compute_levels_for_field_id(field_id, &wtxn)?;
|
2022-08-30 14:03:18 +02:00
|
|
|
|
2022-09-06 11:52:57 +02:00
|
|
|
handle_all_docids(wtxn, field_id, all_docids)?;
|
2022-08-30 14:03:18 +02:00
|
|
|
|
|
|
|
for level_reader in level_readers {
|
2022-09-05 12:52:05 +02:00
|
|
|
let mut cursor = level_reader.into_cursor()?;
|
|
|
|
while let Some((k, v)) = cursor.move_on_next()? {
|
2022-09-06 11:52:57 +02:00
|
|
|
self.db.remap_types::<ByteSlice, ByteSlice>().put(wtxn, k, v)?;
|
2022-09-05 12:52:05 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2022-09-06 11:52:57 +02:00
|
|
|
fn clear_levels(&self, wtxn: &mut heed::RwTxn, field_id: FieldId) -> Result<()> {
|
|
|
|
let left = FacetGroupKey::<&[u8]> { field_id, level: 1, left_bound: &[] };
|
|
|
|
let right = FacetGroupKey::<&[u8]> { field_id, level: u8::MAX, left_bound: &[] };
|
|
|
|
let range = left..=right;
|
|
|
|
self.db.delete_range(wtxn, &range).map(drop)?;
|
|
|
|
Ok(())
|
|
|
|
}
|
2022-09-05 12:52:05 +02:00
|
|
|
fn update_level0(&mut self, wtxn: &mut RwTxn) -> Result<()> {
|
|
|
|
let new_data = match self.new_data.take() {
|
|
|
|
Some(x) => x,
|
|
|
|
None => return Ok(()),
|
|
|
|
};
|
2022-09-06 11:52:57 +02:00
|
|
|
if self.db.is_empty(wtxn)? {
|
2022-09-05 12:52:05 +02:00
|
|
|
let mut buffer = Vec::new();
|
2022-09-06 11:52:57 +02:00
|
|
|
let mut database = self.db.iter_mut(wtxn)?.remap_types::<ByteSlice, ByteSlice>();
|
2022-09-05 12:52:05 +02:00
|
|
|
let mut cursor = new_data.into_cursor()?;
|
|
|
|
while let Some((key, value)) = cursor.move_on_next()? {
|
2022-09-05 13:49:52 +02:00
|
|
|
buffer.clear();
|
|
|
|
// the group size for level 0
|
|
|
|
buffer.push(1);
|
|
|
|
// then we extend the buffer with the docids bitmap
|
|
|
|
buffer.extend_from_slice(value);
|
|
|
|
unsafe { database.append(key, &buffer)? };
|
2022-09-05 12:52:05 +02:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
let mut buffer = Vec::new();
|
2022-09-06 11:52:57 +02:00
|
|
|
let database = self.db.remap_types::<ByteSlice, ByteSlice>();
|
2022-09-05 12:52:05 +02:00
|
|
|
|
|
|
|
let mut cursor = new_data.into_cursor()?;
|
|
|
|
while let Some((key, value)) = cursor.move_on_next()? {
|
2022-09-05 13:49:52 +02:00
|
|
|
// the value is a CboRoaringBitmap, but I still need to prepend the
|
|
|
|
// group size for level 0 (= 1) to it
|
|
|
|
buffer.clear();
|
|
|
|
buffer.push(1);
|
|
|
|
// then we extend the buffer with the docids bitmap
|
|
|
|
match database.get(wtxn, key)? {
|
|
|
|
Some(prev_value) => {
|
|
|
|
let old_bitmap = &prev_value[1..];
|
|
|
|
CboRoaringBitmapCodec::merge_into(
|
|
|
|
&[Cow::Borrowed(value), Cow::Borrowed(old_bitmap)],
|
|
|
|
&mut buffer,
|
|
|
|
)?;
|
|
|
|
}
|
|
|
|
None => {
|
|
|
|
buffer.extend_from_slice(value);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
database.put(wtxn, key, &buffer)?;
|
2022-07-16 20:26:59 +02:00
|
|
|
}
|
2020-11-17 21:19:25 +01:00
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
2022-08-30 14:03:18 +02:00
|
|
|
fn compute_levels_for_field_id(
|
|
|
|
&self,
|
|
|
|
field_id: FieldId,
|
|
|
|
txn: &RoTxn,
|
|
|
|
) -> Result<(Vec<grenad::Reader<File>>, RoaringBitmap)> {
|
2022-09-05 13:49:52 +02:00
|
|
|
// TODO: first check whether there is anything in level 0?
|
2022-08-30 14:03:18 +02:00
|
|
|
|
|
|
|
let mut all_docids = RoaringBitmap::new();
|
2022-09-06 11:52:57 +02:00
|
|
|
let subwriters = self.compute_higher_levels(txn, field_id, 32, &mut |bitmaps, _| {
|
2022-08-30 14:03:18 +02:00
|
|
|
for bitmap in bitmaps {
|
|
|
|
all_docids |= bitmap;
|
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
})?;
|
2022-07-18 09:59:08 +02:00
|
|
|
|
2022-08-30 14:03:18 +02:00
|
|
|
Ok((subwriters, all_docids))
|
2022-07-18 09:59:08 +02:00
|
|
|
}
|
2022-09-06 11:52:57 +02:00
|
|
|
fn read_level_0<'t>(
|
2022-08-30 14:03:18 +02:00
|
|
|
&self,
|
2022-09-06 11:52:57 +02:00
|
|
|
rtxn: &'t RoTxn,
|
|
|
|
field_id: u16,
|
2022-08-30 14:03:18 +02:00
|
|
|
handle_group: &mut dyn FnMut(&[RoaringBitmap], &'t [u8]) -> Result<()>,
|
|
|
|
) -> Result<()> {
|
2022-07-20 09:49:40 +02:00
|
|
|
// we read the elements one by one and
|
2022-08-30 14:03:18 +02:00
|
|
|
// 1. keep track of the left bound
|
2022-07-20 09:49:40 +02:00
|
|
|
// 2. fill the `bitmaps` vector to give it to level 1 once `level_group_size` elements were read
|
2022-07-18 09:59:08 +02:00
|
|
|
let mut bitmaps = vec![];
|
2022-07-16 20:26:59 +02:00
|
|
|
|
2022-08-30 14:03:18 +02:00
|
|
|
let mut level_0_prefix = vec![];
|
2022-09-06 11:52:57 +02:00
|
|
|
level_0_prefix.extend_from_slice(&field_id.to_be_bytes());
|
2022-08-30 14:03:18 +02:00
|
|
|
level_0_prefix.push(0);
|
2022-08-29 16:01:54 +02:00
|
|
|
|
2022-08-30 14:03:18 +02:00
|
|
|
let level_0_iter = self
|
|
|
|
.db
|
|
|
|
.as_polymorph()
|
2022-09-06 11:52:57 +02:00
|
|
|
.prefix_iter::<_, ByteSlice, ByteSlice>(rtxn, level_0_prefix.as_slice())?
|
2022-09-05 13:01:36 +02:00
|
|
|
.remap_types::<FacetGroupKeyCodec<ByteSliceRef>, FacetGroupValueCodec>();
|
2022-07-18 09:59:08 +02:00
|
|
|
|
2022-08-30 14:03:18 +02:00
|
|
|
let mut left_bound: &[u8] = &[];
|
|
|
|
let mut first_iteration_for_new_group = true;
|
|
|
|
for el in level_0_iter {
|
|
|
|
let (key, value) = el?;
|
2022-08-29 16:01:54 +02:00
|
|
|
let bound = key.left_bound;
|
|
|
|
let docids = value.bitmap;
|
2022-07-18 09:59:08 +02:00
|
|
|
|
2022-07-16 20:26:59 +02:00
|
|
|
if first_iteration_for_new_group {
|
2022-08-30 14:03:18 +02:00
|
|
|
left_bound = bound;
|
2022-07-16 20:26:59 +02:00
|
|
|
first_iteration_for_new_group = false;
|
|
|
|
}
|
|
|
|
bitmaps.push(docids);
|
|
|
|
|
2022-09-06 11:52:57 +02:00
|
|
|
if bitmaps.len() == self.group_size as usize {
|
2022-08-30 14:17:40 +02:00
|
|
|
handle_group(&bitmaps, left_bound)?;
|
2022-07-16 20:26:59 +02:00
|
|
|
first_iteration_for_new_group = true;
|
|
|
|
bitmaps.clear();
|
|
|
|
}
|
|
|
|
}
|
2022-07-20 09:49:40 +02:00
|
|
|
// don't forget to give the leftover bitmaps as well
|
2022-07-16 20:26:59 +02:00
|
|
|
if !bitmaps.is_empty() {
|
2022-08-30 14:17:40 +02:00
|
|
|
handle_group(&bitmaps, left_bound)?;
|
2022-07-16 20:26:59 +02:00
|
|
|
bitmaps.clear();
|
|
|
|
}
|
2022-08-30 14:03:18 +02:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Compute the content of the database levels from its level 0 for the given field id.
|
|
|
|
///
|
|
|
|
/// ## Returns:
|
2022-09-05 13:49:52 +02:00
|
|
|
/// A vector of grenad::Reader. The reader at index `i` corresponds to the elements of level `i + 1`
|
2022-08-30 14:03:18 +02:00
|
|
|
/// that must be inserted into the database.
|
2022-09-06 11:52:57 +02:00
|
|
|
fn compute_higher_levels<'t>(
|
2022-08-30 14:03:18 +02:00
|
|
|
&self,
|
2022-09-06 11:52:57 +02:00
|
|
|
rtxn: &'t RoTxn,
|
|
|
|
field_id: u16,
|
2022-08-30 14:03:18 +02:00
|
|
|
level: u8,
|
|
|
|
handle_group: &mut dyn FnMut(&[RoaringBitmap], &'t [u8]) -> Result<()>,
|
|
|
|
) -> Result<Vec<grenad::Reader<File>>> {
|
|
|
|
if level == 0 {
|
2022-09-06 11:52:57 +02:00
|
|
|
self.read_level_0(rtxn, field_id, handle_group)?;
|
2022-08-30 14:03:18 +02:00
|
|
|
// Level 0 is already in the database
|
|
|
|
return Ok(vec![]);
|
|
|
|
}
|
2022-07-20 09:49:40 +02:00
|
|
|
// level >= 1
|
|
|
|
// we compute each element of this level based on the elements of the level below it
|
2022-08-30 14:03:18 +02:00
|
|
|
// once we have computed `level_group_size` elements, we give the left bound
|
2022-07-20 09:49:40 +02:00
|
|
|
// of those elements, and their bitmaps, to the level above
|
|
|
|
|
2022-09-05 12:52:05 +02:00
|
|
|
let mut cur_writer = create_writer(CompressionType::None, None, tempfile::tempfile()?);
|
2022-09-06 11:52:57 +02:00
|
|
|
let mut cur_writer_len: usize = 0;
|
2022-07-16 20:26:59 +02:00
|
|
|
|
2022-08-30 14:03:18 +02:00
|
|
|
let mut group_sizes = vec![];
|
|
|
|
let mut left_bounds = vec![];
|
2022-07-16 20:26:59 +02:00
|
|
|
let mut bitmaps = vec![];
|
|
|
|
|
2022-07-20 09:49:40 +02:00
|
|
|
// compute the levels below
|
|
|
|
// in the callback, we fill `cur_writer` with the correct elements for this level
|
2022-09-06 11:52:57 +02:00
|
|
|
let mut sub_writers = self.compute_higher_levels(
|
|
|
|
rtxn,
|
|
|
|
field_id,
|
|
|
|
level - 1,
|
|
|
|
&mut |sub_bitmaps, left_bound| {
|
|
|
|
// TODO: is this done unnecessarily for all 32 levels?
|
|
|
|
println!("level: {level}");
|
2022-07-16 20:26:59 +02:00
|
|
|
let mut combined_bitmap = RoaringBitmap::default();
|
|
|
|
for bitmap in sub_bitmaps {
|
|
|
|
combined_bitmap |= bitmap;
|
|
|
|
}
|
2022-08-30 14:03:18 +02:00
|
|
|
group_sizes.push(sub_bitmaps.len() as u8);
|
|
|
|
left_bounds.push(left_bound);
|
2022-07-16 20:26:59 +02:00
|
|
|
|
|
|
|
bitmaps.push(combined_bitmap);
|
2022-09-06 11:52:57 +02:00
|
|
|
if bitmaps.len() != self.group_size as usize {
|
2022-08-30 14:03:18 +02:00
|
|
|
return Ok(());
|
|
|
|
}
|
|
|
|
let left_bound = left_bounds.first().unwrap();
|
|
|
|
handle_group(&bitmaps, left_bound)?;
|
|
|
|
|
|
|
|
for ((bitmap, left_bound), group_size) in
|
|
|
|
bitmaps.drain(..).zip(left_bounds.drain(..)).zip(group_sizes.drain(..))
|
|
|
|
{
|
2022-09-06 11:52:57 +02:00
|
|
|
let key = FacetGroupKey { field_id, level, left_bound };
|
2022-09-05 13:49:52 +02:00
|
|
|
let key = FacetGroupKeyCodec::<ByteSliceRef>::bytes_encode(&key)
|
|
|
|
.ok_or(Error::Encoding)?;
|
2022-08-30 14:03:18 +02:00
|
|
|
let value = FacetGroupValue { size: group_size, bitmap };
|
|
|
|
let value =
|
|
|
|
FacetGroupValueCodec::bytes_encode(&value).ok_or(Error::Encoding)?;
|
|
|
|
cur_writer.insert(key, value)?;
|
|
|
|
cur_writer_len += 1;
|
2022-07-16 20:26:59 +02:00
|
|
|
}
|
|
|
|
Ok(())
|
2022-09-06 11:52:57 +02:00
|
|
|
},
|
|
|
|
)?;
|
2022-07-20 09:49:40 +02:00
|
|
|
// don't forget to insert the leftover elements into the writer as well
|
2022-09-06 11:52:57 +02:00
|
|
|
|
|
|
|
// but only do so if the current number of elements to be inserted into this
|
|
|
|
// levelcould grow to the minimum level size
|
|
|
|
|
|
|
|
if !bitmaps.is_empty() && (cur_writer_len >= self.min_level_size as usize - 1) {
|
|
|
|
// the length of bitmaps is between 0 and group_size
|
|
|
|
assert!(bitmaps.len() < self.group_size as usize);
|
|
|
|
assert!(cur_writer_len > 0);
|
|
|
|
|
2022-08-30 14:03:18 +02:00
|
|
|
let left_bound = left_bounds.first().unwrap();
|
|
|
|
handle_group(&bitmaps, left_bound)?;
|
2022-09-06 11:52:57 +02:00
|
|
|
|
|
|
|
// Note: how many bitmaps are there here?
|
2022-08-30 14:03:18 +02:00
|
|
|
for ((bitmap, left_bound), group_size) in
|
|
|
|
bitmaps.drain(..).zip(left_bounds.drain(..)).zip(group_sizes.drain(..))
|
|
|
|
{
|
2022-09-06 11:52:57 +02:00
|
|
|
let key = FacetGroupKey { field_id, level, left_bound };
|
2022-09-05 13:49:52 +02:00
|
|
|
let key = FacetGroupKeyCodec::<ByteSliceRef>::bytes_encode(&key)
|
|
|
|
.ok_or(Error::Encoding)?;
|
2022-08-30 14:03:18 +02:00
|
|
|
let value = FacetGroupValue { size: group_size, bitmap };
|
|
|
|
let value = FacetGroupValueCodec::bytes_encode(&value).ok_or(Error::Encoding)?;
|
|
|
|
cur_writer.insert(key, value)?;
|
|
|
|
cur_writer_len += 1;
|
2022-07-16 20:26:59 +02:00
|
|
|
}
|
|
|
|
}
|
2022-09-06 11:52:57 +02:00
|
|
|
// if we inserted enough elements to reach the minimum level size, then we push the writer
|
|
|
|
if cur_writer_len as u8 >= self.min_level_size {
|
2022-08-30 14:03:18 +02:00
|
|
|
sub_writers.push(writer_into_reader(cur_writer)?);
|
2022-09-05 13:49:52 +02:00
|
|
|
} else {
|
2022-09-06 11:52:57 +02:00
|
|
|
// otherwise, if there are still leftover elements, we give them to the level above
|
|
|
|
// this is necessary in order to get the union of all docids
|
2022-09-05 13:49:52 +02:00
|
|
|
if !bitmaps.is_empty() {
|
|
|
|
handle_group(&bitmaps, left_bounds.first().unwrap())?;
|
|
|
|
}
|
2022-08-30 14:03:18 +02:00
|
|
|
}
|
2022-07-16 20:26:59 +02:00
|
|
|
return Ok(sub_writers);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-03 16:33:36 +02:00
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
2022-09-06 11:52:57 +02:00
|
|
|
use crate::heed_codec::facet::OrderedF64Codec;
|
|
|
|
use crate::milli_snap;
|
|
|
|
use crate::update::facet::tests::FacetIndex;
|
|
|
|
use roaring::RoaringBitmap;
|
|
|
|
use std::iter::once;
|
2022-08-03 16:33:36 +02:00
|
|
|
|
|
|
|
#[test]
|
2022-09-06 11:52:57 +02:00
|
|
|
fn insert() {
|
|
|
|
let test = |name: &str, group_size: u8, min_level_size: u8| {
|
|
|
|
let index =
|
|
|
|
FacetIndex::<OrderedF64Codec>::new(group_size, 0 /*NA*/, min_level_size);
|
|
|
|
|
|
|
|
let mut elements = Vec::<((u16, f64), RoaringBitmap)>::new();
|
|
|
|
for i in 0..1_000u32 {
|
|
|
|
// field id = 0, left_bound = i, docids = [i]
|
|
|
|
elements.push(((0, i as f64), once(i).collect()));
|
2022-08-03 16:33:36 +02:00
|
|
|
}
|
2022-09-06 11:52:57 +02:00
|
|
|
for i in 0..100u32 {
|
|
|
|
// field id = 1, left_bound = i, docids = [i]
|
|
|
|
elements.push(((1, i as f64), once(i).collect()));
|
2022-08-03 16:33:36 +02:00
|
|
|
}
|
2022-09-06 11:52:57 +02:00
|
|
|
let mut wtxn = index.env.write_txn().unwrap();
|
|
|
|
index.bulk_insert(&mut wtxn, &[0, 1], elements.iter());
|
2022-09-05 17:31:26 +02:00
|
|
|
|
2022-09-06 11:52:57 +02:00
|
|
|
index.verify_structure_validity(&wtxn, 0);
|
|
|
|
index.verify_structure_validity(&wtxn, 1);
|
2022-09-05 17:31:26 +02:00
|
|
|
|
2022-09-06 11:52:57 +02:00
|
|
|
wtxn.commit().unwrap();
|
2022-09-05 17:31:26 +02:00
|
|
|
|
2022-09-06 11:52:57 +02:00
|
|
|
milli_snap!(format!("{index}"), name);
|
|
|
|
};
|
2022-09-05 17:31:26 +02:00
|
|
|
|
2022-09-06 11:52:57 +02:00
|
|
|
test("default", 4, 5);
|
|
|
|
test("small_group_small_min_level", 2, 2);
|
|
|
|
test("small_group_large_min_level", 2, 128);
|
|
|
|
test("large_group_small_min_level", 16, 2);
|
|
|
|
test("odd_group_odd_min_level", 7, 3);
|
2022-09-05 17:31:26 +02:00
|
|
|
}
|
|
|
|
#[test]
|
2022-09-06 11:52:57 +02:00
|
|
|
fn insert_delete_field_insert() {
|
|
|
|
let test = |name: &str, group_size: u8, min_level_size: u8| {
|
|
|
|
let index =
|
|
|
|
FacetIndex::<OrderedF64Codec>::new(group_size, 0 /*NA*/, min_level_size);
|
|
|
|
let mut wtxn = index.env.write_txn().unwrap();
|
|
|
|
|
|
|
|
let mut elements = Vec::<((u16, f64), RoaringBitmap)>::new();
|
|
|
|
for i in 0..100u32 {
|
|
|
|
// field id = 0, left_bound = i, docids = [i]
|
|
|
|
elements.push(((0, i as f64), once(i).collect()));
|
|
|
|
}
|
|
|
|
for i in 0..100u32 {
|
|
|
|
// field id = 1, left_bound = i, docids = [i]
|
|
|
|
elements.push(((1, i as f64), once(i).collect()));
|
|
|
|
}
|
|
|
|
index.bulk_insert(&mut wtxn, &[0, 1], elements.iter());
|
2022-09-05 17:31:26 +02:00
|
|
|
|
2022-09-06 11:52:57 +02:00
|
|
|
index.verify_structure_validity(&wtxn, 0);
|
|
|
|
index.verify_structure_validity(&wtxn, 1);
|
|
|
|
// delete all the elements for the facet id 0
|
|
|
|
for i in 0..100u32 {
|
|
|
|
index.delete(&mut wtxn, 0, &(i as f64), i);
|
|
|
|
}
|
|
|
|
index.verify_structure_validity(&wtxn, 0);
|
|
|
|
index.verify_structure_validity(&wtxn, 1);
|
|
|
|
|
|
|
|
let mut elements = Vec::<((u16, f64), RoaringBitmap)>::new();
|
|
|
|
// then add some elements again for the facet id 1
|
|
|
|
for i in 0..110u32 {
|
|
|
|
// field id = 1, left_bound = i, docids = [i]
|
|
|
|
elements.push(((1, i as f64), once(i).collect()));
|
|
|
|
}
|
|
|
|
index.verify_structure_validity(&wtxn, 0);
|
|
|
|
index.verify_structure_validity(&wtxn, 1);
|
|
|
|
index.bulk_insert(&mut wtxn, &[0, 1], elements.iter());
|
2022-09-05 17:31:26 +02:00
|
|
|
|
2022-09-06 11:52:57 +02:00
|
|
|
wtxn.commit().unwrap();
|
2022-09-05 17:31:26 +02:00
|
|
|
|
2022-09-06 11:52:57 +02:00
|
|
|
milli_snap!(format!("{index}"), name);
|
|
|
|
};
|
2022-09-05 17:31:26 +02:00
|
|
|
|
2022-09-06 11:52:57 +02:00
|
|
|
test("default", 4, 5);
|
|
|
|
test("small_group_small_min_level", 2, 2);
|
|
|
|
test("small_group_large_min_level", 2, 128);
|
|
|
|
test("large_group_small_min_level", 16, 2);
|
|
|
|
test("odd_group_odd_min_level", 7, 3);
|
2022-09-05 17:31:26 +02:00
|
|
|
}
|
2022-08-03 16:33:36 +02:00
|
|
|
}
|