fixes after review

This commit is contained in:
Alexey Shekhirin 2021-04-08 16:35:28 +03:00
parent 698a1ea582
commit ae1655586c
No known key found for this signature in database
GPG Key ID: AF9A26AA133B5B98
9 changed files with 56 additions and 89 deletions

16
Cargo.lock generated
View File

@ -1894,22 +1894,6 @@ dependencies = [
"whatlang",
]
[[package]]
name = "meilisearch-tokenizer"
version = "0.1.1"
source = "git+https://github.com/meilisearch/Tokenizer.git?tag=v0.2.0#833c48b2ee39071f8b4f51abd15122afdb3c8c06"
dependencies = [
"character_converter",
"cow-utils",
"deunicode",
"fst",
"jieba-rs",
"once_cell",
"slice-group-by",
"unicode-segmentation",
"whatlang",
]
[[package]]
name = "memchr"
version = "2.3.4"

View File

@ -1827,22 +1827,6 @@ dependencies = [
"vergen",
]
[[package]]
name = "meilisearch-tokenizer"
version = "0.1.1"
source = "git+https://github.com/meilisearch/Tokenizer.git?branch=main#147b6154b1b34cb8f5da2df6a416b7da191bc850"
dependencies = [
"character_converter",
"cow-utils",
"deunicode",
"fst",
"jieba-rs",
"once_cell",
"slice-group-by",
"unicode-segmentation",
"whatlang",
]
[[package]]
name = "memchr"
version = "2.3.4"

View File

@ -4,9 +4,11 @@ use std::sync::Arc;
use sha2::Digest;
use crate::index::Settings;
use crate::index_controller::IndexController;
use crate::index_controller::{IndexController, IndexStats};
use crate::index_controller::{IndexMetadata, IndexSettings};
use crate::option::Opt;
use std::collections::HashMap;
use chrono::{DateTime, Utc};
pub mod search;
mod updates;
@ -37,6 +39,13 @@ pub struct ApiKeys {
pub master: Option<String>,
}
#[derive(Default)]
pub struct Stats {
pub database_size: u64,
pub last_update: Option<DateTime<Utc>>,
pub indexes: HashMap<String, IndexStats>,
}
impl ApiKeys {
pub fn generate_missing_api_keys(&mut self) {
if let Some(master_key) = &self.master {
@ -104,6 +113,27 @@ impl Data {
Ok(meta)
}
pub async fn get_index_stats(&self, uid: String) -> anyhow::Result<IndexStats> {
Ok(self.index_controller.get_stats(uid).await?)
}
pub async fn get_stats(&self) -> anyhow::Result<Stats> {
let mut stats = Stats::default();
for index in self.index_controller.list_indexes().await? {
let index_stats = self.index_controller.get_stats(index.uid.clone()).await?;
stats.database_size += index_stats.size;
stats.last_update = Some(match stats.last_update {
Some(last_update) => last_update.max(index.meta.updated_at),
None => index.meta.updated_at,
});
stats.indexes.insert(index.uid, index_stats);
}
Ok(stats)
}
#[inline]
pub fn http_payload_size_limit(&self) -> usize {
self.options.http_payload_size_limit.get_bytes() as usize

View File

@ -44,7 +44,7 @@ impl<S: IndexStore + Sync + Send> IndexActor<S> {
read_receiver,
write_receiver,
update_handler,
processing: RwLock::new(Default::default()),
processing: RwLock::new(None),
store,
})
}
@ -183,23 +183,22 @@ impl<S: IndexStore + Sync + Send> IndexActor<S> {
meta: Processing<UpdateMeta>,
data: File,
) -> Result<UpdateResult> {
let uuid = meta.index_uuid().clone();
*self.processing.write().await = Some(uuid);
let result = {
async fn get_result<S: IndexStore>(actor: &IndexActor<S>, meta: Processing<UpdateMeta>, data: File) -> Result<UpdateResult> {
debug!("Processing update {}", meta.id());
let update_handler = self.update_handler.clone();
let index = match self.store.get(uuid).await? {
let uuid = *meta.index_uuid();
let update_handler = actor.update_handler.clone();
let index = match actor.store.get(uuid).await? {
Some(index) => index,
None => self.store.create(uuid, None).await?,
None => actor.store.create(uuid, None).await?,
};
spawn_blocking(move || update_handler.handle_update(meta, data, index))
.await
.map_err(|e| IndexError::Error(e.into()))
};
}
*self.processing.write().await = Some(meta.index_uuid().clone());
let result = get_result(self, meta, data).await;
*self.processing.write().await = None;
result

View File

@ -72,9 +72,6 @@ where
Some(Snapshot { uuid, path, ret }) => {
let _ = ret.send(self.handle_snapshot(uuid, path).await);
}
Some(IsLocked { uuid, ret }) => {
let _ = ret.send(self.handle_is_locked(uuid).await);
}
None => break,
}
}
@ -226,14 +223,4 @@ where
Ok(())
}
async fn handle_is_locked(&self, uuid: Uuid) -> Result<bool> {
let store = self
.store
.get(uuid)
.await?
.ok_or(UpdateError::UnexistingIndex(uuid))?;
Ok(store.update_lock.is_locked())
}
}

