fix: Update the DatabaseView to retrieve the index at creation

remove this computation from the QueryBuilder
This commit is contained in:
Clément Renault 2018-12-07 16:20:12 +01:00
parent 7c98771068
commit bec463a61a
No known key found for this signature in database
GPG Key ID: 0151CDAB43460DAE
2 changed files with 13 additions and 9 deletions

View File

@ -5,7 +5,9 @@ use rocksdb::rocksdb::{DB, DBVector, Snapshot, SeekKey};
use rocksdb::rocksdb_options::ReadOptions; use rocksdb::rocksdb_options::ReadOptions;
use serde::de::DeserializeOwned; use serde::de::DeserializeOwned;
use crate::database::{retrieve_data_schema, DocumentKey, DocumentKeyAttr}; use crate::database::{DocumentKey, DocumentKeyAttr};
use crate::database::{retrieve_data_schema, retrieve_data_index};
use crate::database::blob::positive::PositiveBlob;
use crate::database::deserializer::Deserializer; use crate::database::deserializer::Deserializer;
use crate::rank::criterion::Criterion; use crate::rank::criterion::Criterion;
use crate::database::schema::Schema; use crate::database::schema::Schema;
@ -14,19 +16,25 @@ use crate::DocumentId;
pub struct DatabaseView<'a> { pub struct DatabaseView<'a> {
snapshot: Snapshot<&'a DB>, snapshot: Snapshot<&'a DB>,
blob: PositiveBlob,
schema: Schema, schema: Schema,
} }
impl<'a> DatabaseView<'a> { impl<'a> DatabaseView<'a> {
pub fn new(snapshot: Snapshot<&'a DB>) -> Result<DatabaseView, Box<Error>> { pub fn new(snapshot: Snapshot<&'a DB>) -> Result<DatabaseView, Box<Error>> {
let schema = retrieve_data_schema(&snapshot)?; let schema = retrieve_data_schema(&snapshot)?;
Ok(DatabaseView { snapshot, schema }) let blob = retrieve_data_index(&snapshot)?;
Ok(DatabaseView { snapshot, blob, schema })
} }
pub fn schema(&self) -> &Schema { pub fn schema(&self) -> &Schema {
&self.schema &self.schema
} }
pub fn blob(&self) -> &PositiveBlob {
&self.blob
}
pub fn into_snapshot(self) -> Snapshot<&'a DB> { pub fn into_snapshot(self) -> Snapshot<&'a DB> {
self.snapshot self.snapshot
} }

View File

@ -10,8 +10,6 @@ use fst::Streamer;
use crate::automaton::{self, DfaExt, AutomatonExt}; use crate::automaton::{self, DfaExt, AutomatonExt};
use crate::rank::criterion::{self, Criterion}; use crate::rank::criterion::{self, Criterion};
use crate::rank::distinct_map::DistinctMap; use crate::rank::distinct_map::DistinctMap;
use crate::database::retrieve_data_index;
use crate::database::blob::PositiveBlob;
use crate::database::DatabaseView; use crate::database::DatabaseView;
use crate::{Match, DocumentId}; use crate::{Match, DocumentId};
use crate::rank::Document; use crate::rank::Document;
@ -27,7 +25,6 @@ fn split_whitespace_automatons(query: &str) -> Vec<DfaExt> {
pub struct QueryBuilder<'a, C> { pub struct QueryBuilder<'a, C> {
view: &'a DatabaseView<'a>, view: &'a DatabaseView<'a>,
blob: PositiveBlob,
criteria: Vec<C>, criteria: Vec<C>,
} }
@ -39,8 +36,7 @@ impl<'a> QueryBuilder<'a, Box<dyn Criterion>> {
impl<'a, C> QueryBuilder<'a, C> { impl<'a, C> QueryBuilder<'a, C> {
pub fn with_criteria(view: &'a DatabaseView<'a>, criteria: Vec<C>) -> Result<Self, Box<Error>> { pub fn with_criteria(view: &'a DatabaseView<'a>, criteria: Vec<C>) -> Result<Self, Box<Error>> {
let blob = retrieve_data_index(view.snapshot())?; Ok(QueryBuilder { view, criteria })
Ok(QueryBuilder { view, blob, criteria })
} }
pub fn criteria(&mut self, criteria: Vec<C>) -> &mut Self { pub fn criteria(&mut self, criteria: Vec<C>) -> &mut Self {
@ -62,7 +58,7 @@ impl<'a, C> QueryBuilder<'a, C> {
let mut stream = { let mut stream = {
let mut op_builder = fst::map::OpBuilder::new(); let mut op_builder = fst::map::OpBuilder::new();
for automaton in &automatons { for automaton in &automatons {
let stream = self.blob.as_map().search(automaton); let stream = self.view.blob().as_map().search(automaton);
op_builder.push(stream); op_builder.push(stream);
} }
op_builder.union() op_builder.union()
@ -76,7 +72,7 @@ impl<'a, C> QueryBuilder<'a, C> {
let distance = automaton.eval(input).to_u8(); let distance = automaton.eval(input).to_u8();
let is_exact = distance == 0 && input.len() == automaton.query_len(); let is_exact = distance == 0 && input.len() == automaton.query_len();
let doc_indexes = self.blob.as_indexes(); let doc_indexes = self.view.blob().as_indexes();
let doc_indexes = &doc_indexes[iv.value as usize]; let doc_indexes = &doc_indexes[iv.value as usize];
for doc_index in doc_indexes { for doc_index in doc_indexes {