use serde_json::to_writer instead of serializing + writing

This commit is contained in:
Tamo 2025-02-11 10:48:04 +01:00
parent 9293e7f2c1
commit 00eb47d42e

View File

@ -93,7 +93,7 @@ impl KeyWriter {
} }
pub fn push_key(&mut self, key: &Key) -> Result<()> { pub fn push_key(&mut self, key: &Key) -> Result<()> {
self.keys.write_all(&serde_json::to_vec(key)?)?; serde_json::to_writer(&mut self.keys, &key)?;
self.keys.write_all(b"\n")?; self.keys.write_all(b"\n")?;
Ok(()) Ok(())
} }
@ -123,7 +123,7 @@ impl TaskWriter {
/// Pushes tasks in the dump. /// Pushes tasks in the dump.
/// If the tasks has an associated `update_file` it'll use the `task_id` as its name. /// If the tasks has an associated `update_file` it'll use the `task_id` as its name.
pub fn push_task(&mut self, task: &TaskDump) -> Result<UpdateFile> { pub fn push_task(&mut self, task: &TaskDump) -> Result<UpdateFile> {
self.queue.write_all(&serde_json::to_vec(task)?)?; serde_json::to_writer(&mut self.queue, &task)?;
self.queue.write_all(b"\n")?; self.queue.write_all(b"\n")?;
Ok(UpdateFile::new(self.update_files.join(format!("{}.jsonl", task.uid)))) Ok(UpdateFile::new(self.update_files.join(format!("{}.jsonl", task.uid))))
@ -148,7 +148,7 @@ impl BatchWriter {
/// Pushes batches in the dump. /// Pushes batches in the dump.
pub fn push_batch(&mut self, batch: &Batch) -> Result<()> { pub fn push_batch(&mut self, batch: &Batch) -> Result<()> {
self.queue.write_all(&serde_json::to_vec(batch)?)?; serde_json::to_writer(&mut self.queue, &batch)?;
self.queue.write_all(b"\n")?; self.queue.write_all(b"\n")?;
Ok(()) Ok(())
} }
@ -170,8 +170,8 @@ impl UpdateFile {
} }
pub fn push_document(&mut self, document: &Document) -> Result<()> { pub fn push_document(&mut self, document: &Document) -> Result<()> {
if let Some(writer) = self.writer.as_mut() { if let Some(mut writer) = self.writer.as_mut() {
writer.write_all(&serde_json::to_vec(document)?)?; serde_json::to_writer(&mut writer, &document)?;
writer.write_all(b"\n")?; writer.write_all(b"\n")?;
} else { } else {
let file = File::create(&self.path).unwrap(); let file = File::create(&self.path).unwrap();