2021-03-04 19:03:06 +08:00
|
|
|
mod index_actor;
|
|
|
|
mod update_actor;
|
|
|
|
mod update_handler;
|
2021-03-06 19:57:56 +08:00
|
|
|
mod update_store;
|
|
|
|
mod updates;
|
|
|
|
mod uuid_resolver;
|
2021-01-14 00:50:36 +08:00
|
|
|
|
2021-03-04 19:03:06 +08:00
|
|
|
use std::path::Path;
|
2021-03-06 19:57:56 +08:00
|
|
|
use std::sync::Arc;
|
|
|
|
use std::time::Duration;
|
2021-03-04 19:03:06 +08:00
|
|
|
|
2021-03-04 22:59:18 +08:00
|
|
|
use actix_web::web::{Bytes, Payload};
|
2021-03-04 19:38:55 +08:00
|
|
|
use futures::stream::StreamExt;
|
2021-03-04 18:56:32 +08:00
|
|
|
use milli::update::{IndexDocumentsMethod, UpdateFormat};
|
|
|
|
use serde::{Serialize, Deserialize};
|
2021-03-04 19:38:55 +08:00
|
|
|
use tokio::sync::{mpsc, oneshot};
|
2021-03-06 19:57:56 +08:00
|
|
|
use tokio::time::sleep;
|
2021-02-09 23:08:13 +08:00
|
|
|
use uuid::Uuid;
|
2021-01-16 22:09:48 +08:00
|
|
|
|
2021-02-02 02:51:47 +08:00
|
|
|
pub use updates::{Processed, Processing, Failed};
|
2021-03-04 21:20:19 +08:00
|
|
|
use crate::index::{SearchResult, SearchQuery, Document};
|
2021-03-04 18:56:32 +08:00
|
|
|
use crate::index::{UpdateResult, Settings, Facets};
|
2021-02-02 02:51:47 +08:00
|
|
|
|
|
|
|
pub type UpdateStatus = updates::UpdateStatus<UpdateMeta, UpdateResult, String>;
|
2021-01-28 21:12:34 +08:00
|
|
|
|
2021-02-04 00:44:20 +08:00
|
|
|
#[derive(Debug, Serialize, Deserialize, Clone)]
|
|
|
|
#[serde(rename_all = "camelCase")]
|
|
|
|
pub struct IndexMetadata {
|
2021-03-07 03:12:20 +08:00
|
|
|
name: String,
|
|
|
|
#[serde(flatten)]
|
|
|
|
meta: index_actor::IndexMeta,
|
2021-02-04 00:44:20 +08:00
|
|
|
}
|
|
|
|
|
2021-01-16 22:09:48 +08:00
|
|
|
#[derive(Debug, Clone, Serialize, Deserialize)]
|
|
|
|
#[serde(tag = "type")]
|
|
|
|
pub enum UpdateMeta {
|
2021-02-13 19:22:59 +08:00
|
|
|
DocumentsAddition {
|
|
|
|
method: IndexDocumentsMethod,
|
|
|
|
format: UpdateFormat,
|
|
|
|
primary_key: Option<String>,
|
|
|
|
},
|
2021-01-16 22:09:48 +08:00
|
|
|
ClearDocuments,
|
2021-02-13 00:39:14 +08:00
|
|
|
DeleteDocuments,
|
2021-01-16 22:09:48 +08:00
|
|
|
Settings(Settings),
|
|
|
|
Facets(Facets),
|
2021-01-14 00:50:36 +08:00
|
|
|
}
|
|
|
|
|
2021-02-09 23:08:13 +08:00
|
|
|
#[derive(Clone, Debug)]
|
2021-02-09 18:41:26 +08:00
|
|
|
pub struct IndexSettings {
|
|
|
|
pub name: Option<String>,
|
|
|
|
pub primary_key: Option<String>,
|
|
|
|
}
|
2021-03-04 19:03:06 +08:00
|
|
|
|
|
|
|
|
|
|
|
pub struct IndexController {
|
|
|
|
uuid_resolver: uuid_resolver::UuidResolverHandle,
|
|
|
|
index_handle: index_actor::IndexActorHandle,
|
|
|
|
update_handle: update_actor::UpdateActorHandle<Bytes>,
|
|
|
|
}
|
|
|
|
|
|
|
|
enum IndexControllerMsg {
|
|
|
|
CreateIndex {
|
|
|
|
uuid: Uuid,
|
|
|
|
primary_key: Option<String>,
|
|
|
|
ret: oneshot::Sender<anyhow::Result<IndexMetadata>>,
|
|
|
|
},
|
|
|
|
Shutdown,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl IndexController {
|
2021-03-08 22:53:16 +08:00
|
|
|
pub fn new(path: impl AsRef<Path>) -> anyhow::Result<Self> {
|
2021-03-11 01:04:20 +08:00
|
|
|
let uuid_resolver = uuid_resolver::UuidResolverHandle::new(&path)?;
|
2021-03-08 22:53:16 +08:00
|
|
|
let index_actor = index_actor::IndexActorHandle::new(&path)?;
|
2021-03-12 00:59:47 +08:00
|
|
|
let update_handle = update_actor::UpdateActorHandle::new(index_actor.clone(), &path)?;
|
2021-03-08 22:53:16 +08:00
|
|
|
Ok(Self { uuid_resolver, index_handle: index_actor, update_handle })
|
2021-03-04 19:03:06 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
pub async fn add_documents(
|
|
|
|
&self,
|
|
|
|
index: String,
|
|
|
|
method: milli::update::IndexDocumentsMethod,
|
|
|
|
format: milli::update::UpdateFormat,
|
|
|
|
mut payload: Payload,
|
|
|
|
primary_key: Option<String>,
|
|
|
|
) -> anyhow::Result<UpdateStatus> {
|
|
|
|
let uuid = self.uuid_resolver.get_or_create(index).await?;
|
|
|
|
let meta = UpdateMeta::DocumentsAddition { method, format, primary_key };
|
|
|
|
let (sender, receiver) = mpsc::channel(10);
|
|
|
|
|
|
|
|
// It is necessary to spawn a local task to senf the payload to the update handle to
|
|
|
|
// prevent dead_locking between the update_handle::update that waits for the update to be
|
|
|
|
// registered and the update_actor that waits for the the payload to be sent to it.
|
|
|
|
tokio::task::spawn_local(async move {
|
|
|
|
while let Some(bytes) = payload.next().await {
|
|
|
|
match bytes {
|
|
|
|
Ok(bytes) => { sender.send(Ok(bytes)).await; },
|
|
|
|
Err(e) => {
|
|
|
|
let error: Box<dyn std::error::Error + Sync + Send + 'static> = Box::new(e);
|
|
|
|
sender.send(Err(error)).await; },
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
// This must be done *AFTER* spawning the task.
|
|
|
|
let status = self.update_handle.update(meta, receiver, uuid).await?;
|
|
|
|
Ok(status)
|
|
|
|
}
|
|
|
|
|
2021-03-04 23:04:12 +08:00
|
|
|
pub async fn clear_documents(&self, index: String) -> anyhow::Result<UpdateStatus> {
|
2021-03-08 23:27:29 +08:00
|
|
|
let uuid = self.uuid_resolver.resolve(index).await?;
|
2021-03-04 23:04:12 +08:00
|
|
|
let meta = UpdateMeta::ClearDocuments;
|
|
|
|
let (_, receiver) = mpsc::channel(1);
|
|
|
|
let status = self.update_handle.update(meta, receiver, uuid).await?;
|
|
|
|
Ok(status)
|
2021-03-04 19:03:06 +08:00
|
|
|
}
|
|
|
|
|
2021-03-04 22:59:18 +08:00
|
|
|
pub async fn delete_documents(&self, index: String, document_ids: Vec<String>) -> anyhow::Result<UpdateStatus> {
|
2021-03-08 23:27:29 +08:00
|
|
|
let uuid = self.uuid_resolver.resolve(index).await?;
|
2021-03-04 22:59:18 +08:00
|
|
|
let meta = UpdateMeta::DeleteDocuments;
|
|
|
|
let (sender, receiver) = mpsc::channel(10);
|
|
|
|
|
|
|
|
tokio::task::spawn(async move {
|
|
|
|
let json = serde_json::to_vec(&document_ids).unwrap();
|
|
|
|
let bytes = Bytes::from(json);
|
|
|
|
let _ = sender.send(Ok(bytes)).await;
|
|
|
|
});
|
|
|
|
|
|
|
|
let status = self.update_handle.update(meta, receiver, uuid).await?;
|
|
|
|
Ok(status)
|
2021-03-04 19:03:06 +08:00
|
|
|
}
|
|
|
|
|
2021-03-04 19:20:14 +08:00
|
|
|
pub async fn update_settings(&self, index_uid: String, settings: Settings) -> anyhow::Result<UpdateStatus> {
|
|
|
|
let uuid = self.uuid_resolver.get_or_create(index_uid).await?;
|
|
|
|
let meta = UpdateMeta::Settings(settings);
|
|
|
|
// Nothing so send, drop the sender right away, as not to block the update actor.
|
|
|
|
let (_, receiver) = mpsc::channel(1);
|
|
|
|
|
|
|
|
let status = self.update_handle.update(meta, receiver, uuid).await?;
|
|
|
|
Ok(status)
|
2021-03-04 19:03:06 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
pub async fn create_index(&self, index_settings: IndexSettings) -> anyhow::Result<IndexMetadata> {
|
|
|
|
let IndexSettings { name, primary_key } = index_settings;
|
2021-03-07 03:12:20 +08:00
|
|
|
let name = name.unwrap();
|
|
|
|
let uuid = self.uuid_resolver.create(name.clone()).await?;
|
|
|
|
let meta = self.index_handle.create_index(uuid, primary_key).await?;
|
|
|
|
let meta = IndexMetadata { name, meta };
|
|
|
|
|
|
|
|
Ok(meta)
|
2021-03-04 19:03:06 +08:00
|
|
|
}
|
|
|
|
|
2021-03-06 19:57:56 +08:00
|
|
|
pub async fn delete_index(&self, index_uid: String) -> anyhow::Result<()> {
|
|
|
|
let uuid = self.uuid_resolver
|
|
|
|
.delete(index_uid)
|
2021-03-08 23:27:29 +08:00
|
|
|
.await?;
|
2021-03-06 19:57:56 +08:00
|
|
|
self.update_handle.delete(uuid.clone()).await?;
|
|
|
|
self.index_handle.delete(uuid).await?;
|
|
|
|
Ok(())
|
2021-03-04 19:03:06 +08:00
|
|
|
}
|
|
|
|
|
2021-03-06 17:51:52 +08:00
|
|
|
pub async fn update_status(&self, index: String, id: u64) -> anyhow::Result<Option<UpdateStatus>> {
|
|
|
|
let uuid = self.uuid_resolver
|
|
|
|
.resolve(index)
|
2021-03-08 23:27:29 +08:00
|
|
|
.await?;
|
2021-03-06 17:51:52 +08:00
|
|
|
let result = self.update_handle.update_status(uuid, id).await?;
|
|
|
|
Ok(result)
|
2021-03-04 19:03:06 +08:00
|
|
|
}
|
|
|
|
|
2021-03-06 01:34:04 +08:00
|
|
|
pub async fn all_update_status(&self, index: String) -> anyhow::Result<Vec<UpdateStatus>> {
|
|
|
|
let uuid = self.uuid_resolver
|
2021-03-08 23:27:29 +08:00
|
|
|
.resolve(index).await?;
|
2021-03-06 01:34:04 +08:00
|
|
|
let result = self.update_handle.get_all_updates_status(uuid).await?;
|
|
|
|
Ok(result)
|
2021-03-04 19:03:06 +08:00
|
|
|
}
|
|
|
|
|
2021-03-07 03:12:20 +08:00
|
|
|
pub async fn list_indexes(&self) -> anyhow::Result<Vec<IndexMetadata>> {
|
|
|
|
let uuids = self.uuid_resolver.list().await?;
|
|
|
|
|
|
|
|
let mut ret = Vec::new();
|
|
|
|
|
|
|
|
for (name, uuid) in uuids {
|
|
|
|
if let Some(meta) = self.index_handle.get_index_meta(uuid).await? {
|
|
|
|
let meta = IndexMetadata { name, meta };
|
|
|
|
ret.push(meta);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(ret)
|
2021-03-04 19:03:06 +08:00
|
|
|
}
|
|
|
|
|
2021-03-04 19:38:55 +08:00
|
|
|
pub async fn settings(&self, index: String) -> anyhow::Result<Settings> {
|
|
|
|
let uuid = self.uuid_resolver
|
|
|
|
.resolve(index.clone())
|
2021-03-08 23:27:29 +08:00
|
|
|
.await?;
|
2021-03-04 19:38:55 +08:00
|
|
|
let settings = self.index_handle.settings(uuid).await?;
|
|
|
|
Ok(settings)
|
|
|
|
}
|
|
|
|
|
2021-03-04 21:20:19 +08:00
|
|
|
pub async fn documents(
|
|
|
|
&self,
|
|
|
|
index: String,
|
|
|
|
offset: usize,
|
|
|
|
limit: usize,
|
|
|
|
attributes_to_retrieve: Option<Vec<String>>,
|
|
|
|
) -> anyhow::Result<Vec<Document>> {
|
|
|
|
let uuid = self.uuid_resolver
|
|
|
|
.resolve(index.clone())
|
2021-03-08 23:27:29 +08:00
|
|
|
.await?;
|
2021-03-04 21:20:19 +08:00
|
|
|
let documents = self.index_handle.documents(uuid, offset, limit, attributes_to_retrieve).await?;
|
|
|
|
Ok(documents)
|
|
|
|
}
|
|
|
|
|
2021-03-04 22:09:00 +08:00
|
|
|
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())
|
2021-03-08 23:27:29 +08:00
|
|
|
.await?;
|
2021-03-04 22:09:00 +08:00
|
|
|
let document = self.index_handle.document(uuid, doc_id, attributes_to_retrieve).await?;
|
|
|
|
Ok(document)
|
|
|
|
}
|
|
|
|
|
2021-03-04 19:03:06 +08:00
|
|
|
fn update_index(&self, name: String, index_settings: IndexSettings) -> anyhow::Result<IndexMetadata> {
|
|
|
|
todo!()
|
|
|
|
}
|
|
|
|
|
|
|
|
pub async fn search(&self, name: String, query: SearchQuery) -> anyhow::Result<SearchResult> {
|
2021-03-08 23:27:29 +08:00
|
|
|
let uuid = self.uuid_resolver.resolve(name).await?;
|
2021-03-04 19:03:06 +08:00
|
|
|
let result = self.index_handle.search(uuid, query).await?;
|
|
|
|
Ok(result)
|
|
|
|
}
|
2021-03-07 03:17:58 +08:00
|
|
|
|
|
|
|
pub async fn get_index(&self, name: String) -> anyhow::Result<Option<IndexMetadata>> {
|
|
|
|
let uuid = self.uuid_resolver.resolve(name.clone()).await?;
|
2021-03-08 23:27:29 +08:00
|
|
|
let result = self.index_handle
|
|
|
|
.get_index_meta(uuid)
|
|
|
|
.await?
|
|
|
|
.map(|meta| IndexMetadata { name, meta });
|
|
|
|
Ok(result)
|
2021-03-07 03:17:58 +08:00
|
|
|
}
|
2021-03-04 19:03:06 +08:00
|
|
|
}
|
2021-03-06 19:57:56 +08:00
|
|
|
|
|
|
|
pub async fn get_arc_ownership_blocking<T>(mut item: Arc<T>) -> T {
|
|
|
|
loop {
|
|
|
|
match Arc::try_unwrap(item) {
|
|
|
|
Ok(item) => return item,
|
|
|
|
Err(item_arc) => {
|
|
|
|
item = item_arc;
|
|
|
|
sleep(Duration::from_millis(100)).await;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|