Skip to content
This repository has been archived by the owner on Apr 4, 2023. It is now read-only.

Directly store the number of documents in an index #48

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
30 changes: 22 additions & 8 deletions src/index.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use roaring::RoaringBitmap;
use crate::facet::FacetType;
use crate::fields_ids_map::FieldsIdsMap;
use crate::{default_criteria, Criterion, Search};
use crate::{BEU32, DocumentId, FieldId, ExternalDocumentsIds};
use crate::{BEU32, BEU64, DocumentId, FieldId, ExternalDocumentsIds};
use crate::{
RoaringBitmapCodec, BEU32StrCodec, StrStrU8Codec, ObkvCodec,
BoRoaringBitmapCodec, CboRoaringBitmapCodec,
Expand All @@ -19,6 +19,7 @@ use crate::{
pub const CRITERIA_KEY: &str = "criteria";
pub const DISPLAYED_FIELDS_KEY: &str = "displayed-fields";
pub const DOCUMENTS_IDS_KEY: &str = "documents-ids";
pub const NUMBER_DOCUMENTS_IDS_KEY: &str = "number-documents-ids";
pub const FACETED_DOCUMENTS_IDS_PREFIX: &str = "faceted-documents-ids";
pub const FACETED_FIELDS_KEY: &str = "faceted-fields";
pub const FIELDS_IDS_MAP_KEY: &str = "fields-ids-map";
Expand Down Expand Up @@ -101,12 +102,25 @@ impl Index {

/// Writes the documents ids that corresponds to the user-ids-documents-ids FST.
pub fn put_documents_ids(&self, wtxn: &mut RwTxn, docids: &RoaringBitmap) -> heed::Result<()> {
self.main.put::<_, Str, RoaringBitmapCodec>(wtxn, DOCUMENTS_IDS_KEY, docids)
let len = docids.len();
self.main.put::<_, Str, RoaringBitmapCodec>(wtxn, DOCUMENTS_IDS_KEY, docids)?;
self.main.put::<_, Str, OwnedType<BEU64>>(wtxn, NUMBER_DOCUMENTS_IDS_KEY, &BEU64::new(len))?;
Ok(())
}

/// Returns the internal documents ids.
pub fn documents_ids(&self, rtxn: &RoTxn) -> heed::Result<RoaringBitmap> {
Ok(self.main.get::<_, Str, RoaringBitmapCodec>(rtxn, DOCUMENTS_IDS_KEY)?.unwrap_or_default())
let documents_ids = self.main.get::<_, Str, RoaringBitmapCodec>(rtxn, DOCUMENTS_IDS_KEY)?
.unwrap_or_default();
Ok(documents_ids)
}

/// Returns the number of documents indexed in the database.
pub fn number_of_documents(&self, rtxn: &RoTxn) -> anyhow::Result<u64> {
let count = self.main.get::<_, Str, OwnedType<BEU64>>(rtxn, NUMBER_DOCUMENTS_IDS_KEY)?
.map(BEU64::get)
.unwrap_or_default();
Ok(count)
}

/* primary key */
Expand Down Expand Up @@ -251,6 +265,11 @@ impl Index {
}
}

/// Retrieve the number documents ids that faceted under this field id.
pub fn number_faceted_documents_ids(&self, rtxn: &RoTxn, field_id: FieldId) -> heed::Result<u64> {
todo!()
}

/* criteria */

pub fn put_criteria(&self, wtxn: &mut RwTxn, criteria: &[Criterion]) -> heed::Result<()> {
Expand Down Expand Up @@ -301,11 +320,6 @@ impl Index {
Ok(documents)
}

/// Returns the number of documents indexed in the database.
pub fn number_of_documents(&self, rtxn: &RoTxn) -> anyhow::Result<usize> {
Ok(self.documents_ids(rtxn).map(|docids| docids.len() as usize)?)
}

pub fn search<'a>(&'a self, rtxn: &'a RoTxn) -> Search<'a> {
Search::new(rtxn, self)
}
Expand Down
2 changes: 1 addition & 1 deletion src/update/clear_documents.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ impl<'t, 'u, 'i> ClearDocuments<'t, 'u, 'i> {
ClearDocuments { wtxn, index }
}

pub fn execute(self) -> anyhow::Result<usize> {
pub fn execute(self) -> anyhow::Result<u64> {
let Index {
env: _env,
main: _main,
Expand Down
4 changes: 2 additions & 2 deletions src/update/delete_documents.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ impl<'t, 'u, 'i> DeleteDocuments<'t, 'u, 'i> {
Some(docid)
}

pub fn execute(self) -> anyhow::Result<usize> {
pub fn execute(self) -> anyhow::Result<u64> {
// We retrieve the current documents ids that are in the database.
let mut documents_ids = self.index.documents_ids(self.wtxn)?;

Expand Down Expand Up @@ -242,6 +242,6 @@ impl<'t, 'u, 'i> DeleteDocuments<'t, 'u, 'i> {

drop(iter);

Ok(self.documents_ids.len() as usize)
Ok(self.documents_ids.len())
}
}