From 0104e93ba9539e3f93493643f55b56a4da63c096 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment=20Renault?= Date: Mon, 15 Apr 2019 16:07:41 +0200 Subject: [PATCH] feat: Introduce index events to update the WordIndex --- meilidb-data/Cargo.toml | 1 + meilidb-data/src/index_event.rs | 45 +++++++++++++++++++++++++++++++++ meilidb-data/src/lib.rs | 2 +- 3 files changed, 47 insertions(+), 1 deletion(-) create mode 100644 meilidb-data/src/index_event.rs diff --git a/meilidb-data/Cargo.toml b/meilidb-data/Cargo.toml index 7c13e9f72..18b8ad1e6 100644 --- a/meilidb-data/Cargo.toml +++ b/meilidb-data/Cargo.toml @@ -6,6 +6,7 @@ edition = "2018" [dependencies] bincode = "1.1.2" +byteorder = "1.3.1" hashbrown = { version = "0.1.8", features = ["serde"] } linked-hash-map = { version = "0.5.2", features = ["serde_impl"] } meilidb-core = { path = "../meilidb-core", version = "0.1.0" } diff --git a/meilidb-data/src/index_event.rs b/meilidb-data/src/index_event.rs new file mode 100644 index 000000000..40d54cbf3 --- /dev/null +++ b/meilidb-data/src/index_event.rs @@ -0,0 +1,45 @@ +use std::error::Error; + +use byteorder::{ReadBytesExt, WriteBytesExt}; + +use meilidb_core::{Index as WordIndex}; +use meilidb_core::data::DocIds; +use meilidb_core::write_to_bytes::WriteToBytes; +use meilidb_core::shared_data_cursor::{SharedDataCursor, FromSharedDataCursor}; + +enum NewIndexEvent<'a> { + RemovedDocuments(&'a DocIds), + UpdatedDocuments(&'a WordIndex), +} + +impl<'a> WriteToBytes for NewIndexEvent<'a> { + fn write_to_bytes(&self, bytes: &mut Vec) { + match self { + NewIndexEvent::RemovedDocuments(doc_ids) => { + let _ = bytes.write_u8(0); + doc_ids.write_to_bytes(bytes); + }, + NewIndexEvent::UpdatedDocuments(index) => { + let _ = bytes.write_u8(1); + index.write_to_bytes(bytes); + } + } + } +} + +enum IndexEvent { + RemovedDocuments(DocIds), + UpdatedDocuments(WordIndex), +} + +impl FromSharedDataCursor for IndexEvent { + type Error = Box; + + fn from_shared_data_cursor(cursor: &mut SharedDataCursor) -> Result { + match cursor.read_u8()? { + 0 => DocIds::from_shared_data_cursor(cursor).map(IndexEvent::RemovedDocuments), + 1 => WordIndex::from_shared_data_cursor(cursor).map(IndexEvent::UpdatedDocuments), + _ => Err("invalid index event type".into()), + } + } +} diff --git a/meilidb-data/src/lib.rs b/meilidb-data/src/lib.rs index c601105ed..a19418a3f 100644 --- a/meilidb-data/src/lib.rs +++ b/meilidb-data/src/lib.rs @@ -1,4 +1,5 @@ mod database; +mod index_event; mod indexer; mod number; mod ranked_map; @@ -8,4 +9,3 @@ pub use self::database::{Database, Index}; pub use self::number::Number; pub use self::ranked_map::RankedMap; pub use self::schema::{Schema, SchemaAttr}; -pub use self::indexer::Indexer;