From ddbd336387681dfa4750ceff4979049147089494 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment=20Renault?= Date: Sat, 31 Oct 2020 11:28:48 +0100 Subject: [PATCH] Introduce primary key methods on the index --- src/index.rs | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/src/index.rs b/src/index.rs index 3de742d22..ab7da0ed5 100644 --- a/src/index.rs +++ b/src/index.rs @@ -14,10 +14,11 @@ use crate::{ BoRoaringBitmapCodec, CboRoaringBitmapCodec, }; -pub const WORDS_FST_KEY: &str = "words-fst"; -pub const FIELDS_IDS_MAP_KEY: &str = "fields-ids-map"; pub const DOCUMENTS_IDS_KEY: &str = "documents-ids"; +pub const FIELDS_IDS_MAP_KEY: &str = "fields-ids-map"; +pub const PRIMARY_KEY_KEY: &str = "primary-key"; pub const USERS_IDS_DOCUMENTS_IDS_KEY: &str = "users-ids-documents-ids"; +pub const WORDS_FST_KEY: &str = "words-fst"; #[derive(Clone)] pub struct Index { @@ -83,6 +84,21 @@ impl Index { Ok(self.main.get::<_, Str, RoaringBitmapCodec>(rtxn, DOCUMENTS_IDS_KEY)?.unwrap_or_default()) } + /// Writes the documents primary key, this is the field name that is used to store the id. + pub fn put_primary_key(&self, wtxn: &mut heed::RwTxn, primary_key: u8) -> heed::Result<()> { + self.main.put::<_, Str, OwnedType>(wtxn, PRIMARY_KEY_KEY, &primary_key) + } + + /// Delete the primary key of the documents, this can be done to reset indexes settings. + pub fn delete_primary_key(&self, wtxn: &mut heed::RwTxn) -> heed::Result { + self.main.delete::<_, Str>(wtxn, PRIMARY_KEY_KEY) + } + + /// Returns the documents primary key, `None` if it hasn't been defined. + pub fn primary_key(&self, rtxn: &heed::RoTxn) -> heed::Result> { + self.main.get::<_, Str, OwnedType>(rtxn, PRIMARY_KEY_KEY) + } + /// Writes the users ids documents ids, a user id is a byte slice (i.e. `[u8]`) /// and refers to an internal id (i.e. `u32`). pub fn put_users_ids_documents_ids>(&self, wtxn: &mut heed::RwTxn, fst: &fst::Map) -> heed::Result<()> {