Introduce an HTTP route to clear the documents

This commit is contained in:
Clément Renault 2020-10-30 13:12:55 +01:00
parent 7cc1a358f5
commit 955302fd95
No known key found for this signature in database
GPG Key ID: 92ADA4E935E71FA4

View File

@ -7,7 +7,6 @@ use std::str::FromStr;
use std::sync::Arc; use std::sync::Arc;
use std::time::Instant; use std::time::Instant;
use anyhow::anyhow;
use askama_warp::Template; use askama_warp::Template;
use flate2::read::GzDecoder; use flate2::read::GzDecoder;
use futures::stream; use futures::stream;
@ -159,9 +158,7 @@ enum UpdateStatus<M, P, N> {
#[serde(tag = "type")] #[serde(tag = "type")]
enum UpdateMeta { enum UpdateMeta {
DocumentsAddition, DocumentsAddition,
DocumentsAdditionFromPath { ClearDocuments,
path: PathBuf,
},
} }
#[derive(Debug, Clone, Serialize, Deserialize)] #[derive(Debug, Clone, Serialize, Deserialize)]
@ -255,8 +252,15 @@ pub fn run(opt: Opt) -> anyhow::Result<()> {
Err(e) => Err(e.into()) Err(e) => Err(e.into())
} }
}, },
UpdateMeta::DocumentsAdditionFromPath { path: _ } => { UpdateMeta::ClearDocuments => {
Err(anyhow!("indexing from a file is not supported yet")) // We must use the write transaction of the update here.
let mut wtxn = index_cloned.write_txn()?;
let builder = update_builder.clear_documents(&mut wtxn, &index_cloned);
match builder.execute() {
Ok(_count) => wtxn.commit().map_err(Into::into),
Err(e) => Err(e.into())
}
} }
}; };
@ -489,16 +493,12 @@ pub fn run(opt: Opt) -> anyhow::Result<()> {
buf_stream(update_store_cloned.clone(), update_status_sender_cloned.clone(), stream) buf_stream(update_store_cloned.clone(), update_status_sender_cloned.clone(), stream)
}); });
let update_store_cloned = update_store.clone();
let update_status_sender_cloned = update_status_sender.clone(); let update_status_sender_cloned = update_status_sender.clone();
let indexing_route_filepath = warp::filters::method::post() let clearing_route = warp::filters::method::post()
.and(warp::path!("documents")) .and(warp::path!("clear-documents"))
.and(warp::header::exact_ignore_case("content-type", "text/x-filepath")) .map(move || {
.and(warp::body::bytes()) let meta = UpdateMeta::ClearDocuments;
.map(move |bytes: bytes::Bytes| { let update_id = update_store.register_update(&meta, &[]).unwrap();
let string = std::str::from_utf8(&bytes).unwrap().trim();
let meta = UpdateMeta::DocumentsAdditionFromPath { path: PathBuf::from(string) };
let update_id = update_store_cloned.register_update(&meta, &[]).unwrap();
let _ = update_status_sender_cloned.send(UpdateStatus::Pending { update_id, meta }); let _ = update_status_sender_cloned.send(UpdateStatus::Pending { update_id, meta });
eprintln!("update {} registered", update_id); eprintln!("update {} registered", update_id);
Ok(warp::reply()) Ok(warp::reply())
@ -547,7 +547,7 @@ pub fn run(opt: Opt) -> anyhow::Result<()> {
.or(dash_logo_black_route) .or(dash_logo_black_route)
.or(query_route) .or(query_route)
.or(indexing_route_csv) .or(indexing_route_csv)
.or(indexing_route_filepath) .or(clearing_route)
.or(update_ws_route); .or(update_ws_route);
let addr = SocketAddr::from_str(&opt.http_listen_addr)?; let addr = SocketAddr::from_str(&opt.http_listen_addr)?;