2020-11-28 19:43:43 +08:00
|
|
|
use std::cmp;
|
2020-11-18 04:19:25 +08:00
|
|
|
use std::fs::File;
|
|
|
|
use std::num::NonZeroUsize;
|
|
|
|
|
2021-03-12 01:42:21 +08:00
|
|
|
use chrono::Utc;
|
2020-11-18 04:19:25 +08:00
|
|
|
use grenad::{CompressionType, Reader, Writer, FileFuse};
|
|
|
|
use heed::types::{ByteSlice, DecodeIgnore};
|
|
|
|
use heed::{BytesEncode, Error};
|
|
|
|
use log::debug;
|
|
|
|
use roaring::RoaringBitmap;
|
|
|
|
|
|
|
|
use crate::facet::FacetType;
|
2020-11-19 04:52:08 +08:00
|
|
|
use crate::heed_codec::CboRoaringBitmapCodec;
|
2021-04-07 17:57:16 +08:00
|
|
|
use crate::heed_codec::facet::FacetLevelValueF64Codec;
|
2020-11-18 04:19:25 +08:00
|
|
|
use crate::Index;
|
|
|
|
use crate::update::index_documents::WriteMethod;
|
|
|
|
use crate::update::index_documents::{create_writer, writer_into_reader, write_into_lmdb_database};
|
|
|
|
|
2020-11-23 20:08:57 +08:00
|
|
|
pub struct Facets<'t, 'u, 'i> {
|
2020-11-18 04:19:25 +08:00
|
|
|
wtxn: &'t mut heed::RwTxn<'i, 'u>,
|
|
|
|
index: &'i Index,
|
|
|
|
pub(crate) chunk_compression_type: CompressionType,
|
|
|
|
pub(crate) chunk_compression_level: Option<u32>,
|
|
|
|
pub(crate) chunk_fusing_shrink_size: Option<u64>,
|
2020-11-28 19:43:43 +08:00
|
|
|
level_group_size: NonZeroUsize,
|
|
|
|
min_level_size: NonZeroUsize,
|
2020-12-22 23:21:07 +08:00
|
|
|
_update_id: u64,
|
2020-11-18 04:19:25 +08:00
|
|
|
}
|
|
|
|
|
2020-11-23 20:08:57 +08:00
|
|
|
impl<'t, 'u, 'i> Facets<'t, 'u, 'i> {
|
2020-12-22 23:21:07 +08:00
|
|
|
pub fn new(
|
|
|
|
wtxn: &'t mut heed::RwTxn<'i, 'u>,
|
|
|
|
index: &'i Index,
|
|
|
|
update_id: u64,
|
2021-02-17 18:12:38 +08:00
|
|
|
) -> Facets<'t, 'u, 'i>
|
|
|
|
{
|
2020-11-23 20:08:57 +08:00
|
|
|
Facets {
|
2020-11-18 04:19:25 +08:00
|
|
|
wtxn,
|
|
|
|
index,
|
|
|
|
chunk_compression_type: CompressionType::None,
|
|
|
|
chunk_compression_level: None,
|
|
|
|
chunk_fusing_shrink_size: None,
|
2020-11-28 19:43:43 +08:00
|
|
|
level_group_size: NonZeroUsize::new(4).unwrap(),
|
|
|
|
min_level_size: NonZeroUsize::new(5).unwrap(),
|
2020-12-22 23:21:07 +08:00
|
|
|
_update_id: update_id,
|
2020-11-18 04:19:25 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-28 19:43:43 +08:00
|
|
|
pub fn level_group_size(&mut self, value: NonZeroUsize) -> &mut Self {
|
|
|
|
self.level_group_size = NonZeroUsize::new(cmp::max(value.get(), 2)).unwrap();
|
2020-11-18 04:19:25 +08:00
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2020-11-28 19:43:43 +08:00
|
|
|
pub fn min_level_size(&mut self, value: NonZeroUsize) -> &mut Self {
|
|
|
|
self.min_level_size = value;
|
2020-11-18 04:19:25 +08:00
|
|
|
self
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn execute(self) -> anyhow::Result<()> {
|
2021-03-12 01:42:21 +08:00
|
|
|
self.index.set_updated_at(self.wtxn, &Utc::now())?;
|
2020-11-18 04:19:25 +08:00
|
|
|
// We get the faceted fields to be able to create the facet levels.
|
2021-01-21 00:27:43 +08:00
|
|
|
let faceted_fields = self.index.faceted_fields_ids(self.wtxn)?;
|
2020-11-18 04:19:25 +08:00
|
|
|
|
|
|
|
debug!("Computing and writing the facet values levels docids into LMDB on disk...");
|
|
|
|
for (field_id, facet_type) in faceted_fields {
|
2020-11-23 20:08:57 +08:00
|
|
|
let (content, documents_ids) = match facet_type {
|
2021-04-07 17:57:16 +08:00
|
|
|
FacetType::String => {
|
2020-11-23 20:08:57 +08:00
|
|
|
let documents_ids = compute_faceted_documents_ids(
|
|
|
|
self.wtxn,
|
|
|
|
self.index.facet_field_id_value_docids,
|
|
|
|
field_id,
|
|
|
|
)?;
|
|
|
|
|
2021-04-07 17:57:16 +08:00
|
|
|
(None, documents_ids)
|
2020-11-19 04:52:08 +08:00
|
|
|
},
|
2021-04-07 17:57:16 +08:00
|
|
|
FacetType::Number => {
|
|
|
|
clear_field_number_levels(
|
2020-11-19 04:52:08 +08:00
|
|
|
self.wtxn,
|
2021-04-07 17:57:16 +08:00
|
|
|
self.index.facet_field_id_value_docids.remap_key_type::<FacetLevelValueF64Codec>(),
|
2020-11-19 04:52:08 +08:00
|
|
|
field_id,
|
|
|
|
)?;
|
|
|
|
|
2020-11-23 20:08:57 +08:00
|
|
|
let documents_ids = compute_faceted_documents_ids(
|
|
|
|
self.wtxn,
|
|
|
|
self.index.facet_field_id_value_docids,
|
|
|
|
field_id,
|
|
|
|
)?;
|
|
|
|
|
2021-04-07 17:57:16 +08:00
|
|
|
let content = compute_facet_number_levels(
|
2020-11-19 04:52:08 +08:00
|
|
|
self.wtxn,
|
2021-04-07 17:57:16 +08:00
|
|
|
self.index.facet_field_id_value_docids.remap_key_type::<FacetLevelValueF64Codec>(),
|
2020-11-19 04:52:08 +08:00
|
|
|
self.chunk_compression_type,
|
|
|
|
self.chunk_compression_level,
|
|
|
|
self.chunk_fusing_shrink_size,
|
2020-11-28 19:43:43 +08:00
|
|
|
self.level_group_size,
|
|
|
|
self.min_level_size,
|
2020-11-19 04:52:08 +08:00
|
|
|
field_id,
|
2020-11-23 20:08:57 +08:00
|
|
|
)?;
|
|
|
|
|
|
|
|
(Some(content), documents_ids)
|
|
|
|
},
|
2020-11-19 04:52:08 +08:00
|
|
|
};
|
2020-11-18 04:19:25 +08:00
|
|
|
|
2020-11-23 20:08:57 +08:00
|
|
|
if let Some(content) = content {
|
|
|
|
write_into_lmdb_database(
|
|
|
|
self.wtxn,
|
|
|
|
*self.index.facet_field_id_value_docids.as_polymorph(),
|
|
|
|
content,
|
|
|
|
|_, _| anyhow::bail!("invalid facet level merging"),
|
|
|
|
WriteMethod::GetMergePut,
|
|
|
|
)?;
|
|
|
|
}
|
|
|
|
|
|
|
|
self.index.put_faceted_documents_ids(self.wtxn, field_id, &documents_ids)?;
|
2020-11-18 04:19:25 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-07 17:57:16 +08:00
|
|
|
fn clear_field_number_levels<'t, >(
|
2020-11-19 04:52:08 +08:00
|
|
|
wtxn: &'t mut heed::RwTxn,
|
2021-04-07 17:57:16 +08:00
|
|
|
db: heed::Database<FacetLevelValueF64Codec, CboRoaringBitmapCodec>,
|
2020-11-18 04:19:25 +08:00
|
|
|
field_id: u8,
|
|
|
|
) -> heed::Result<()>
|
|
|
|
{
|
2021-04-07 17:57:16 +08:00
|
|
|
let left = (field_id, 1, f64::MIN, f64::MIN);
|
|
|
|
let right = (field_id, u8::MAX, f64::MAX, f64::MAX);
|
2020-11-19 04:52:08 +08:00
|
|
|
let range = left..=right;
|
2021-04-07 17:57:16 +08:00
|
|
|
db.delete_range(wtxn, &range).map(drop)
|
2020-11-18 04:19:25 +08:00
|
|
|
}
|
|
|
|
|
2021-04-07 17:57:16 +08:00
|
|
|
fn compute_facet_number_levels<'t>(
|
2020-11-19 04:52:08 +08:00
|
|
|
rtxn: &'t heed::RoTxn,
|
2021-04-07 17:57:16 +08:00
|
|
|
db: heed::Database<FacetLevelValueF64Codec, CboRoaringBitmapCodec>,
|
2020-11-18 04:19:25 +08:00
|
|
|
compression_type: CompressionType,
|
|
|
|
compression_level: Option<u32>,
|
|
|
|
shrink_size: Option<u64>,
|
2020-11-28 19:43:43 +08:00
|
|
|
level_group_size: NonZeroUsize,
|
|
|
|
min_level_size: NonZeroUsize,
|
2020-11-18 04:19:25 +08:00
|
|
|
field_id: u8,
|
|
|
|
) -> anyhow::Result<Reader<FileFuse>>
|
|
|
|
{
|
2021-04-07 17:57:16 +08:00
|
|
|
let first_level_size = db
|
|
|
|
.remap_key_type::<ByteSlice>()
|
|
|
|
.prefix_iter(rtxn, &[field_id])?
|
2020-11-18 04:19:25 +08:00
|
|
|
.remap_types::<DecodeIgnore, DecodeIgnore>()
|
|
|
|
.fold(Ok(0usize), |count, result| result.and(count).map(|c| c + 1))?;
|
|
|
|
|
|
|
|
// It is forbidden to keep a cursor and write in a database at the same time with LMDB
|
|
|
|
// therefore we write the facet levels entries into a grenad file before transfering them.
|
|
|
|
let mut writer = tempfile::tempfile().and_then(|file| {
|
|
|
|
create_writer(compression_type, compression_level, file)
|
|
|
|
})?;
|
|
|
|
|
2020-11-19 04:52:08 +08:00
|
|
|
let level_0_range = {
|
2021-04-07 17:57:16 +08:00
|
|
|
let left = (field_id, 0, f64::MIN, f64::MIN);
|
|
|
|
let right = (field_id, 0, f64::MAX, f64::MAX);
|
2020-11-19 04:52:08 +08:00
|
|
|
left..=right
|
|
|
|
};
|
|
|
|
|
2020-11-28 19:43:43 +08:00
|
|
|
// Groups sizes are always a power of the original level_group_size and therefore a group
|
|
|
|
// always maps groups of the previous level and never splits previous levels groups in half.
|
|
|
|
let group_size_iter = (1u8..)
|
|
|
|
.map(|l| (l, level_group_size.get().pow(l as u32)))
|
|
|
|
.take_while(|(_, s)| first_level_size / *s >= min_level_size.get());
|
2020-11-18 04:19:25 +08:00
|
|
|
|
2020-11-28 19:43:43 +08:00
|
|
|
for (level, group_size) in group_size_iter {
|
2021-04-07 17:57:16 +08:00
|
|
|
let mut left = 0.0;
|
|
|
|
let mut right = 0.0;
|
2020-11-18 04:19:25 +08:00
|
|
|
let mut group_docids = RoaringBitmap::new();
|
|
|
|
|
|
|
|
for (i, result) in db.range(rtxn, &level_0_range)?.enumerate() {
|
|
|
|
let ((_field_id, _level, value, _right), docids) = result?;
|
|
|
|
|
|
|
|
if i == 0 {
|
|
|
|
left = value;
|
2020-11-28 19:43:43 +08:00
|
|
|
} else if i % group_size == 0 {
|
2020-11-18 04:19:25 +08:00
|
|
|
// we found the first bound of the next group, we must store the left
|
|
|
|
// and right bounds associated with the docids.
|
2021-04-07 17:57:16 +08:00
|
|
|
write_number_entry(&mut writer, field_id, level, left, right, &group_docids)?;
|
2020-11-18 04:19:25 +08:00
|
|
|
|
|
|
|
// We save the left bound for the new group and also reset the docids.
|
|
|
|
group_docids = RoaringBitmap::new();
|
|
|
|
left = value;
|
|
|
|
}
|
|
|
|
|
|
|
|
// The right bound is always the bound we run through.
|
|
|
|
group_docids.union_with(&docids);
|
|
|
|
right = value;
|
|
|
|
}
|
|
|
|
|
|
|
|
if !group_docids.is_empty() {
|
2021-04-07 17:57:16 +08:00
|
|
|
write_number_entry(&mut writer, field_id, level, left, right, &group_docids)?;
|
2020-11-18 04:19:25 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
writer_into_reader(writer, shrink_size)
|
|
|
|
}
|
|
|
|
|
2020-11-23 20:08:57 +08:00
|
|
|
fn compute_faceted_documents_ids(
|
|
|
|
rtxn: &heed::RoTxn,
|
|
|
|
db: heed::Database<ByteSlice, CboRoaringBitmapCodec>,
|
|
|
|
field_id: u8,
|
|
|
|
) -> anyhow::Result<RoaringBitmap>
|
|
|
|
{
|
|
|
|
let mut documents_ids = RoaringBitmap::new();
|
|
|
|
for result in db.prefix_iter(rtxn, &[field_id])? {
|
|
|
|
let (_key, docids) = result?;
|
|
|
|
documents_ids.union_with(&docids);
|
|
|
|
}
|
|
|
|
Ok(documents_ids)
|
|
|
|
}
|
|
|
|
|
2021-04-07 17:57:16 +08:00
|
|
|
fn write_number_entry(
|
2020-11-18 04:19:25 +08:00
|
|
|
writer: &mut Writer<File>,
|
|
|
|
field_id: u8,
|
|
|
|
level: u8,
|
2021-04-07 17:57:16 +08:00
|
|
|
left: f64,
|
|
|
|
right: f64,
|
2020-11-18 04:19:25 +08:00
|
|
|
ids: &RoaringBitmap,
|
|
|
|
) -> anyhow::Result<()>
|
|
|
|
{
|
|
|
|
let key = (field_id, level, left, right);
|
2021-04-07 17:57:16 +08:00
|
|
|
let key = FacetLevelValueF64Codec::bytes_encode(&key).ok_or(Error::Encoding)?;
|
2020-11-18 04:19:25 +08:00
|
|
|
let data = CboRoaringBitmapCodec::bytes_encode(&ids).ok_or(Error::Encoding)?;
|
|
|
|
writer.insert(&key, &data)?;
|
|
|
|
Ok(())
|
|
|
|
}
|