2019-04-08 15:19:57 +02:00
|
|
|
use std::path::Path;
|
2019-04-16 10:47:52 +02:00
|
|
|
use std::sync::Arc;
|
2019-04-08 15:19:57 +02:00
|
|
|
|
2019-04-16 10:47:52 +02:00
|
|
|
use arc_swap::{ArcSwap, Lease};
|
|
|
|
use hashbrown::HashMap;
|
2019-04-11 14:51:17 +02:00
|
|
|
use meilidb_core::shared_data_cursor::{FromSharedDataCursor, SharedDataCursor};
|
|
|
|
use meilidb_core::write_to_bytes::WriteToBytes;
|
2019-04-16 10:47:52 +02:00
|
|
|
use meilidb_core::{DocumentId, Index as WordIndex};
|
2019-04-11 14:51:17 +02:00
|
|
|
use sled::IVec;
|
|
|
|
|
2019-04-16 12:06:40 +02:00
|
|
|
use crate::{Schema, SchemaAttr, RankedMap};
|
2019-04-08 15:19:57 +02:00
|
|
|
|
|
|
|
#[derive(Debug)]
|
|
|
|
pub enum Error {
|
2019-04-16 10:47:52 +02:00
|
|
|
SchemaDiffer,
|
2019-04-11 14:51:17 +02:00
|
|
|
SchemaMissing,
|
|
|
|
WordIndexMissing,
|
2019-04-08 15:19:57 +02:00
|
|
|
SledError(sled::Error),
|
|
|
|
BincodeError(bincode::Error),
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<sled::Error> for Error {
|
|
|
|
fn from(error: sled::Error) -> Error {
|
|
|
|
Error::SledError(error)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<bincode::Error> for Error {
|
|
|
|
fn from(error: bincode::Error) -> Error {
|
|
|
|
Error::BincodeError(error)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn index_name(name: &str) -> Vec<u8> {
|
|
|
|
format!("index-{}", name).into_bytes()
|
|
|
|
}
|
2019-03-29 17:01:10 +01:00
|
|
|
|
2019-04-12 18:46:36 +02:00
|
|
|
fn document_key(id: DocumentId, attr: SchemaAttr) -> Vec<u8> {
|
|
|
|
let DocumentId(document_id) = id;
|
|
|
|
let SchemaAttr(schema_attr) = attr;
|
|
|
|
|
|
|
|
let mut bytes = Vec::new();
|
|
|
|
bytes.extend_from_slice(b"document-");
|
|
|
|
bytes.extend_from_slice(&document_id.to_be_bytes()[..]);
|
|
|
|
bytes.extend_from_slice(&schema_attr.to_be_bytes()[..]);
|
|
|
|
bytes
|
|
|
|
}
|
|
|
|
|
2019-04-11 14:51:17 +02:00
|
|
|
fn ivec_into_arc(ivec: IVec) -> Arc<[u8]> {
|
|
|
|
match ivec {
|
|
|
|
IVec::Inline(len, bytes) => Arc::from(&bytes[..len as usize]),
|
|
|
|
IVec::Remote { buf } => buf,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-29 17:01:10 +01:00
|
|
|
#[derive(Clone)]
|
2019-04-16 10:47:52 +02:00
|
|
|
pub struct Database {
|
|
|
|
opened: Arc<ArcSwap<HashMap<String, Index>>>,
|
|
|
|
inner: sled::Db,
|
|
|
|
}
|
2019-03-29 17:01:10 +01:00
|
|
|
|
|
|
|
impl Database {
|
2019-04-08 15:19:57 +02:00
|
|
|
pub fn start_default<P: AsRef<Path>>(path: P) -> Result<Database, Error> {
|
2019-04-16 10:47:52 +02:00
|
|
|
let inner = sled::Db::start_default(path)?;
|
|
|
|
let opened = Arc::new(ArcSwap::new(Arc::new(HashMap::new())));
|
|
|
|
Ok(Database { opened, inner })
|
2019-03-29 17:01:10 +01:00
|
|
|
}
|
|
|
|
|
2019-04-08 15:19:57 +02:00
|
|
|
pub fn open_index(&self, name: &str) -> Result<Option<Index>, Error> {
|
2019-04-16 10:47:52 +02:00
|
|
|
// check if the index was already opened
|
|
|
|
if let Some(index) = self.opened.lease().get(name) {
|
|
|
|
return Ok(Some(index.clone()))
|
|
|
|
}
|
2019-03-29 17:01:10 +01:00
|
|
|
|
2019-04-16 10:47:52 +02:00
|
|
|
let raw_name = index_name(name);
|
|
|
|
if self.inner.tree_names().into_iter().any(|tn| tn == raw_name) {
|
|
|
|
let tree = self.inner.open_tree(raw_name)?;
|
2019-04-08 15:19:57 +02:00
|
|
|
let index = Index::from_raw(tree)?;
|
2019-04-16 10:47:52 +02:00
|
|
|
|
|
|
|
self.opened.rcu(|opened| {
|
|
|
|
let mut opened = HashMap::clone(opened);
|
|
|
|
opened.insert(name.to_string(), index.clone());
|
|
|
|
opened
|
|
|
|
});
|
|
|
|
|
2019-04-08 15:19:57 +02:00
|
|
|
return Ok(Some(index))
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(None)
|
|
|
|
}
|
|
|
|
|
2019-04-16 10:47:52 +02:00
|
|
|
pub fn create_index(&self, name: String, schema: Schema) -> Result<Index, Error> {
|
|
|
|
match self.open_index(&name)? {
|
2019-04-08 15:19:57 +02:00
|
|
|
Some(index) => {
|
2019-04-16 10:47:52 +02:00
|
|
|
if index.schema != schema {
|
|
|
|
return Err(Error::SchemaDiffer);
|
|
|
|
}
|
|
|
|
|
2019-04-08 15:19:57 +02:00
|
|
|
Ok(index)
|
|
|
|
},
|
|
|
|
None => {
|
2019-04-16 10:47:52 +02:00
|
|
|
let raw_name = index_name(&name);
|
|
|
|
let tree = self.inner.open_tree(raw_name)?;
|
2019-04-08 15:19:57 +02:00
|
|
|
let index = Index::new_from_raw(tree, schema)?;
|
2019-04-16 10:47:52 +02:00
|
|
|
|
|
|
|
self.opened.rcu(|opened| {
|
|
|
|
let mut opened = HashMap::clone(opened);
|
|
|
|
opened.insert(name.clone(), index.clone());
|
|
|
|
opened
|
|
|
|
});
|
|
|
|
|
2019-04-08 15:19:57 +02:00
|
|
|
Ok(index)
|
|
|
|
},
|
|
|
|
}
|
2019-03-29 17:01:10 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-11 14:51:17 +02:00
|
|
|
#[derive(Clone)]
|
2019-04-08 15:19:57 +02:00
|
|
|
pub struct Index {
|
|
|
|
schema: Schema,
|
2019-04-16 10:47:52 +02:00
|
|
|
word_index: Arc<ArcSwap<WordIndex>>,
|
2019-04-16 12:06:40 +02:00
|
|
|
ranked_map: Arc<ArcSwap<RankedMap>>,
|
2019-04-08 15:19:57 +02:00
|
|
|
inner: Arc<sled::Tree>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Index {
|
|
|
|
fn from_raw(inner: Arc<sled::Tree>) -> Result<Index, Error> {
|
2019-04-16 12:06:40 +02:00
|
|
|
let schema = {
|
|
|
|
let bytes = inner.get("schema")?;
|
|
|
|
let bytes = bytes.ok_or(Error::SchemaMissing)?;
|
|
|
|
Schema::read_from_bin(bytes.as_ref())?
|
|
|
|
};
|
2019-04-11 14:51:17 +02:00
|
|
|
|
|
|
|
let bytes = inner.get("word-index")?;
|
|
|
|
let bytes = bytes.ok_or(Error::WordIndexMissing)?;
|
|
|
|
let word_index = {
|
|
|
|
let len = bytes.len();
|
|
|
|
let bytes = ivec_into_arc(bytes);
|
|
|
|
let mut cursor = SharedDataCursor::from_shared_bytes(bytes, 0, len);
|
|
|
|
|
|
|
|
// TODO must handle this error
|
|
|
|
let word_index = WordIndex::from_shared_data_cursor(&mut cursor).unwrap();
|
|
|
|
|
2019-04-16 10:47:52 +02:00
|
|
|
Arc::new(ArcSwap::new(Arc::new(word_index)))
|
2019-04-11 14:51:17 +02:00
|
|
|
};
|
|
|
|
|
2019-04-16 12:06:40 +02:00
|
|
|
let ranked_map = {
|
|
|
|
let map = match inner.get("ranked-map")? {
|
|
|
|
Some(bytes) => bincode::deserialize(bytes.as_ref())?,
|
|
|
|
None => RankedMap::default(),
|
|
|
|
};
|
|
|
|
|
|
|
|
Arc::new(ArcSwap::new(Arc::new(map)))
|
|
|
|
};
|
|
|
|
|
|
|
|
Ok(Index { schema, word_index, ranked_map, inner })
|
2019-04-08 15:19:57 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
fn new_from_raw(inner: Arc<sled::Tree>, schema: Schema) -> Result<Index, Error> {
|
|
|
|
let mut schema_bytes = Vec::new();
|
2019-04-11 14:51:17 +02:00
|
|
|
schema.write_to_bin(&mut schema_bytes)?;
|
2019-04-08 15:19:57 +02:00
|
|
|
inner.set("schema", schema_bytes)?;
|
2019-04-11 14:51:17 +02:00
|
|
|
|
|
|
|
let word_index = WordIndex::default();
|
|
|
|
inner.set("word-index", word_index.into_bytes())?;
|
2019-04-16 10:47:52 +02:00
|
|
|
let word_index = Arc::new(ArcSwap::new(Arc::new(word_index)));
|
2019-04-11 14:51:17 +02:00
|
|
|
|
2019-04-16 12:06:40 +02:00
|
|
|
let ranked_map = Arc::new(ArcSwap::new(Arc::new(RankedMap::default())));
|
|
|
|
|
|
|
|
Ok(Index { schema, word_index, ranked_map, inner })
|
2019-04-08 15:19:57 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn schema(&self) -> &Schema {
|
|
|
|
&self.schema
|
|
|
|
}
|
2019-04-11 14:51:17 +02:00
|
|
|
|
2019-04-16 10:47:52 +02:00
|
|
|
pub fn word_index(&self) -> Lease<Arc<WordIndex>> {
|
|
|
|
self.word_index.lease()
|
|
|
|
}
|
|
|
|
|
2019-04-16 12:06:40 +02:00
|
|
|
pub fn ranked_map(&self) -> Lease<Arc<RankedMap>> {
|
|
|
|
self.ranked_map.lease()
|
|
|
|
}
|
|
|
|
|
2019-04-16 10:47:52 +02:00
|
|
|
fn update_word_index(&self, word_index: Arc<WordIndex>) {
|
|
|
|
self.word_index.store(word_index)
|
2019-04-11 14:51:17 +02:00
|
|
|
}
|
2019-04-12 18:46:36 +02:00
|
|
|
|
2019-04-16 12:06:40 +02:00
|
|
|
fn update_ranked_map(&self, ranked_map: Arc<RankedMap>) {
|
|
|
|
self.ranked_map.store(ranked_map)
|
|
|
|
}
|
|
|
|
|
2019-04-12 18:46:36 +02:00
|
|
|
pub fn set_document_attribute<V>(
|
|
|
|
&self,
|
|
|
|
id: DocumentId,
|
|
|
|
attr: SchemaAttr,
|
|
|
|
value: V,
|
|
|
|
) -> Result<Option<IVec>, Error>
|
|
|
|
where IVec: From<V>,
|
|
|
|
{
|
|
|
|
let key = document_key(id, attr);
|
|
|
|
Ok(self.inner.set(key, value)?)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn get_document_attribute(
|
|
|
|
&self,
|
|
|
|
id: DocumentId,
|
|
|
|
attr: SchemaAttr
|
|
|
|
) -> Result<Option<IVec>, Error>
|
|
|
|
{
|
|
|
|
let key = document_key(id, attr);
|
|
|
|
Ok(self.inner.get(key)?)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn del_document_attribute(
|
|
|
|
&self,
|
|
|
|
id: DocumentId,
|
|
|
|
attr: SchemaAttr
|
|
|
|
) -> Result<Option<IVec>, Error>
|
|
|
|
{
|
|
|
|
let key = document_key(id, attr);
|
|
|
|
Ok(self.inner.del(key)?)
|
|
|
|
}
|
2019-04-08 15:19:57 +02:00
|
|
|
}
|