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

104 lines
3.0 KiB
Rust
Raw Normal View History

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;
pub use handle_impl::IndexActorHandleImpl;
use message::IndexMsg;
use store::{IndexStore, MapIndexStore};
2021-03-23 11:00:50 +01:00
use crate::index::UpdateResult as UResult;
use crate::index::{Document, Index, SearchQuery, SearchResult, Settings};
use crate::index_controller::{
updates::{Failed, Processed, Processing},
2021-04-01 16:44:42 +02:00
IndexStats, UpdateMeta,
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-03-23 11:00:50 +01:00
pub type Result<T> = std::result::Result<T, IndexError>;
type UpdateResult = std::result::Result<Processed<UpdateMeta, UResult>, Failed<UpdateMeta, String>>;
#[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-03-23 11:00:50 +01:00
primary_key: Option<String>,
}
impl IndexMeta {
fn new(index: &Index) -> Result<Self> {
let txn = index.read_txn()?;
Self::new_txn(index, &txn)
}
fn new_txn(index: &Index, txn: &heed::RoTxn) -> Result<Self> {
let created_at = index.created_at(&txn)?;
let updated_at = index.updated_at(&txn)?;
let primary_key = index.primary_key(&txn)?.map(String::from);
Ok(Self {
primary_key,
updated_at,
created_at,
})
}
}
#[derive(Error, Debug)]
pub enum IndexError {
#[error("error with index: {0}")]
Error(#[from] anyhow::Error),
#[error("index already exists")]
IndexAlreadyExists,
#[error("Index doesn't exists")]
UnexistingIndex,
#[error("Heed error: {0}")]
HeedError(#[from] heed::Error),
#[error("Existing primary key")]
ExistingPrimaryKey,
}
#[async_trait::async_trait]
2021-03-23 16:19:01 +01:00
#[cfg_attr(test, automock)]
pub trait IndexActorHandle {
2021-03-23 11:00:50 +01:00
async fn create_index(&self, uuid: Uuid, primary_key: Option<String>) -> Result<IndexMeta>;
async fn update(
&self,
2021-04-13 17:14:02 +02:00
uuid: Uuid,
2021-03-23 11:00:50 +01:00
meta: Processing<UpdateMeta>,
data: std::fs::File,
) -> anyhow::Result<UpdateResult>;
async fn search(&self, uuid: Uuid, query: SearchQuery) -> Result<SearchResult>;
async fn settings(&self, uuid: Uuid) -> Result<Settings>;
async fn documents(
&self,
uuid: Uuid,
offset: usize,
limit: usize,
attributes_to_retrieve: Option<Vec<String>>,
) -> Result<Vec<Document>>;
async fn document(
&self,
uuid: Uuid,
doc_id: String,
attributes_to_retrieve: Option<Vec<String>>,
) -> Result<Document>;
async fn delete(&self, uuid: Uuid) -> Result<()>;
async fn get_index_meta(&self, uuid: Uuid) -> Result<IndexMeta>;
2021-03-24 11:29:11 +01:00
async fn update_index(&self, uuid: Uuid, index_settings: IndexSettings) -> Result<IndexMeta>;
2021-03-23 11:00:50 +01:00
async fn snapshot(&self, uuid: Uuid, path: PathBuf) -> Result<()>;
2021-04-01 16:44:42 +02:00
async fn get_index_stats(&self, uuid: Uuid) -> Result<IndexStats>;
2021-03-23 11:00:50 +01:00
}