first mostly working version

This commit is contained in:
Tamo 2022-10-16 01:39:01 +02:00 committed by Clément Renault
parent 6fae317277
commit cac924b663
No known key found for this signature in database
GPG key ID: 92ADA4E935E71FA4
18 changed files with 403 additions and 57 deletions

View file

@ -1,8 +1,10 @@
use meilisearch_types::{
error::ResponseError,
keys::Key,
milli::update::IndexDocumentsMethod,
settings::Unchecked,
tasks::{Details, KindWithContent, Status, Task, TaskId},
InstanceUid,
};
use serde::{Deserialize, Serialize};
use time::OffsetDateTime;
@ -12,7 +14,7 @@ mod reader;
mod writer;
pub use error::Error;
pub use reader::DumpReader;
pub use reader::{DumpReader, UpdateFile};
pub use writer::DumpWriter;
const CURRENT_DUMP_VERSION: Version = Version::V6;
@ -49,14 +51,13 @@ pub enum Version {
V6,
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[derive(Debug, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct TaskDump {
pub uid: TaskId,
#[serde(default)]
pub index_uid: Option<String>,
pub status: Status,
// TODO use our own Kind for the user
#[serde(rename = "type")]
pub kind: KindDump,
@ -82,7 +83,7 @@ pub struct TaskDump {
}
// A `Kind` specific version made for the dump. If modified you may break the dump.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[derive(Debug, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub enum KindDump {
DocumentImport {
@ -118,7 +119,9 @@ pub enum KindDump {
query: String,
tasks: Vec<TaskId>,
},
DumpExport,
DumpExport {
dump_uid: String,
},
Snapshot,
}
@ -177,7 +180,7 @@ impl From<KindWithContent> for KindDump {
KindWithContent::IndexSwap { lhs, rhs } => KindDump::IndexSwap { lhs, rhs },
KindWithContent::CancelTask { tasks } => KindDump::CancelTask { tasks },
KindWithContent::DeleteTasks { query, tasks } => KindDump::DeleteTasks { query, tasks },
KindWithContent::DumpExport { .. } => KindDump::DumpExport,
KindWithContent::DumpExport { dump_uid, .. } => KindDump::DumpExport { dump_uid },
KindWithContent::Snapshot => KindDump::Snapshot,
}
}
@ -206,8 +209,7 @@ pub(crate) mod test {
use uuid::Uuid;
use crate::{
reader::{self, Document},
DumpReader, DumpWriter, IndexMetadata, KindDump, TaskDump, Version,
reader::Document, DumpReader, DumpWriter, IndexMetadata, KindDump, TaskDump, Version,
};
pub fn create_test_instance_uid() -> Uuid {

View file

@ -116,7 +116,9 @@ impl CompatV5ToV6 {
allow_index_creation,
settings: settings.into(),
},
v5::tasks::TaskContent::Dump { .. } => v6::Kind::DumpExport,
v5::tasks::TaskContent::Dump { uid } => {
v6::Kind::DumpExport { dump_uid: uid }
}
},
details: task_view.details.map(|details| match details {
v5::Details::DocumentAddition {
@ -412,7 +414,7 @@ pub(crate) mod test {
// tasks
let tasks = dump.tasks().collect::<Result<Vec<_>>>().unwrap();
let (tasks, update_files): (Vec<_>, Vec<_>) = tasks.into_iter().unzip();
meili_snap::snapshot_hash!(meili_snap::json_string!(tasks), @"0fff3c32487e3d3058d51ed951c1057f");
meili_snap::snapshot_hash!(meili_snap::json_string!(tasks), @"84d5b8eb31735d643483fcee28080edf");
assert_eq!(update_files.len(), 22);
assert!(update_files[0].is_none()); // the dump creation
assert!(update_files[1].is_some()); // the enqueued document addition

View file

@ -203,7 +203,7 @@ pub(crate) mod test {
// tasks
let tasks = dump.tasks().collect::<Result<Vec<_>>>().unwrap();
let (tasks, update_files): (Vec<_>, Vec<_>) = tasks.into_iter().unzip();
meili_snap::snapshot_hash!(meili_snap::json_string!(tasks), @"0fff3c32487e3d3058d51ed951c1057f");
meili_snap::snapshot_hash!(meili_snap::json_string!(tasks), @"84d5b8eb31735d643483fcee28080edf");
assert_eq!(update_files.len(), 22);
assert!(update_files[0].is_none()); // the dump creation
assert!(update_files[1].is_some()); // the enqueued document addition

View file

@ -109,7 +109,7 @@ impl V6Reader {
&mut self,
) -> Box<dyn Iterator<Item = Result<(Task, Option<Box<super::UpdateFile>>)>> + '_> {
Box::new((&mut self.tasks).lines().map(|line| -> Result<_> {
let task: Task = serde_json::from_str(&line?)?;
let task: Task = serde_json::from_str(&dbg!(line?)).unwrap();
let update_file_path = self
.dump
@ -121,7 +121,8 @@ impl V6Reader {
if update_file_path.exists() {
Ok((
task,
Some(Box::new(UpdateFile::new(&update_file_path)?) as Box<super::UpdateFile>),
Some(Box::new(UpdateFile::new(&update_file_path).unwrap())
as Box<super::UpdateFile>),
))
} else {
Ok((task, None))

View file

@ -71,24 +71,26 @@ impl DumpWriter {
}
pub struct KeyWriter {
file: File,
keys: BufWriter<File>,
}
impl KeyWriter {
pub(crate) fn new(path: PathBuf) -> Result<Self> {
let file = File::create(path.join("keys.jsonl"))?;
Ok(KeyWriter { file })
let keys = File::create(path.join("keys.jsonl"))?;
Ok(KeyWriter {
keys: BufWriter::new(keys),
})
}
pub fn push_key(&mut self, key: &Key) -> Result<()> {
self.file.write_all(&serde_json::to_vec(key)?)?;
self.file.write_all(b"\n")?;
self.keys.write_all(&serde_json::to_vec(key)?)?;
self.keys.write_all(b"\n")?;
Ok(())
}
}
pub struct TaskWriter {
queue: File,
queue: BufWriter<File>,
update_files: PathBuf,
}
@ -101,7 +103,7 @@ impl TaskWriter {
std::fs::create_dir(&update_files)?;
Ok(TaskWriter {
queue,
queue: BufWriter::new(queue),
update_files,
})
}
@ -111,6 +113,7 @@ impl TaskWriter {
pub fn push_task(&mut self, task: &TaskDump) -> Result<UpdateFile> {
self.queue.write_all(&serde_json::to_vec(task)?)?;
self.queue.write_all(b"\n")?;
self.queue.flush()?;
Ok(UpdateFile::new(
self.update_files.join(format!("{}.jsonl", task.uid)),