From 8a17fcdda556802c1de3d6b13cb1e7ad21eba3da Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment=20Renault?= Date: Tue, 29 Oct 2019 11:30:44 +0100 Subject: [PATCH] Introduce a function to get all updates results --- meilidb-core/examples/from_file.rs | 30 +++++++++++++++++++++++++++++- meilidb-core/src/store/mod.rs | 14 ++++++++++++++ meilidb-core/src/update/mod.rs | 6 +++--- 3 files changed, 46 insertions(+), 4 deletions(-) diff --git a/meilidb-core/examples/from_file.rs b/meilidb-core/examples/from_file.rs index c04efb73c..8ba7dd1d5 100644 --- a/meilidb-core/examples/from_file.rs +++ b/meilidb-core/examples/from_file.rs @@ -40,7 +40,7 @@ struct IndexCommand { #[derive(Debug, StructOpt)] struct SearchCommand { - /// The destination where the database must be created. + /// The path of the database to work with. #[structopt(parse(from_os_str))] database_path: PathBuf, @@ -65,10 +65,18 @@ struct SearchCommand { displayed_fields: Vec, } +#[derive(Debug, StructOpt)] +struct ShowUpdatesCommand { + /// The path of the database to work with. + #[structopt(parse(from_os_str))] + database_path: PathBuf, +} + #[derive(Debug, StructOpt)] enum Command { Index(IndexCommand), Search(SearchCommand), + ShowUpdates(ShowUpdatesCommand), } impl Command { @@ -76,6 +84,7 @@ impl Command { match self { Command::Index(command) => &command.database_path, Command::Search(command) => &command.database_path, + Command::ShowUpdates(command) => &command.database_path, } } } @@ -303,6 +312,7 @@ fn search_command(command: SearchCommand, database: Database) -> Result<(), Box< let reader = env.read_txn().unwrap(); let schema = index.main.schema(&reader)?; reader.abort(); + let schema = schema.ok_or(meilidb_core::Error::SchemaMissing)?; let fields = command.displayed_fields.iter().map(String::as_str); @@ -418,6 +428,23 @@ fn search_command(command: SearchCommand, database: Database) -> Result<(), Box< Ok(()) } +fn show_updates_command( + _command: ShowUpdatesCommand, + database: Database, +) -> Result<(), Box> { + let env = &database.env; + let index = database + .open_index(INDEX_NAME) + .expect("Could not find index"); + + let reader = env.read_txn().unwrap(); + let updates = index.all_updates_status(&reader)?; + println!("{:#?}", updates); + reader.abort(); + + Ok(()) +} + fn main() -> Result<(), Box> { env_logger::init(); @@ -427,5 +454,6 @@ fn main() -> Result<(), Box> { match opt { Command::Index(command) => index_command(command, database), Command::Search(command) => search_command(command, database), + Command::ShowUpdates(command) => show_updates_command(command, database), } } diff --git a/meilidb-core/src/store/mod.rs b/meilidb-core/src/store/mod.rs index c384d62ce..4909ce77b 100644 --- a/meilidb-core/src/store/mod.rs +++ b/meilidb-core/src/store/mod.rs @@ -202,6 +202,20 @@ impl Index { update::update_status(reader, self.updates, self.updates_results, update_id) } + pub fn all_updates_status(&self, reader: &heed::RoTxn) -> MResult> { + match self.updates_results.last_update_id(reader)? { + Some((last_id, _)) => { + let mut updates = Vec::with_capacity(last_id as usize + 1); + for id in 0..=last_id { + let update = self.update_status(reader, id)?; + updates.push(update); + } + Ok(updates) + } + None => Ok(Vec::new()), + } + } + pub fn query_builder(&self) -> QueryBuilder { QueryBuilder::new( self.main, diff --git a/meilidb-core/src/update/mod.rs b/meilidb-core/src/update/mod.rs index abb19995f..82290cb4f 100644 --- a/meilidb-core/src/update/mod.rs +++ b/meilidb-core/src/update/mod.rs @@ -47,12 +47,12 @@ pub enum UpdateType { SynonymsDeletion { number: usize }, } -#[derive(Clone, Serialize, Deserialize)] +#[derive(Debug, Clone, Serialize, Deserialize)] pub struct DetailedDuration { pub main: Duration, } -#[derive(Clone, Serialize, Deserialize)] +#[derive(Debug, Clone, Serialize, Deserialize)] pub struct UpdateResult { pub update_id: u64, pub update_type: UpdateType, @@ -60,7 +60,7 @@ pub struct UpdateResult { pub detailed_duration: DetailedDuration, } -#[derive(Clone, Serialize, Deserialize)] +#[derive(Debug, Clone, Serialize, Deserialize)] pub enum UpdateStatus { Enqueued, Processed(UpdateResult),