2020-12-12 20:32:06 +08:00
|
|
|
use std::ops::Deref;
|
|
|
|
use std::sync::Arc;
|
2020-12-23 23:12:37 +08:00
|
|
|
use std::fs::create_dir_all;
|
2020-12-12 20:32:06 +08:00
|
|
|
|
2020-12-23 20:52:28 +08:00
|
|
|
use async_compression::tokio_02::write::GzipEncoder;
|
|
|
|
use futures_util::stream::StreamExt;
|
|
|
|
use tokio::io::AsyncWriteExt;
|
2020-12-22 21:02:41 +08:00
|
|
|
use milli::Index;
|
2020-12-23 20:52:28 +08:00
|
|
|
use milli::update::{IndexDocumentsMethod, UpdateFormat};
|
|
|
|
use sha2::Digest;
|
2020-12-12 20:32:06 +08:00
|
|
|
|
|
|
|
use crate::option::Opt;
|
2020-12-23 20:52:28 +08:00
|
|
|
use crate::updates::{UpdateQueue, UpdateMeta, UpdateStatus, UpdateMetaProgress};
|
2020-12-12 20:32:06 +08:00
|
|
|
|
|
|
|
#[derive(Clone)]
|
|
|
|
pub struct Data {
|
|
|
|
inner: Arc<DataInner>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Deref for Data {
|
|
|
|
type Target = DataInner;
|
|
|
|
|
|
|
|
fn deref(&self) -> &Self::Target {
|
|
|
|
&self.inner
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Clone)]
|
|
|
|
pub struct DataInner {
|
2020-12-22 21:02:41 +08:00
|
|
|
pub indexes: Arc<Index>,
|
2020-12-23 23:12:37 +08:00
|
|
|
pub update_queue: Arc<UpdateQueue>,
|
2020-12-23 00:53:13 +08:00
|
|
|
api_keys: ApiKeys,
|
|
|
|
options: Opt,
|
2020-12-12 20:32:06 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Clone)]
|
|
|
|
pub struct ApiKeys {
|
|
|
|
pub public: Option<String>,
|
|
|
|
pub private: Option<String>,
|
|
|
|
pub master: Option<String>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl ApiKeys {
|
|
|
|
pub fn generate_missing_api_keys(&mut self) {
|
|
|
|
if let Some(master_key) = &self.master {
|
|
|
|
if self.private.is_none() {
|
|
|
|
let key = format!("{}-private", master_key);
|
|
|
|
let sha = sha2::Sha256::digest(key.as_bytes());
|
|
|
|
self.private = Some(format!("{:x}", sha));
|
|
|
|
}
|
|
|
|
if self.public.is_none() {
|
|
|
|
let key = format!("{}-public", master_key);
|
|
|
|
let sha = sha2::Sha256::digest(key.as_bytes());
|
|
|
|
self.public = Some(format!("{:x}", sha));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Data {
|
2020-12-23 00:53:13 +08:00
|
|
|
pub fn new(options: Opt) -> anyhow::Result<Data> {
|
|
|
|
let db_size = options.max_mdb_size.get_bytes() as usize;
|
|
|
|
let path = options.db_path.join("main");
|
2020-12-23 23:12:37 +08:00
|
|
|
create_dir_all(&path)?;
|
2020-12-23 00:53:13 +08:00
|
|
|
let indexes = Index::new(&path, Some(db_size))?;
|
|
|
|
let indexes = Arc::new(indexes);
|
|
|
|
|
2020-12-23 23:12:37 +08:00
|
|
|
let update_queue = Arc::new(UpdateQueue::new(&options, indexes.clone())?);
|
2020-12-23 00:53:13 +08:00
|
|
|
|
|
|
|
let mut api_keys = ApiKeys {
|
|
|
|
master: options.clone().master_key,
|
|
|
|
private: None,
|
|
|
|
public: None,
|
|
|
|
};
|
|
|
|
|
|
|
|
api_keys.generate_missing_api_keys();
|
|
|
|
|
|
|
|
let inner = DataInner { indexes, options, update_queue, api_keys };
|
|
|
|
let inner = Arc::new(inner);
|
|
|
|
|
|
|
|
Ok(Data { inner })
|
|
|
|
}
|
|
|
|
|
2020-12-23 20:52:28 +08:00
|
|
|
pub async fn add_documents<B, E>(
|
|
|
|
&self,
|
|
|
|
method: IndexDocumentsMethod,
|
|
|
|
format: UpdateFormat,
|
|
|
|
mut stream: impl futures::Stream<Item=Result<B, E>> + Unpin,
|
|
|
|
) -> anyhow::Result<UpdateStatus<UpdateMeta, UpdateMetaProgress, String>>
|
|
|
|
where
|
|
|
|
B: Deref<Target = [u8]>,
|
|
|
|
E: std::error::Error + Send + Sync + 'static,
|
|
|
|
{
|
2020-12-23 23:12:37 +08:00
|
|
|
let file = tokio::task::spawn_blocking(tempfile::tempfile).await?;
|
|
|
|
let file = tokio::fs::File::from_std(file?);
|
2020-12-23 20:52:28 +08:00
|
|
|
let mut encoder = GzipEncoder::new(file);
|
|
|
|
|
|
|
|
while let Some(result) = stream.next().await {
|
|
|
|
let bytes = &*result?;
|
|
|
|
encoder.write_all(&bytes[..]).await?;
|
|
|
|
}
|
|
|
|
|
|
|
|
encoder.shutdown().await?;
|
|
|
|
let mut file = encoder.into_inner();
|
|
|
|
file.sync_all().await?;
|
|
|
|
let file = file.into_std().await;
|
|
|
|
let mmap = unsafe { memmap::Mmap::map(&file)? };
|
|
|
|
|
|
|
|
let meta = UpdateMeta::DocumentsAddition { method, format };
|
2020-12-23 23:12:37 +08:00
|
|
|
|
|
|
|
let queue = self.update_queue.clone();
|
|
|
|
let meta_cloned = meta.clone();
|
|
|
|
let update_id = tokio::task::spawn_blocking(move || queue.register_update(&meta_cloned, &mmap[..])).await??;
|
2020-12-23 20:52:28 +08:00
|
|
|
|
|
|
|
Ok(UpdateStatus::Pending { update_id, meta })
|
|
|
|
}
|
|
|
|
|
2020-12-23 00:53:13 +08:00
|
|
|
#[inline]
|
|
|
|
pub fn http_payload_size_limit(&self) -> usize {
|
|
|
|
self.options.http_payload_size_limit.get_bytes() as usize
|
|
|
|
}
|
|
|
|
|
2020-12-23 20:52:28 +08:00
|
|
|
#[inline]
|
2020-12-23 00:53:13 +08:00
|
|
|
pub fn api_keys(&self) -> &ApiKeys {
|
|
|
|
&self.api_keys
|
2020-12-12 20:32:06 +08:00
|
|
|
}
|
|
|
|
}
|