View File

@ -95,11 +95,4 @@ where
let _ = self.sender.send(msg).await;
receiver.await.expect("update actor killed.")
}
async fn is_locked(&self, uuid: Uuid) -> Result<bool> {
let (ret, receiver) = oneshot::channel();
let msg = UpdateMsg::IsLocked { uuid, ret };
let _ = self.sender.send(msg).await;
receiver.await.expect("update actor killed.")
}
}

View File

@ -34,8 +34,4 @@ pub enum UpdateMsg<D> {
path: PathBuf,
ret: oneshot::Sender<Result<()>>,
},
IsLocked {
uuid: Uuid,
ret: oneshot::Sender<Result<bool>>,
},
}

View File

@ -52,5 +52,4 @@ pub trait UpdateActorHandle {
data: mpsc::Receiver<PayloadData<Self::Data>>,
uuid: Uuid,
) -> Result<UpdateStatus>;
async fn is_locked(&self, uuid: Uuid) -> Result<bool>;
}

View File

@ -12,6 +12,7 @@ use crate::helpers::Authentication;
use crate::index_controller::IndexStats;
use crate::routes::IndexParam;
use crate::Data;
use crate::data::Stats;
pub fn services(cfg: &mut web::ServiceConfig) {
cfg.service(get_index_stats)
@ -42,11 +43,7 @@ async fn get_index_stats(
data: web::Data<Data>,
path: web::Path<IndexParam>,
) -> Result<HttpResponse, ResponseError> {
let response: IndexStatsResponse = data
.index_controller
.get_stats(path.index_uid.clone())
.await?
.into();
let response: IndexStatsResponse = data.get_index_stats(path.index_uid.clone()).await?.into();
Ok(HttpResponse::Ok().json(response))
}
@ -59,24 +56,22 @@ struct StatsResponse {
indexes: HashMap<String, IndexStatsResponse>,
}
impl From<Stats> for StatsResponse {
fn from(stats: Stats) -> Self {
Self {
database_size: stats.database_size,
last_update: stats.last_update,
indexes: stats.indexes
.into_iter()
.map(|(uid, index_stats)| (uid, index_stats.into()))
.collect(),
}
}
}
#[get("/stats", wrap = "Authentication::Private")]
async fn get_stats(data: web::Data<Data>) -> Result<HttpResponse, ResponseError> {
let mut response = StatsResponse {
database_size: 0,
last_update: None,
indexes: HashMap::new(),
};
for index in data.index_controller.list_indexes().await? {
let stats = data.index_controller.get_stats(index.uid.clone()).await?;
response.database_size += stats.size;
response.last_update = Some(match response.last_update {
Some(last_update) => last_update.max(index.meta.updated_at),
None => index.meta.updated_at,
});
response.indexes.insert(index.uid, stats.into());
}
let response: StatsResponse = data.get_stats().await?.into();
Ok(HttpResponse::Ok().json(response))
}