2022-10-21 00:00:07 +08:00
|
|
|
use std::fs::{self, File};
|
|
|
|
use std::io::{BufWriter, Write};
|
|
|
|
use std::path::PathBuf;
|
|
|
|
|
|
|
|
use flate2::write::GzEncoder;
|
|
|
|
use flate2::Compression;
|
2023-06-26 18:24:55 +08:00
|
|
|
use meilisearch_types::features::RuntimeTogglableFeatures;
|
2022-10-21 00:00:07 +08:00
|
|
|
use meilisearch_types::keys::Key;
|
|
|
|
use meilisearch_types::settings::{Checked, Settings};
|
2022-10-04 00:50:06 +08:00
|
|
|
use serde_json::{Map, Value};
|
2022-10-02 19:24:59 +08:00
|
|
|
use tempfile::TempDir;
|
|
|
|
use time::OffsetDateTime;
|
|
|
|
use uuid::Uuid;
|
|
|
|
|
2022-10-21 00:00:07 +08:00
|
|
|
use crate::reader::Document;
|
|
|
|
use crate::{IndexMetadata, Metadata, Result, TaskDump, CURRENT_DUMP_VERSION};
|
2022-10-02 19:24:59 +08:00
|
|
|
|
|
|
|
pub struct DumpWriter {
|
|
|
|
dir: TempDir,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl DumpWriter {
|
2022-10-13 21:02:59 +08:00
|
|
|
pub fn new(instance_uuid: Option<Uuid>) -> Result<DumpWriter> {
|
2022-10-02 19:24:59 +08:00
|
|
|
let dir = TempDir::new()?;
|
2022-10-05 01:13:30 +08:00
|
|
|
|
2022-10-13 21:02:59 +08:00
|
|
|
if let Some(instance_uuid) = instance_uuid {
|
|
|
|
fs::write(
|
|
|
|
dir.path().join("instance_uid.uuid"),
|
2022-12-19 21:12:26 +08:00
|
|
|
instance_uuid.as_hyphenated().to_string(),
|
2022-10-13 21:02:59 +08:00
|
|
|
)?;
|
|
|
|
}
|
2022-10-02 19:24:59 +08:00
|
|
|
|
|
|
|
let metadata = Metadata {
|
2022-10-03 22:12:01 +08:00
|
|
|
dump_version: CURRENT_DUMP_VERSION,
|
2022-10-02 19:24:59 +08:00
|
|
|
db_version: env!("CARGO_PKG_VERSION").to_string(),
|
|
|
|
dump_date: OffsetDateTime::now_utc(),
|
|
|
|
};
|
2022-10-21 00:00:07 +08:00
|
|
|
fs::write(dir.path().join("metadata.json"), serde_json::to_string(&metadata)?)?;
|
2022-10-02 19:24:59 +08:00
|
|
|
|
2022-12-19 21:12:26 +08:00
|
|
|
std::fs::create_dir(dir.path().join("indexes"))?;
|
2022-10-02 19:24:59 +08:00
|
|
|
|
|
|
|
Ok(DumpWriter { dir })
|
|
|
|
}
|
|
|
|
|
2022-10-05 01:13:30 +08:00
|
|
|
pub fn create_index(&self, index_name: &str, metadata: &IndexMetadata) -> Result<IndexWriter> {
|
|
|
|
IndexWriter::new(self.dir.path().join("indexes").join(index_name), metadata)
|
2022-10-02 19:24:59 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn create_keys(&self) -> Result<KeyWriter> {
|
|
|
|
KeyWriter::new(self.dir.path().to_path_buf())
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn create_tasks_queue(&self) -> Result<TaskWriter> {
|
|
|
|
TaskWriter::new(self.dir.path().join("tasks"))
|
|
|
|
}
|
|
|
|
|
2023-06-26 18:24:55 +08:00
|
|
|
pub fn create_experimental_features(&self, features: RuntimeTogglableFeatures) -> Result<()> {
|
|
|
|
Ok(std::fs::write(
|
|
|
|
self.dir.path().join("experimental-features.json"),
|
|
|
|
serde_json::to_string(&features)?,
|
|
|
|
)?)
|
|
|
|
}
|
|
|
|
|
2022-10-02 19:24:59 +08:00
|
|
|
pub fn persist_to(self, mut writer: impl Write) -> Result<()> {
|
|
|
|
let gz_encoder = GzEncoder::new(&mut writer, Compression::default());
|
|
|
|
let mut tar_encoder = tar::Builder::new(gz_encoder);
|
|
|
|
tar_encoder.append_dir_all(".", self.dir.path())?;
|
|
|
|
let gz_encoder = tar_encoder.into_inner()?;
|
|
|
|
gz_encoder.finish()?;
|
|
|
|
writer.flush()?;
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub struct KeyWriter {
|
2022-10-16 07:39:01 +08:00
|
|
|
keys: BufWriter<File>,
|
2022-10-02 19:24:59 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
impl KeyWriter {
|
|
|
|
pub(crate) fn new(path: PathBuf) -> Result<Self> {
|
2022-10-16 07:39:01 +08:00
|
|
|
let keys = File::create(path.join("keys.jsonl"))?;
|
2022-10-21 00:00:07 +08:00
|
|
|
Ok(KeyWriter { keys: BufWriter::new(keys) })
|
2022-10-02 19:24:59 +08:00
|
|
|
}
|
|
|
|
|
2022-10-04 00:50:06 +08:00
|
|
|
pub fn push_key(&mut self, key: &Key) -> Result<()> {
|
2022-10-16 07:39:01 +08:00
|
|
|
self.keys.write_all(&serde_json::to_vec(key)?)?;
|
|
|
|
self.keys.write_all(b"\n")?;
|
2022-10-02 19:24:59 +08:00
|
|
|
Ok(())
|
|
|
|
}
|
2022-10-17 23:04:52 +08:00
|
|
|
|
|
|
|
pub fn flush(mut self) -> Result<()> {
|
|
|
|
self.keys.flush()?;
|
|
|
|
Ok(())
|
|
|
|
}
|
2022-10-02 19:24:59 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
pub struct TaskWriter {
|
2022-10-16 07:39:01 +08:00
|
|
|
queue: BufWriter<File>,
|
2022-10-02 19:24:59 +08:00
|
|
|
update_files: PathBuf,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl TaskWriter {
|
|
|
|
pub(crate) fn new(path: PathBuf) -> Result<Self> {
|
|
|
|
std::fs::create_dir(&path)?;
|
|
|
|
|
|
|
|
let queue = File::create(path.join("queue.jsonl"))?;
|
|
|
|
let update_files = path.join("update_files");
|
|
|
|
std::fs::create_dir(&update_files)?;
|
|
|
|
|
2022-10-21 00:00:07 +08:00
|
|
|
Ok(TaskWriter { queue: BufWriter::new(queue), update_files })
|
2022-10-02 19:24:59 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Pushes tasks in the dump.
|
|
|
|
/// If the tasks has an associated `update_file` it'll use the `task_id` as its name.
|
2022-10-13 22:03:23 +08:00
|
|
|
pub fn push_task(&mut self, task: &TaskDump) -> Result<UpdateFile> {
|
2022-10-12 06:43:24 +08:00
|
|
|
self.queue.write_all(&serde_json::to_vec(task)?)?;
|
2022-10-02 19:24:59 +08:00
|
|
|
self.queue.write_all(b"\n")?;
|
2022-10-11 01:57:47 +08:00
|
|
|
|
2022-10-21 00:00:07 +08:00
|
|
|
Ok(UpdateFile::new(self.update_files.join(format!("{}.jsonl", task.uid))))
|
2022-10-11 01:57:47 +08:00
|
|
|
}
|
2022-10-17 23:04:52 +08:00
|
|
|
|
|
|
|
pub fn flush(mut self) -> Result<()> {
|
|
|
|
self.queue.flush()?;
|
|
|
|
Ok(())
|
|
|
|
}
|
2022-10-11 01:57:47 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
pub struct UpdateFile {
|
|
|
|
path: PathBuf,
|
|
|
|
writer: Option<BufWriter<File>>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl UpdateFile {
|
|
|
|
pub(crate) fn new(path: PathBuf) -> UpdateFile {
|
|
|
|
UpdateFile { path, writer: None }
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn push_document(&mut self, document: &Document) -> Result<()> {
|
|
|
|
if let Some(writer) = self.writer.as_mut() {
|
|
|
|
writer.write_all(&serde_json::to_vec(document)?)?;
|
|
|
|
writer.write_all(b"\n")?;
|
|
|
|
} else {
|
|
|
|
let file = File::create(&self.path).unwrap();
|
|
|
|
self.writer = Some(BufWriter::new(file));
|
|
|
|
self.push_document(document)?;
|
2022-10-02 19:24:59 +08:00
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
2022-10-17 23:04:52 +08:00
|
|
|
|
|
|
|
pub fn flush(self) -> Result<()> {
|
|
|
|
if let Some(mut writer) = self.writer {
|
|
|
|
writer.flush()?;
|
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
2022-10-02 19:24:59 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
pub struct IndexWriter {
|
2022-10-16 07:54:49 +08:00
|
|
|
documents: BufWriter<File>,
|
2022-10-02 19:24:59 +08:00
|
|
|
settings: File,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl IndexWriter {
|
2022-10-05 01:13:30 +08:00
|
|
|
pub(self) fn new(path: PathBuf, metadata: &IndexMetadata) -> Result<Self> {
|
2022-10-02 19:24:59 +08:00
|
|
|
std::fs::create_dir(&path)?;
|
|
|
|
|
2022-10-05 01:13:30 +08:00
|
|
|
let metadata_file = File::create(path.join("metadata.json"))?;
|
|
|
|
serde_json::to_writer(metadata_file, metadata)?;
|
|
|
|
|
2022-10-02 19:24:59 +08:00
|
|
|
let documents = File::create(path.join("documents.jsonl"))?;
|
|
|
|
let settings = File::create(path.join("settings.json"))?;
|
|
|
|
|
2022-10-21 00:00:07 +08:00
|
|
|
Ok(IndexWriter { documents: BufWriter::new(documents), settings })
|
2022-10-02 19:24:59 +08:00
|
|
|
}
|
|
|
|
|
2022-10-04 00:50:06 +08:00
|
|
|
pub fn push_document(&mut self, document: &Map<String, Value>) -> Result<()> {
|
2022-10-17 23:04:52 +08:00
|
|
|
serde_json::to_writer(&mut self.documents, document)?;
|
2022-10-02 19:24:59 +08:00
|
|
|
self.documents.write_all(b"\n")?;
|
2022-10-17 23:04:52 +08:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn flush(&mut self) -> Result<()> {
|
2022-10-16 07:54:49 +08:00
|
|
|
self.documents.flush()?;
|
2022-10-02 19:24:59 +08:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2022-10-04 00:50:06 +08:00
|
|
|
pub fn settings(mut self, settings: &Settings<Checked>) -> Result<()> {
|
2022-10-02 19:24:59 +08:00
|
|
|
self.settings.write_all(&serde_json::to_vec(&settings)?)?;
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
pub(crate) mod test {
|
2022-10-21 00:00:07 +08:00
|
|
|
use std::fmt::Write;
|
|
|
|
use std::io::BufReader;
|
|
|
|
use std::path::Path;
|
|
|
|
use std::str::FromStr;
|
2022-10-02 19:24:59 +08:00
|
|
|
|
2022-10-04 00:50:06 +08:00
|
|
|
use flate2::bufread::GzDecoder;
|
2022-10-27 00:49:47 +08:00
|
|
|
use meili_snap::insta;
|
2022-10-12 00:26:49 +08:00
|
|
|
use meilisearch_types::settings::Unchecked;
|
2022-10-04 00:50:06 +08:00
|
|
|
|
2022-10-02 19:24:59 +08:00
|
|
|
use super::*;
|
2022-10-21 00:00:07 +08:00
|
|
|
use crate::reader::Document;
|
|
|
|
use crate::test::{
|
|
|
|
create_test_api_keys, create_test_documents, create_test_dump, create_test_instance_uid,
|
|
|
|
create_test_settings, create_test_tasks,
|
|
|
|
};
|
2022-10-02 19:24:59 +08:00
|
|
|
|
|
|
|
fn create_directory_hierarchy(dir: &Path) -> String {
|
|
|
|
let mut ret = String::new();
|
|
|
|
writeln!(ret, ".").unwrap();
|
|
|
|
ret.push_str(&_create_directory_hierarchy(dir, 0));
|
|
|
|
ret
|
|
|
|
}
|
|
|
|
|
|
|
|
fn _create_directory_hierarchy(dir: &Path, depth: usize) -> String {
|
|
|
|
let mut ret = String::new();
|
|
|
|
|
|
|
|
// the entries are not guarenteed to be returned in the same order thus we need to sort them.
|
2022-10-21 00:00:07 +08:00
|
|
|
let mut entries =
|
|
|
|
fs::read_dir(dir).unwrap().collect::<std::result::Result<Vec<_>, _>>().unwrap();
|
2022-10-02 19:24:59 +08:00
|
|
|
|
|
|
|
// I want the directories first and then sort by name.
|
|
|
|
entries.sort_by(|a, b| {
|
|
|
|
let (aft, bft) = (a.file_type().unwrap(), b.file_type().unwrap());
|
|
|
|
|
|
|
|
if aft.is_dir() && bft.is_dir() {
|
2022-10-26 20:32:19 +08:00
|
|
|
a.file_name().cmp(&b.file_name())
|
|
|
|
} else if aft.is_file() && bft.is_dir() {
|
2022-10-02 19:24:59 +08:00
|
|
|
std::cmp::Ordering::Greater
|
2022-10-26 20:32:19 +08:00
|
|
|
} else if bft.is_file() && aft.is_dir() {
|
2022-10-02 19:24:59 +08:00
|
|
|
std::cmp::Ordering::Less
|
|
|
|
} else {
|
2022-10-26 20:32:19 +08:00
|
|
|
a.file_name().cmp(&b.file_name())
|
2022-10-02 19:24:59 +08:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
for (idx, entry) in entries.iter().enumerate() {
|
|
|
|
let mut ident = String::new();
|
|
|
|
|
|
|
|
for _ in 0..depth {
|
2022-10-22 22:35:42 +08:00
|
|
|
ident.push('│');
|
2022-10-02 19:24:59 +08:00
|
|
|
ident.push_str(&" ".repeat(4));
|
|
|
|
}
|
|
|
|
if idx == entries.len() - 1 {
|
2022-10-22 22:35:42 +08:00
|
|
|
ident.push('└');
|
2022-10-02 19:24:59 +08:00
|
|
|
} else {
|
2022-10-22 22:35:42 +08:00
|
|
|
ident.push('├');
|
2022-10-02 19:24:59 +08:00
|
|
|
}
|
|
|
|
ident.push_str(&"-".repeat(4));
|
|
|
|
|
|
|
|
let name = entry.file_name().into_string().unwrap();
|
|
|
|
let file_type = entry.file_type().unwrap();
|
2022-10-22 23:17:09 +08:00
|
|
|
let is_dir = if file_type.is_dir() { "/" } else { "" };
|
2022-10-02 19:24:59 +08:00
|
|
|
|
|
|
|
assert!(!file_type.is_symlink());
|
|
|
|
writeln!(ret, "{ident} {name}{is_dir}").unwrap();
|
|
|
|
|
|
|
|
if file_type.is_dir() {
|
|
|
|
ret.push_str(&_create_directory_hierarchy(&entry.path(), depth + 1));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
ret
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_creating_dump() {
|
2022-10-04 00:50:06 +08:00
|
|
|
let file = create_test_dump();
|
|
|
|
let mut file = BufReader::new(file);
|
2022-10-02 19:24:59 +08:00
|
|
|
|
2022-10-04 00:50:06 +08:00
|
|
|
// ============ ensuring we wrote everything in the correct place.
|
2022-10-02 19:24:59 +08:00
|
|
|
let dump = tempfile::tempdir().unwrap();
|
|
|
|
|
|
|
|
let gz = GzDecoder::new(&mut file);
|
|
|
|
let mut tar = tar::Archive::new(gz);
|
|
|
|
tar.unpack(dump.path()).unwrap();
|
|
|
|
|
|
|
|
let dump_path = dump.path();
|
|
|
|
|
|
|
|
// ==== checking global file hierarchy (we want to be sure there isn't too many files or too few)
|
|
|
|
insta::assert_display_snapshot!(create_directory_hierarchy(dump_path), @r###"
|
|
|
|
.
|
|
|
|
├---- indexes/
|
|
|
|
│ └---- doggos/
|
2022-10-26 20:55:37 +08:00
|
|
|
│ │ ├---- documents.jsonl
|
2022-10-20 19:58:11 +08:00
|
|
|
│ │ ├---- metadata.json
|
2022-10-26 20:55:37 +08:00
|
|
|
│ │ └---- settings.json
|
2022-10-02 19:24:59 +08:00
|
|
|
├---- tasks/
|
|
|
|
│ ├---- update_files/
|
2022-10-11 01:57:47 +08:00
|
|
|
│ │ └---- 1.jsonl
|
2022-10-02 19:24:59 +08:00
|
|
|
│ └---- queue.jsonl
|
2023-07-06 20:22:29 +08:00
|
|
|
├---- experimental-features.json
|
2022-10-26 20:55:37 +08:00
|
|
|
├---- instance_uid.uuid
|
2022-10-02 19:24:59 +08:00
|
|
|
├---- keys.jsonl
|
2022-10-26 20:55:37 +08:00
|
|
|
└---- metadata.json
|
2022-10-02 19:24:59 +08:00
|
|
|
"###);
|
|
|
|
|
|
|
|
// ==== checking the top level infos
|
|
|
|
let metadata = fs::read_to_string(dump_path.join("metadata.json")).unwrap();
|
|
|
|
let metadata: Metadata = serde_json::from_str(&metadata).unwrap();
|
2022-12-07 20:26:02 +08:00
|
|
|
insta::assert_json_snapshot!(metadata, { ".dumpDate" => "[date]", ".dbVersion" => "[version]" }, @r###"
|
2022-10-02 19:24:59 +08:00
|
|
|
{
|
|
|
|
"dumpVersion": "V6",
|
2022-12-07 20:26:02 +08:00
|
|
|
"dbVersion": "[version]",
|
2022-10-02 19:24:59 +08:00
|
|
|
"dumpDate": "[date]"
|
|
|
|
}
|
|
|
|
"###);
|
|
|
|
|
2022-10-05 01:13:30 +08:00
|
|
|
let instance_uid = fs::read_to_string(dump_path.join("instance_uid.uuid")).unwrap();
|
2022-10-21 00:00:07 +08:00
|
|
|
assert_eq!(Uuid::from_str(&instance_uid).unwrap(), create_test_instance_uid());
|
2022-10-02 19:24:59 +08:00
|
|
|
|
|
|
|
// ==== checking the index
|
|
|
|
let docs = fs::read_to_string(dump_path.join("indexes/doggos/documents.jsonl")).unwrap();
|
2022-10-04 00:50:06 +08:00
|
|
|
for (document, expected) in docs.lines().zip(create_test_documents()) {
|
2022-10-21 00:00:07 +08:00
|
|
|
assert_eq!(serde_json::from_str::<Map<String, Value>>(document).unwrap(), expected);
|
2022-10-02 19:24:59 +08:00
|
|
|
}
|
|
|
|
let test_settings =
|
|
|
|
fs::read_to_string(dump_path.join("indexes/doggos/settings.json")).unwrap();
|
2022-10-04 00:50:06 +08:00
|
|
|
assert_eq!(
|
|
|
|
serde_json::from_str::<Settings<Unchecked>>(&test_settings).unwrap(),
|
|
|
|
create_test_settings().into_unchecked()
|
|
|
|
);
|
2022-10-05 01:13:30 +08:00
|
|
|
let metadata = fs::read_to_string(dump_path.join("indexes/doggos/metadata.json")).unwrap();
|
|
|
|
let metadata: IndexMetadata = serde_json::from_str(&metadata).unwrap();
|
|
|
|
insta::assert_json_snapshot!(metadata, { ".createdAt" => "[date]", ".updatedAt" => "[date]" }, @r###"
|
|
|
|
{
|
|
|
|
"uid": "doggo",
|
|
|
|
"primaryKey": null,
|
|
|
|
"createdAt": "[date]",
|
|
|
|
"updatedAt": "[date]"
|
|
|
|
}
|
|
|
|
"###);
|
2022-10-02 19:24:59 +08:00
|
|
|
|
|
|
|
// ==== checking the task queue
|
|
|
|
let tasks_queue = fs::read_to_string(dump_path.join("tasks/queue.jsonl")).unwrap();
|
2022-10-17 18:47:48 +08:00
|
|
|
for (task, expected) in tasks_queue.lines().zip(create_test_tasks()) {
|
|
|
|
assert_eq!(serde_json::from_str::<TaskDump>(task).unwrap(), expected.0);
|
2022-10-04 00:50:06 +08:00
|
|
|
|
|
|
|
if let Some(expected_update) = expected.1 {
|
2022-10-11 01:57:47 +08:00
|
|
|
let path = dump_path.join(format!("tasks/update_files/{}.jsonl", expected.0.uid));
|
2022-10-02 19:24:59 +08:00
|
|
|
println!("trying to open {}", path.display());
|
2022-10-11 00:57:27 +08:00
|
|
|
let update = fs::read_to_string(path).unwrap();
|
2022-10-21 00:00:07 +08:00
|
|
|
let documents: Vec<Document> =
|
|
|
|
update.lines().map(|line| serde_json::from_str(line).unwrap()).collect();
|
2022-10-11 00:57:27 +08:00
|
|
|
assert_eq!(documents, expected_update);
|
2022-10-02 19:24:59 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// ==== checking the keys
|
|
|
|
let keys = fs::read_to_string(dump_path.join("keys.jsonl")).unwrap();
|
2022-10-04 00:50:06 +08:00
|
|
|
for (key, expected) in keys.lines().zip(create_test_api_keys()) {
|
|
|
|
assert_eq!(serde_json::from_str::<Key>(key).unwrap(), expected);
|
2022-10-02 19:24:59 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|