mod actor; mod handle_impl; mod message; mod store; use std::path::PathBuf; use chrono::{DateTime, Utc}; use serde::{Deserialize, Serialize}; use thiserror::Error; use uuid::Uuid; use super::IndexSettings; use crate::index::UpdateResult as UResult; use crate::index::{Document, Index, SearchQuery, SearchResult, Settings}; use crate::index_controller::{ updates::{Failed, Processed, Processing}, UpdateMeta, }; use message::IndexMsg; use store::{IndexStore, MapIndexStore}; use actor::IndexActor; pub use handle_impl::IndexActorHandleImpl; pub type Result = std::result::Result; type UpdateResult = std::result::Result, Failed>; #[derive(Debug, Serialize, Deserialize, Clone)] #[serde(rename_all = "camelCase")] pub struct IndexMeta { created_at: DateTime, updated_at: DateTime, primary_key: Option, } impl IndexMeta { fn new(index: &Index) -> Result { let txn = index.read_txn()?; Self::new_txn(index, &txn) } fn new_txn(index: &Index, txn: &heed::RoTxn) -> Result { 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] pub trait IndexActorHandle: Sync + Send + Clone { async fn create_index(&self, uuid: Uuid, primary_key: Option) -> Result; async fn update( &self, meta: Processing, data: std::fs::File, ) -> anyhow::Result; async fn search(&self, uuid: Uuid, query: SearchQuery) -> Result; async fn settings(&self, uuid: Uuid) -> Result; async fn documents( &self, uuid: Uuid, offset: usize, limit: usize, attributes_to_retrieve: Option>, ) -> Result>; async fn document( &self, uuid: Uuid, doc_id: String, attributes_to_retrieve: Option>, ) -> Result; async fn delete(&self, uuid: Uuid) -> Result<()>; async fn get_index_meta(&self, uuid: Uuid) -> Result; async fn update_index( &self, uuid: Uuid, index_settings: IndexSettings, ) -> Result; async fn snapshot(&self, uuid: Uuid, path: PathBuf) -> Result<()>; }