MeiliSearch/meilisearch-http/src/index_controller/mod.rs

304 lines
10 KiB
Rust
Raw Normal View History

2021-03-04 12:03:06 +01:00
mod index_actor;
mod update_actor;
mod update_handler;
2021-03-06 12:57:56 +01:00
mod update_store;
mod updates;
mod uuid_resolver;
2021-03-17 11:53:23 +01:00
mod snapshot;
2021-01-13 17:50:36 +01:00
2021-03-04 12:03:06 +01:00
use std::path::Path;
2021-03-06 12:57:56 +01:00
use std::sync::Arc;
use std::time::Duration;
2021-03-04 12:03:06 +01:00
2021-03-04 15:59:18 +01:00
use actix_web::web::{Bytes, Payload};
2021-03-15 18:11:10 +01:00
use anyhow::bail;
2021-03-04 12:38:55 +01:00
use futures::stream::StreamExt;
2021-03-04 11:56:32 +01:00
use milli::update::{IndexDocumentsMethod, UpdateFormat};
2021-03-15 18:11:10 +01:00
use serde::{Deserialize, Serialize};
2021-03-12 17:44:39 +01:00
use tokio::sync::mpsc;
2021-03-06 12:57:56 +01:00
use tokio::time::sleep;
2021-03-15 18:11:10 +01:00
use crate::index::{Document, SearchQuery, SearchResult};
use crate::index::{Facets, Settings, UpdateResult};
2021-03-17 11:53:23 +01:00
2021-03-15 18:11:10 +01:00
pub use updates::{Failed, Processed, Processing};
2021-03-17 11:53:23 +01:00
use snapshot::SnapshotService;
2021-02-01 19:51:47 +01:00
pub type UpdateStatus = updates::UpdateStatus<UpdateMeta, UpdateResult, String>;
2021-01-28 14:12:34 +01:00
2021-02-03 17:44:20 +01:00
#[derive(Debug, Serialize, Deserialize, Clone)]
#[serde(rename_all = "camelCase")]
pub struct IndexMetadata {
2021-03-11 22:47:29 +01:00
uid: String,
2021-03-15 18:35:16 +01:00
name: String,
2021-03-06 20:12:20 +01:00
#[serde(flatten)]
meta: index_actor::IndexMeta,
2021-02-03 17:44:20 +01:00
}
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(tag = "type")]
pub enum UpdateMeta {
2021-02-13 12:22:59 +01:00
DocumentsAddition {
method: IndexDocumentsMethod,
format: UpdateFormat,
primary_key: Option<String>,
},
ClearDocuments,
2021-02-12 17:39:14 +01:00
DeleteDocuments,
Settings(Settings),
Facets(Facets),
2021-01-13 17:50:36 +01:00
}
2021-02-09 16:08:13 +01:00
#[derive(Clone, Debug)]
pub struct IndexSettings {
2021-03-11 22:47:29 +01:00
pub uid: Option<String>,
pub primary_key: Option<String>,
}
2021-03-04 12:03:06 +01:00
pub struct IndexController {
uuid_resolver: uuid_resolver::UuidResolverHandle,
index_handle: index_actor::IndexActorHandle,
update_handle: update_actor::UpdateActorHandle<Bytes>,
}
impl IndexController {
2021-03-15 18:11:10 +01:00
pub fn new(
path: impl AsRef<Path>,
index_size: usize,
update_store_size: usize,
) -> anyhow::Result<Self> {
2021-03-10 18:04:20 +01:00
let uuid_resolver = uuid_resolver::UuidResolverHandle::new(&path)?;
2021-03-17 11:53:23 +01:00
let index_handle = index_actor::IndexActorHandle::new(&path, index_size)?;
2021-03-15 18:11:10 +01:00
let update_handle =
2021-03-17 11:53:23 +01:00
update_actor::UpdateActorHandle::new(index_handle.clone(), &path, update_store_size)?;
let snapshot_service = SnapshotService::new(
index_handle.clone(),
uuid_resolver.clone(),
update_handle.clone(),
Duration::from_millis(10000),
"/dev/toto".into());
tokio::task::spawn(snapshot_service.run());
2021-03-15 18:11:10 +01:00
Ok(Self {
uuid_resolver,
2021-03-17 11:53:23 +01:00
index_handle,
2021-03-15 18:11:10 +01:00
update_handle,
})
2021-03-04 12:03:06 +01:00
}
pub async fn add_documents(
&self,
2021-03-11 22:47:29 +01:00
uid: String,
2021-03-04 12:03:06 +01:00
method: milli::update::IndexDocumentsMethod,
format: milli::update::UpdateFormat,
mut payload: Payload,
primary_key: Option<String>,
) -> anyhow::Result<UpdateStatus> {
2021-03-11 22:47:29 +01:00
let uuid = self.uuid_resolver.get_or_create(uid).await?;
2021-03-15 18:11:10 +01:00
let meta = UpdateMeta::DocumentsAddition {
method,
format,
primary_key,
};
2021-03-04 12:03:06 +01: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-15 18:11:10 +01:00
Ok(bytes) => {
let _ = sender.send(Ok(bytes)).await;
}
2021-03-04 12:03:06 +01:00
Err(e) => {
let error: Box<dyn std::error::Error + Sync + Send + 'static> = Box::new(e);
2021-03-15 18:11:10 +01:00
let _ = sender.send(Err(error)).await;
}
2021-03-04 12:03:06 +01:00
}
}
});
// This must be done *AFTER* spawning the task.
let status = self.update_handle.update(meta, receiver, uuid).await?;
Ok(status)
}
2021-03-11 22:47:29 +01:00
pub async fn clear_documents(&self, uid: String) -> anyhow::Result<UpdateStatus> {
let uuid = self.uuid_resolver.resolve(uid).await?;
2021-03-04 16:04:12 +01: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 12:03:06 +01:00
}
2021-03-15 18:11:10 +01:00
pub async fn delete_documents(
&self,
uid: String,
document_ids: Vec<String>,
) -> anyhow::Result<UpdateStatus> {
2021-03-11 22:47:29 +01:00
let uuid = self.uuid_resolver.resolve(uid).await?;
2021-03-04 15:59:18 +01: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 12:03:06 +01:00
}
2021-03-15 18:11:10 +01:00
pub async fn update_settings(
&self,
uid: String,
settings: Settings,
create: bool,
) -> anyhow::Result<UpdateStatus> {
let uuid = if create {
2021-03-12 00:37:43 +01: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 16:52:05 +01:00
match self.index_handle.create_index(uuid, None).await {
2021-03-12 00:37:43 +01:00
Ok(_) | Err(index_actor::IndexError::IndexAlreadyExists) => (),
Err(e) => return Err(e.into()),
}
uuid
} else {
2021-03-11 22:47:29 +01:00
self.uuid_resolver.resolve(uid).await?
};
2021-03-04 12:20:14 +01: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 12:03:06 +01:00
}
2021-03-15 18:11:10 +01: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-11 22:47:29 +01:00
let uuid = self.uuid_resolver.create(uid.clone()).await?;
2021-03-06 20:12:20 +01:00
let meta = self.index_handle.create_index(uuid, primary_key).await?;
let _ = self.update_handle.create(uuid).await?;
2021-03-15 18:35:16 +01:00
let meta = IndexMetadata { name: uid.clone(), uid, meta };
2021-03-06 20:12:20 +01:00
Ok(meta)
2021-03-04 12:03:06 +01:00
}
2021-03-11 22:47:29 +01:00
pub async fn delete_index(&self, uid: String) -> anyhow::Result<()> {
2021-03-15 18:11:10 +01:00
let uuid = self.uuid_resolver.delete(uid).await?;
2021-03-15 16:52:05 +01:00
self.update_handle.delete(uuid).await?;
2021-03-06 12:57:56 +01:00
self.index_handle.delete(uuid).await?;
Ok(())
2021-03-04 12:03:06 +01:00
}
2021-03-15 16:52:05 +01:00
pub async fn update_status(&self, uid: String, id: u64) -> anyhow::Result<UpdateStatus> {
2021-03-15 18:11:10 +01:00
let uuid = self.uuid_resolver.resolve(uid).await?;
2021-03-06 10:51:52 +01:00
let result = self.update_handle.update_status(uuid, id).await?;
Ok(result)
2021-03-04 12:03:06 +01:00
}
2021-03-11 22:47:29 +01:00
pub async fn all_update_status(&self, uid: String) -> anyhow::Result<Vec<UpdateStatus>> {
2021-03-15 18:11:10 +01:00
let uuid = self.uuid_resolver.resolve(uid).await?;
2021-03-05 18:34:04 +01:00
let result = self.update_handle.get_all_updates_status(uuid).await?;
Ok(result)
2021-03-04 12:03:06 +01:00
}
2021-03-06 20:12:20 +01: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-11 22:47:29 +01:00
for (uid, uuid) in uuids {
2021-03-15 16:52:05 +01:00
let meta = self.index_handle.get_index_meta(uuid).await?;
2021-03-15 18:35:16 +01:00
let meta = IndexMetadata { name: uid.clone(), uid, meta };
2021-03-15 16:52:05 +01:00
ret.push(meta);
2021-03-06 20:12:20 +01:00
}
Ok(ret)
2021-03-04 12:03:06 +01:00
}
2021-03-11 22:47:29 +01:00
pub async fn settings(&self, uid: String) -> anyhow::Result<Settings> {
2021-03-15 18:11:10 +01:00
let uuid = self.uuid_resolver.resolve(uid.clone()).await?;
2021-03-04 12:38:55 +01:00
let settings = self.index_handle.settings(uuid).await?;
Ok(settings)
}
2021-03-04 14:20:19 +01:00
pub async fn documents(
&self,
2021-03-11 22:47:29 +01:00
uid: String,
2021-03-04 14:20:19 +01:00
offset: usize,
limit: usize,
attributes_to_retrieve: Option<Vec<String>>,
) -> anyhow::Result<Vec<Document>> {
2021-03-15 18:11:10 +01: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 16:27:29 +01:00
.await?;
2021-03-04 14:20:19 +01:00
Ok(documents)
}
2021-03-04 15:09:00 +01:00
pub async fn document(
&self,
2021-03-11 22:47:29 +01:00
uid: String,
2021-03-04 15:09:00 +01:00
doc_id: String,
attributes_to_retrieve: Option<Vec<String>>,
) -> anyhow::Result<Document> {
2021-03-15 18:11:10 +01: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 16:27:29 +01:00
.await?;
2021-03-04 15:09:00 +01:00
Ok(document)
}
2021-03-15 18:11:10 +01:00
pub async fn update_index(
&self,
uid: String,
index_settings: IndexSettings,
) -> anyhow::Result<IndexMetadata> {
2021-03-12 14:48:43 +01:00
if index_settings.uid.is_some() {
bail!("Can't change the index uid.")
}
2021-03-15 18:11:10 +01:00
let uuid = self.uuid_resolver.resolve(uid.clone()).await?;
2021-03-12 14:48:43 +01:00
let meta = self.index_handle.update_index(uuid, index_settings).await?;
2021-03-15 18:35:16 +01:00
let meta = IndexMetadata { name: uid.clone(), uid, meta };
2021-03-12 14:48:43 +01:00
Ok(meta)
2021-03-04 12:03:06 +01:00
}
2021-03-11 22:47:29 +01:00
pub async fn search(&self, uid: String, query: SearchQuery) -> anyhow::Result<SearchResult> {
let uuid = self.uuid_resolver.resolve(uid).await?;
2021-03-04 12:03:06 +01:00
let result = self.index_handle.search(uuid, query).await?;
Ok(result)
}
2021-03-06 20:17:58 +01:00
2021-03-15 16:52:05 +01:00
pub async fn get_index(&self, uid: String) -> anyhow::Result<IndexMetadata> {
2021-03-11 22:47:29 +01:00
let uuid = self.uuid_resolver.resolve(uid.clone()).await?;
2021-03-15 18:11:10 +01:00
let meta = self.index_handle.get_index_meta(uuid).await?;
2021-03-15 18:35:16 +01:00
let meta = IndexMetadata { name: uid.clone(), uid, meta };
2021-03-15 16:52:05 +01:00
Ok(meta)
2021-03-06 20:17:58 +01:00
}
2021-03-04 12:03:06 +01:00
}
2021-03-06 12:57:56 +01: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;
}
}
}
}