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

211 lines
5.9 KiB
Rust
Raw Normal View History

2021-04-22 10:14:29 +02:00
use std::fs::File;
2021-03-23 11:00:50 +01:00
use std::path::PathBuf;
use chrono::{DateTime, Utc};
2021-04-01 16:44:42 +02:00
#[cfg(test)]
use mockall::automock;
2021-03-23 11:00:50 +01:00
use serde::{Deserialize, Serialize};
use thiserror::Error;
use uuid::Uuid;
2021-04-01 16:44:42 +02:00
use actor::IndexActor;
2021-04-14 17:53:12 +02:00
pub use actor::CONCURRENT_INDEX_MSG;
2021-04-01 16:44:42 +02:00
pub use handle_impl::IndexActorHandleImpl;
use message::IndexMsg;
use store::{IndexStore, MapIndexStore};
2021-05-10 17:30:09 +02:00
use crate::index::{Checked, Document, Index, SearchQuery, SearchResult, Settings};
2021-05-31 16:03:39 +02:00
use crate::index_controller::{Failed, IndexStats, Processed, Processing};
2021-03-23 11:00:50 +01:00
2021-04-01 16:44:42 +02:00
use super::IndexSettings;
2021-03-23 11:00:50 +01:00
2021-04-01 16:44:42 +02:00
mod actor;
mod handle_impl;
mod message;
mod store;
2021-03-23 16:19:01 +01:00
2021-04-22 10:14:29 +02:00
pub type IndexResult<T> = std::result::Result<T, IndexError>;
2021-03-23 11:00:50 +01:00
#[derive(Debug, Serialize, Deserialize, Clone)]
#[serde(rename_all = "camelCase")]
pub struct IndexMeta {
created_at: DateTime<Utc>,
2021-04-01 16:44:42 +02:00
pub updated_at: DateTime<Utc>,
2021-05-06 18:44:16 +02:00
pub primary_key: Option<String>,
2021-03-23 11:00:50 +01:00
}
impl IndexMeta {
2021-04-22 10:14:29 +02:00
fn new(index: &Index) -> IndexResult<Self> {
2021-03-23 11:00:50 +01:00
let txn = index.read_txn()?;
Self::new_txn(index, &txn)
}
2021-04-22 10:14:29 +02:00
fn new_txn(index: &Index, txn: &heed::RoTxn) -> IndexResult<Self> {
2021-03-23 11:00:50 +01:00
let created_at = index.created_at(&txn)?;
let updated_at = index.updated_at(&txn)?;
let primary_key = index.primary_key(&txn)?.map(String::from);
2021-05-31 16:03:39 +02:00
Ok(Self {
created_at,
updated_at,
primary_key,
})
2021-03-23 11:00:50 +01:00
}
}
#[derive(Error, Debug)]
pub enum IndexError {
#[error("index already exists")]
IndexAlreadyExists,
#[error("Index doesn't exists")]
UnexistingIndex,
#[error("Existing primary key")]
ExistingPrimaryKey,
2021-05-24 17:20:44 +02:00
#[error("Internal Index Error: {0}")]
2021-05-31 16:03:39 +02:00
Internal(String),
2021-03-23 11:00:50 +01:00
}
2021-05-24 17:20:44 +02:00
macro_rules! internal_error {
($($other:path), *) => {
$(
impl From<$other> for IndexError {
fn from(other: $other) -> Self {
Self::Internal(other.to_string())
}
}
)*
}
}
2021-05-31 16:03:39 +02:00
internal_error!(
anyhow::Error,
heed::Error,
tokio::task::JoinError,
std::io::Error
);
2021-05-24 17:20:44 +02:00
2021-04-15 19:54:25 +02:00
#[async_trait::async_trait]
2021-04-22 10:14:29 +02:00
#[cfg_attr(test, automock)]
pub trait IndexActorHandle {
async fn create_index(&self, uuid: Uuid, primary_key: Option<String>)
-> IndexResult<IndexMeta>;
2021-04-15 19:54:25 +02:00
async fn update(
&self,
uuid: Uuid,
2021-04-22 10:14:29 +02:00
meta: Processing,
data: Option<File>,
) -> anyhow::Result<Result<Processed, Failed>>;
async fn search(&self, uuid: Uuid, query: SearchQuery) -> IndexResult<SearchResult>;
2021-05-10 17:30:09 +02:00
async fn settings(&self, uuid: Uuid) -> IndexResult<Settings<Checked>>;
2021-04-15 19:54:25 +02:00
async fn documents(
&self,
uuid: Uuid,
offset: usize,
limit: usize,
attributes_to_retrieve: Option<Vec<String>>,
2021-04-22 10:14:29 +02:00
) -> IndexResult<Vec<Document>>;
2021-04-15 19:54:25 +02:00
async fn document(
&self,
uuid: Uuid,
doc_id: String,
attributes_to_retrieve: Option<Vec<String>>,
2021-04-22 10:14:29 +02:00
) -> IndexResult<Document>;
async fn delete(&self, uuid: Uuid) -> IndexResult<()>;
async fn get_index_meta(&self, uuid: Uuid) -> IndexResult<IndexMeta>;
async fn update_index(
2021-03-23 11:00:50 +01:00
&self,
2021-04-13 17:14:02 +02:00
uuid: Uuid,
2021-04-22 10:14:29 +02:00
index_settings: IndexSettings,
) -> IndexResult<IndexMeta>;
async fn snapshot(&self, uuid: Uuid, path: PathBuf) -> IndexResult<()>;
2021-05-24 18:16:35 +02:00
async fn dump(&self, uuid: Uuid, path: PathBuf) -> IndexResult<()>;
2021-04-22 10:14:29 +02:00
async fn get_index_stats(&self, uuid: Uuid) -> IndexResult<IndexStats>;
}
2021-03-23 11:00:50 +01:00
2021-04-22 10:14:29 +02:00
#[cfg(test)]
mod test {
use std::sync::Arc;
use super::*;
#[async_trait::async_trait]
/// Useful for passing around an `Arc<MockIndexActorHandle>` in tests.
impl IndexActorHandle for Arc<MockIndexActorHandle> {
async fn create_index(
&self,
uuid: Uuid,
primary_key: Option<String>,
) -> IndexResult<IndexMeta> {
self.as_ref().create_index(uuid, primary_key).await
}
async fn update(
&self,
uuid: Uuid,
meta: Processing,
data: Option<std::fs::File>,
) -> anyhow::Result<Result<Processed, Failed>> {
self.as_ref().update(uuid, meta, data).await
}
async fn search(&self, uuid: Uuid, query: SearchQuery) -> IndexResult<SearchResult> {
self.as_ref().search(uuid, query).await
}
2021-05-10 17:30:09 +02:00
async fn settings(&self, uuid: Uuid) -> IndexResult<Settings<Checked>> {
2021-04-22 10:14:29 +02:00
self.as_ref().settings(uuid).await
}
async fn documents(
&self,
uuid: Uuid,
offset: usize,
limit: usize,
attributes_to_retrieve: Option<Vec<String>>,
) -> IndexResult<Vec<Document>> {
self.as_ref()
.documents(uuid, offset, limit, attributes_to_retrieve)
.await
}
async fn document(
&self,
uuid: Uuid,
doc_id: String,
attributes_to_retrieve: Option<Vec<String>>,
) -> IndexResult<Document> {
self.as_ref()
.document(uuid, doc_id, attributes_to_retrieve)
.await
}
async fn delete(&self, uuid: Uuid) -> IndexResult<()> {
self.as_ref().delete(uuid).await
}
async fn get_index_meta(&self, uuid: Uuid) -> IndexResult<IndexMeta> {
self.as_ref().get_index_meta(uuid).await
}
async fn update_index(
&self,
uuid: Uuid,
index_settings: IndexSettings,
) -> IndexResult<IndexMeta> {
self.as_ref().update_index(uuid, index_settings).await
}
async fn snapshot(&self, uuid: Uuid, path: PathBuf) -> IndexResult<()> {
self.as_ref().snapshot(uuid, path).await
}
2021-05-31 16:03:39 +02:00
async fn dump(&self, uuid: Uuid, path: PathBuf) -> IndexResult<()> {
self.as_ref().dump(uuid, path).await
2021-04-29 14:45:08 +02:00
}
2021-04-22 10:14:29 +02:00
async fn get_index_stats(&self, uuid: Uuid) -> IndexResult<IndexStats> {
self.as_ref().get_index_stats(uuid).await
}
2021-05-10 20:24:14 +02:00
}
2021-03-23 11:00:50 +01:00
}