implement retrieve one document

This commit is contained in:
mpostma 2021-03-04 15:09:00 +01:00
parent f3d65ec5e9
commit 581dcd5735
No known key found for this signature in database
GPG key ID: CBC8A7C1D7A28C3A
5 changed files with 109 additions and 58 deletions

View file

@ -8,7 +8,6 @@ use chrono::Utc;
use futures::stream::StreamExt;
use heed::EnvOpenOptions;
use log::info;
use serde_json::{Map, Value};
use thiserror::Error;
use tokio::sync::{mpsc, oneshot, RwLock};
use uuid::Uuid;
@ -51,8 +50,14 @@ enum IndexMsg {
attributes_to_retrieve: Option<Vec<String>>,
offset: usize,
limit: usize,
ret: oneshot::Sender<Result<Vec<Map<String, Value>>>>,
ret: oneshot::Sender<Result<Vec<Document>>>,
},
Document {
uuid: Uuid,
attributes_to_retrieve: Option<Vec<String>>,
doc_id: String,
ret: oneshot::Sender<Result<Document>>,
}
}
struct IndexActor<S> {
@ -126,6 +131,7 @@ impl<S: IndexStore + Sync + Send> IndexActor<S> {
self.handle_fetch_documents(uuid, offset, limit, attributes_to_retrieve, ret)
.await
}
Document { uuid, attributes_to_retrieve, doc_id, ret } => self.handle_fetch_document(uuid, doc_id, attributes_to_retrieve, ret).await,
}
});
@ -196,6 +202,21 @@ impl<S: IndexStore + Sync + Send> IndexActor<S> {
let _ = ret.send(result);
}).await;
}
async fn handle_fetch_document(
&self,
uuid: Uuid,
doc_id: String,
attributes_to_retrieve: Option<Vec<String>>,
ret: oneshot::Sender<Result<Document>>,
) {
let index = self.store.get(uuid).await.unwrap().unwrap();
tokio::task::spawn_blocking(move || {
let result = index.retrieve_document(doc_id, attributes_to_retrieve)
.map_err(|e| IndexError::Error(e));
let _ = ret.send(result);
}).await;
}
}
#[derive(Clone)]
@ -267,6 +288,23 @@ impl IndexActorHandle {
let _ = self.sender.send(msg).await;
Ok(receiver.await.expect("IndexActor has been killed")?)
}
pub async fn document(
&self,
uuid: Uuid,
doc_id: String,
attributes_to_retrieve: Option<Vec<String>>,
) -> Result<Document> {
let (ret, receiver) = oneshot::channel();
let msg = IndexMsg::Document {
uuid,
ret,
doc_id,
attributes_to_retrieve,
};
let _ = self.sender.send(msg).await;
Ok(receiver.await.expect("IndexActor has been killed")?)
}
}
struct MapIndexStore {

View file

@ -172,6 +172,20 @@ impl IndexController {
Ok(documents)
}
pub async fn document(
&self,
index: String,
doc_id: String,
attributes_to_retrieve: Option<Vec<String>>,
) -> anyhow::Result<Document> {
let uuid = self.uuid_resolver
.resolve(index.clone())
.await?
.with_context(|| format!("Index {:?} doesn't exist", index))?;
let document = self.index_handle.document(uuid, doc_id, attributes_to_retrieve).await?;
Ok(document)
}
fn update_index(&self, name: String, index_settings: IndexSettings) -> anyhow::Result<IndexMetadata> {
todo!()
}