From 6dcc5851b5c78a37a7d65788771ce0ff70a6c68e Mon Sep 17 00:00:00 2001 From: Tamo Date: Thu, 6 Oct 2022 14:24:28 +0200 Subject: [PATCH] get rids of the trait in most places --- dump/src/reader/mod.rs | 70 ++----- dump/src/reader/v5/mod.rs | 71 ++----- ...mp__reader__v5__test__read_dump_v5-12.snap | 2 +- ...ump__reader__v5__test__read_dump_v5-6.snap | 2 +- ...ump__reader__v5__test__read_dump_v5-9.snap | 2 +- dump/src/reader/v6.rs | 192 +++++++++--------- 6 files changed, 131 insertions(+), 208 deletions(-) diff --git a/dump/src/reader/mod.rs b/dump/src/reader/mod.rs index 5a44c2aac..e6decbf7d 100644 --- a/dump/src/reader/mod.rs +++ b/dump/src/reader/mod.rs @@ -10,32 +10,20 @@ use tempfile::TempDir; use time::OffsetDateTime; use uuid::Uuid; -use crate::reader::compat::Compat; +// use crate::reader::compat::Compat; use crate::{IndexMetadata, Result, Version}; // use self::loaders::{v2, v3, v4, v5}; // pub mod error; -mod compat; +// mod compat; // mod loaders; // mod v1; pub(self) mod v4; pub(self) mod v5; pub(self) mod v6; -pub fn open( - dump: impl Read, -) -> Result< - Box< - dyn DumpReader< - Document = serde_json::Map, - Settings = v6::Settings, - Task = TaskView, - UpdateFile = File, - Key = Key, - >, - >, -> { +pub fn open(dump: impl Read) -> Result> { let path = TempDir::new()?; let mut dump = BufReader::new(dump); let gz = GzDecoder::new(&mut dump); @@ -57,6 +45,7 @@ pub fn open( Version::V3 => todo!(), Version::V4 => todo!(), Version::V5 => { + /* let dump_reader = Box::new(v5::V5Reader::open(path)?); let dump_reader = Box::new(Compat::< dyn DumpReader< @@ -77,33 +66,14 @@ pub fn open( >, >; Ok(dump_reader) + */ + todo!() } - Version::V6 => { - let dump_reader = Box::new(v6::V6Reader::open(path)?) - as Box< - dyn DumpReader< - Document = v6::Document, - Settings = v6::Settings, - Task = v6::Task, - UpdateFile = v6::UpdateFile, - Key = v6::Key, - >, - >; - - Ok(dump_reader) - } + Version::V6 => Ok(Box::new(v6::V6Reader::open(path)?)), } } pub trait DumpReader { - type Document; - type Settings; - - type Task; - type UpdateFile; - - type Key; - /// Return the version of the dump. fn version(&self) -> Version; @@ -114,35 +84,19 @@ pub trait DumpReader { fn instance_uid(&self) -> Result>; /// Return an iterator over each indexes. - fn indexes( - &self, - ) -> Result< - Box< - dyn Iterator< - Item = Result< - Box< - dyn IndexReader - + '_, - >, - >, - > + '_, - >, - >; + fn indexes(&self) -> Result>> + '_>>; /// Return all the tasks in the dump with a possible update file. fn tasks( &mut self, - ) -> Box)>> + '_>; + ) -> Box)>> + '_>; /// Return all the keys. - fn keys(&mut self) -> Box> + '_>; + fn keys(&mut self) -> Box> + '_>; } pub trait IndexReader { - type Document; - type Settings; - fn metadata(&self) -> &IndexMetadata; - fn documents(&mut self) -> Result> + '_>>; - fn settings(&mut self) -> Result; + fn documents(&mut self) -> Result> + '_>>; + fn settings(&mut self) -> Result>; } diff --git a/dump/src/reader/v5/mod.rs b/dump/src/reader/v5/mod.rs index 984850ffc..61dab43e5 100644 --- a/dump/src/reader/v5/mod.rs +++ b/dump/src/reader/v5/mod.rs @@ -99,16 +99,6 @@ impl V5Reader { dump, }) } -} - -impl DumpReader for V5Reader { - type Document = serde_json::Map; - type Settings = Settings; - - type Task = Task; - type UpdateFile = File; - - type Key = Key; fn version(&self) -> Version { Version::V5 @@ -123,24 +113,9 @@ impl DumpReader for V5Reader { Ok(Some(Uuid::parse_str(&uuid)?)) } - fn indexes( - &self, - ) -> Result< - 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( + fn indexes(&self) -> Result> + '_> { + Ok(self.index_uuid.iter().map(|index| -> Result<_> { + Ok(V5IndexReader::new( index.uid.clone(), &self .dump @@ -148,17 +123,12 @@ impl DumpReader for V5Reader { .join("indexes") .join(index.index_meta.uuid.to_string()), )?) - as Box< - dyn IndexReader, - >) - }))) + })) } - fn tasks( - &mut self, - ) -> Box)>> + '_> { - Box::new((&mut self.tasks).lines().map(|line| -> Result<_> { - let task: Self::Task = serde_json::from_str(&line?)?; + fn tasks(&mut self) -> impl Iterator)>> + '_ { + (&mut self.tasks).lines().map(|line| -> Result<_> { + let task: Task = serde_json::from_str(&line?)?; if !task.is_finished() { if let Some(uuid) = task.get_content_uuid() { let update_file_path = self @@ -175,15 +145,13 @@ impl DumpReader for V5Reader { } else { Ok((task, None)) } - })) + }) } - fn keys(&mut self) -> Box> + '_> { - Box::new( - (&mut self.keys) - .lines() - .map(|line| -> Result<_> { Ok(serde_json::from_str(&line?)?) }), - ) + fn keys(&mut self) -> impl Iterator> + '_ { + (&mut self.keys) + .lines() + .map(|line| -> Result<_> { Ok(serde_json::from_str(&line?)?) }) } } @@ -215,23 +183,18 @@ impl V5IndexReader { Ok(ret) } -} - -impl IndexReader for V5IndexReader { - type Document = serde_json::Map; - type Settings = Settings; fn metadata(&self) -> &IndexMetadata { &self.metadata } - fn documents(&mut self) -> Result> + '_>> { - Ok(Box::new((&mut self.documents).lines().map( - |line| -> Result<_> { Ok(serde_json::from_str(&line?)?) }, - ))) + fn documents(&mut self) -> Result> + '_> { + Ok((&mut self.documents) + .lines() + .map(|line| -> Result<_> { Ok(serde_json::from_str(&line?)?) })) } - fn settings(&mut self) -> Result { + fn settings(&mut self) -> Result> { Ok(self.settings.clone()) } } diff --git a/dump/src/reader/v5/snapshots/dump__reader__v5__test__read_dump_v5-12.snap b/dump/src/reader/v5/snapshots/dump__reader__v5__test__read_dump_v5-12.snap index 740690903..d71100f6f 100644 --- a/dump/src/reader/v5/snapshots/dump__reader__v5__test__read_dump_v5-12.snap +++ b/dump/src/reader/v5/snapshots/dump__reader__v5__test__read_dump_v5-12.snap @@ -66,6 +66,6 @@ Ok( ), }, ), - _kind: PhantomData, + _kind: PhantomData, }, ) diff --git a/dump/src/reader/v5/snapshots/dump__reader__v5__test__read_dump_v5-6.snap b/dump/src/reader/v5/snapshots/dump__reader__v5__test__read_dump_v5-6.snap index 246e6368e..a97392c22 100644 --- a/dump/src/reader/v5/snapshots/dump__reader__v5__test__read_dump_v5-6.snap +++ b/dump/src/reader/v5/snapshots/dump__reader__v5__test__read_dump_v5-6.snap @@ -80,6 +80,6 @@ Ok( ), }, ), - _kind: PhantomData, + _kind: PhantomData, }, ) diff --git a/dump/src/reader/v5/snapshots/dump__reader__v5__test__read_dump_v5-9.snap b/dump/src/reader/v5/snapshots/dump__reader__v5__test__read_dump_v5-9.snap index ea8aacdc0..50902304d 100644 --- a/dump/src/reader/v5/snapshots/dump__reader__v5__test__read_dump_v5-9.snap +++ b/dump/src/reader/v5/snapshots/dump__reader__v5__test__read_dump_v5-9.snap @@ -72,6 +72,6 @@ Ok( ), }, ), - _kind: PhantomData, + _kind: PhantomData, }, ) diff --git a/dump/src/reader/v6.rs b/dump/src/reader/v6.rs index d031cfec7..c3db72df1 100644 --- a/dump/src/reader/v6.rs +++ b/dump/src/reader/v6.rs @@ -54,12 +54,6 @@ pub struct V6Reader { keys: BufReader, } -struct V6IndexReader { - metadata: IndexMetadata, - documents: BufReader, - settings: BufReader, -} - impl V6Reader { pub fn open(dump: TempDir) -> Result { let meta_file = fs::read(dump.path().join("metadata.json"))?; @@ -74,31 +68,6 @@ impl V6Reader { dump, }) } -} - -impl V6IndexReader { - pub fn new(name: String, path: &Path) -> Result { - 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; - type Settings = Settings; - - type Task = Task; - type UpdateFile = File; - - type Key = Key; - fn version(&self) -> Version { Version::V6 } @@ -111,55 +80,30 @@ impl DumpReader for V6Reader { Ok(Some(self.instance_uid)) } - fn indexes( - &self, - ) -> Result< - Box< - dyn Iterator< - Item = Result< - Box< - dyn super::IndexReader< - Document = Self::Document, - Settings = Self::Settings, - > + '_, - >, - >, - > + '_, - >, - > { + fn indexes(&self) -> Result> + '_> { let entries = fs::read_dir(self.dump.path().join("indexes"))?; - Ok(Box::new( - entries - .map(|entry| -> Result> { - let entry = entry?; - if entry.file_type()?.is_dir() { - let index = Box::new(V6IndexReader::new( - entry - .file_name() - .to_str() - .ok_or(Error::BadIndexName)? - .to_string(), - &entry.path(), - )?) - as Box< - dyn IndexReader< - Document = Self::Document, - Settings = Self::Settings, - >, - >; - Ok(Some(index)) - } else { - Ok(None) - } - }) - .filter_map(|entry| entry.transpose()), - )) + Ok(entries + .map(|entry| -> Result> { + let entry = entry?; + if entry.file_type()?.is_dir() { + let index = V6IndexReader::new( + entry + .file_name() + .to_str() + .ok_or(Error::BadIndexName)? + .to_string(), + &entry.path(), + )?; + Ok(Some(index)) + } else { + Ok(None) + } + }) + .filter_map(|entry| entry.transpose())) } - fn tasks( - &mut self, - ) -> Box)>> + '_> { - Box::new((&mut self.tasks).lines().map(|line| -> Result<_> { + fn tasks(&mut self) -> impl Iterator)>> + '_ { + (&mut self.tasks).lines().map(|line| -> Result<_> { 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`. if let Some((started_at, finished_at)) = task.started_at.zip(task.finished_at) { @@ -177,34 +121,96 @@ impl DumpReader for V6Reader { } else { Ok((task, None)) } - })) + }) } - fn keys(&mut self) -> Box> + '_> { - Box::new( - (&mut self.keys) - .lines() - .map(|line| -> Result<_> { Ok(serde_json::from_str(&line?)?) }), - ) + fn keys(&mut self) -> impl Iterator> + '_ { + (&mut self.keys) + .lines() + .map(|line| -> Result<_> { Ok(serde_json::from_str(&line?)?) }) } } -impl IndexReader for V6IndexReader { - type Document = serde_json::Map; - type Settings = Settings; +impl DumpReader for V6Reader { + fn version(&self) -> Version { + self.version() + } + + fn date(&self) -> Option { + self.date() + } + + fn instance_uid(&self) -> Result> { + self.instance_uid() + } + + fn indexes( + &self, + ) -> Result>> + '_>> { + self.indexes().map(|iter| { + Box::new(iter.map(|result| { + result.map(|index| Box::new(index) as Box) + })) as Box> + }) + } + + fn tasks( + &mut self, + ) -> Box)>> + '_> { + Box::new(self.tasks()) + } + + fn keys(&mut self) -> Box> + '_> { + Box::new(self.keys()) + } +} + +struct V6IndexReader { + metadata: IndexMetadata, + documents: BufReader, + settings: BufReader, +} + +impl V6IndexReader { + pub fn new(name: String, path: &Path) -> Result { + 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 { &self.metadata } - fn documents(&mut self) -> Result> + '_>> { - Ok(Box::new((&mut self.documents).lines().map( - |line| -> Result<_> { Ok(serde_json::from_str(&line?)?) }, - ))) + fn documents(&mut self) -> Result> + '_> { + Ok((&mut self.documents) + .lines() + .map(|line| -> Result<_> { Ok(serde_json::from_str(&line?)?) })) } - fn settings(&mut self) -> Result { - let settings: index::Settings = serde_json::from_reader(&mut self.settings)?; + fn settings(&mut self) -> Result> { + let settings: Settings = serde_json::from_reader(&mut self.settings)?; Ok(settings.check()) } } + +impl IndexReader for V6IndexReader { + fn metadata(&self) -> &IndexMetadata { + self.metadata() + } + + fn documents(&mut self) -> Result> + '_>> { + self.documents() + .map(|iter| Box::new(iter) as Box> + '_>) + } + + fn settings(&mut self) -> Result> { + self.settings() + } +}