2021-02-27 10:19:05 +01:00
|
|
|
use std::fs::File;
|
|
|
|
|
2021-03-15 18:11:10 +01:00
|
|
|
use crate::index::Index;
|
2021-02-27 10:19:05 +01:00
|
|
|
use grenad::CompressionType;
|
2021-03-04 11:56:32 +01:00
|
|
|
use milli::update::UpdateBuilder;
|
2021-02-27 10:19:05 +01:00
|
|
|
use rayon::ThreadPool;
|
|
|
|
|
2021-03-04 11:56:32 +01:00
|
|
|
use crate::index_controller::UpdateMeta;
|
2021-04-22 10:14:29 +02:00
|
|
|
use crate::index_controller::{Failed, Processed, Processing};
|
2021-02-27 10:19:05 +01:00
|
|
|
use crate::option::IndexerOpts;
|
|
|
|
|
|
|
|
pub struct UpdateHandler {
|
|
|
|
max_nb_chunks: Option<usize>,
|
|
|
|
chunk_compression_level: Option<u32>,
|
|
|
|
thread_pool: ThreadPool,
|
|
|
|
log_frequency: usize,
|
|
|
|
max_memory: usize,
|
|
|
|
linked_hash_map_size: usize,
|
|
|
|
chunk_compression_type: CompressionType,
|
|
|
|
chunk_fusing_shrink_size: u64,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl UpdateHandler {
|
2021-06-15 17:39:07 +02:00
|
|
|
pub fn new(opt: &IndexerOpts) -> anyhow::Result<Self> {
|
2021-02-27 10:19:05 +01:00
|
|
|
let thread_pool = rayon::ThreadPoolBuilder::new()
|
2021-06-28 14:35:50 +02:00
|
|
|
.num_threads(opt.indexing_jobs.unwrap_or(num_cpus::get() / 2))
|
2021-02-27 10:19:05 +01:00
|
|
|
.build()?;
|
|
|
|
Ok(Self {
|
|
|
|
max_nb_chunks: opt.max_nb_chunks,
|
|
|
|
chunk_compression_level: opt.chunk_compression_level,
|
|
|
|
thread_pool,
|
|
|
|
log_frequency: opt.log_every_n,
|
|
|
|
max_memory: opt.max_memory.get_bytes() as usize,
|
|
|
|
linked_hash_map_size: opt.linked_hash_map_size,
|
|
|
|
chunk_compression_type: opt.chunk_compression_type,
|
|
|
|
chunk_fusing_shrink_size: opt.chunk_fusing_shrink_size.get_bytes(),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-05-26 22:52:06 +02:00
|
|
|
pub fn update_builder(&self, update_id: u64) -> UpdateBuilder {
|
2021-02-27 10:19:05 +01:00
|
|
|
// We prepare the update by using the update builder.
|
|
|
|
let mut update_builder = UpdateBuilder::new(update_id);
|
|
|
|
if let Some(max_nb_chunks) = self.max_nb_chunks {
|
|
|
|
update_builder.max_nb_chunks(max_nb_chunks);
|
|
|
|
}
|
|
|
|
if let Some(chunk_compression_level) = self.chunk_compression_level {
|
|
|
|
update_builder.chunk_compression_level(chunk_compression_level);
|
|
|
|
}
|
|
|
|
update_builder.thread_pool(&self.thread_pool);
|
|
|
|
update_builder.log_every_n(self.log_frequency);
|
|
|
|
update_builder.max_memory(self.max_memory);
|
|
|
|
update_builder.linked_hash_map_size(self.linked_hash_map_size);
|
|
|
|
update_builder.chunk_compression_type(self.chunk_compression_type);
|
|
|
|
update_builder.chunk_fusing_shrink_size(self.chunk_fusing_shrink_size);
|
|
|
|
update_builder
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn handle_update(
|
|
|
|
&self,
|
2021-04-22 10:14:29 +02:00
|
|
|
meta: Processing,
|
|
|
|
content: Option<File>,
|
2021-03-04 11:23:41 +01:00
|
|
|
index: Index,
|
2021-04-22 10:14:29 +02:00
|
|
|
) -> Result<Processed, Failed> {
|
2021-02-27 10:19:05 +01:00
|
|
|
use UpdateMeta::*;
|
|
|
|
|
|
|
|
let update_id = meta.id();
|
|
|
|
|
2021-04-02 13:44:35 +02:00
|
|
|
let update_builder = self.update_builder(update_id);
|
2021-02-27 10:19:05 +01:00
|
|
|
|
|
|
|
let result = match meta.meta() {
|
|
|
|
DocumentsAddition {
|
|
|
|
method,
|
|
|
|
format,
|
|
|
|
primary_key,
|
2021-03-04 11:56:32 +01:00
|
|
|
} => index.update_documents(
|
2021-02-27 10:19:05 +01:00
|
|
|
*format,
|
|
|
|
*method,
|
|
|
|
content,
|
|
|
|
update_builder,
|
|
|
|
primary_key.as_deref(),
|
|
|
|
),
|
2021-03-04 11:56:32 +01:00
|
|
|
ClearDocuments => index.clear_documents(update_builder),
|
2021-06-10 15:55:44 +02:00
|
|
|
DeleteDocuments { ids } => index.delete_documents(ids, update_builder),
|
2021-06-01 20:15:51 +02:00
|
|
|
Settings(settings) => index.update_settings(&settings.clone().check(), update_builder),
|
2021-02-27 10:19:05 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
match result {
|
|
|
|
Ok(result) => Ok(meta.process(result)),
|
2021-06-21 18:42:47 +02:00
|
|
|
Err(e) => Err(meta.fail(e.into())),
|
2021-02-27 10:19:05 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|