get rids of the trait in most places

This commit is contained in:
Tamo 2022-10-06 14:24:28 +02:00 committed by Clément Renault
parent 0972587cfc
commit 6dcc5851b5
No known key found for this signature in database
GPG Key ID: 92ADA4E935E71FA4
6 changed files with 131 additions and 208 deletions

View File

@ -10,32 +10,20 @@ use tempfile::TempDir;
use time::OffsetDateTime; use time::OffsetDateTime;
use uuid::Uuid; use uuid::Uuid;
use crate::reader::compat::Compat; // use crate::reader::compat::Compat;
use crate::{IndexMetadata, Result, Version}; use crate::{IndexMetadata, Result, Version};
// use self::loaders::{v2, v3, v4, v5}; // use self::loaders::{v2, v3, v4, v5};
// pub mod error; // pub mod error;
mod compat; // mod compat;
// mod loaders; // mod loaders;
// mod v1; // mod v1;
pub(self) mod v4; pub(self) mod v4;
pub(self) mod v5; pub(self) mod v5;
pub(self) mod v6; pub(self) mod v6;
pub fn open( pub fn open(dump: impl Read) -> Result<Box<dyn DumpReader>> {
dump: impl Read,
) -> Result<
Box<
dyn DumpReader<
Document = serde_json::Map<String, serde_json::Value>,
Settings = v6::Settings<v6::Checked>,
Task = TaskView,
UpdateFile = File,
Key = Key,
>,
>,
> {
let path = TempDir::new()?; let path = TempDir::new()?;
let mut dump = BufReader::new(dump); let mut dump = BufReader::new(dump);
let gz = GzDecoder::new(&mut dump); let gz = GzDecoder::new(&mut dump);
@ -57,6 +45,7 @@ pub fn open(
Version::V3 => todo!(), Version::V3 => todo!(),
Version::V4 => todo!(), Version::V4 => todo!(),
Version::V5 => { Version::V5 => {
/*
let dump_reader = Box::new(v5::V5Reader::open(path)?); let dump_reader = Box::new(v5::V5Reader::open(path)?);
let dump_reader = Box::new(Compat::< let dump_reader = Box::new(Compat::<
dyn DumpReader< dyn DumpReader<
@ -77,33 +66,14 @@ pub fn open(
>, >,
>; >;
Ok(dump_reader) Ok(dump_reader)
*/
todo!()
} }
Version::V6 => { Version::V6 => Ok(Box::new(v6::V6Reader::open(path)?)),
let dump_reader = Box::new(v6::V6Reader::open(path)?)
as Box<
dyn DumpReader<
Document = v6::Document,
Settings = v6::Settings<v6::Checked>,
Task = v6::Task,
UpdateFile = v6::UpdateFile,
Key = v6::Key,
>,
>;
Ok(dump_reader)
}
} }
} }
pub trait DumpReader { pub trait DumpReader {
type Document;
type Settings;
type Task;
type UpdateFile;
type Key;
/// Return the version of the dump. /// Return the version of the dump.
fn version(&self) -> Version; fn version(&self) -> Version;
@ -114,35 +84,19 @@ pub trait DumpReader {
fn instance_uid(&self) -> Result<Option<Uuid>>; fn instance_uid(&self) -> Result<Option<Uuid>>;
/// Return an iterator over each indexes. /// Return an iterator over each indexes.
fn indexes( fn indexes(&self) -> Result<Box<dyn Iterator<Item = Result<Box<dyn IndexReader + '_>>> + '_>>;
&self,
) -> Result<
Box<
dyn Iterator<
Item = Result<
Box<
dyn IndexReader<Document = Self::Document, Settings = Self::Settings>
+ '_,
>,
>,
> + '_,
>,
>;
/// Return all the tasks in the dump with a possible update file. /// Return all the tasks in the dump with a possible update file.
fn tasks( fn tasks(
&mut self, &mut self,
) -> Box<dyn Iterator<Item = Result<(Self::Task, Option<Self::UpdateFile>)>> + '_>; ) -> Box<dyn Iterator<Item = Result<(v6::Task, Option<v6::UpdateFile>)>> + '_>;
/// Return all the keys. /// Return all the keys.
fn keys(&mut self) -> Box<dyn Iterator<Item = Result<Self::Key>> + '_>; fn keys(&mut self) -> Box<dyn Iterator<Item = Result<v6::Key>> + '_>;
} }
pub trait IndexReader { pub trait IndexReader {
type Document;
type Settings;
fn metadata(&self) -> &IndexMetadata; fn metadata(&self) -> &IndexMetadata;
fn documents(&mut self) -> Result<Box<dyn Iterator<Item = Result<Self::Document>> + '_>>; fn documents(&mut self) -> Result<Box<dyn Iterator<Item = Result<v6::Document>> + '_>>;
fn settings(&mut self) -> Result<Self::Settings>; fn settings(&mut self) -> Result<v6::Settings<v6::Checked>>;
} }

View File

@ -99,16 +99,6 @@ impl V5Reader {
dump, dump,
}) })
} }
}
impl DumpReader for V5Reader {
type Document = serde_json::Map<String, serde_json::Value>;
type Settings = Settings<Checked>;
type Task = Task;
type UpdateFile = File;
type Key = Key;
fn version(&self) -> Version { fn version(&self) -> Version {
Version::V5 Version::V5
@ -123,24 +113,9 @@ impl DumpReader for V5Reader {
Ok(Some(Uuid::parse_str(&uuid)?)) Ok(Some(Uuid::parse_str(&uuid)?))
} }
fn indexes( fn indexes(&self) -> Result<impl Iterator<Item = Result<V5IndexReader>> + '_> {
&self, Ok(self.index_uuid.iter().map(|index| -> Result<_> {
) -> Result< Ok(V5IndexReader::new(
Box<
dyn Iterator<
Item = Result<
Box<
dyn super::IndexReader<
Document = Self::Document,
Settings = Self::Settings,
> + '_,
>,
>,
> + '_,
>,
> {
Ok(Box::new(self.index_uuid.iter().map(|index| -> Result<_> {
Ok(Box::new(V5IndexReader::new(
index.uid.clone(), index.uid.clone(),
&self &self
.dump .dump
@ -148,17 +123,12 @@ impl DumpReader for V5Reader {
.join("indexes") .join("indexes")
.join(index.index_meta.uuid.to_string()), .join(index.index_meta.uuid.to_string()),
)?) )?)
as Box< }))
dyn IndexReader<Document = Self::Document, Settings = Self::Settings>,
>)
})))
} }
fn tasks( fn tasks(&mut self) -> impl Iterator<Item = Result<(Task, Option<UpdateFile>)>> + '_ {
&mut self, (&mut self.tasks).lines().map(|line| -> Result<_> {
) -> Box<dyn Iterator<Item = Result<(Self::Task, Option<Self::UpdateFile>)>> + '_> { let task: Task = serde_json::from_str(&line?)?;
Box::new((&mut self.tasks).lines().map(|line| -> Result<_> {
let task: Self::Task = serde_json::from_str(&line?)?;
if !task.is_finished() { if !task.is_finished() {
if let Some(uuid) = task.get_content_uuid() { if let Some(uuid) = task.get_content_uuid() {
let update_file_path = self let update_file_path = self
@ -175,15 +145,13 @@ impl DumpReader for V5Reader {
} else { } else {
Ok((task, None)) Ok((task, None))
} }
})) })
} }
fn keys(&mut self) -> Box<dyn Iterator<Item = Result<Self::Key>> + '_> { fn keys(&mut self) -> impl Iterator<Item = Result<Key>> + '_ {
Box::new( (&mut self.keys)
(&mut self.keys) .lines()
.lines() .map(|line| -> Result<_> { Ok(serde_json::from_str(&line?)?) })
.map(|line| -> Result<_> { Ok(serde_json::from_str(&line?)?) }),
)
} }
} }
@ -215,23 +183,18 @@ impl V5IndexReader {
Ok(ret) Ok(ret)
} }
}
impl IndexReader for V5IndexReader {
type Document = serde_json::Map<String, serde_json::Value>;
type Settings = Settings<Checked>;
fn metadata(&self) -> &IndexMetadata { fn metadata(&self) -> &IndexMetadata {
&self.metadata &self.metadata
} }
fn documents(&mut self) -> Result<Box<dyn Iterator<Item = Result<Self::Document>> + '_>> { fn documents(&mut self) -> Result<impl Iterator<Item = Result<Document>> + '_> {
Ok(Box::new((&mut self.documents).lines().map( Ok((&mut self.documents)
|line| -> Result<_> { Ok(serde_json::from_str(&line?)?) }, .lines()
))) .map(|line| -> Result<_> { Ok(serde_json::from_str(&line?)?) }))
} }
fn settings(&mut self) -> Result<Self::Settings> { fn settings(&mut self) -> Result<Settings<Checked>> {
Ok(self.settings.clone()) Ok(self.settings.clone())
} }
} }

