diff --git a/meilidb-data/src/database.rs b/meilidb-data/src/database.rs index 006612a1e..baacddc35 100644 --- a/meilidb-data/src/database.rs +++ b/meilidb-data/src/database.rs @@ -8,7 +8,7 @@ use meilidb_core::write_to_bytes::WriteToBytes; use meilidb_core::{DocumentId, Index as WordIndex}; use sled::IVec; -use crate::{Schema, SchemaAttr}; +use crate::{Schema, SchemaAttr, RankedMap}; #[derive(Debug)] pub enum Error { @@ -119,14 +119,17 @@ impl Database { pub struct Index { schema: Schema, word_index: Arc>, + ranked_map: Arc>, inner: Arc, } impl Index { fn from_raw(inner: Arc) -> Result { - let bytes = inner.get("schema")?; - let bytes = bytes.ok_or(Error::SchemaMissing)?; - let schema = Schema::read_from_bin(bytes.as_ref())?; + let schema = { + let bytes = inner.get("schema")?; + let bytes = bytes.ok_or(Error::SchemaMissing)?; + Schema::read_from_bin(bytes.as_ref())? + }; let bytes = inner.get("word-index")?; let bytes = bytes.ok_or(Error::WordIndexMissing)?; @@ -141,7 +144,16 @@ impl Index { Arc::new(ArcSwap::new(Arc::new(word_index))) }; - Ok(Index { schema, word_index, inner }) + let ranked_map = { + let map = match inner.get("ranked-map")? { + Some(bytes) => bincode::deserialize(bytes.as_ref())?, + None => RankedMap::default(), + }; + + Arc::new(ArcSwap::new(Arc::new(map))) + }; + + Ok(Index { schema, word_index, ranked_map, inner }) } fn new_from_raw(inner: Arc, schema: Schema) -> Result { @@ -153,7 +165,9 @@ impl Index { inner.set("word-index", word_index.into_bytes())?; let word_index = Arc::new(ArcSwap::new(Arc::new(word_index))); - Ok(Index { schema, word_index, inner }) + let ranked_map = Arc::new(ArcSwap::new(Arc::new(RankedMap::default()))); + + Ok(Index { schema, word_index, ranked_map, inner }) } pub fn schema(&self) -> &Schema { @@ -164,10 +178,18 @@ impl Index { self.word_index.lease() } + pub fn ranked_map(&self) -> Lease> { + self.ranked_map.lease() + } + fn update_word_index(&self, word_index: Arc) { self.word_index.store(word_index) } + fn update_ranked_map(&self, ranked_map: Arc) { + self.ranked_map.store(ranked_map) + } + pub fn set_document_attribute( &self, id: DocumentId,