feat: Introduce index events to update the WordIndex

This commit is contained in:
Clément Renault 2019-04-15 16:07:41 +02:00
parent 25a4961453
commit 0104e93ba9
No known key found for this signature in database
GPG Key ID: 0151CDAB43460DAE
3 changed files with 47 additions and 1 deletions

View File

@ -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" }

View File

@ -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<u8>) {
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<Error>;
fn from_shared_data_cursor(cursor: &mut SharedDataCursor) -> Result<Self, Self::Error> {
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()),
}
}
}

View File

@ -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;