2022-09-07 18:04:07 +02:00
|
|
|
use std::fs::File;
|
2023-09-28 16:26:01 +02:00
|
|
|
use std::io::BufReader;
|
2022-09-07 18:04:07 +02:00
|
|
|
|
2024-01-22 16:23:12 +01:00
|
|
|
use grenad::{CompressionType, Merger};
|
2023-11-27 11:52:22 +01:00
|
|
|
use heed::types::Bytes;
|
2023-11-22 18:21:19 +01:00
|
|
|
use heed::{BytesDecode, BytesEncode, Error, PutFlags, RoTxn, RwTxn};
|
2022-09-05 13:49:52 +02:00
|
|
|
use roaring::RoaringBitmap;
|
2020-11-17 21:19:25 +01:00
|
|
|
|
2022-09-07 16:44:08 +02:00
|
|
|
use super::{FACET_GROUP_SIZE, FACET_MIN_LEVEL_SIZE};
|
2022-09-07 18:04:07 +02:00
|
|
|
use crate::facet::FacetType;
|
|
|
|
use crate::heed_codec::facet::{
|
2022-10-12 09:42:55 +02:00
|
|
|
FacetGroupKey, FacetGroupKeyCodec, FacetGroupValue, FacetGroupValueCodec,
|
2022-09-07 18:04:07 +02:00
|
|
|
};
|
2023-11-27 11:52:22 +01:00
|
|
|
use crate::heed_codec::BytesRefCodec;
|
2023-10-23 15:55:35 +02:00
|
|
|
use crate::update::del_add::{DelAdd, KvReaderDelAdd};
|
2022-11-14 14:16:14 +01:00
|
|
|
use crate::update::index_documents::{create_writer, valid_lmdb_key, writer_into_reader};
|
2024-01-22 16:23:12 +01:00
|
|
|
use crate::update::MergeFn;
|
2023-10-31 09:53:55 +01:00
|
|
|
use crate::{CboRoaringBitmapCodec, CboRoaringBitmapLenCodec, FieldId, Index, Result};
|
2022-09-05 17:31:26 +02:00
|
|
|
|
2022-09-07 16:44:08 +02:00
|
|
|
/// Algorithm to insert elememts into the `facet_id_(string/f64)_docids` databases
|
|
|
|
/// by rebuilding the database "from scratch".
|
|
|
|
///
|
|
|
|
/// First, the new elements are inserted into the level 0 of the database. Then, the
|
|
|
|
/// higher levels are cleared and recomputed from the content of level 0.
|
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-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-21 15:53:39 +02:00
|
|
|
field_ids: Vec<FieldId>,
|
2022-09-05 12:52:05 +02:00
|
|
|
// None if level 0 does not need to be updated
|
2024-01-22 16:23:12 +01:00
|
|
|
delta_data: Option<Merger<BufReader<File>, MergeFn>>,
|
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,
|
2022-09-21 15:53:39 +02:00
|
|
|
field_ids: Vec<FieldId>,
|
2022-09-05 12:52:05 +02:00
|
|
|
facet_type: FacetType,
|
2024-01-22 16:23:12 +01:00
|
|
|
delta_data: Merger<BufReader<File>, MergeFn>,
|
2022-09-07 16:44:08 +02:00
|
|
|
group_size: u8,
|
|
|
|
min_level_size: u8,
|
2022-09-05 12:52:05 +02:00
|
|
|
) -> FacetsUpdateBulk<'i> {
|
2022-09-21 15:53:39 +02:00
|
|
|
FacetsUpdateBulk {
|
|
|
|
index,
|
|
|
|
field_ids,
|
|
|
|
group_size,
|
|
|
|
min_level_size,
|
|
|
|
facet_type,
|
2023-10-19 11:56:42 +02:00
|
|
|
delta_data: Some(delta_data),
|
2022-09-21 15:53:39 +02:00
|
|
|
}
|
2022-09-05 12:52:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn new_not_updating_level_0(
|
|
|
|
index: &'i Index,
|
2022-09-21 15:53:39 +02:00
|
|
|
field_ids: Vec<FieldId>,
|
2022-09-05 12:52:05 +02:00
|
|
|
facet_type: FacetType,
|
|
|
|
) -> FacetsUpdateBulk<'i> {
|
|
|
|
FacetsUpdateBulk {
|
|
|
|
index,
|
2022-09-21 15:53:39 +02:00
|
|
|
field_ids,
|
2022-09-07 16:44:08 +02:00
|
|
|
group_size: FACET_GROUP_SIZE,
|
|
|
|
min_level_size: FACET_MIN_LEVEL_SIZE,
|
2022-09-05 12:52:05 +02:00
|
|
|
facet_type,
|
2023-10-19 11:56:42 +02:00
|
|
|
delta_data: None,
|
2020-11-17 21:19:25 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-02-08 17:37:15 +01:00
|
|
|
#[tracing::instrument(level = "trace", skip_all, target = "indexing::facets::bulk")]
|
2022-09-06 11:52:57 +02:00
|
|
|
pub fn execute(self, wtxn: &mut heed::RwTxn) -> Result<()> {
|
2023-10-19 11:56:42 +02:00
|
|
|
let Self { index, field_ids, group_size, min_level_size, facet_type, delta_data } = self;
|
2022-09-07 16:44:08 +02:00
|
|
|
|
|
|
|
let db = match facet_type {
|
2023-11-27 11:52:22 +01:00
|
|
|
FacetType::String => {
|
|
|
|
index.facet_id_string_docids.remap_key_type::<FacetGroupKeyCodec<BytesRefCodec>>()
|
|
|
|
}
|
2022-09-07 16:44:08 +02:00
|
|
|
FacetType::Number => {
|
2023-11-27 11:52:22 +01:00
|
|
|
index.facet_id_f64_docids.remap_key_type::<FacetGroupKeyCodec<BytesRefCodec>>()
|
2022-09-07 16:44:08 +02:00
|
|
|
}
|
|
|
|
};
|
2022-09-06 11:52:57 +02:00
|
|
|
|
2023-10-19 11:56:42 +02:00
|
|
|
let inner = FacetsUpdateBulkInner { db, delta_data, group_size, min_level_size };
|
2022-09-06 11:52:57 +02:00
|
|
|
|
2023-10-23 15:19:33 +02:00
|
|
|
inner.update(wtxn, &field_ids)?;
|
2022-09-06 11:52:57 +02:00
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-07 16:44:08 +02:00
|
|
|
/// Implementation of `FacetsUpdateBulk` that is independent of milli's `Index` type
|
2022-09-06 11:52:57 +02:00
|
|
|
pub(crate) struct FacetsUpdateBulkInner<R: std::io::Read + std::io::Seek> {
|
2023-11-27 11:52:22 +01:00
|
|
|
pub db: heed::Database<FacetGroupKeyCodec<BytesRefCodec>, FacetGroupValueCodec>,
|
2024-01-22 16:23:12 +01:00
|
|
|
pub delta_data: Option<Merger<R, MergeFn>>,
|
2022-09-06 11:52:57 +02:00
|
|
|
pub group_size: u8,
|
|
|
|
pub min_level_size: u8,
|
|
|
|
}
|
|
|
|
impl<R: std::io::Read + std::io::Seek> FacetsUpdateBulkInner<R> {
|
2023-10-23 15:19:33 +02:00
|
|
|
pub fn update(mut self, wtxn: &mut RwTxn, field_ids: &[u16]) -> Result<()> {
|
2022-09-06 11:52:57 +02:00
|
|
|
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() {
|
2023-10-23 15:19:33 +02:00
|
|
|
let level_readers = self.compute_levels_for_field_id(field_id, wtxn)?;
|
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()? {
|
2023-11-27 11:52:22 +01:00
|
|
|
self.db.remap_types::<Bytes, Bytes>().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(())
|
|
|
|
}
|
2023-10-17 18:09:41 +02:00
|
|
|
|
2022-09-05 12:52:05 +02:00
|
|
|
fn update_level0(&mut self, wtxn: &mut RwTxn) -> Result<()> {
|
2023-10-19 11:56:42 +02:00
|
|
|
let delta_data = match self.delta_data.take() {
|
2022-09-05 12:52:05 +02:00
|
|
|
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();
|
2023-11-27 11:52:22 +01:00
|
|
|
let mut database = self.db.iter_mut(wtxn)?.remap_types::<Bytes, Bytes>();
|
2024-01-22 16:23:12 +01:00
|
|
|
let mut iter = delta_data.into_stream_merger_iter()?;
|
|
|
|
while let Some((key, value)) = iter.next()? {
|
2022-11-14 14:16:14 +01:00
|
|
|
if !valid_lmdb_key(key) {
|
|
|
|
continue;
|
|
|
|
}
|
2023-10-23 15:55:35 +02:00
|
|
|
let value = KvReaderDelAdd::new(value);
|
2023-10-19 11:56:42 +02:00
|
|
|
|
|
|
|
// DB is empty, it is safe to ignore Del operations
|
|
|
|
let Some(value) = value.get(DelAdd::Addition) else {
|
|
|
|
continue;
|
|
|
|
};
|
|
|
|
|
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);
|
2023-11-22 18:21:19 +01:00
|
|
|
unsafe {
|
2023-11-27 11:52:22 +01:00
|
|
|
database.put_current_with_options::<Bytes>(PutFlags::APPEND, key, &buffer)?
|
2023-11-22 18:21:19 +01:00
|
|
|
};
|
2022-09-05 12:52:05 +02:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
let mut buffer = Vec::new();
|
2023-11-27 11:52:22 +01:00
|
|
|
let database = self.db.remap_types::<Bytes, Bytes>();
|
2022-09-05 12:52:05 +02:00
|
|
|
|
2024-01-22 16:23:12 +01:00
|
|
|
let mut iter = delta_data.into_stream_merger_iter()?;
|
|
|
|
while let Some((key, value)) = iter.next()? {
|
2022-11-14 14:16:14 +01:00
|
|
|
if !valid_lmdb_key(key) {
|
|
|
|
continue;
|
|
|
|
}
|
2023-10-19 11:56:42 +02:00
|
|
|
|
2023-10-23 15:55:35 +02:00
|
|
|
let value = KvReaderDelAdd::new(value);
|
2023-10-19 11:56:42 +02:00
|
|
|
|
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) => {
|
2023-10-23 16:06:06 +02:00
|
|
|
// prev_value is the group size for level 0, followed by the previous bitmap.
|
2022-09-05 13:49:52 +02:00
|
|
|
let old_bitmap = &prev_value[1..];
|
2023-10-19 11:56:42 +02:00
|
|
|
CboRoaringBitmapCodec::merge_deladd_into(value, old_bitmap, &mut buffer)?;
|
2022-09-05 13:49:52 +02:00
|
|
|
}
|
|
|
|
None => {
|
2023-10-19 11:56:42 +02:00
|
|
|
// it is safe to ignore the del in that case.
|
|
|
|
let Some(value) = value.get(DelAdd::Addition) else {
|
|
|
|
// won't put the key in DB as the value would be empty
|
|
|
|
continue;
|
|
|
|
};
|
|
|
|
|
2022-09-05 13:49:52 +02:00
|
|
|
buffer.extend_from_slice(value);
|
|
|
|
}
|
|
|
|
};
|
2023-10-31 09:53:55 +01:00
|
|
|
let new_bitmap = &buffer[1..];
|
|
|
|
// if the new bitmap is empty, let's remove it
|
|
|
|
if CboRoaringBitmapLenCodec::bytes_decode(new_bitmap).unwrap_or_default() == 0 {
|
|
|
|
database.delete(wtxn, key)?;
|
|
|
|
} else {
|
|
|
|
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,
|
2023-10-23 15:19:33 +02:00
|
|
|
) -> Result<Vec<grenad::Reader<BufReader<File>>>> {
|
|
|
|
let subwriters = self.compute_higher_levels(txn, field_id, 32, &mut |_, _| Ok(()))?;
|
2022-07-18 09:59:08 +02:00
|
|
|
|
2023-10-23 15:19:33 +02:00
|
|
|
Ok(subwriters)
|
2022-07-18 09:59:08 +02:00
|
|
|
}
|
2022-10-28 16:41:35 +02:00
|
|
|
#[allow(clippy::type_complexity)]
|
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-10-28 16:42:23 +02:00
|
|
|
handle_group: &mut dyn FnMut(&[RoaringBitmap], &'t [u8]) -> Result<()>,
|
2022-08-30 14:03:18 +02:00
|
|
|
) -> 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
|
2023-11-27 11:52:22 +01:00
|
|
|
.remap_types::<Bytes, Bytes>()
|
2023-11-22 18:21:19 +01:00
|
|
|
.prefix_iter(rtxn, level_0_prefix.as_slice())?
|
2023-11-27 11:52:22 +01:00
|
|
|
.remap_types::<FacetGroupKeyCodec<BytesRefCodec>, 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-10-28 16:41:35 +02:00
|
|
|
#[allow(clippy::type_complexity)]
|
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,
|
2022-10-28 16:42:23 +02:00
|
|
|
handle_group: &mut dyn FnMut(&[RoaringBitmap], &'t [u8]) -> Result<()>,
|
2023-09-28 16:26:01 +02:00
|
|
|
) -> Result<Vec<grenad::Reader<BufReader<File>>>> {
|
2022-08-30 14:03:18 +02:00
|
|
|
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| {
|
2022-07-16 20:26:59 +02:00
|
|
|
let mut combined_bitmap = RoaringBitmap::default();
|
|
|
|
for bitmap in sub_bitmaps {
|
|
|
|
combined_bitmap |= bitmap;
|
|
|
|
}
|
2022-11-30 14:27:36 +01:00
|
|
|
// The conversion of sub_bitmaps.len() to a u8 will always be correct
|
|
|
|
// since its length is bounded by max_group_size, which is a u8.
|
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 };
|
2023-11-27 11:52:22 +01:00
|
|
|
let key = FacetGroupKeyCodec::<BytesRefCodec>::bytes_encode(&key)
|
2023-11-22 18:21:19 +01:00
|
|
|
.map_err(Error::Encoding)?;
|
2022-08-30 14:03:18 +02:00
|
|
|
let value = FacetGroupValue { size: group_size, bitmap };
|
|
|
|
let value =
|
2023-11-22 18:21:19 +01:00
|
|
|
FacetGroupValueCodec::bytes_encode(&value).map_err(Error::Encoding)?;
|
2022-08-30 14:03:18 +02:00
|
|
|
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 };
|
2023-11-27 11:52:22 +01:00
|
|
|
let key = FacetGroupKeyCodec::<BytesRefCodec>::bytes_encode(&key)
|
2023-11-22 18:21:19 +01:00
|
|
|
.map_err(Error::Encoding)?;
|
2022-08-30 14:03:18 +02:00
|
|
|
let value = FacetGroupValue { size: group_size, bitmap };
|
2023-11-22 18:21:19 +01:00
|
|
|
let value = FacetGroupValueCodec::bytes_encode(&value).map_err(Error::Encoding)?;
|
2022-08-30 14:03:18 +02:00
|
|
|
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
|
2022-11-30 14:27:36 +01:00
|
|
|
if cur_writer_len >= self.min_level_size as usize {
|
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-10-27 16:58:13 +02:00
|
|
|
Ok(sub_writers)
|
2022-07-16 20:26:59 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-03 16:33:36 +02:00
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
2022-09-07 18:04:07 +02:00
|
|
|
use std::iter::once;
|
|
|
|
|
2022-11-30 14:27:36 +01:00
|
|
|
use big_s::S;
|
|
|
|
use maplit::hashset;
|
2022-09-07 18:04:07 +02:00
|
|
|
use roaring::RoaringBitmap;
|
|
|
|
|
2022-11-30 14:27:36 +01:00
|
|
|
use crate::documents::documents_batch_reader_from_objects;
|
2022-09-06 11:52:57 +02:00
|
|
|
use crate::heed_codec::facet::OrderedF64Codec;
|
2022-12-05 10:33:31 +01:00
|
|
|
use crate::heed_codec::StrRefCodec;
|
2022-12-05 10:26:53 +01:00
|
|
|
use crate::index::tests::TempIndex;
|
2022-12-05 10:33:31 +01:00
|
|
|
use crate::update::facet::test_helpers::{ordered_string, FacetIndex};
|
2022-12-05 10:26:53 +01:00
|
|
|
use crate::{db_snap, milli_snap};
|
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 {
|
2022-09-21 15:53:39 +02:00
|
|
|
index.delete_single_docid(&mut wtxn, 0, &(i as f64), i);
|
2022-09-06 11:52:57 +02:00
|
|
|
}
|
|
|
|
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-11-30 14:27:36 +01:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn bug_3165() {
|
|
|
|
// Indexing a number of facet values that falls within certains ranges (e.g. 22_540 qualifies)
|
|
|
|
// would lead to a facet DB which was missing some levels.
|
|
|
|
// That was because before writing a level into the database, we would
|
|
|
|
// check that its size was higher than the minimum level size using
|
|
|
|
// a lossy integer conversion: `level_size as u8 >= min_level_size`.
|
|
|
|
//
|
|
|
|
// This missing level in the facet DBs would make the incremental indexer
|
|
|
|
// (and other search algorithms) crash.
|
|
|
|
//
|
|
|
|
// https://github.com/meilisearch/meilisearch/issues/3165
|
|
|
|
let index = TempIndex::new_with_map_size(4096 * 1000 * 100);
|
|
|
|
|
|
|
|
index
|
|
|
|
.update_settings(|settings| {
|
|
|
|
settings.set_primary_key("id".to_owned());
|
|
|
|
settings.set_filterable_fields(hashset! { S("id") });
|
|
|
|
})
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
let mut documents = vec![];
|
|
|
|
for i in 0..=22_540 {
|
|
|
|
documents.push(
|
|
|
|
serde_json::json! {
|
|
|
|
{
|
|
|
|
"id": i as u64,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
.as_object()
|
|
|
|
.unwrap()
|
|
|
|
.clone(),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
let documents = documents_batch_reader_from_objects(documents);
|
|
|
|
index.add_documents(documents).unwrap();
|
|
|
|
|
|
|
|
db_snap!(index, facet_id_f64_docids, "initial", @"c34f499261f3510d862fa0283bbe843a");
|
|
|
|
}
|
2022-12-05 10:33:31 +01:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn insert_string() {
|
|
|
|
let test = |name: &str, group_size: u8, min_level_size: u8| {
|
|
|
|
let index = FacetIndex::<StrRefCodec>::new(group_size, 0 /*NA*/, min_level_size);
|
|
|
|
|
|
|
|
let strings = (0..1_000).map(|i| ordered_string(i as usize)).collect::<Vec<_>>();
|
|
|
|
let mut elements = Vec::<((u16, &str), RoaringBitmap)>::new();
|
|
|
|
for i in 0..1_000u32 {
|
|
|
|
// field id = 0, left_bound = i, docids = [i]
|
|
|
|
elements.push(((0, &strings[i as usize]), once(i).collect()));
|
|
|
|
}
|
|
|
|
for i in 0..100u32 {
|
|
|
|
// field id = 1, left_bound = i, docids = [i]
|
|
|
|
elements.push(((1, &strings[i as usize]), once(i).collect()));
|
|
|
|
}
|
|
|
|
let mut wtxn = index.env.write_txn().unwrap();
|
|
|
|
index.bulk_insert(&mut wtxn, &[0, 1], elements.iter());
|
|
|
|
|
|
|
|
index.verify_structure_validity(&wtxn, 0);
|
|
|
|
index.verify_structure_validity(&wtxn, 1);
|
|
|
|
|
|
|
|
wtxn.commit().unwrap();
|
|
|
|
|
|
|
|
milli_snap!(format!("{index}"), name);
|
|
|
|
};
|
|
|
|
|
|
|
|
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-08-03 16:33:36 +02:00
|
|
|
}
|