2022-07-20 15:49:40 +08:00
|
|
|
use std::cmp;
|
|
|
|
use std::fs::File;
|
2022-08-30 20:03:18 +08:00
|
|
|
use std::num::NonZeroUsize;
|
2022-07-20 15:49:40 +08:00
|
|
|
|
2022-08-30 20:03:18 +08:00
|
|
|
use grenad::CompressionType;
|
|
|
|
use heed::types::ByteSlice;
|
|
|
|
use heed::{BytesEncode, Error, RoTxn};
|
2020-11-18 04:19:25 +08:00
|
|
|
use log::debug;
|
|
|
|
use roaring::RoaringBitmap;
|
2022-02-15 18:41:55 +08:00
|
|
|
use time::OffsetDateTime;
|
2020-11-18 04:19:25 +08:00
|
|
|
|
2021-06-14 22:46:19 +08:00
|
|
|
use crate::error::InternalError;
|
2022-08-29 22:01:54 +08:00
|
|
|
use crate::heed_codec::facet::new::{
|
2022-08-30 20:03:18 +08:00
|
|
|
FacetGroupValue, FacetGroupValueCodec, FacetKey, FacetKeyCodec, MyByteSlice,
|
2021-06-23 23:20:04 +08:00
|
|
|
};
|
2022-08-29 22:01:54 +08:00
|
|
|
// use crate::heed_codec::CboRoaringBitmapCodec;
|
2022-01-26 00:12:12 +08:00
|
|
|
use crate::update::index_documents::{create_writer, write_into_lmdb_database, writer_into_reader};
|
2021-07-06 17:31:24 +08:00
|
|
|
use crate::{FieldId, Index, Result};
|
2020-11-18 04:19:25 +08:00
|
|
|
|
2022-08-30 20:03:18 +08:00
|
|
|
pub struct Facets<'i> {
|
2020-11-18 04:19:25 +08:00
|
|
|
index: &'i Index,
|
2022-08-30 20:03:18 +08:00
|
|
|
database: heed::Database<FacetKeyCodec<MyByteSlice>, FacetGroupValueCodec>,
|
2020-11-18 04:19:25 +08:00
|
|
|
pub(crate) chunk_compression_type: CompressionType,
|
|
|
|
pub(crate) chunk_compression_level: Option<u32>,
|
2022-08-30 20:03:18 +08:00
|
|
|
level_group_size: usize,
|
|
|
|
min_level_size: usize,
|
2020-11-18 04:19:25 +08:00
|
|
|
}
|
|
|
|
|
2022-08-30 20:03:18 +08:00
|
|
|
impl<'i> Facets<'i> {
|
|
|
|
pub fn new(
|
|
|
|
index: &'i Index,
|
|
|
|
database: heed::Database<FacetKeyCodec<MyByteSlice>, FacetGroupValueCodec>,
|
|
|
|
) -> Facets<'i> {
|
2020-11-23 20:08:57 +08:00
|
|
|
Facets {
|
2020-11-18 04:19:25 +08:00
|
|
|
index,
|
2022-08-30 20:03:18 +08:00
|
|
|
database,
|
2020-11-18 04:19:25 +08:00
|
|
|
chunk_compression_type: CompressionType::None,
|
|
|
|
chunk_compression_level: None,
|
2022-08-30 20:03:18 +08:00
|
|
|
level_group_size: 4,
|
|
|
|
min_level_size: 5,
|
2020-11-18 04:19:25 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-17 18:58:11 +08: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.
|
2020-11-28 19:43:43 +08:00
|
|
|
pub fn level_group_size(&mut self, value: NonZeroUsize) -> &mut Self {
|
2022-08-30 20:03:18 +08:00
|
|
|
self.level_group_size = cmp::max(value.get(), 2);
|
2020-11-18 04:19:25 +08:00
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2022-08-17 18:58:11 +08:00
|
|
|
/// The minimum number of elements that a level is allowed to have.
|
2020-11-28 19:43:43 +08:00
|
|
|
pub fn min_level_size(&mut self, value: NonZeroUsize) -> &mut Self {
|
2022-08-30 20:03:18 +08:00
|
|
|
self.min_level_size = value.get();
|
2020-11-18 04:19:25 +08:00
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2022-08-30 20:03:18 +08:00
|
|
|
fn clear_levels(&self, wtxn: &mut heed::RwTxn, field_id: FieldId) -> Result<()> {
|
|
|
|
let left = FacetKey::<&[u8]> { field_id, level: 1, left_bound: &[] };
|
|
|
|
let right = FacetKey::<&[u8]> { field_id, level: u8::MAX, left_bound: &[] };
|
|
|
|
let range = left..=right;
|
|
|
|
self.database.delete_range(wtxn, &range).map(drop)?;
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2021-08-24 19:55:53 +08:00
|
|
|
#[logging_timer::time("Facets::{}")]
|
2022-08-30 20:17:40 +08:00
|
|
|
pub fn execute(self, wtxn: &mut heed::RwTxn) -> Result<()> {
|
2022-08-30 20:03:18 +08:00
|
|
|
self.index.set_updated_at(wtxn, &OffsetDateTime::now_utc())?;
|
2020-11-18 04:19:25 +08:00
|
|
|
// We get the faceted fields to be able to create the facet levels.
|
2022-08-30 20:03:18 +08:00
|
|
|
let faceted_fields = self.index.faceted_fields_ids(wtxn)?.clone();
|
2020-11-18 04:19:25 +08:00
|
|
|
|
|
|
|
debug!("Computing and writing the facet values levels docids into LMDB on disk...");
|
2020-11-23 20:08:57 +08:00
|
|
|
|
2022-08-30 20:03:18 +08:00
|
|
|
for &field_id in faceted_fields.iter() {
|
|
|
|
self.clear_levels(wtxn, field_id)?;
|
|
|
|
}
|
2022-07-18 15:59:08 +08:00
|
|
|
|
2022-08-30 20:03:18 +08:00
|
|
|
let mut nested_wtxn = self.index.env.nested_write_txn(wtxn)?;
|
|
|
|
|
|
|
|
for &field_id in faceted_fields.iter() {
|
|
|
|
let (level_readers, all_docids) =
|
|
|
|
self.compute_levels_for_field_id(field_id, &nested_wtxn)?;
|
|
|
|
|
|
|
|
// TODO: this will need to be an argument to Facets as well
|
|
|
|
self.index.put_string_faceted_documents_ids(&mut nested_wtxn, field_id, &all_docids)?;
|
|
|
|
|
|
|
|
for level_reader in level_readers {
|
|
|
|
// TODO: append instead of write with merge
|
2022-07-17 02:26:59 +08:00
|
|
|
write_into_lmdb_database(
|
2022-08-29 22:01:54 +08:00
|
|
|
&mut nested_wtxn,
|
2022-08-30 20:03:18 +08:00
|
|
|
*self.database.as_polymorph(),
|
|
|
|
level_reader,
|
2022-07-17 02:26:59 +08:00
|
|
|
|_, _| {
|
2022-08-30 20:03:18 +08:00
|
|
|
Err(InternalError::IndexingMergingKeys { process: "facet string levels" })?
|
2022-07-17 02:26:59 +08:00
|
|
|
},
|
|
|
|
)?;
|
|
|
|
}
|
2020-11-18 04:19:25 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2022-08-30 20:03:18 +08:00
|
|
|
fn compute_levels_for_field_id(
|
|
|
|
&self,
|
|
|
|
field_id: FieldId,
|
|
|
|
txn: &RoTxn,
|
|
|
|
) -> Result<(Vec<grenad::Reader<File>>, RoaringBitmap)> {
|
|
|
|
let algo = CreateFacetsAlgo {
|
|
|
|
rtxn: txn,
|
|
|
|
db: &self.database,
|
2022-08-29 22:01:54 +08:00
|
|
|
field_id,
|
2022-08-30 20:03:18 +08:00
|
|
|
level_group_size: self.level_group_size,
|
|
|
|
min_level_size: self.min_level_size,
|
|
|
|
chunk_compression_type: self.chunk_compression_type,
|
|
|
|
chunk_compression_level: self.chunk_compression_level,
|
|
|
|
};
|
|
|
|
// TODO: first check whether there is anything in level 0
|
|
|
|
|
|
|
|
let mut all_docids = RoaringBitmap::new();
|
|
|
|
let subwriters = algo.compute_higher_levels(32, &mut |bitmaps, _| {
|
|
|
|
for bitmap in bitmaps {
|
|
|
|
all_docids |= bitmap;
|
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
})?;
|
|
|
|
drop(algo);
|
2022-07-18 15:59:08 +08:00
|
|
|
|
2022-08-30 20:03:18 +08:00
|
|
|
Ok((subwriters, all_docids))
|
2022-07-18 15:59:08 +08:00
|
|
|
}
|
2020-11-18 04:19:25 +08:00
|
|
|
}
|
|
|
|
|
2022-08-30 20:03:18 +08:00
|
|
|
pub struct CreateFacetsAlgo<'t> {
|
|
|
|
rtxn: &'t heed::RoTxn<'t>,
|
|
|
|
db: &'t heed::Database<FacetKeyCodec<MyByteSlice>, FacetGroupValueCodec>,
|
|
|
|
chunk_compression_type: CompressionType,
|
|
|
|
chunk_compression_level: Option<u32>,
|
|
|
|
field_id: u16,
|
|
|
|
level_group_size: usize,
|
|
|
|
min_level_size: usize,
|
2022-07-17 02:26:59 +08:00
|
|
|
}
|
2022-08-30 20:03:18 +08:00
|
|
|
impl<'t> CreateFacetsAlgo<'t> {
|
|
|
|
fn read_level_0(
|
|
|
|
&self,
|
|
|
|
handle_group: &mut dyn FnMut(&[RoaringBitmap], &'t [u8]) -> Result<()>,
|
|
|
|
) -> Result<()> {
|
2022-07-20 15:49:40 +08:00
|
|
|
// we read the elements one by one and
|
2022-08-30 20:03:18 +08:00
|
|
|
// 1. keep track of the left bound
|
2022-07-20 15:49:40 +08:00
|
|
|
// 2. fill the `bitmaps` vector to give it to level 1 once `level_group_size` elements were read
|
2022-07-18 15:59:08 +08:00
|
|
|
let mut bitmaps = vec![];
|
2022-07-17 02:26:59 +08:00
|
|
|
|
2022-08-30 20:03:18 +08:00
|
|
|
let mut level_0_prefix = vec![];
|
|
|
|
level_0_prefix.extend_from_slice(&self.field_id.to_be_bytes());
|
|
|
|
level_0_prefix.push(0);
|
2022-08-29 22:01:54 +08:00
|
|
|
|
2022-08-30 20:03:18 +08:00
|
|
|
let level_0_iter = self
|
|
|
|
.db
|
|
|
|
.as_polymorph()
|
|
|
|
.prefix_iter::<_, ByteSlice, ByteSlice>(self.rtxn, level_0_prefix.as_slice())?
|
|
|
|
.remap_types::<FacetKeyCodec<MyByteSlice>, FacetGroupValueCodec>();
|
2022-07-18 15:59:08 +08:00
|
|
|
|
2022-08-30 20:03:18 +08: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 22:01:54 +08:00
|
|
|
let bound = key.left_bound;
|
|
|
|
let docids = value.bitmap;
|
2022-07-18 15:59:08 +08:00
|
|
|
|
2022-07-17 02:26:59 +08:00
|
|
|
if first_iteration_for_new_group {
|
2022-08-30 20:03:18 +08:00
|
|
|
left_bound = bound;
|
2022-07-17 02:26:59 +08:00
|
|
|
first_iteration_for_new_group = false;
|
|
|
|
}
|
|
|
|
bitmaps.push(docids);
|
|
|
|
|
2022-08-30 20:03:18 +08:00
|
|
|
if bitmaps.len() == self.level_group_size {
|
2022-08-30 20:17:40 +08:00
|
|
|
handle_group(&bitmaps, left_bound)?;
|
2022-07-17 02:26:59 +08:00
|
|
|
first_iteration_for_new_group = true;
|
|
|
|
bitmaps.clear();
|
|
|
|
}
|
|
|
|
}
|
2022-07-20 15:49:40 +08:00
|
|
|
// don't forget to give the leftover bitmaps as well
|
2022-07-17 02:26:59 +08:00
|
|
|
if !bitmaps.is_empty() {
|
2022-08-30 20:17:40 +08:00
|
|
|
handle_group(&bitmaps, left_bound)?;
|
2022-07-17 02:26:59 +08:00
|
|
|
bitmaps.clear();
|
|
|
|
}
|
2022-08-30 20:03:18 +08:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Compute the content of the database levels from its level 0 for the given field id.
|
|
|
|
///
|
|
|
|
/// ## Returns:
|
|
|
|
/// 1. a vector of grenad::Reader. The reader at index `i` corresponds to the elements of level `i + 1`
|
|
|
|
/// that must be inserted into the database.
|
|
|
|
/// 2. a roaring bitmap of all the document ids present in the database
|
|
|
|
fn compute_higher_levels(
|
|
|
|
&self,
|
|
|
|
level: u8,
|
|
|
|
handle_group: &mut dyn FnMut(&[RoaringBitmap], &'t [u8]) -> Result<()>,
|
|
|
|
) -> Result<Vec<grenad::Reader<File>>> {
|
|
|
|
if level == 0 {
|
2022-08-30 20:17:40 +08:00
|
|
|
self.read_level_0(handle_group)?;
|
2022-08-30 20:03:18 +08:00
|
|
|
// Level 0 is already in the database
|
|
|
|
return Ok(vec![]);
|
|
|
|
}
|
2022-07-20 15:49:40 +08:00
|
|
|
// level >= 1
|
|
|
|
// we compute each element of this level based on the elements of the level below it
|
2022-08-30 20:03:18 +08:00
|
|
|
// once we have computed `level_group_size` elements, we give the left bound
|
2022-07-20 15:49:40 +08:00
|
|
|
// of those elements, and their bitmaps, to the level above
|
|
|
|
|
2022-08-30 20:03:18 +08:00
|
|
|
let mut cur_writer = create_writer(
|
|
|
|
self.chunk_compression_type,
|
|
|
|
self.chunk_compression_level,
|
|
|
|
tempfile::tempfile()?,
|
|
|
|
);
|
|
|
|
let mut cur_writer_len = 0;
|
2022-07-17 02:26:59 +08:00
|
|
|
|
2022-08-30 20:03:18 +08:00
|
|
|
let mut group_sizes = vec![];
|
|
|
|
let mut left_bounds = vec![];
|
2022-07-17 02:26:59 +08:00
|
|
|
let mut bitmaps = vec![];
|
|
|
|
|
2022-07-20 15:49:40 +08:00
|
|
|
// compute the levels below
|
|
|
|
// in the callback, we fill `cur_writer` with the correct elements for this level
|
2022-08-30 20:03:18 +08:00
|
|
|
let mut sub_writers =
|
|
|
|
self.compute_higher_levels(level - 1, &mut |sub_bitmaps, left_bound| {
|
2022-07-17 02:26:59 +08:00
|
|
|
let mut combined_bitmap = RoaringBitmap::default();
|
|
|
|
for bitmap in sub_bitmaps {
|
|
|
|
combined_bitmap |= bitmap;
|
|
|
|
}
|
2022-08-30 20:03:18 +08:00
|
|
|
group_sizes.push(sub_bitmaps.len() as u8);
|
|
|
|
left_bounds.push(left_bound);
|
2022-07-17 02:26:59 +08:00
|
|
|
|
|
|
|
bitmaps.push(combined_bitmap);
|
2022-08-30 20:03:18 +08:00
|
|
|
if bitmaps.len() != self.level_group_size {
|
|
|
|
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(..))
|
|
|
|
{
|
|
|
|
let key = FacetKey { field_id: self.field_id, level, left_bound };
|
|
|
|
let key =
|
|
|
|
FacetKeyCodec::<MyByteSlice>::bytes_encode(&key).ok_or(Error::Encoding)?;
|
|
|
|
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-17 02:26:59 +08:00
|
|
|
}
|
|
|
|
Ok(())
|
2022-08-30 20:03:18 +08:00
|
|
|
})?;
|
2022-07-20 15:49:40 +08:00
|
|
|
// don't forget to insert the leftover elements into the writer as well
|
2022-08-30 20:03:18 +08:00
|
|
|
if !bitmaps.is_empty() && cur_writer_len >= self.level_group_size * self.min_level_size {
|
|
|
|
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(..))
|
|
|
|
{
|
|
|
|
let key = FacetKey { field_id: self.field_id, level, left_bound };
|
|
|
|
let key =
|
|
|
|
FacetKeyCodec::<MyByteSlice>::bytes_encode(&key).ok_or(Error::Encoding)?;
|
|
|
|
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-17 02:26:59 +08:00
|
|
|
}
|
|
|
|
}
|
2022-08-30 20:03:18 +08:00
|
|
|
if cur_writer_len > self.level_group_size * self.min_level_size {
|
|
|
|
sub_writers.push(writer_into_reader(cur_writer)?);
|
|
|
|
}
|
2022-07-17 02:26:59 +08:00
|
|
|
return Ok(sub_writers);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-03 22:33:36 +08:00
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use std::num::NonZeroUsize;
|
|
|
|
|
2022-08-04 16:50:38 +08:00
|
|
|
use crate::db_snap;
|
|
|
|
use crate::documents::documents_batch_reader_from_objects;
|
|
|
|
use crate::index::tests::TempIndex;
|
2022-08-03 22:33:36 +08:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_facets_number() {
|
|
|
|
let test =
|
|
|
|
|name: &str, group_size: Option<NonZeroUsize>, min_level_size: Option<NonZeroUsize>| {
|
|
|
|
let mut index = TempIndex::new_with_map_size(4096 * 1000 * 10); // 40MB
|
|
|
|
index.index_documents_config.autogenerate_docids = true;
|
|
|
|
index.index_documents_config.facet_level_group_size = group_size;
|
|
|
|
index.index_documents_config.facet_min_level_size = min_level_size;
|
|
|
|
|
|
|
|
index
|
|
|
|
.update_settings(|settings| {
|
|
|
|
settings.set_filterable_fields(
|
|
|
|
IntoIterator::into_iter(["facet".to_owned(), "facet2".to_owned()])
|
|
|
|
.collect(),
|
|
|
|
);
|
|
|
|
})
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
let mut documents = vec![];
|
|
|
|
for i in 0..1_000 {
|
|
|
|
documents.push(serde_json::json!({ "facet": i }).as_object().unwrap().clone());
|
|
|
|
}
|
|
|
|
for i in 0..100 {
|
|
|
|
documents.push(serde_json::json!({ "facet2": i }).as_object().unwrap().clone());
|
|
|
|
}
|
2022-08-04 16:50:38 +08:00
|
|
|
let documents = documents_batch_reader_from_objects(documents);
|
2022-08-03 22:33:36 +08:00
|
|
|
|
|
|
|
index.add_documents(documents).unwrap();
|
|
|
|
|
|
|
|
db_snap!(index, facet_id_f64_docids, name);
|
|
|
|
};
|
|
|
|
|
|
|
|
test("default", None, None);
|
|
|
|
test("tiny_groups_tiny_levels", NonZeroUsize::new(1), NonZeroUsize::new(1));
|
|
|
|
test("small_groups_small_levels", NonZeroUsize::new(2), NonZeroUsize::new(2));
|
|
|
|
test("small_groups_large_levels", NonZeroUsize::new(2), NonZeroUsize::new(128));
|
|
|
|
test("large_groups_small_levels", NonZeroUsize::new(16), NonZeroUsize::new(2));
|
|
|
|
test("large_groups_large_levels", NonZeroUsize::new(16), NonZeroUsize::new(256));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_facets_string() {
|
|
|
|
let test = |name: &str,
|
|
|
|
group_size: Option<NonZeroUsize>,
|
|
|
|
min_level_size: Option<NonZeroUsize>| {
|
|
|
|
let mut index = TempIndex::new_with_map_size(4096 * 1000 * 10); // 40MB
|
|
|
|
index.index_documents_config.autogenerate_docids = true;
|
|
|
|
index.index_documents_config.facet_level_group_size = group_size;
|
|
|
|
index.index_documents_config.facet_min_level_size = min_level_size;
|
|
|
|
|
|
|
|
index
|
|
|
|
.update_settings(|settings| {
|
|
|
|
settings.set_filterable_fields(
|
|
|
|
IntoIterator::into_iter(["facet".to_owned(), "facet2".to_owned()])
|
|
|
|
.collect(),
|
|
|
|
);
|
|
|
|
})
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
let mut documents = vec![];
|
|
|
|
for i in 0..100 {
|
|
|
|
documents.push(
|
|
|
|
serde_json::json!({ "facet": format!("s{i:X}") }).as_object().unwrap().clone(),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
for i in 0..10 {
|
|
|
|
documents.push(
|
|
|
|
serde_json::json!({ "facet2": format!("s{i:X}") }).as_object().unwrap().clone(),
|
|
|
|
);
|
|
|
|
}
|
2022-08-03 22:49:03 +08:00
|
|
|
let documents = documents_batch_reader_from_objects(documents);
|
2022-08-03 22:33:36 +08:00
|
|
|
|
|
|
|
index.add_documents(documents).unwrap();
|
|
|
|
|
|
|
|
db_snap!(index, facet_id_string_docids, name);
|
|
|
|
};
|
|
|
|
|
|
|
|
test("default", None, None);
|
|
|
|
test("tiny_groups_tiny_levels", NonZeroUsize::new(1), NonZeroUsize::new(1));
|
|
|
|
}
|
|
|
|
}
|