MeiliSearch/meilisearch-http/src/index_controller/dump_actor/loaders/v1.rs

184 lines
6.8 KiB
Rust
Raw Normal View History

2021-05-31 16:40:59 +02:00
use std::collections::{BTreeMap, BTreeSet};
use std::fs::{create_dir_all, File};
use std::io::BufRead;
use std::marker::PhantomData;
use std::path::Path;
use std::sync::Arc;
2021-05-31 10:42:31 +02:00
use heed::EnvOpenOptions;
use log::{error, info, warn};
2021-05-31 16:40:59 +02:00
use milli::update::{IndexDocumentsMethod, UpdateFormat};
2021-05-26 20:42:09 +02:00
use serde::{Deserialize, Serialize};
2021-05-31 10:42:31 +02:00
use uuid::Uuid;
2021-05-26 20:42:09 +02:00
2021-05-31 16:40:59 +02:00
use crate::index_controller::{self, uuid_resolver::HeedUuidStore, IndexMetadata};
2021-05-31 10:42:31 +02:00
use crate::{
2021-05-31 16:40:59 +02:00
index::{deserialize_some, update_handler::UpdateHandler, Index, Unchecked},
option::IndexerOpts,
2021-05-31 10:42:31 +02:00
};
2021-05-26 20:42:09 +02:00
#[derive(Serialize, Deserialize, Debug)]
2021-05-31 10:42:31 +02:00
#[serde(rename_all = "camelCase")]
2021-05-26 20:42:09 +02:00
pub struct MetadataV1 {
db_version: String,
indexes: Vec<IndexMetadata>,
}
impl MetadataV1 {
2021-05-31 10:42:31 +02:00
pub fn load_dump(
self,
src: impl AsRef<Path>,
dst: impl AsRef<Path>,
size: usize,
2021-05-31 16:40:59 +02:00
indexer_options: &IndexerOpts,
2021-05-31 10:42:31 +02:00
) -> anyhow::Result<()> {
info!(
"Loading dump, dump database version: {}, dump version: V1",
self.db_version
);
let uuid_store = HeedUuidStore::new(&dst)?;
for index in self.indexes {
let uuid = Uuid::new_v4();
uuid_store.insert(index.uid.clone(), uuid)?;
let src = src.as_ref().join(index.uid);
2021-05-31 16:40:59 +02:00
load_index(
&src,
&dst,
uuid,
index.meta.primary_key.as_deref(),
size,
indexer_options,
)?;
2021-05-31 10:42:31 +02:00
}
Ok(())
}
}
2021-05-31 16:40:59 +02:00
// These are the settings used in legacy meilisearch (<v0.21.0).
2021-05-31 10:42:31 +02:00
#[derive(Default, Clone, Serialize, Deserialize, Debug)]
#[serde(rename_all = "camelCase", deny_unknown_fields)]
struct Settings {
#[serde(default, deserialize_with = "deserialize_some")]
pub ranking_rules: Option<Option<Vec<String>>>,
#[serde(default, deserialize_with = "deserialize_some")]
pub distinct_attribute: Option<Option<String>>,
#[serde(default, deserialize_with = "deserialize_some")]
pub searchable_attributes: Option<Option<Vec<String>>>,
#[serde(default, deserialize_with = "deserialize_some")]
pub displayed_attributes: Option<Option<BTreeSet<String>>>,
#[serde(default, deserialize_with = "deserialize_some")]
pub stop_words: Option<Option<BTreeSet<String>>>,
#[serde(default, deserialize_with = "deserialize_some")]
pub synonyms: Option<Option<BTreeMap<String, Vec<String>>>>,
#[serde(default, deserialize_with = "deserialize_some")]
pub filterable_attributes: Option<Option<Vec<String>>>,
2021-05-31 10:42:31 +02:00
}
fn load_index(
src: impl AsRef<Path>,
dst: impl AsRef<Path>,
uuid: Uuid,
primary_key: Option<&str>,
size: usize,
2021-05-31 16:40:59 +02:00
indexer_options: &IndexerOpts,
2021-05-31 10:42:31 +02:00
) -> anyhow::Result<()> {
let index_path = dst.as_ref().join(&format!("indexes/index-{}", uuid));
2021-05-31 16:40:59 +02:00
create_dir_all(&index_path)?;
2021-05-31 10:42:31 +02:00
let mut options = EnvOpenOptions::new();
options.map_size(size);
let index = milli::Index::new(options, index_path)?;
let index = Index(Arc::new(index));
// extract `settings.json` file and import content
let settings = import_settings(&src)?;
let settings: index_controller::Settings<Unchecked> = settings.into();
2021-05-31 16:40:59 +02:00
let mut txn = index.write_txn()?;
let handler = UpdateHandler::new(&indexer_options)?;
index.update_settings_txn(&mut txn, &settings.check(), handler.update_builder(0))?;
2021-05-31 10:42:31 +02:00
let file = File::open(&src.as_ref().join("documents.jsonl"))?;
2021-05-31 10:58:51 +02:00
let mut reader = std::io::BufReader::new(file);
reader.fill_buf()?;
if !reader.buffer().is_empty() {
2021-05-31 16:40:59 +02:00
index.update_documents_txn(
&mut txn,
2021-05-31 10:58:51 +02:00
UpdateFormat::JsonStream,
IndexDocumentsMethod::ReplaceDocuments,
Some(reader),
2021-05-31 16:40:59 +02:00
handler.update_builder(0),
2021-05-31 10:58:51 +02:00
primary_key,
)?;
}
2021-05-31 10:42:31 +02:00
2021-05-31 16:40:59 +02:00
txn.commit()?;
// Finaly, we extract the original milli::Index and close it
2021-05-31 10:42:31 +02:00
Arc::try_unwrap(index.0)
.map_err(|_e| "Couldn't close the index properly")
2021-05-31 10:42:31 +02:00
.unwrap()
.prepare_for_closing()
.wait();
2021-05-31 16:40:59 +02:00
// Updates are ignored in dumps V1.
2021-05-31 10:42:31 +02:00
Ok(())
}
/// we need to **always** be able to convert the old settings to the settings currently being used
impl From<Settings> for index_controller::Settings<Unchecked> {
fn from(settings: Settings) -> Self {
if settings.synonyms.flatten().is_some() {
error!("`synonyms` are not yet implemented and thus will be ignored");
}
Self {
distinct_attribute: settings.distinct_attribute,
// we need to convert the old `Vec<String>` into a `BTreeSet<String>`
displayed_attributes: settings.displayed_attributes.map(|o| o.map(|vec| vec.into_iter().collect())),
searchable_attributes: settings.searchable_attributes,
// we previously had a `Vec<String>` but now we have a `HashMap<String, String>`
// representing the name of the faceted field + the type of the field. Since the type
// was not known in the V1 of the dump we are just going to assume everything is a
// String
filterable_attributes: settings.filterable_attributes.map(|o| o.map(|vec| vec.into_iter().collect())),
2021-05-31 10:42:31 +02:00
// we need to convert the old `Vec<String>` into a `BTreeSet<String>`
ranking_rules: settings.ranking_rules.map(|o| o.map(|vec| vec.into_iter().filter_map(|criterion| {
match criterion.as_str() {
"words" | "typo" | "proximity" | "attribute" => Some(criterion),
s if s.starts_with("asc") || s.starts_with("desc") => Some(criterion),
"wordsPosition" => {
warn!("The criteria `words` and `wordsPosition` have been merged into a single criterion `words` so `wordsPositon` will be ignored");
Some(String::from("words"))
}
"exactness" => {
error!("The criterion `{}` is not implemented currently and thus will be ignored", criterion);
None
}
s => {
error!("Unknown criterion found in the dump: `{}`, it will be ignored", s);
None
}
}
}).collect())),
// we need to convert the old `Vec<String>` into a `BTreeSet<String>`
stop_words: settings.stop_words.map(|o| o.map(|vec| vec.into_iter().collect())),
_kind: PhantomData,
}
}
}
/// Extract Settings from `settings.json` file present at provided `dir_path`
fn import_settings(dir_path: impl AsRef<Path>) -> anyhow::Result<Settings> {
2021-05-31 16:40:59 +02:00
let path = dir_path.as_ref().join("settings.json");
2021-05-31 10:42:31 +02:00
let file = File::open(path)?;
let reader = std::io::BufReader::new(file);
let metadata = serde_json::from_reader(reader)?;
Ok(metadata)
}