add db::Writer::load

This commit is contained in:
Dmitry Belyaev 2022-10-09 18:26:00 +03:00
parent 3b24a1be2f
commit f51f2be18f
2 changed files with 19 additions and 6 deletions

View File

@ -79,6 +79,7 @@ where
_t: PhantomData,
})
}
pub fn push(&mut self, item: T) -> Result<(), String> {
let pos: LSize = self.data_buf.position() as LSize;
@ -102,6 +103,18 @@ where
Ok(())
}
pub fn load<I>(&mut self, iter: &mut I) -> Result<(), String>
where
I: Iterator<Item = T>,
{
for item in iter {
self.push(item)?;
}
Ok(())
}
pub fn finish(mut self) -> Result<(), String> {
// finish tab
let pos: LSize = self.data_buf.position() as LSize;

View File

@ -269,16 +269,16 @@ fn db_writer2_task(rx: mpsc::Receiver<Question>) {
db::Writer::new(NEW_DB_FILENAME, writer_opts).expect("new db writer");
let mut num = 1;
rx.into_iter().for_each(|mut q| {
let mut iter = rx.iter().map(|mut q| {
q.num = num;
writer
.push(q)
.unwrap_or_else(|e| panic!("db writer push, num={num}, {e:#?}"));
num += 1;
q
});
writer
.load(&mut iter)
.unwrap_or_else(|e| panic!("db writer push, num={num}, {e:#?}"));
writer.finish().expect("db writer finish");
println!("write done");