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-16 01:11:10 +08:00
|
|
|
use anyhow::bail;
|
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};
|
2021-03-16 01:11:10 +08:00
|
|
|
use serde::{Deserialize, Serialize};
|
2021-03-13 00:44:39 +08:00
|
|
|
use tokio::sync::mpsc;
|
2021-03-06 19:57:56 +08:00
|
|
|
use tokio::time::sleep;
|
2021-01-16 22:09:48 +08:00
|
|
|
|
2021-03-16 01:11:10 +08:00
|
|
|
use crate::index::{Document, SearchQuery, SearchResult};
|
|
|
|
use crate::index::{Facets, Settings, UpdateResult};
|
|
|
|
pub use updates::{Failed, Processed, Processing};
|
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-12 05:47:29 +08:00
|
|
|
uid: String,
|
2021-03-07 03:12:20 +08:00
|
|
|
#[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 {
|
2021-03-12 05:47:29 +08:00
|
|
|
pub uid: Option<String>,
|
2021-02-09 18:41:26 +08:00
|
|
|
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>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl IndexController {
|
2021-03-16 01:11:10 +08:00
|
|
|
pub fn new(
|
|
|
|
path: impl AsRef<Path>,
|
|
|
|
index_size: usize,
|
|
|
|
update_store_size: usize,
|
|
|
|
) -> anyhow::Result<Self> {
|
2021-03-11 01:04:20 +08:00
|
|
|
let uuid_resolver = uuid_resolver::UuidResolverHandle::new(&path)?;
|
2021-03-13 17:09:10 +08:00
|
|
|
let index_actor = index_actor::IndexActorHandle::new(&path, index_size)?;
|
2021-03-16 01:11:10 +08:00
|
|
|
let update_handle =
|
|
|
|
update_actor::UpdateActorHandle::new(index_actor.clone(), &path, update_store_size)?;
|
|
|
|
Ok(Self {
|
|
|
|
uuid_resolver,
|
|
|
|
index_handle: index_actor,
|
|
|
|
update_handle,
|
|
|
|
})
|
2021-03-04 19:03:06 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
pub async fn add_documents(
|
|
|
|
&self,
|
2021-03-12 05:47:29 +08:00
|
|
|
uid: String,
|
2021-03-04 19:03:06 +08:00
|
|
|
method: milli::update::IndexDocumentsMethod,
|
|
|
|
format: milli::update::UpdateFormat,
|
|
|
|
mut payload: Payload,
|
|
|
|
primary_key: Option<String>,
|
|
|
|
) -> anyhow::Result<UpdateStatus> {
|
2021-03-12 05:47:29 +08:00
|
|
|
let uuid = self.uuid_resolver.get_or_create(uid).await?;
|
2021-03-16 01:11:10 +08:00
|
|
|
let meta = UpdateMeta::DocumentsAddition {
|
|
|
|
method,
|
|
|
|
format,
|
|
|
|
primary_key,
|
|
|
|
};
|
2021-03-04 19:03:06 +08:00
|
|
|
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 {
|
2021-03-16 01:11:10 +08:00
|
|
|
Ok(bytes) => {
|
|
|
|
let _ = sender.send(Ok(bytes)).await;
|
|
|
|
}
|
2021-03-04 19:03:06 +08:00
|
|
|
Err(e) => {
|
|
|
|
let error: Box<dyn std::error::Error + Sync + Send + 'static> = Box::new(e);
|
2021-03-16 01:11:10 +08:00
|
|
|
let _ = sender.send(Err(error)).await;
|
|
|
|
}
|
2021-03-04 19:03:06 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
// This must be done *AFTER* spawning the task.
|
|
|
|
let status = self.update_handle.update(meta, receiver, uuid).await?;
|
|
|
|
Ok(status)
|
|
|
|
}
|
|
|
|
|
2021-03-12 05:47:29 +08:00
|
|
|
pub async fn clear_documents(&self, uid: String) -> anyhow::Result<UpdateStatus> {
|
|
|
|
let uuid = self.uuid_resolver.resolve(uid).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-16 01:11:10 +08:00
|
|
|
pub async fn delete_documents(
|
|
|
|
&self,
|
|
|
|
uid: String,
|
|
|
|
document_ids: Vec<String>,
|
|
|
|
) -> anyhow::Result<UpdateStatus> {
|
2021-03-12 05:47:29 +08:00
|
|
|
let uuid = self.uuid_resolver.resolve(uid).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-16 01:11:10 +08:00
|
|
|
pub async fn update_settings(
|
|
|
|
&self,
|
|
|
|
uid: String,
|
|
|
|
settings: Settings,
|
|
|
|
create: bool,
|
|
|
|
) -> anyhow::Result<UpdateStatus> {
|
2021-03-12 05:33:31 +08:00
|
|
|
let uuid = if create {
|
2021-03-12 07:37:43 +08:00
|
|
|
let uuid = self.uuid_resolver.get_or_create(uid).await?;
|
|
|
|
// We need to create the index upfront, since it would otherwise only be created when
|
|
|
|
// the update is processed. This would make calls to GET index to fail until the update
|
|
|
|
// is complete. Since this is get or create, we ignore the error when the index already
|
|
|
|
// exists.
|
2021-03-15 23:52:05 +08:00
|
|
|
match self.index_handle.create_index(uuid, None).await {
|
2021-03-12 07:37:43 +08:00
|
|
|
Ok(_) | Err(index_actor::IndexError::IndexAlreadyExists) => (),
|
|
|
|
Err(e) => return Err(e.into()),
|
|
|
|
}
|
|
|
|
uuid
|
2021-03-12 05:33:31 +08:00
|
|
|
} else {
|
2021-03-12 05:47:29 +08:00
|
|
|
self.uuid_resolver.resolve(uid).await?
|
2021-03-12 05:33:31 +08:00
|
|
|
};
|
2021-03-04 19:20:14 +08:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2021-03-16 01:11:10 +08:00
|
|
|
pub async fn create_index(
|
|
|
|
&self,
|
|
|
|
index_settings: IndexSettings,
|
|
|
|
) -> anyhow::Result<IndexMetadata> {
|
|
|
|
let IndexSettings { uid, primary_key } = index_settings;
|
|
|
|
let uid = uid.ok_or_else(|| anyhow::anyhow!("Can't create an index without a uid."))?;
|
2021-03-12 05:47:29 +08:00
|
|
|
let uuid = self.uuid_resolver.create(uid.clone()).await?;
|
2021-03-07 03:12:20 +08:00
|
|
|
let meta = self.index_handle.create_index(uuid, primary_key).await?;
|
2021-03-12 05:11:58 +08:00
|
|
|
let _ = self.update_handle.create(uuid).await?;
|
2021-03-12 05:47:29 +08:00
|
|
|
let meta = IndexMetadata { uid, meta };
|
2021-03-07 03:12:20 +08:00
|
|
|
|
|
|
|
Ok(meta)
|
2021-03-04 19:03:06 +08:00
|
|
|
}
|
|
|
|
|
2021-03-12 05:47:29 +08:00
|
|
|
pub async fn delete_index(&self, uid: String) -> anyhow::Result<()> {
|
2021-03-16 01:11:10 +08:00
|
|
|
let uuid = self.uuid_resolver.delete(uid).await?;
|
2021-03-15 23:52:05 +08:00
|
|
|
self.update_handle.delete(uuid).await?;
|
2021-03-06 19:57:56 +08:00
|
|
|
self.index_handle.delete(uuid).await?;
|
|
|
|
Ok(())
|
2021-03-04 19:03:06 +08:00
|
|
|
}
|
|
|
|
|
2021-03-15 23:52:05 +08:00
|
|
|
pub async fn update_status(&self, uid: String, id: u64) -> anyhow::Result<UpdateStatus> {
|
2021-03-16 01:11:10 +08:00
|
|
|
let uuid = self.uuid_resolver.resolve(uid).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-12 05:47:29 +08:00
|
|
|
pub async fn all_update_status(&self, uid: String) -> anyhow::Result<Vec<UpdateStatus>> {
|
2021-03-16 01:11:10 +08:00
|
|
|
let uuid = self.uuid_resolver.resolve(uid).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();
|
|
|
|
|
2021-03-12 05:47:29 +08:00
|
|
|
for (uid, uuid) in uuids {
|
2021-03-15 23:52:05 +08:00
|
|
|
let meta = self.index_handle.get_index_meta(uuid).await?;
|
|
|
|
let meta = IndexMetadata { uid, meta };
|
|
|
|
ret.push(meta);
|
2021-03-07 03:12:20 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
Ok(ret)
|
2021-03-04 19:03:06 +08:00
|
|
|
}
|
|
|
|
|
2021-03-12 05:47:29 +08:00
|
|
|
pub async fn settings(&self, uid: String) -> anyhow::Result<Settings> {
|
2021-03-16 01:11:10 +08:00
|
|
|
let uuid = self.uuid_resolver.resolve(uid.clone()).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,
|
2021-03-12 05:47:29 +08:00
|
|
|
uid: String,
|
2021-03-04 21:20:19 +08:00
|
|
|
offset: usize,
|
|
|
|
limit: usize,
|
|
|
|
attributes_to_retrieve: Option<Vec<String>>,
|
|
|
|
) -> anyhow::Result<Vec<Document>> {
|
2021-03-16 01:11:10 +08:00
|
|
|
let uuid = self.uuid_resolver.resolve(uid.clone()).await?;
|
|
|
|
let documents = self
|
|
|
|
.index_handle
|
|
|
|
.documents(uuid, offset, limit, attributes_to_retrieve)
|
2021-03-08 23:27:29 +08:00
|
|
|
.await?;
|
2021-03-04 21:20:19 +08:00
|
|
|
Ok(documents)
|
|
|
|
}
|
|
|
|
|
2021-03-04 22:09:00 +08:00
|
|
|
pub async fn document(
|
|
|
|
&self,
|
2021-03-12 05:47:29 +08:00
|
|
|
uid: String,
|
2021-03-04 22:09:00 +08:00
|
|
|
doc_id: String,
|
|
|
|
attributes_to_retrieve: Option<Vec<String>>,
|
|
|
|
) -> anyhow::Result<Document> {
|
2021-03-16 01:11:10 +08:00
|
|
|
let uuid = self.uuid_resolver.resolve(uid.clone()).await?;
|
|
|
|
let document = self
|
|
|
|
.index_handle
|
|
|
|
.document(uuid, doc_id, attributes_to_retrieve)
|
2021-03-08 23:27:29 +08:00
|
|
|
.await?;
|
2021-03-04 22:09:00 +08:00
|
|
|
Ok(document)
|
|
|
|
}
|
|
|
|
|
2021-03-16 01:11:10 +08:00
|
|
|
pub async fn update_index(
|
|
|
|
&self,
|
|
|
|
uid: String,
|
|
|
|
index_settings: IndexSettings,
|
|
|
|
) -> anyhow::Result<IndexMetadata> {
|
2021-03-12 21:48:43 +08:00
|
|
|
if index_settings.uid.is_some() {
|
|
|
|
bail!("Can't change the index uid.")
|
|
|
|
}
|
|
|
|
|
2021-03-16 01:11:10 +08:00
|
|
|
let uuid = self.uuid_resolver.resolve(uid.clone()).await?;
|
2021-03-12 21:48:43 +08:00
|
|
|
let meta = self.index_handle.update_index(uuid, index_settings).await?;
|
|
|
|
let meta = IndexMetadata { uid, meta };
|
|
|
|
Ok(meta)
|
2021-03-04 19:03:06 +08:00
|
|
|
}
|
|
|
|
|
2021-03-12 05:47:29 +08:00
|
|
|
pub async fn search(&self, uid: String, query: SearchQuery) -> anyhow::Result<SearchResult> {
|
|
|
|
let uuid = self.uuid_resolver.resolve(uid).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
|
|
|
|
2021-03-15 23:52:05 +08:00
|
|
|
pub async fn get_index(&self, uid: String) -> anyhow::Result<IndexMetadata> {
|
2021-03-12 05:47:29 +08:00
|
|
|
let uuid = self.uuid_resolver.resolve(uid.clone()).await?;
|
2021-03-16 01:11:10 +08:00
|
|
|
let meta = self.index_handle.get_index_meta(uuid).await?;
|
2021-03-15 23:52:05 +08:00
|
|
|
let meta = IndexMetadata { uid, meta };
|
|
|
|
Ok(meta)
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|