mirror of
https://github.com/meilisearch/meilisearch.git
synced 2024-11-23 02:27:40 +08:00
feat: Store the word index into the database index
This commit is contained in:
parent
f7eced03fd
commit
58c020a2e1
@ -1,11 +1,17 @@
|
||||
use std::sync::Arc;
|
||||
use std::path::Path;
|
||||
|
||||
use crate::schema::Schema;
|
||||
use meilidb_core::Index as WordIndex;
|
||||
use meilidb_core::shared_data_cursor::{FromSharedDataCursor, SharedDataCursor};
|
||||
use meilidb_core::write_to_bytes::WriteToBytes;
|
||||
use sled::IVec;
|
||||
|
||||
use crate::Schema;
|
||||
|
||||
#[derive(Debug)]
|
||||
pub enum Error {
|
||||
SchemaNotFound,
|
||||
SchemaMissing,
|
||||
WordIndexMissing,
|
||||
SledError(sled::Error),
|
||||
BincodeError(bincode::Error),
|
||||
}
|
||||
@ -26,6 +32,13 @@ fn index_name(name: &str) -> Vec<u8> {
|
||||
format!("index-{}", name).into_bytes()
|
||||
}
|
||||
|
||||
fn ivec_into_arc(ivec: IVec) -> Arc<[u8]> {
|
||||
match ivec {
|
||||
IVec::Inline(len, bytes) => Arc::from(&bytes[..len as usize]),
|
||||
IVec::Remote { buf } => buf,
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct Database(sled::Db);
|
||||
|
||||
@ -62,29 +75,52 @@ impl Database {
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
#[derive(Clone)]
|
||||
pub struct Index {
|
||||
schema: Schema,
|
||||
word_index: Arc<WordIndex>,
|
||||
inner: Arc<sled::Tree>,
|
||||
}
|
||||
|
||||
impl Index {
|
||||
fn from_raw(inner: Arc<sled::Tree>) -> Result<Index, Error> {
|
||||
let bytes = inner.get("schema")?;
|
||||
let bytes = bytes.ok_or(Error::SchemaNotFound)?;
|
||||
|
||||
let bytes = bytes.ok_or(Error::SchemaMissing)?;
|
||||
let schema = Schema::read_from_bin(bytes.as_ref())?;
|
||||
Ok(Index { schema, inner })
|
||||
|
||||
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();
|
||||
|
||||
Arc::new(word_index)
|
||||
};
|
||||
|
||||
Ok(Index { schema, word_index, inner })
|
||||
}
|
||||
|
||||
fn new_from_raw(inner: Arc<sled::Tree>, schema: Schema) -> Result<Index, Error> {
|
||||
let mut schema_bytes = Vec::new();
|
||||
schema.write_to_bin(&mut schema_bytes);
|
||||
schema.write_to_bin(&mut schema_bytes)?;
|
||||
inner.set("schema", schema_bytes)?;
|
||||
Ok(Index { schema, inner })
|
||||
|
||||
let word_index = WordIndex::default();
|
||||
inner.set("word-index", word_index.into_bytes())?;
|
||||
let word_index = Arc::new(word_index);
|
||||
|
||||
Ok(Index { schema, word_index, inner })
|
||||
}
|
||||
|
||||
pub fn schema(&self) -> &Schema {
|
||||
&self.schema
|
||||
}
|
||||
|
||||
pub fn word_index(&self) -> &WordIndex {
|
||||
&self.word_index
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user