mirror of
https://github.com/meilisearch/MeiliSearch
synced 2025-07-04 20:37:15 +02:00
feat: Make the DatabaseView become Sync + Send
This commit is contained in:
parent
bec463a61a
commit
0e825e05bb
11 changed files with 147 additions and 72 deletions
|
@ -1,4 +1,5 @@
|
|||
use std::error::Error;
|
||||
use std::ops::Deref;
|
||||
use std::{fmt, marker};
|
||||
|
||||
use rocksdb::rocksdb::{DB, DBVector, Snapshot, SeekKey};
|
||||
|
@ -14,14 +15,18 @@ use crate::database::schema::Schema;
|
|||
use crate::rank::QueryBuilder;
|
||||
use crate::DocumentId;
|
||||
|
||||
pub struct DatabaseView<'a> {
|
||||
snapshot: Snapshot<&'a DB>,
|
||||
pub struct DatabaseView<D>
|
||||
where D: Deref<Target=DB>
|
||||
{
|
||||
snapshot: Snapshot<D>,
|
||||
blob: PositiveBlob,
|
||||
schema: Schema,
|
||||
}
|
||||
|
||||
impl<'a> DatabaseView<'a> {
|
||||
pub fn new(snapshot: Snapshot<&'a DB>) -> Result<DatabaseView, Box<Error>> {
|
||||
impl<D> DatabaseView<D>
|
||||
where D: Deref<Target=DB>
|
||||
{
|
||||
pub fn new(snapshot: Snapshot<D>) -> Result<DatabaseView<D>, Box<Error>> {
|
||||
let schema = retrieve_data_schema(&snapshot)?;
|
||||
let blob = retrieve_data_index(&snapshot)?;
|
||||
Ok(DatabaseView { snapshot, blob, schema })
|
||||
|
@ -35,11 +40,11 @@ impl<'a> DatabaseView<'a> {
|
|||
&self.blob
|
||||
}
|
||||
|
||||
pub fn into_snapshot(self) -> Snapshot<&'a DB> {
|
||||
pub fn into_snapshot(self) -> Snapshot<D> {
|
||||
self.snapshot
|
||||
}
|
||||
|
||||
pub fn snapshot(&self) -> &Snapshot<&'a DB> {
|
||||
pub fn snapshot(&self) -> &Snapshot<D> {
|
||||
&self.snapshot
|
||||
}
|
||||
|
||||
|
@ -47,20 +52,20 @@ impl<'a> DatabaseView<'a> {
|
|||
Ok(self.snapshot.get(key)?)
|
||||
}
|
||||
|
||||
pub fn query_builder(&self) -> Result<QueryBuilder<Box<dyn Criterion>>, Box<Error>> {
|
||||
pub fn query_builder(&self) -> Result<QueryBuilder<D, Box<dyn Criterion<D>>>, Box<Error>> {
|
||||
QueryBuilder::new(self)
|
||||
}
|
||||
|
||||
// TODO create an enum error type
|
||||
pub fn retrieve_document<D>(&self, id: DocumentId) -> Result<D, Box<Error>>
|
||||
where D: DeserializeOwned
|
||||
pub fn retrieve_document<T>(&self, id: DocumentId) -> Result<T, Box<Error>>
|
||||
where T: DeserializeOwned
|
||||
{
|
||||
let mut deserializer = Deserializer::new(&self.snapshot, &self.schema, id);
|
||||
Ok(D::deserialize(&mut deserializer)?)
|
||||
Ok(T::deserialize(&mut deserializer)?)
|
||||
}
|
||||
|
||||
pub fn retrieve_documents<D, I>(&self, ids: I) -> DocumentIter<D, I::IntoIter>
|
||||
where D: DeserializeOwned,
|
||||
pub fn retrieve_documents<T, I>(&self, ids: I) -> DocumentIter<D, T, I::IntoIter>
|
||||
where T: DeserializeOwned,
|
||||
I: IntoIterator<Item=DocumentId>,
|
||||
{
|
||||
DocumentIter {
|
||||
|
@ -71,7 +76,9 @@ impl<'a> DatabaseView<'a> {
|
|||
}
|
||||
}
|
||||
|
||||
impl<'a> fmt::Debug for DatabaseView<'a> {
|
||||
impl<D> fmt::Debug for DatabaseView<D>
|
||||
where D: Deref<Target=DB>
|
||||
{
|
||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||
let mut options = ReadOptions::new();
|
||||
let lower = DocumentKey::new(0);
|
||||
|
@ -102,17 +109,20 @@ impl<'a> fmt::Debug for DatabaseView<'a> {
|
|||
}
|
||||
|
||||
// TODO this is just an iter::Map !!!
|
||||
pub struct DocumentIter<'a, D, I> {
|
||||
database_view: &'a DatabaseView<'a>,
|
||||
pub struct DocumentIter<'a, D, T, I>
|
||||
where D: Deref<Target=DB>
|
||||
{
|
||||
database_view: &'a DatabaseView<D>,
|
||||
document_ids: I,
|
||||
_phantom: marker::PhantomData<D>,
|
||||
_phantom: marker::PhantomData<T>,
|
||||
}
|
||||
|
||||
impl<'a, D, I> Iterator for DocumentIter<'a, D, I>
|
||||
where D: DeserializeOwned,
|
||||
impl<'a, D, T, I> Iterator for DocumentIter<'a, D, T, I>
|
||||
where D: Deref<Target=DB>,
|
||||
T: DeserializeOwned,
|
||||
I: Iterator<Item=DocumentId>,
|
||||
{
|
||||
type Item = Result<D, Box<Error>>;
|
||||
type Item = Result<T, Box<Error>>;
|
||||
|
||||
fn size_hint(&self) -> (usize, Option<usize>) {
|
||||
self.document_ids.size_hint()
|
||||
|
@ -126,13 +136,15 @@ where D: DeserializeOwned,
|
|||
}
|
||||
}
|
||||
|
||||
impl<'a, D, I> ExactSizeIterator for DocumentIter<'a, D, I>
|
||||
where D: DeserializeOwned,
|
||||
impl<'a, D, T, I> ExactSizeIterator for DocumentIter<'a, D, T, I>
|
||||
where D: Deref<Target=DB>,
|
||||
T: DeserializeOwned,
|
||||
I: ExactSizeIterator + Iterator<Item=DocumentId>,
|
||||
{ }
|
||||
|
||||
impl<'a, D, I> DoubleEndedIterator for DocumentIter<'a, D, I>
|
||||
where D: DeserializeOwned,
|
||||
impl<'a, D, T, I> DoubleEndedIterator for DocumentIter<'a, D, T, I>
|
||||
where D: Deref<Target=DB>,
|
||||
T: DeserializeOwned,
|
||||
I: DoubleEndedIterator + Iterator<Item=DocumentId>,
|
||||
{
|
||||
fn next_back(&mut self) -> Option<Self::Item> {
|
||||
|
|
|
@ -1,4 +1,5 @@
|
|||
use std::error::Error;
|
||||
use std::ops::Deref;
|
||||
use std::fmt;
|
||||
|
||||
use rocksdb::rocksdb::{DB, Snapshot, SeekKey};
|
||||
|
@ -11,19 +12,25 @@ use crate::database::document_key::{DocumentKey, DocumentKeyAttr};
|
|||
use crate::database::schema::Schema;
|
||||
use crate::DocumentId;
|
||||
|
||||
pub struct Deserializer<'a> {
|
||||
snapshot: &'a Snapshot<&'a DB>,
|
||||
pub struct Deserializer<'a, D>
|
||||
where D: Deref<Target=DB>
|
||||
{
|
||||
snapshot: &'a Snapshot<D>,
|
||||
schema: &'a Schema,
|
||||
document_id: DocumentId,
|
||||
}
|
||||
|
||||
impl<'a> Deserializer<'a> {
|
||||
pub fn new(snapshot: &'a Snapshot<&DB>, schema: &'a Schema, doc: DocumentId) -> Self {
|
||||
impl<'a, D> Deserializer<'a, D>
|
||||
where D: Deref<Target=DB>
|
||||
{
|
||||
pub fn new(snapshot: &'a Snapshot<D>, schema: &'a Schema, doc: DocumentId) -> Self {
|
||||
Deserializer { snapshot, schema, document_id: doc }
|
||||
}
|
||||
}
|
||||
|
||||
impl<'de, 'a, 'b> de::Deserializer<'de> for &'b mut Deserializer<'a> {
|
||||
impl<'de, 'a, 'b, D> de::Deserializer<'de> for &'b mut Deserializer<'a, D>
|
||||
where D: Deref<Target=DB>
|
||||
{
|
||||
type Error = DeserializerError;
|
||||
|
||||
fn deserialize_any<V>(self, visitor: V) -> Result<V::Value, Self::Error>
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
use std::error::Error;
|
||||
use std::path::Path;
|
||||
use std::ops::Deref;
|
||||
use std::sync::Arc;
|
||||
use std::fmt;
|
||||
|
||||
use rocksdb::rocksdb_options::{DBOptions, IngestExternalFileOptions, ColumnFamilyOptions};
|
||||
|
@ -42,7 +43,8 @@ where D: Deref<Target=DB>
|
|||
}
|
||||
}
|
||||
|
||||
pub struct Database(DB);
|
||||
#[derive(Clone)]
|
||||
pub struct Database(Arc<DB>);
|
||||
|
||||
impl Database {
|
||||
pub fn create<P: AsRef<Path>>(path: P, schema: Schema) -> Result<Database, Box<Error>> {
|
||||
|
@ -66,7 +68,7 @@ impl Database {
|
|||
schema.write_to(&mut schema_bytes)?;
|
||||
db.put(DATA_SCHEMA, &schema_bytes)?;
|
||||
|
||||
Ok(Database(db))
|
||||
Ok(Database(Arc::new(db)))
|
||||
}
|
||||
|
||||
pub fn open<P: AsRef<Path>>(path: P) -> Result<Database, Box<Error>> {
|
||||
|
@ -86,7 +88,7 @@ impl Database {
|
|||
None => return Err(String::from("Database does not contain a schema").into()),
|
||||
};
|
||||
|
||||
Ok(Database(db))
|
||||
Ok(Database(Arc::new(db)))
|
||||
}
|
||||
|
||||
pub fn ingest_update_file(&self, update: Update) -> Result<(), Box<Error>> {
|
||||
|
@ -114,10 +116,15 @@ impl Database {
|
|||
Ok(self.0.flush(true)?)
|
||||
}
|
||||
|
||||
pub fn view(&self) -> Result<DatabaseView, Box<Error>> {
|
||||
pub fn view(&self) -> Result<DatabaseView<&DB>, Box<Error>> {
|
||||
let snapshot = self.0.snapshot();
|
||||
DatabaseView::new(snapshot)
|
||||
}
|
||||
|
||||
pub fn view_arc(&self) -> Result<DatabaseView<Arc<DB>>, Box<Error>> {
|
||||
let snapshot = Snapshot::new(self.0.clone());
|
||||
DatabaseView::new(snapshot)
|
||||
}
|
||||
}
|
||||
|
||||
impl fmt::Debug for Database {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue