2021-05-11 02:25:09 +08:00
|
|
|
use std::collections::BTreeMap;
|
2021-09-15 00:39:02 +08:00
|
|
|
use std::path::{Path, PathBuf};
|
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-09-15 00:39:02 +08:00
|
|
|
use actix_web::error::PayloadError;
|
|
|
|
use bytes::Bytes;
|
2021-05-11 02:24:14 +08:00
|
|
|
use chrono::{DateTime, Utc};
|
2021-09-15 00:39:02 +08:00
|
|
|
use futures::Stream;
|
2021-03-24 18:29:11 +08:00
|
|
|
use log::info;
|
2021-09-15 00:39:02 +08:00
|
|
|
use milli::update::IndexDocumentsMethod;
|
2021-03-16 01:11:10 +08:00
|
|
|
use serde::{Deserialize, Serialize};
|
2021-09-24 17:53:11 +08:00
|
|
|
use tokio::task::spawn_blocking;
|
2021-03-06 19:57:56 +08:00
|
|
|
use tokio::time::sleep;
|
2021-03-18 16:09:26 +08:00
|
|
|
use uuid::Uuid;
|
2021-01-16 22:09:48 +08:00
|
|
|
|
2021-05-11 02:25:09 +08:00
|
|
|
use dump_actor::DumpActorHandle;
|
2021-05-27 02:42:09 +08:00
|
|
|
pub use dump_actor::{DumpInfo, DumpStatus};
|
2021-09-15 00:39:02 +08:00
|
|
|
use snapshot::load_snapshot;
|
2021-04-01 22:44:42 +08:00
|
|
|
|
2021-09-24 20:55:57 +08:00
|
|
|
use crate::index::{Checked, Document, IndexMeta, IndexStats, SearchQuery, SearchResult, Settings, Unchecked};
|
2021-09-24 17:53:11 +08:00
|
|
|
use crate::index_controller::index_resolver::create_index_resolver;
|
2021-09-22 21:07:04 +08:00
|
|
|
use crate::options::IndexerOpts;
|
2021-06-23 20:48:33 +08:00
|
|
|
use error::Result;
|
2021-09-24 17:53:11 +08:00
|
|
|
use crate::index::error::Result as IndexResult;
|
2021-04-01 22:44:42 +08:00
|
|
|
|
2021-05-27 20:30:20 +08:00
|
|
|
use self::dump_actor::load_dump;
|
2021-09-24 17:53:11 +08:00
|
|
|
use self::index_resolver::HardStateIndexResolver;
|
|
|
|
use self::index_resolver::error::IndexResolverError;
|
2021-09-22 17:52:29 +08:00
|
|
|
use self::updates::status::UpdateStatus;
|
2021-09-22 21:07:04 +08:00
|
|
|
use self::updates::UpdateMsg;
|
2021-05-27 20:30:20 +08:00
|
|
|
|
2021-05-27 02:42:09 +08:00
|
|
|
mod dump_actor;
|
2021-06-15 23:39:07 +08:00
|
|
|
pub mod error;
|
2021-09-24 17:53:11 +08:00
|
|
|
//pub mod indexes;
|
2021-04-01 22:44:42 +08:00
|
|
|
mod snapshot;
|
2021-09-22 21:07:04 +08:00
|
|
|
pub mod update_file_store;
|
2021-09-22 17:52:29 +08:00
|
|
|
pub mod updates;
|
2021-09-24 17:53:11 +08:00
|
|
|
//mod uuid_resolver;
|
|
|
|
mod index_resolver;
|
2021-09-15 00:39:02 +08:00
|
|
|
|
2021-09-22 21:07:04 +08:00
|
|
|
pub type Payload = Box<
|
|
|
|
dyn Stream<Item = std::result::Result<Bytes, PayloadError>> + Send + Sync + 'static + Unpin,
|
|
|
|
>;
|
2021-02-02 02:51:47 +08:00
|
|
|
|
2021-09-24 17:53:11 +08:00
|
|
|
macro_rules! time {
|
|
|
|
($e:expr) => {
|
|
|
|
{
|
|
|
|
let now = std::time::Instant::now();
|
|
|
|
let result = $e;
|
|
|
|
let elapsed = now.elapsed();
|
|
|
|
println!("elapsed at line {}: {}ms ({}ns)", line!(), elapsed.as_millis(), elapsed.as_nanos());
|
|
|
|
result
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2021-02-04 00:44:20 +08:00
|
|
|
#[derive(Debug, Serialize, Deserialize, Clone)]
|
|
|
|
#[serde(rename_all = "camelCase")]
|
|
|
|
pub struct IndexMetadata {
|
2021-04-15 00:55:04 +08:00
|
|
|
#[serde(skip)]
|
|
|
|
pub uuid: Uuid,
|
2021-04-01 22:44:42 +08:00
|
|
|
pub uid: String,
|
2021-03-16 01:35:16 +08:00
|
|
|
name: String,
|
2021-03-07 03:12:20 +08:00
|
|
|
#[serde(flatten)]
|
2021-09-24 17:53:11 +08:00
|
|
|
pub meta: IndexMeta,
|
2021-02-04 00:44:20 +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
|
|
|
|
2021-05-11 02:25:09 +08:00
|
|
|
#[derive(Clone)]
|
2021-03-04 19:03:06 +08:00
|
|
|
pub struct IndexController {
|
2021-09-24 17:53:11 +08:00
|
|
|
index_resolver: Arc<HardStateIndexResolver>,
|
2021-09-22 17:52:29 +08:00
|
|
|
update_handle: updates::UpdateSender,
|
2021-05-11 02:25:09 +08:00
|
|
|
dump_handle: dump_actor::DumpActorHandleImpl,
|
2021-03-04 19:03:06 +08:00
|
|
|
}
|
|
|
|
|
2021-09-22 21:07:04 +08:00
|
|
|
#[derive(Debug)]
|
2021-09-15 00:39:02 +08:00
|
|
|
pub enum DocumentAdditionFormat {
|
|
|
|
Json,
|
|
|
|
}
|
|
|
|
|
2021-06-23 18:18:34 +08:00
|
|
|
#[derive(Serialize, Debug)]
|
2021-04-16 01:54:25 +08:00
|
|
|
#[serde(rename_all = "camelCase")]
|
2021-04-15 00:55:04 +08:00
|
|
|
pub struct Stats {
|
|
|
|
pub database_size: u64,
|
|
|
|
pub last_update: Option<DateTime<Utc>>,
|
|
|
|
pub indexes: BTreeMap<String, IndexStats>,
|
|
|
|
}
|
|
|
|
|
2021-09-22 21:07:04 +08:00
|
|
|
#[derive(derivative::Derivative)]
|
|
|
|
#[derivative(Debug)]
|
2021-09-15 00:39:02 +08:00
|
|
|
pub enum Update {
|
2021-09-24 21:21:07 +08:00
|
|
|
DeleteDocuments(Vec<String>),
|
|
|
|
ClearDocuments,
|
2021-09-24 20:55:57 +08:00
|
|
|
Settings(Settings<Unchecked>),
|
2021-09-15 00:39:02 +08:00
|
|
|
DocumentAddition {
|
2021-09-22 21:07:04 +08:00
|
|
|
#[derivative(Debug="ignore")]
|
2021-09-15 00:39:02 +08:00
|
|
|
payload: Payload,
|
|
|
|
primary_key: Option<String>,
|
|
|
|
method: IndexDocumentsMethod,
|
|
|
|
format: DocumentAdditionFormat,
|
2021-09-22 21:07:04 +08:00
|
|
|
},
|
2021-09-15 00:39:02 +08:00
|
|
|
}
|
|
|
|
|
2021-09-21 19:23:22 +08:00
|
|
|
#[derive(Default, Debug)]
|
|
|
|
pub struct IndexControllerBuilder {
|
|
|
|
max_index_size: Option<usize>,
|
|
|
|
max_update_store_size: Option<usize>,
|
|
|
|
snapshot_dir: Option<PathBuf>,
|
|
|
|
import_snapshot: Option<PathBuf>,
|
|
|
|
ignore_snapshot_if_db_exists: bool,
|
|
|
|
ignore_missing_snapshot: bool,
|
|
|
|
dump_src: Option<PathBuf>,
|
|
|
|
dump_dst: Option<PathBuf>,
|
|
|
|
}
|
2021-03-17 19:01:56 +08:00
|
|
|
|
2021-09-21 19:23:22 +08:00
|
|
|
impl IndexControllerBuilder {
|
2021-09-22 21:07:04 +08:00
|
|
|
pub fn build(
|
|
|
|
self,
|
|
|
|
db_path: impl AsRef<Path>,
|
|
|
|
indexer_options: IndexerOpts,
|
|
|
|
) -> anyhow::Result<IndexController> {
|
|
|
|
let index_size = self
|
|
|
|
.max_index_size
|
|
|
|
.ok_or_else(|| anyhow::anyhow!("Missing index size"))?;
|
|
|
|
let update_store_size = self
|
|
|
|
.max_index_size
|
|
|
|
.ok_or_else(|| anyhow::anyhow!("Missing update database size"))?;
|
2021-09-21 19:23:22 +08:00
|
|
|
|
|
|
|
if let Some(ref path) = self.import_snapshot {
|
2021-03-24 18:29:11 +08:00
|
|
|
info!("Loading from snapshot {:?}", path);
|
2021-03-23 23:37:46 +08:00
|
|
|
load_snapshot(
|
2021-09-21 19:23:22 +08:00
|
|
|
db_path.as_ref(),
|
2021-03-23 23:37:46 +08:00
|
|
|
path,
|
2021-09-21 19:23:22 +08:00
|
|
|
self.ignore_snapshot_if_db_exists,
|
|
|
|
self.ignore_missing_snapshot,
|
2021-03-23 23:37:46 +08:00
|
|
|
)?;
|
2021-09-21 19:23:22 +08:00
|
|
|
} else if let Some(ref src_path) = self.dump_src {
|
2021-05-27 20:30:20 +08:00
|
|
|
load_dump(
|
2021-09-21 19:23:22 +08:00
|
|
|
db_path.as_ref(),
|
2021-05-27 20:30:20 +08:00
|
|
|
src_path,
|
2021-09-21 19:23:22 +08:00
|
|
|
index_size,
|
|
|
|
update_store_size,
|
|
|
|
&indexer_options,
|
2021-05-27 20:30:20 +08:00
|
|
|
)?;
|
2021-03-23 02:19:37 +08:00
|
|
|
}
|
|
|
|
|
2021-09-21 19:23:22 +08:00
|
|
|
std::fs::create_dir_all(db_path.as_ref())?;
|
2021-03-24 00:23:57 +08:00
|
|
|
|
2021-09-24 17:53:11 +08:00
|
|
|
let index_resolver = Arc::new(create_index_resolver(&db_path, index_size, &indexer_options)?);
|
2021-09-22 17:52:29 +08:00
|
|
|
|
|
|
|
#[allow(unreachable_code)]
|
2021-09-24 17:53:11 +08:00
|
|
|
let update_handle = updates::create_update_handler(index_resolver.clone(), &db_path, update_store_size)?;
|
2021-09-21 19:23:22 +08:00
|
|
|
|
2021-09-24 17:53:11 +08:00
|
|
|
let dump_path = self.dump_dst.ok_or_else(|| anyhow::anyhow!("Missing dump directory path"))?;
|
2021-05-27 02:42:09 +08:00
|
|
|
let dump_handle = dump_actor::DumpActorHandleImpl::new(
|
2021-09-24 17:53:11 +08:00
|
|
|
dump_path,
|
|
|
|
index_resolver.clone(),
|
2021-05-27 02:42:09 +08:00
|
|
|
update_handle.clone(),
|
2021-09-21 19:23:22 +08:00
|
|
|
index_size,
|
|
|
|
update_store_size,
|
2021-05-27 02:42:09 +08:00
|
|
|
)?;
|
2021-03-17 19:01:56 +08:00
|
|
|
|
2021-09-15 00:39:02 +08:00
|
|
|
//if options.schedule_snapshot {
|
2021-09-22 21:07:04 +08:00
|
|
|
//let snapshot_service = SnapshotService::new(
|
|
|
|
//uuid_resolver.clone(),
|
|
|
|
//update_handle.clone(),
|
|
|
|
//Duration::from_secs(options.snapshot_interval_sec),
|
|
|
|
//options.snapshot_dir.clone(),
|
|
|
|
//options
|
|
|
|
//.db_path
|
|
|
|
//.file_name()
|
|
|
|
//.map(|n| n.to_owned().into_string().expect("invalid path"))
|
|
|
|
//.unwrap_or_else(|| String::from("data.ms")),
|
|
|
|
//);
|
|
|
|
|
|
|
|
//tokio::task::spawn(snapshot_service.run());
|
2021-09-15 00:39:02 +08:00
|
|
|
//}
|
2021-03-17 19:01:56 +08:00
|
|
|
|
2021-09-21 19:23:22 +08:00
|
|
|
Ok(IndexController {
|
2021-09-24 17:53:11 +08:00
|
|
|
index_resolver,
|
2021-03-16 01:11:10 +08:00
|
|
|
update_handle,
|
2021-05-11 02:25:09 +08:00
|
|
|
dump_handle,
|
2021-03-16 01:11:10 +08:00
|
|
|
})
|
2021-03-04 19:03:06 +08:00
|
|
|
}
|
|
|
|
|
2021-09-21 19:23:22 +08:00
|
|
|
/// Set the index controller builder's max update store size.
|
|
|
|
pub fn set_max_update_store_size(&mut self, max_update_store_size: usize) -> &mut Self {
|
|
|
|
self.max_update_store_size.replace(max_update_store_size);
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn set_max_index_size(&mut self, size: usize) -> &mut Self {
|
|
|
|
self.max_index_size.replace(size);
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Set the index controller builder's snapshot path.
|
|
|
|
pub fn set_snapshot_dir(&mut self, snapshot_dir: PathBuf) -> &mut Self {
|
|
|
|
self.snapshot_dir.replace(snapshot_dir);
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Set the index controller builder's ignore snapshot if db exists.
|
2021-09-22 21:07:04 +08:00
|
|
|
pub fn set_ignore_snapshot_if_db_exists(
|
|
|
|
&mut self,
|
|
|
|
ignore_snapshot_if_db_exists: bool,
|
|
|
|
) -> &mut Self {
|
2021-09-21 19:23:22 +08:00
|
|
|
self.ignore_snapshot_if_db_exists = ignore_snapshot_if_db_exists;
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Set the index controller builder's ignore missing snapshot.
|
|
|
|
pub fn set_ignore_missing_snapshot(&mut self, ignore_missing_snapshot: bool) -> &mut Self {
|
|
|
|
self.ignore_missing_snapshot = ignore_missing_snapshot;
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Set the index controller builder's dump src.
|
|
|
|
pub fn set_dump_src(&mut self, dump_src: PathBuf) -> &mut Self {
|
|
|
|
self.dump_src.replace(dump_src);
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Set the index controller builder's dump dst.
|
|
|
|
pub fn set_dump_dst(&mut self, dump_dst: PathBuf) -> &mut Self {
|
|
|
|
self.dump_dst.replace(dump_dst);
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Set the index controller builder's import snapshot.
|
|
|
|
pub fn set_import_snapshot(&mut self, import_snapshot: PathBuf) -> &mut Self {
|
|
|
|
self.import_snapshot.replace(import_snapshot);
|
|
|
|
self
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl IndexController {
|
|
|
|
pub fn builder() -> IndexControllerBuilder {
|
|
|
|
IndexControllerBuilder::default()
|
|
|
|
}
|
|
|
|
|
2021-09-24 20:55:57 +08:00
|
|
|
pub async fn register_update(&self, uid: String, update: Update) -> Result<UpdateStatus> {
|
|
|
|
match self.index_resolver.get_uuid(uid).await {
|
2021-09-15 00:39:02 +08:00
|
|
|
Ok(uuid) => {
|
2021-09-22 17:52:29 +08:00
|
|
|
let update_result = UpdateMsg::update(&self.update_handle, uuid, update).await?;
|
2021-09-15 00:39:02 +08:00
|
|
|
Ok(update_result)
|
2021-09-22 21:07:04 +08:00
|
|
|
}
|
2021-09-24 17:53:11 +08:00
|
|
|
Err(IndexResolverError::UnexistingIndex(name)) => {
|
|
|
|
let (uuid, _) = self.index_resolver.create_index(name, None).await?;
|
2021-09-22 17:52:29 +08:00
|
|
|
let update_result = UpdateMsg::update(&self.update_handle, uuid, update).await?;
|
2021-04-22 16:14:29 +08:00
|
|
|
// ignore if index creation fails now, since it may already have been created
|
2021-09-22 16:49:59 +08:00
|
|
|
|
2021-09-15 00:39:02 +08:00
|
|
|
Ok(update_result)
|
2021-03-04 19:03:06 +08:00
|
|
|
}
|
2021-03-18 16:09:26 +08:00
|
|
|
Err(e) => Err(e.into()),
|
|
|
|
}
|
2021-03-04 19:03:06 +08:00
|
|
|
}
|
|
|
|
|
2021-09-15 00:39:02 +08:00
|
|
|
//pub async fn add_documents(
|
2021-09-22 21:07:04 +08:00
|
|
|
//&self,
|
|
|
|
//uid: String,
|
|
|
|
//method: milli::update::IndexDocumentsMethod,
|
|
|
|
//payload: Payload,
|
|
|
|
//primary_key: Option<String>,
|
2021-09-15 00:39:02 +08:00
|
|
|
//) -> Result<UpdateStatus> {
|
2021-09-22 21:07:04 +08:00
|
|
|
//let perform_update = |uuid| async move {
|
|
|
|
//let meta = UpdateMeta::DocumentsAddition {
|
|
|
|
//method,
|
|
|
|
//primary_key,
|
|
|
|
//};
|
|
|
|
//let (sender, receiver) = mpsc::channel(10);
|
|
|
|
|
|
|
|
//// It is necessary to spawn a local task to send 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 {
|
|
|
|
//payload
|
|
|
|
//.for_each(|r| async {
|
|
|
|
//let _ = sender.send(r).await;
|
|
|
|
//})
|
|
|
|
//.await
|
|
|
|
//});
|
|
|
|
|
|
|
|
//// This must be done *AFTER* spawning the task.
|
|
|
|
//self.update_handle.update(meta, receiver, uuid).await
|
|
|
|
//};
|
|
|
|
|
|
|
|
//match self.uuid_resolver.get(uid).await {
|
|
|
|
//Ok(uuid) => Ok(perform_update(uuid).await?),
|
|
|
|
//Err(UuidResolverError::UnexistingIndex(name)) => {
|
|
|
|
//let uuid = Uuid::new_v4();
|
|
|
|
//let status = perform_update(uuid).await?;
|
|
|
|
//// ignore if index creation fails now, since it may already have been created
|
|
|
|
//let _ = self.index_handle.create_index(uuid, None).await;
|
|
|
|
//self.uuid_resolver.insert(name, uuid).await?;
|
|
|
|
//Ok(status)
|
|
|
|
//}
|
|
|
|
//Err(e) => Err(e.into()),
|
|
|
|
//}
|
2021-09-15 00:39:02 +08:00
|
|
|
//}
|
|
|
|
|
|
|
|
//pub async fn clear_documents(&self, uid: String) -> Result<UpdateStatus> {
|
2021-09-22 21:07:04 +08:00
|
|
|
//let uuid = self.uuid_resolver.get(uid).await?;
|
|
|
|
//let meta = UpdateMeta::ClearDocuments;
|
|
|
|
//let (_, receiver) = mpsc::channel(1);
|
|
|
|
//let status = self.update_handle.update(meta, receiver, uuid).await?;
|
|
|
|
//Ok(status)
|
2021-09-15 00:39:02 +08:00
|
|
|
//}
|
|
|
|
|
|
|
|
//pub async fn delete_documents(
|
2021-09-22 21:07:04 +08:00
|
|
|
//&self,
|
|
|
|
//uid: String,
|
|
|
|
//documents: Vec<String>,
|
2021-09-15 00:39:02 +08:00
|
|
|
//) -> Result<UpdateStatus> {
|
2021-09-22 21:07:04 +08:00
|
|
|
//let uuid = self.uuid_resolver.get(uid).await?;
|
|
|
|
//let meta = UpdateMeta::DeleteDocuments { ids: documents };
|
|
|
|
//let (_, receiver) = mpsc::channel(1);
|
|
|
|
//let status = self.update_handle.update(meta, receiver, uuid).await?;
|
|
|
|
//Ok(status)
|
2021-09-15 00:39:02 +08:00
|
|
|
//}
|
|
|
|
|
|
|
|
//pub async fn update_settings(
|
2021-09-22 21:07:04 +08:00
|
|
|
//&self,
|
|
|
|
//uid: String,
|
|
|
|
//settings: Settings<Checked>,
|
|
|
|
//create: bool,
|
2021-09-15 00:39:02 +08:00
|
|
|
//) -> Result<UpdateStatus> {
|
2021-09-22 21:07:04 +08:00
|
|
|
//let perform_udpate = |uuid| async move {
|
|
|
|
//let meta = UpdateMeta::Settings(settings.into_unchecked());
|
|
|
|
//// Nothing so send, drop the sender right away, as not to block the update actor.
|
|
|
|
//let (_, receiver) = mpsc::channel(1);
|
|
|
|
//self.update_handle.update(meta, receiver, uuid).await
|
|
|
|
//};
|
|
|
|
|
|
|
|
//match self.uuid_resolver.get(uid).await {
|
|
|
|
//Ok(uuid) => Ok(perform_udpate(uuid).await?),
|
|
|
|
//Err(UuidResolverError::UnexistingIndex(name)) if create => {
|
|
|
|
//let uuid = Uuid::new_v4();
|
|
|
|
//let status = perform_udpate(uuid).await?;
|
|
|
|
//// ignore if index creation fails now, since it may already have been created
|
|
|
|
//let _ = self.index_handle.create_index(uuid, None).await;
|
|
|
|
//self.uuid_resolver.insert(name, uuid).await?;
|
|
|
|
//Ok(status)
|
|
|
|
//}
|
|
|
|
//Err(e) => Err(e.into()),
|
|
|
|
//}
|
2021-09-15 00:39:02 +08:00
|
|
|
//}
|
|
|
|
|
|
|
|
//pub async fn create_index(&self, index_settings: IndexSettings) -> Result<IndexMetadata> {
|
2021-09-22 21:07:04 +08:00
|
|
|
//let IndexSettings { uid, primary_key } = index_settings;
|
|
|
|
//let uid = uid.ok_or(IndexControllerError::MissingUid)?;
|
|
|
|
//let uuid = Uuid::new_v4();
|
|
|
|
//let meta = self.index_handle.create_index(uuid, primary_key).await?;
|
|
|
|
//self.uuid_resolver.insert(uid.clone(), uuid).await?;
|
|
|
|
//let meta = IndexMetadata {
|
|
|
|
//uuid,
|
|
|
|
//name: uid.clone(),
|
|
|
|
//uid,
|
|
|
|
//meta,
|
|
|
|
//};
|
|
|
|
|
|
|
|
//Ok(meta)
|
2021-09-15 00:39:02 +08:00
|
|
|
//}
|
|
|
|
|
|
|
|
//pub async fn delete_index(&self, uid: String) -> Result<()> {
|
2021-09-22 21:07:04 +08:00
|
|
|
//let uuid = self.uuid_resolver.delete(uid).await?;
|
|
|
|
|
|
|
|
//// We remove the index from the resolver synchronously, and effectively perform the index
|
|
|
|
//// deletion as a background task.
|
|
|
|
//let update_handle = self.update_handle.clone();
|
|
|
|
//let index_handle = self.index_handle.clone();
|
|
|
|
//tokio::spawn(async move {
|
|
|
|
//if let Err(e) = update_handle.delete(uuid).await {
|
|
|
|
//error!("Error while deleting index: {}", e);
|
|
|
|
//}
|
|
|
|
//if let Err(e) = index_handle.delete(uuid).await {
|
|
|
|
//error!("Error while deleting index: {}", e);
|
|
|
|
//}
|
|
|
|
//});
|
|
|
|
|
|
|
|
//Ok(())
|
2021-09-15 00:39:02 +08:00
|
|
|
//}
|
2021-03-04 19:03:06 +08:00
|
|
|
|
2021-06-15 03:26:35 +08:00
|
|
|
pub async fn update_status(&self, uid: String, id: u64) -> Result<UpdateStatus> {
|
2021-09-24 17:53:11 +08:00
|
|
|
let uuid = self.index_resolver.get_uuid(uid).await?;
|
2021-09-22 17:52:29 +08:00
|
|
|
let result = UpdateMsg::get_update(&self.update_handle, uuid, id).await?;
|
2021-03-06 17:51:52 +08:00
|
|
|
Ok(result)
|
2021-03-04 19:03:06 +08:00
|
|
|
}
|
|
|
|
|
2021-06-15 03:26:35 +08:00
|
|
|
pub async fn all_update_status(&self, uid: String) -> Result<Vec<UpdateStatus>> {
|
2021-09-24 17:53:11 +08:00
|
|
|
let uuid = self.index_resolver.get_uuid(uid).await?;
|
2021-09-22 17:52:29 +08:00
|
|
|
let result = UpdateMsg::list_updates(&self.update_handle, uuid).await?;
|
2021-03-06 01:34:04 +08:00
|
|
|
Ok(result)
|
2021-03-04 19:03:06 +08:00
|
|
|
}
|
|
|
|
|
2021-06-15 03:26:35 +08:00
|
|
|
pub async fn list_indexes(&self) -> Result<Vec<IndexMetadata>> {
|
2021-09-24 17:53:11 +08:00
|
|
|
let indexes = self.index_resolver.list().await?;
|
2021-03-07 03:12:20 +08:00
|
|
|
let mut ret = Vec::new();
|
2021-09-24 17:53:11 +08:00
|
|
|
for (uid, index) in indexes {
|
|
|
|
let meta = index.meta()?;
|
2021-03-22 17:17:38 +08:00
|
|
|
let meta = IndexMetadata {
|
2021-09-24 17:53:11 +08:00
|
|
|
uuid: index.uuid,
|
2021-03-22 17:17:38 +08:00
|
|
|
name: uid.clone(),
|
|
|
|
uid,
|
|
|
|
meta,
|
|
|
|
};
|
2021-03-15 23:52:05 +08:00
|
|
|
ret.push(meta);
|
2021-03-07 03:12:20 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
Ok(ret)
|
2021-03-04 19:03:06 +08:00
|
|
|
}
|
|
|
|
|
2021-06-15 03:26:35 +08:00
|
|
|
pub async fn settings(&self, uid: String) -> Result<Settings<Checked>> {
|
2021-09-24 17:53:11 +08:00
|
|
|
let index = self.index_resolver.get_index(uid).await?;
|
|
|
|
let settings = spawn_blocking(move || index.settings()).await??;
|
2021-03-04 19:38:55 +08:00
|
|
|
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>>,
|
2021-06-15 03:26:35 +08:00
|
|
|
) -> Result<Vec<Document>> {
|
2021-09-24 17:53:11 +08:00
|
|
|
let index = self.index_resolver.get_index(uid).await?;
|
|
|
|
let documents = spawn_blocking(move || index.retrieve_documents(offset, limit, attributes_to_retrieve)).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>>,
|
2021-06-15 03:26:35 +08:00
|
|
|
) -> Result<Document> {
|
2021-09-24 17:53:11 +08:00
|
|
|
let index = self.index_resolver.get_index(uid).await?;
|
|
|
|
let document = spawn_blocking(move || index.retrieve_document(doc_id, attributes_to_retrieve)).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,
|
2021-06-21 19:57:32 +08:00
|
|
|
mut index_settings: IndexSettings,
|
2021-06-15 03:26:35 +08:00
|
|
|
) -> Result<IndexMetadata> {
|
2021-03-12 21:48:43 +08:00
|
|
|
|
2021-09-24 17:53:11 +08:00
|
|
|
index_settings.uid.take();
|
|
|
|
|
|
|
|
let index = self.index_resolver.get_index(uid.clone()).await?;
|
|
|
|
let uuid = index.uuid;
|
|
|
|
let meta = spawn_blocking(move || index.update_primary_key(index_settings.primary_key)).await??;
|
2021-03-22 17:17:38 +08:00
|
|
|
let meta = IndexMetadata {
|
2021-04-15 00:55:04 +08:00
|
|
|
uuid,
|
2021-03-22 17:17:38 +08:00
|
|
|
name: uid.clone(),
|
|
|
|
uid,
|
|
|
|
meta,
|
|
|
|
};
|
2021-03-12 21:48:43 +08:00
|
|
|
Ok(meta)
|
2021-03-04 19:03:06 +08:00
|
|
|
}
|
|
|
|
|
2021-06-15 03:26:35 +08:00
|
|
|
pub async fn search(&self, uid: String, query: SearchQuery) -> Result<SearchResult> {
|
2021-09-24 17:53:11 +08:00
|
|
|
let index = time!(self.index_resolver.get_index(uid.clone()).await?);
|
|
|
|
let result = time!(spawn_blocking(move || time!(index.perform_search(query))).await??);
|
2021-03-04 19:03:06 +08:00
|
|
|
Ok(result)
|
|
|
|
}
|
2021-03-07 03:17:58 +08:00
|
|
|
|
2021-06-15 03:26:35 +08:00
|
|
|
pub async fn get_index(&self, uid: String) -> Result<IndexMetadata> {
|
2021-09-24 17:53:11 +08:00
|
|
|
let index = self.index_resolver.get_index(uid.clone()).await?;
|
|
|
|
let uuid = index.uuid;
|
|
|
|
let meta = spawn_blocking(move || index.meta()).await??;
|
2021-03-22 17:17:38 +08:00
|
|
|
let meta = IndexMetadata {
|
2021-04-15 00:55:04 +08:00
|
|
|
uuid,
|
2021-03-22 17:17:38 +08:00
|
|
|
name: uid.clone(),
|
|
|
|
uid,
|
|
|
|
meta,
|
|
|
|
};
|
2021-03-15 23:52:05 +08:00
|
|
|
Ok(meta)
|
2021-03-07 03:17:58 +08:00
|
|
|
}
|
2021-04-01 22:44:42 +08:00
|
|
|
|
2021-06-15 03:26:35 +08:00
|
|
|
pub async fn get_uuids_size(&self) -> Result<u64> {
|
2021-09-24 17:53:11 +08:00
|
|
|
let size = self.index_resolver.get_size().await?;
|
2021-09-22 16:49:59 +08:00
|
|
|
Ok(size)
|
2021-04-01 22:44:42 +08:00
|
|
|
}
|
2021-04-09 20:41:24 +08:00
|
|
|
|
2021-06-15 03:26:35 +08:00
|
|
|
pub async fn get_index_stats(&self, uid: String) -> Result<IndexStats> {
|
2021-09-22 17:52:29 +08:00
|
|
|
let update_infos = UpdateMsg::get_info(&self.update_handle).await?;
|
2021-09-24 17:53:11 +08:00
|
|
|
let index = self.index_resolver.get_index(uid).await?;
|
|
|
|
let uuid = index.uuid;
|
|
|
|
let mut stats = spawn_blocking(move || index.stats()).await??;
|
|
|
|
// Check if the currently indexing update is from our index.
|
2021-04-22 16:14:29 +08:00
|
|
|
stats.is_indexing = Some(Some(uuid) == update_infos.processing);
|
2021-04-15 00:55:04 +08:00
|
|
|
Ok(stats)
|
2021-04-09 20:41:24 +08:00
|
|
|
}
|
|
|
|
|
2021-06-15 03:26:35 +08:00
|
|
|
pub async fn get_all_stats(&self) -> Result<Stats> {
|
2021-09-22 17:52:29 +08:00
|
|
|
let update_infos = UpdateMsg::get_info(&self.update_handle).await?;
|
2021-04-15 00:55:04 +08:00
|
|
|
let mut database_size = self.get_uuids_size().await? + update_infos.size;
|
|
|
|
let mut last_update: Option<DateTime<_>> = None;
|
|
|
|
let mut indexes = BTreeMap::new();
|
|
|
|
|
2021-09-24 17:53:11 +08:00
|
|
|
for (index_uid, index) in self.index_resolver.list().await? {
|
|
|
|
let uuid = index.uuid;
|
|
|
|
let (mut stats, meta) = spawn_blocking::<_, IndexResult<_>>(move || {
|
|
|
|
let stats = index.stats()?;
|
|
|
|
let meta = index.meta()?;
|
|
|
|
Ok((stats, meta))
|
|
|
|
}).await??;
|
|
|
|
|
|
|
|
database_size += stats.size;
|
2021-04-15 00:55:04 +08:00
|
|
|
|
2021-09-24 17:53:11 +08:00
|
|
|
last_update = last_update.map_or(Some(meta.updated_at), |last| {
|
|
|
|
Some(last.max(meta.updated_at))
|
2021-04-15 00:55:04 +08:00
|
|
|
});
|
|
|
|
|
2021-09-24 17:53:11 +08:00
|
|
|
// Check if the currently indexing update is from our index.
|
|
|
|
stats.is_indexing = Some(Some(uuid) == update_infos.processing);
|
2021-04-15 00:55:04 +08:00
|
|
|
|
2021-09-24 17:53:11 +08:00
|
|
|
indexes.insert(index_uid, stats);
|
2021-04-15 00:55:04 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
Ok(Stats {
|
|
|
|
database_size,
|
|
|
|
last_update,
|
|
|
|
indexes,
|
|
|
|
})
|
2021-04-09 20:41:24 +08:00
|
|
|
}
|
2021-05-11 02:25:09 +08:00
|
|
|
|
2021-06-15 03:26:35 +08:00
|
|
|
pub async fn create_dump(&self) -> Result<DumpInfo> {
|
2021-05-11 02:25:09 +08:00
|
|
|
Ok(self.dump_handle.create_dump().await?)
|
|
|
|
}
|
|
|
|
|
2021-06-15 03:26:35 +08:00
|
|
|
pub async fn dump_info(&self, uid: String) -> Result<DumpInfo> {
|
2021-05-11 02:25:09 +08:00
|
|
|
Ok(self.dump_handle.dump_info(uid).await?)
|
|
|
|
}
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-09-08 17:43:10 +08:00
|
|
|
|
|
|
|
/// Parses the v1 version of the Asc ranking rules `asc(price)`and returns the field name.
|
|
|
|
pub fn asc_ranking_rule(text: &str) -> Option<&str> {
|
|
|
|
text.split_once("asc(")
|
|
|
|
.and_then(|(_, tail)| tail.rsplit_once(")"))
|
|
|
|
.map(|(field, _)| field)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Parses the v1 version of the Desc ranking rules `asc(price)`and returns the field name.
|
|
|
|
pub fn desc_ranking_rule(text: &str) -> Option<&str> {
|
|
|
|
text.split_once("desc(")
|
|
|
|
.and_then(|(_, tail)| tail.rsplit_once(")"))
|
|
|
|
.map(|(field, _)| field)
|
|
|
|
}
|
2021-09-15 00:39:02 +08:00
|
|
|
|
|
|
|
fn update_files_path(path: impl AsRef<Path>) -> PathBuf {
|
|
|
|
path.as_ref().join("updates/updates_files")
|
|
|
|
}
|