feat: Introduce the SortByAttr custom ranking helper

This commit is contained in:
Clément Renault
2019-02-09 13:49:18 +01:00
parent 084c3a95b6
commit 83cd071827
6 changed files with 136 additions and 96 deletions

View File

@ -23,6 +23,8 @@ pub use self::serde::SerializerError;
pub use self::schema::Schema;
pub use self::index::Index;
pub type RankedMap = HashMap<(DocumentId, SchemaAttr), i64>;
const DATA_INDEX: &[u8] = b"data-index";
const DATA_RANKED_MAP: &[u8] = b"data-ranked-map";
const DATA_SCHEMA: &[u8] = b"data-schema";
@ -65,9 +67,8 @@ where D: Deref<Target=DB>
Ok(index)
}
fn retrieve_data_ranked_map<D>(snapshot: &Snapshot<D>)
-> Result<HashMap<(DocumentId, SchemaAttr), i64>, Box<Error>>
where D: Deref<Target=DB>
fn retrieve_data_ranked_map<D>(snapshot: &Snapshot<D>) -> Result<RankedMap, Box<Error>>
where D: Deref<Target=DB>,
{
match snapshot.get(DATA_RANKED_MAP)? {
Some(vector) => Ok(bincode::deserialize(&*vector)?),
@ -94,9 +95,9 @@ fn merge_indexes(existing: Option<&[u8]>, operands: &mut MergeOperands) -> Vec<u
}
fn merge_ranked_maps(existing: Option<&[u8]>, operands: &mut MergeOperands) -> Vec<u8> {
let mut ranked_map: Option<HashMap<_, _>> = None;
let mut ranked_map: Option<RankedMap> = None;
for bytes in existing.into_iter().chain(operands) {
let operand: HashMap<(DocumentId, SchemaAttr), i64> = bincode::deserialize(bytes).unwrap();
let operand: RankedMap = bincode::deserialize(bytes).unwrap();
match ranked_map {
Some(ref mut ranked_map) => ranked_map.extend(operand),
None => { ranked_map.replace(operand); },
@ -174,7 +175,6 @@ impl DatabaseIndex {
let snapshot = Snapshot::new(db.clone());
let view = ArcCell::new(Arc::new(DatabaseView::new(snapshot)?));
Ok(DatabaseIndex {
db: db,
view: view,

View File

@ -16,8 +16,9 @@ use crate::tokenizer::TokenizerBuilder;
use crate::data::{DocIds, DocIndexes};
use crate::database::schema::Schema;
use crate::database::index::Index;
use crate::{DocumentId, DocIndex};
use crate::database::RankedMap;
use crate::database::{DATA_INDEX, DATA_RANKED_MAP};
use crate::{DocumentId, DocIndex};
pub type Token = Vec<u8>; // TODO could be replaced by a SmallVec
@ -78,7 +79,7 @@ use UpdateType::{Updated, Deleted};
pub struct RawUpdateBuilder {
documents_update: HashMap<DocumentId, UpdateType>,
documents_ranked_fields: HashMap<(DocumentId, SchemaAttr), i64>,
documents_ranked_fields: RankedMap,
indexed_words: BTreeMap<Token, Vec<DocIndex>>,
batch: WriteBatch,
}

View File

@ -1,4 +1,3 @@
use hashbrown::HashMap;
use std::error::Error;
use std::path::Path;
use std::ops::Deref;
@ -15,6 +14,7 @@ use crate::rank::{QueryBuilder, FilterFunc};
use crate::database::schema::SchemaAttr;
use crate::database::schema::Schema;
use crate::database::index::Index;
use crate::database::RankedMap;
use crate::DocumentId;
pub struct DatabaseView<D>
@ -22,7 +22,7 @@ where D: Deref<Target=DB>
{
snapshot: Snapshot<D>,
index: Index,
ranked_map: HashMap<(DocumentId, SchemaAttr), i64>,
ranked_map: RankedMap,
schema: Schema,
}
@ -44,7 +44,7 @@ where D: Deref<Target=DB>
&self.index
}
pub fn ranked_map(&self) -> &HashMap<(DocumentId, SchemaAttr), i64> {
pub fn ranked_map(&self) -> &RankedMap {
&self.ranked_map
}