2019-11-19 17:39:30 +01:00
|
|
|
use chrono::{DateTime, Utc};
|
2019-11-20 17:28:46 +01:00
|
|
|
use log::error;
|
2019-11-26 11:06:55 +01:00
|
|
|
use meilisearch_core::ProcessedUpdateResult;
|
2020-01-28 17:45:29 +01:00
|
|
|
use meilisearch_schema::Schema;
|
2019-11-19 16:07:24 +01:00
|
|
|
use rand::seq::SliceRandom;
|
|
|
|
use serde::{Deserialize, Serialize};
|
2019-10-31 15:00:36 +01:00
|
|
|
use serde_json::json;
|
2020-01-15 17:10:33 +01:00
|
|
|
use tide::{Request, Response};
|
2019-10-31 15:00:36 +01:00
|
|
|
|
2020-01-23 11:30:18 +01:00
|
|
|
use crate::error::{IntoInternalError, ResponseError, SResult};
|
2020-01-15 17:10:33 +01:00
|
|
|
use crate::helpers::tide::RequestExt;
|
2020-02-06 15:41:11 +01:00
|
|
|
use crate::helpers::tide::ACL::*;
|
2019-10-31 15:00:36 +01:00
|
|
|
use crate::Data;
|
|
|
|
|
2019-11-19 16:07:24 +01:00
|
|
|
fn generate_uid() -> String {
|
|
|
|
let mut rng = rand::thread_rng();
|
2019-11-19 17:42:47 +01:00
|
|
|
let sample = b"abcdefghijklmnopqrstuvwxyz0123456789";
|
2019-11-19 16:07:24 +01:00
|
|
|
sample
|
|
|
|
.choose_multiple(&mut rng, 8)
|
|
|
|
.map(|c| *c as char)
|
|
|
|
.collect()
|
|
|
|
}
|
|
|
|
|
2020-01-15 17:10:33 +01:00
|
|
|
pub async fn list_indexes(ctx: Request<Data>) -> SResult<Response> {
|
2020-02-06 15:41:11 +01:00
|
|
|
ctx.is_allowed(Private)?;
|
2019-11-20 11:24:08 +01:00
|
|
|
|
|
|
|
let indexes_uids = ctx.state().db.indexes_uids();
|
2019-11-20 09:57:27 +01:00
|
|
|
|
2019-11-26 16:12:06 +01:00
|
|
|
let db = &ctx.state().db;
|
2020-01-16 16:58:57 +01:00
|
|
|
let reader = db.main_read_txn()?;
|
2019-11-20 09:57:27 +01:00
|
|
|
|
|
|
|
let mut response_body = Vec::new();
|
|
|
|
|
|
|
|
for index_uid in indexes_uids {
|
2019-11-20 17:28:46 +01:00
|
|
|
let index = ctx.state().db.open_index(&index_uid);
|
|
|
|
|
|
|
|
match index {
|
|
|
|
Some(index) => {
|
2020-01-16 16:58:57 +01:00
|
|
|
let name = index.main.name(&reader)?.into_internal_error()?;
|
|
|
|
let created_at = index.main.created_at(&reader)?.into_internal_error()?;
|
|
|
|
let updated_at = index.main.updated_at(&reader)?.into_internal_error()?;
|
2019-11-20 17:28:46 +01:00
|
|
|
|
2020-02-12 17:00:14 +01:00
|
|
|
let identifier = match index.main.schema(&reader) {
|
|
|
|
Ok(Some(schema)) => Some(schema.identifier().to_owned()),
|
2020-02-26 18:49:17 +01:00
|
|
|
_ => None,
|
2020-02-12 17:00:14 +01:00
|
|
|
};
|
|
|
|
|
2019-12-19 10:32:17 +01:00
|
|
|
let index_response = IndexResponse {
|
2019-11-20 17:28:46 +01:00
|
|
|
name,
|
|
|
|
uid: index_uid,
|
|
|
|
created_at,
|
|
|
|
updated_at,
|
2020-02-12 17:00:14 +01:00
|
|
|
identifier,
|
2019-11-20 17:28:46 +01:00
|
|
|
};
|
2019-12-19 10:32:17 +01:00
|
|
|
response_body.push(index_response);
|
2019-11-20 17:28:46 +01:00
|
|
|
}
|
|
|
|
None => error!(
|
|
|
|
"Index {} is referenced in the indexes list but cannot be found",
|
|
|
|
index_uid
|
|
|
|
),
|
|
|
|
}
|
2019-11-20 09:57:27 +01:00
|
|
|
}
|
|
|
|
|
2020-01-29 18:30:21 +01:00
|
|
|
Ok(tide::Response::new(200).body_json(&response_body)?)
|
2019-10-31 15:00:36 +01:00
|
|
|
}
|
|
|
|
|
2019-11-20 17:28:46 +01:00
|
|
|
#[derive(Debug, Serialize)]
|
|
|
|
#[serde(rename_all = "camelCase")]
|
2019-11-20 09:57:27 +01:00
|
|
|
struct IndexResponse {
|
2019-11-19 17:38:02 +01:00
|
|
|
name: String,
|
|
|
|
uid: String,
|
|
|
|
created_at: DateTime<Utc>,
|
|
|
|
updated_at: DateTime<Utc>,
|
2020-02-12 17:00:14 +01:00
|
|
|
identifier: Option<String>,
|
2019-11-19 17:38:02 +01:00
|
|
|
}
|
|
|
|
|
2020-01-15 17:10:33 +01:00
|
|
|
pub async fn get_index(ctx: Request<Data>) -> SResult<Response> {
|
2020-02-06 15:41:11 +01:00
|
|
|
ctx.is_allowed(Private)?;
|
2019-11-19 17:38:02 +01:00
|
|
|
|
|
|
|
let index = ctx.index()?;
|
|
|
|
|
2019-11-26 16:12:06 +01:00
|
|
|
let db = &ctx.state().db;
|
2020-01-16 16:58:57 +01:00
|
|
|
let reader = db.main_read_txn()?;
|
2019-11-19 17:38:02 +01:00
|
|
|
|
2019-11-20 17:28:46 +01:00
|
|
|
let uid = ctx.url_param("index")?;
|
2020-01-16 16:58:57 +01:00
|
|
|
let name = index.main.name(&reader)?.into_internal_error()?;
|
|
|
|
let created_at = index.main.created_at(&reader)?.into_internal_error()?;
|
|
|
|
let updated_at = index.main.updated_at(&reader)?.into_internal_error()?;
|
2019-11-19 17:38:02 +01:00
|
|
|
|
2020-02-12 17:00:14 +01:00
|
|
|
let identifier = match index.main.schema(&reader) {
|
|
|
|
Ok(Some(schema)) => Some(schema.identifier().to_owned()),
|
2020-02-26 18:49:17 +01:00
|
|
|
_ => None,
|
2020-02-12 17:00:14 +01:00
|
|
|
};
|
|
|
|
|
2019-11-20 09:57:27 +01:00
|
|
|
let response_body = IndexResponse {
|
2019-11-19 17:38:02 +01:00
|
|
|
name,
|
|
|
|
uid,
|
|
|
|
created_at,
|
|
|
|
updated_at,
|
2020-02-26 18:49:17 +01:00
|
|
|
identifier,
|
2019-11-19 17:38:02 +01:00
|
|
|
};
|
|
|
|
|
2020-01-29 18:30:21 +01:00
|
|
|
Ok(tide::Response::new(200).body_json(&response_body)?)
|
2019-11-19 17:38:02 +01:00
|
|
|
}
|
|
|
|
|
2019-11-20 17:28:46 +01:00
|
|
|
#[derive(Debug, Deserialize)]
|
2019-11-19 16:07:24 +01:00
|
|
|
#[serde(rename_all = "camelCase", deny_unknown_fields)]
|
|
|
|
struct IndexCreateRequest {
|
2020-01-15 17:11:10 +01:00
|
|
|
name: Option<String>,
|
2019-11-22 11:18:46 +01:00
|
|
|
uid: Option<String>,
|
2020-01-29 18:30:21 +01:00
|
|
|
identifier: Option<String>,
|
2019-11-19 16:07:24 +01:00
|
|
|
}
|
|
|
|
|
2019-11-20 17:28:46 +01:00
|
|
|
#[derive(Debug, Serialize)]
|
|
|
|
#[serde(rename_all = "camelCase")]
|
2019-11-19 16:07:24 +01:00
|
|
|
struct IndexCreateResponse {
|
|
|
|
name: String,
|
|
|
|
uid: String,
|
|
|
|
created_at: DateTime<Utc>,
|
|
|
|
updated_at: DateTime<Utc>,
|
2020-02-12 17:00:14 +01:00
|
|
|
identifier: Option<String>,
|
2019-11-19 16:07:24 +01:00
|
|
|
}
|
|
|
|
|
2020-01-15 17:10:33 +01:00
|
|
|
pub async fn create_index(mut ctx: Request<Data>) -> SResult<Response> {
|
2020-02-06 15:41:11 +01:00
|
|
|
ctx.is_allowed(Private)?;
|
2019-10-31 15:00:36 +01:00
|
|
|
|
2019-11-20 11:24:08 +01:00
|
|
|
let body = ctx
|
|
|
|
.body_json::<IndexCreateRequest>()
|
|
|
|
.await
|
|
|
|
.map_err(ResponseError::bad_request)?;
|
2019-10-31 15:00:36 +01:00
|
|
|
|
2020-01-15 17:11:10 +01:00
|
|
|
if let (None, None) = (body.name.clone(), body.uid.clone()) {
|
2020-01-23 11:30:18 +01:00
|
|
|
return Err(ResponseError::bad_request(
|
|
|
|
"Index creation must have an uid",
|
|
|
|
));
|
2020-01-15 17:11:10 +01:00
|
|
|
}
|
|
|
|
|
2019-10-31 15:00:36 +01:00
|
|
|
let db = &ctx.state().db;
|
|
|
|
|
2019-11-22 11:18:46 +01:00
|
|
|
let uid = match body.uid {
|
2020-03-05 11:44:30 +01:00
|
|
|
Some(uid) => {
|
|
|
|
if uid.chars().all(|x| x.is_ascii_alphanumeric() || x == '-' || x == '_') {
|
|
|
|
uid
|
|
|
|
} else {
|
|
|
|
return Err(ResponseError::InvalidIndexUid)
|
|
|
|
}
|
|
|
|
},
|
2019-11-22 11:18:46 +01:00
|
|
|
None => loop {
|
|
|
|
let uid = generate_uid();
|
|
|
|
if db.open_index(&uid).is_none() {
|
|
|
|
break uid;
|
|
|
|
}
|
|
|
|
},
|
2019-11-22 11:08:29 +01:00
|
|
|
};
|
|
|
|
|
2019-11-22 11:18:46 +01:00
|
|
|
let created_index = match db.create_index(&uid) {
|
2019-10-31 15:00:36 +01:00
|
|
|
Ok(index) => index,
|
|
|
|
Err(e) => return Err(ResponseError::create_index(e)),
|
|
|
|
};
|
|
|
|
|
2020-01-16 16:58:57 +01:00
|
|
|
let mut writer = db.main_write_txn()?;
|
2020-01-15 17:11:10 +01:00
|
|
|
let name = body.name.unwrap_or(uid.clone());
|
2020-01-16 16:58:57 +01:00
|
|
|
created_index.main.put_name(&mut writer, &name)?;
|
2020-01-23 11:30:18 +01:00
|
|
|
let created_at = created_index
|
|
|
|
.main
|
|
|
|
.created_at(&writer)?
|
|
|
|
.into_internal_error()?;
|
|
|
|
let updated_at = created_index
|
|
|
|
.main
|
|
|
|
.updated_at(&writer)?
|
|
|
|
.into_internal_error()?;
|
2019-11-19 16:07:24 +01:00
|
|
|
|
2020-02-12 17:00:14 +01:00
|
|
|
if let Some(id) = body.identifier.clone() {
|
2020-01-28 17:46:51 +01:00
|
|
|
created_index
|
|
|
|
.main
|
2020-01-29 18:30:21 +01:00
|
|
|
.put_schema(&mut writer, &Schema::with_identifier(&id))?;
|
2020-01-28 17:45:29 +01:00
|
|
|
}
|
|
|
|
|
2020-01-16 16:58:57 +01:00
|
|
|
writer.commit()?;
|
2019-10-31 15:00:36 +01:00
|
|
|
|
2019-11-19 16:07:24 +01:00
|
|
|
let response_body = IndexCreateResponse {
|
2020-01-31 10:50:28 +01:00
|
|
|
name,
|
2019-11-22 11:18:46 +01:00
|
|
|
uid,
|
2020-01-16 16:14:30 +01:00
|
|
|
created_at,
|
|
|
|
updated_at,
|
2020-02-12 17:00:14 +01:00
|
|
|
identifier: body.identifier,
|
2019-11-19 16:07:24 +01:00
|
|
|
};
|
|
|
|
|
2020-01-29 18:30:21 +01:00
|
|
|
Ok(tide::Response::new(201).body_json(&response_body)?)
|
2019-10-31 15:00:36 +01:00
|
|
|
}
|
|
|
|
|
2019-11-20 17:28:46 +01:00
|
|
|
#[derive(Debug, Deserialize)]
|
2019-11-20 15:00:06 +01:00
|
|
|
#[serde(rename_all = "camelCase", deny_unknown_fields)]
|
|
|
|
struct UpdateIndexRequest {
|
2020-02-12 17:00:14 +01:00
|
|
|
name: Option<String>,
|
|
|
|
identifier: Option<String>,
|
2019-11-20 15:00:06 +01:00
|
|
|
}
|
|
|
|
|
2019-11-20 17:28:46 +01:00
|
|
|
#[derive(Debug, Serialize)]
|
|
|
|
#[serde(rename_all = "camelCase")]
|
2019-11-20 15:00:06 +01:00
|
|
|
struct UpdateIndexResponse {
|
|
|
|
name: String,
|
|
|
|
uid: String,
|
|
|
|
created_at: DateTime<Utc>,
|
|
|
|
updated_at: DateTime<Utc>,
|
2020-02-12 17:00:14 +01:00
|
|
|
identifier: Option<String>,
|
2019-11-20 15:00:06 +01:00
|
|
|
}
|
|
|
|
|
2020-01-15 17:10:33 +01:00
|
|
|
pub async fn update_index(mut ctx: Request<Data>) -> SResult<Response> {
|
2020-02-06 15:41:11 +01:00
|
|
|
ctx.is_allowed(Private)?;
|
2019-11-20 15:00:06 +01:00
|
|
|
|
|
|
|
let body = ctx
|
|
|
|
.body_json::<UpdateIndexRequest>()
|
|
|
|
.await
|
|
|
|
.map_err(ResponseError::bad_request)?;
|
|
|
|
|
|
|
|
let index_uid = ctx.url_param("index")?;
|
|
|
|
let index = ctx.index()?;
|
|
|
|
|
|
|
|
let db = &ctx.state().db;
|
2020-01-16 16:58:57 +01:00
|
|
|
let mut writer = db.main_write_txn()?;
|
|
|
|
|
2020-02-12 17:00:14 +01:00
|
|
|
if let Some(name) = body.name {
|
|
|
|
index.main.put_name(&mut writer, &name)?;
|
|
|
|
}
|
2020-01-16 16:58:57 +01:00
|
|
|
|
2020-02-12 17:00:14 +01:00
|
|
|
if let Some(identifier) = body.identifier {
|
|
|
|
if let Ok(Some(_)) = index.main.schema(&writer) {
|
2020-02-26 18:49:17 +01:00
|
|
|
return Err(ResponseError::bad_request(
|
|
|
|
"The index identifier cannot be updated",
|
|
|
|
));
|
2020-02-12 17:00:14 +01:00
|
|
|
}
|
2020-02-26 18:49:17 +01:00
|
|
|
index
|
|
|
|
.main
|
|
|
|
.put_schema(&mut writer, &Schema::with_identifier(&identifier))?;
|
2020-02-12 17:00:14 +01:00
|
|
|
}
|
2020-01-16 16:58:57 +01:00
|
|
|
|
2020-02-12 17:00:14 +01:00
|
|
|
index.main.put_updated_at(&mut writer)?;
|
2020-01-16 16:58:57 +01:00
|
|
|
writer.commit()?;
|
|
|
|
|
2020-02-12 17:00:14 +01:00
|
|
|
let reader = db.main_read_txn()?;
|
|
|
|
let name = index.main.name(&reader)?.into_internal_error()?;
|
2020-01-16 16:58:57 +01:00
|
|
|
let created_at = index.main.created_at(&reader)?.into_internal_error()?;
|
|
|
|
let updated_at = index.main.updated_at(&reader)?.into_internal_error()?;
|
2019-11-20 15:00:06 +01:00
|
|
|
|
2020-02-12 17:00:14 +01:00
|
|
|
let identifier = match index.main.schema(&reader) {
|
|
|
|
Ok(Some(schema)) => Some(schema.identifier().to_owned()),
|
2020-02-26 18:49:17 +01:00
|
|
|
_ => None,
|
2020-02-12 17:00:14 +01:00
|
|
|
};
|
|
|
|
|
2019-11-20 15:00:06 +01:00
|
|
|
let response_body = UpdateIndexResponse {
|
2020-02-12 17:00:14 +01:00
|
|
|
name,
|
2019-11-20 15:00:06 +01:00
|
|
|
uid: index_uid,
|
|
|
|
created_at,
|
|
|
|
updated_at,
|
2020-02-26 18:49:17 +01:00
|
|
|
identifier,
|
2019-11-20 15:00:06 +01:00
|
|
|
};
|
|
|
|
|
2020-01-29 18:30:21 +01:00
|
|
|
Ok(tide::Response::new(200).body_json(&response_body)?)
|
2019-11-20 15:00:06 +01:00
|
|
|
}
|
|
|
|
|
2020-01-15 17:10:33 +01:00
|
|
|
pub async fn get_update_status(ctx: Request<Data>) -> SResult<Response> {
|
2020-02-06 15:41:11 +01:00
|
|
|
ctx.is_allowed(Private)?;
|
2019-10-31 15:00:36 +01:00
|
|
|
|
2019-11-26 16:12:06 +01:00
|
|
|
let db = &ctx.state().db;
|
2020-01-16 16:58:57 +01:00
|
|
|
let reader = db.update_read_txn()?;
|
2019-10-31 15:00:36 +01:00
|
|
|
|
|
|
|
let update_id = ctx
|
|
|
|
.param::<u64>("update_id")
|
|
|
|
.map_err(|e| ResponseError::bad_parameter("update_id", e))?;
|
|
|
|
|
|
|
|
let index = ctx.index()?;
|
2020-01-16 16:58:57 +01:00
|
|
|
let status = index.update_status(&reader, update_id)?;
|
2019-10-31 15:00:36 +01:00
|
|
|
|
|
|
|
let response = match status {
|
2020-01-15 17:10:33 +01:00
|
|
|
Some(status) => tide::Response::new(200).body_json(&status).unwrap(),
|
2020-01-23 11:30:18 +01:00
|
|
|
None => tide::Response::new(404)
|
|
|
|
.body_json(&json!({ "message": "unknown update id" }))
|
|
|
|
.unwrap(),
|
2019-10-31 15:00:36 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
Ok(response)
|
|
|
|
}
|
|
|
|
|
2020-01-15 17:10:33 +01:00
|
|
|
pub async fn get_all_updates_status(ctx: Request<Data>) -> SResult<Response> {
|
2020-02-06 15:41:11 +01:00
|
|
|
ctx.is_allowed(Private)?;
|
2019-11-26 16:12:06 +01:00
|
|
|
let db = &ctx.state().db;
|
2020-01-16 16:58:57 +01:00
|
|
|
let reader = db.update_read_txn()?;
|
2019-10-31 15:00:36 +01:00
|
|
|
let index = ctx.index()?;
|
2020-01-16 16:58:57 +01:00
|
|
|
let response = index.all_updates_status(&reader)?;
|
2020-01-15 17:10:33 +01:00
|
|
|
Ok(tide::Response::new(200).body_json(&response).unwrap())
|
2019-10-31 15:00:36 +01:00
|
|
|
}
|
|
|
|
|
2020-01-15 17:10:33 +01:00
|
|
|
pub async fn delete_index(ctx: Request<Data>) -> SResult<Response> {
|
2020-02-06 15:41:11 +01:00
|
|
|
ctx.is_allowed(Private)?;
|
2019-12-12 14:06:16 +01:00
|
|
|
let _ = ctx.index()?;
|
2019-11-19 16:15:49 +01:00
|
|
|
let index_uid = ctx.url_param("index")?;
|
2020-01-16 16:58:57 +01:00
|
|
|
ctx.state().db.delete_index(&index_uid)?;
|
2020-01-15 17:10:33 +01:00
|
|
|
Ok(tide::Response::new(204))
|
2019-10-31 15:00:36 +01:00
|
|
|
}
|
|
|
|
|
2019-11-20 17:28:46 +01:00
|
|
|
pub fn index_update_callback(index_uid: &str, data: &Data, status: ProcessedUpdateResult) {
|
|
|
|
if status.error.is_some() {
|
|
|
|
return;
|
|
|
|
}
|
2019-10-31 15:00:36 +01:00
|
|
|
|
2019-11-20 11:19:17 +01:00
|
|
|
if let Some(index) = data.db.open_index(&index_uid) {
|
2019-11-26 16:12:06 +01:00
|
|
|
let db = &data.db;
|
|
|
|
let mut writer = match db.main_write_txn() {
|
2019-11-20 17:28:46 +01:00
|
|
|
Ok(writer) => writer,
|
|
|
|
Err(e) => {
|
|
|
|
error!("Impossible to get write_txn; {}", e);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
if let Err(e) = data.compute_stats(&mut writer, &index_uid) {
|
|
|
|
error!("Impossible to compute stats; {}", e)
|
|
|
|
}
|
|
|
|
|
|
|
|
if let Err(e) = data.set_last_update(&mut writer) {
|
|
|
|
error!("Impossible to update last_update; {}", e)
|
|
|
|
}
|
|
|
|
|
2019-11-20 11:19:17 +01:00
|
|
|
if let Err(e) = index.main.put_updated_at(&mut writer) {
|
|
|
|
error!("Impossible to update updated_at; {}", e)
|
|
|
|
}
|
|
|
|
|
2019-11-20 17:28:46 +01:00
|
|
|
if let Err(e) = writer.commit() {
|
|
|
|
error!("Impossible to get write_txn; {}", e);
|
|
|
|
}
|
|
|
|
}
|
2019-10-31 15:00:36 +01:00
|
|
|
}
|