MeiliSearch/dump/src/reader/mod.rs

82 lines
2.4 KiB
Rust
Raw Normal View History

2022-10-03 18:50:06 +02:00
use std::io::Read;
use std::{fs::File, io::BufReader};
use flate2::bufread::GzDecoder;
use index_scheduler::TaskView;
use meilisearch_auth::Key;
use serde::Deserialize;
use tempfile::TempDir;
use time::OffsetDateTime;
2022-10-03 18:50:06 +02:00
use uuid::Uuid;
2022-10-06 14:24:28 +02:00
// use crate::reader::compat::Compat;
2022-10-04 19:13:30 +02:00
use crate::{IndexMetadata, Result, Version};
use self::compat::Compat;
// use self::loaders::{v2, v3, v4, v5};
// pub mod error;
2022-10-06 14:41:21 +02:00
mod compat;
// mod loaders;
2022-10-03 16:12:01 +02:00
// mod v1;
2022-10-07 16:43:05 +02:00
pub(self) mod v3;
pub(self) mod v4;
pub(self) mod v5;
pub(self) mod v6;
pub fn open(dump: impl Read) -> Result<Compat> {
let path = TempDir::new()?;
let mut dump = BufReader::new(dump);
let gz = GzDecoder::new(&mut dump);
let mut archive = tar::Archive::new(gz);
archive.unpack(path.path())?;
#[derive(Deserialize)]
2022-10-03 18:50:06 +02:00
#[serde(rename_all = "camelCase")]
struct MetadataVersion {
pub dump_version: Version,
}
let mut meta_file = File::open(path.path().join("metadata.json"))?;
let MetadataVersion { dump_version } = serde_json::from_reader(&mut meta_file)?;
match dump_version {
// Version::V1 => Ok(Box::new(v1::Reader::open(path)?)),
Version::V1 => todo!(),
Version::V2 => todo!(),
Version::V3 => Ok(v3::V3Reader::open(path)?.to_v4().to_v5().to_v6().into()),
Version::V4 => Ok(v4::V4Reader::open(path)?.to_v5().to_v6().into()),
Version::V5 => Ok(v5::V5Reader::open(path)?.to_v6().into()),
Version::V6 => Ok(v6::V6Reader::open(path)?.into()),
2022-10-03 16:12:01 +02:00
}
}
pub trait DumpReader {
/// Return the version of the dump.
fn version(&self) -> Version;
2022-10-03 18:50:06 +02:00
/// Return at which date the dump was created if there was one.
2022-10-03 16:12:01 +02:00
fn date(&self) -> Option<OffsetDateTime>;
2022-10-03 18:50:06 +02:00
/// Return the instance-uid if there was one.
fn instance_uid(&self) -> Result<Option<Uuid>>;
/// Return an iterator over each indexes.
2022-10-06 14:24:28 +02:00
fn indexes(&self) -> Result<Box<dyn Iterator<Item = Result<Box<dyn IndexReader + '_>>> + '_>>;
/// Return all the tasks in the dump with a possible update file.
fn tasks(
2022-10-03 16:12:01 +02:00
&mut self,
2022-10-06 14:24:28 +02:00
) -> Box<dyn Iterator<Item = Result<(v6::Task, Option<v6::UpdateFile>)>> + '_>;
/// Return all the keys.
2022-10-06 14:24:28 +02:00
fn keys(&mut self) -> Box<dyn Iterator<Item = Result<v6::Key>> + '_>;
}
pub trait IndexReader {
2022-10-04 19:13:30 +02:00
fn metadata(&self) -> &IndexMetadata;
2022-10-06 14:24:28 +02:00
fn documents(&mut self) -> Result<Box<dyn Iterator<Item = Result<v6::Document>> + '_>>;
fn settings(&mut self) -> Result<v6::Settings<v6::Checked>>;
}