View File

@ -66,6 +66,6 @@ Ok(
), ),
}, },
), ),
_kind: PhantomData, _kind: PhantomData<dump::reader::v5::settings::Checked>,
}, },
) )

View File

@ -80,6 +80,6 @@ Ok(
), ),
}, },
), ),
_kind: PhantomData, _kind: PhantomData<dump::reader::v5::settings::Checked>,
}, },
) )

View File

@ -72,6 +72,6 @@ Ok(
), ),
}, },
), ),
_kind: PhantomData, _kind: PhantomData<dump::reader::v5::settings::Checked>,
}, },
) )

View File

@ -54,12 +54,6 @@ pub struct V6Reader {
keys: BufReader<File>, keys: BufReader<File>,
} }
struct V6IndexReader {
metadata: IndexMetadata,
documents: BufReader<File>,
settings: BufReader<File>,
}
impl V6Reader { impl V6Reader {
pub fn open(dump: TempDir) -> Result<Self> { pub fn open(dump: TempDir) -> Result<Self> {
let meta_file = fs::read(dump.path().join("metadata.json"))?; let meta_file = fs::read(dump.path().join("metadata.json"))?;
@ -74,31 +68,6 @@ impl V6Reader {
dump, dump,
}) })
} }
}
impl V6IndexReader {
pub fn new(name: String, path: &Path) -> Result<Self> {
let metadata = File::open(path.join("metadata.json"))?;
let ret = V6IndexReader {
metadata: serde_json::from_reader(metadata)?,
documents: BufReader::new(File::open(path.join("documents.jsonl"))?),
settings: BufReader::new(File::open(path.join("settings.json"))?),
};
Ok(ret)
}
}
impl DumpReader for V6Reader {
type Document = serde_json::Map<String, serde_json::Value>;
type Settings = Settings<Checked>;
type Task = Task;
type UpdateFile = File;
type Key = Key;
fn version(&self) -> Version { fn version(&self) -> Version {
Version::V6 Version::V6
} }
@ -111,55 +80,30 @@ impl DumpReader for V6Reader {
Ok(Some(self.instance_uid)) Ok(Some(self.instance_uid))
} }
fn indexes( fn indexes(&self) -> Result<impl Iterator<Item = Result<V6IndexReader>> + '_> {
&self,
) -> Result<
Box<
dyn Iterator<
Item = Result<
Box<
dyn super::IndexReader<
Document = Self::Document,
Settings = Self::Settings,
> + '_,
>,
>,
> + '_,
>,
> {
let entries = fs::read_dir(self.dump.path().join("indexes"))?; let entries = fs::read_dir(self.dump.path().join("indexes"))?;
Ok(Box::new( Ok(entries
entries .map(|entry| -> Result<Option<_>> {
.map(|entry| -> Result<Option<_>> { let entry = entry?;
let entry = entry?; if entry.file_type()?.is_dir() {
if entry.file_type()?.is_dir() { let index = V6IndexReader::new(
let index = Box::new(V6IndexReader::new( entry
entry .file_name()
.file_name() .to_str()
.to_str() .ok_or(Error::BadIndexName)?
.ok_or(Error::BadIndexName)? .to_string(),
.to_string(), &entry.path(),
&entry.path(), )?;
)?) Ok(Some(index))
as Box< } else {
dyn IndexReader< Ok(None)
Document = Self::Document, }
Settings = Self::Settings, })
>, .filter_map(|entry| entry.transpose()))
>;
Ok(Some(index))
} else {
Ok(None)
}
})
.filter_map(|entry| entry.transpose()),
))
} }
fn tasks( fn tasks(&mut self) -> impl Iterator<Item = Result<(Task, Option<UpdateFile>)>> + '_ {
&mut self, (&mut self.tasks).lines().map(|line| -> Result<_> {
) -> Box<dyn Iterator<Item = Result<(Self::Task, Option<Self::UpdateFile>)>> + '_> {
Box::new((&mut self.tasks).lines().map(|line| -> Result<_> {
let mut task: index_scheduler::TaskView = serde_json::from_str(&line?)?; let mut task: index_scheduler::TaskView = serde_json::from_str(&line?)?;
// TODO: this can be removed once we can `Deserialize` the duration from the `TaskView`. // TODO: this can be removed once we can `Deserialize` the duration from the `TaskView`.
if let Some((started_at, finished_at)) = task.started_at.zip(task.finished_at) { if let Some((started_at, finished_at)) = task.started_at.zip(task.finished_at) {
@ -177,34 +121,96 @@ impl DumpReader for V6Reader {
} else { } else {
Ok((task, None)) Ok((task, None))
} }
})) })
} }
fn keys(&mut self) -> Box<dyn Iterator<Item = Result<Self::Key>> + '_> { fn keys(&mut self) -> impl Iterator<Item = Result<Key>> + '_ {
Box::new( (&mut self.keys)
(&mut self.keys) .lines()
.lines() .map(|line| -> Result<_> { Ok(serde_json::from_str(&line?)?) })
.map(|line| -> Result<_> { Ok(serde_json::from_str(&line?)?) }),
)
} }
} }
impl IndexReader for V6IndexReader { impl DumpReader for V6Reader {
type Document = serde_json::Map<String, serde_json::Value>; fn version(&self) -> Version {
type Settings = Settings<Checked>; self.version()
}
fn date(&self) -> Option<OffsetDateTime> {
self.date()
}
fn instance_uid(&self) -> Result<Option<Uuid>> {
self.instance_uid()
}
fn indexes(
&self,
) -> Result<Box<dyn Iterator<Item = Result<Box<dyn super::IndexReader + '_>>> + '_>> {
self.indexes().map(|iter| {
Box::new(iter.map(|result| {
result.map(|index| Box::new(index) as Box<dyn super::IndexReader + '_>)
})) as Box<dyn Iterator<Item = _>>
})
}
fn tasks(
&mut self,
) -> Box<dyn Iterator<Item = Result<(self::Task, Option<self::UpdateFile>)>> + '_> {
Box::new(self.tasks())
}
fn keys(&mut self) -> Box<dyn Iterator<Item = Result<self::Key>> + '_> {
Box::new(self.keys())
}
}
struct V6IndexReader {
metadata: IndexMetadata,
documents: BufReader<File>,
settings: BufReader<File>,
}
impl V6IndexReader {
pub fn new(name: String, path: &Path) -> Result<Self> {
let metadata = File::open(path.join("metadata.json"))?;
let ret = V6IndexReader {
metadata: serde_json::from_reader(metadata)?,
documents: BufReader::new(File::open(path.join("documents.jsonl"))?),
settings: BufReader::new(File::open(path.join("settings.json"))?),
};
Ok(ret)
}
fn metadata(&self) -> &IndexMetadata { fn metadata(&self) -> &IndexMetadata {
&self.metadata &self.metadata
} }
fn documents(&mut self) -> Result<Box<dyn Iterator<Item = Result<Self::Document>> + '_>> { fn documents(&mut self) -> Result<impl Iterator<Item = Result<Document>> + '_> {
Ok(Box::new((&mut self.documents).lines().map( Ok((&mut self.documents)
|line| -> Result<_> { Ok(serde_json::from_str(&line?)?) }, .lines()
))) .map(|line| -> Result<_> { Ok(serde_json::from_str(&line?)?) }))
} }
fn settings(&mut self) -> Result<Self::Settings> { fn settings(&mut self) -> Result<Settings<Checked>> {
let settings: index::Settings<Unchecked> = serde_json::from_reader(&mut self.settings)?; let settings: Settings<Unchecked> = serde_json::from_reader(&mut self.settings)?;
Ok(settings.check()) Ok(settings.check())
} }
} }
impl IndexReader for V6IndexReader {
fn metadata(&self) -> &IndexMetadata {
self.metadata()
}
fn documents(&mut self) -> Result<Box<dyn Iterator<Item = Result<Document>> + '_>> {
self.documents()
.map(|iter| Box::new(iter) as Box<dyn Iterator<Item = Result<Document>> + '_>)
}
fn settings(&mut self) -> Result<Settings<Checked>> {
self.settings()
}
}