From 1d71239ad955d2d455e7c8c65167bf478fb0487f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=96zg=C3=BCn=20=C3=96zerk?= Date: Thu, 10 Feb 2022 15:36:44 +0300 Subject: [PATCH] on_progress_change for plotting (#249) --- crates/subspace-farmer/src/plot.rs | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/crates/subspace-farmer/src/plot.rs b/crates/subspace-farmer/src/plot.rs index 67ea0c293adbb..d60f4fd3d303e 100644 --- a/crates/subspace-farmer/src/plot.rs +++ b/crates/subspace-farmer/src/plot.rs @@ -1,6 +1,7 @@ #[cfg(test)] mod tests; +use event_listener_primitives::{Bag, HandlerId}; use log::error; use rocksdb::DB; use std::fs::OpenOptions; @@ -23,6 +24,16 @@ pub enum PlotError { MetadataDbOpen(rocksdb::Error), } +#[derive(Debug, Copy, Clone)] +pub struct PlottedPieces { + pub plotted_piece_count: usize, +} + +#[derive(Default, Debug)] +struct Handlers { + progress_change: Bag, PlottedPieces>, +} + #[derive(Debug)] enum ReadRequests { ReadEncoding { @@ -48,6 +59,7 @@ enum WriteRequests { struct Inner { any_requests_sender: Option>, + handlers: Handlers, read_requests_sender: Option>, write_requests_sender: Option>, plot_metadata_db: Option>, @@ -179,6 +191,7 @@ impl Plot { let inner = Inner { any_requests_sender: Some(any_requests_sender), + handlers: Handlers::default(), read_requests_sender: Some(read_requests_sender), write_requests_sender: Some(write_requests_sender), plot_metadata_db: Some(Arc::new(plot_metadata_db)), @@ -239,6 +252,13 @@ impl Plot { if encodings.is_empty() { return Ok(()); } + self.inner + .handlers + .progress_change + .call_simple(&PlottedPieces { + plotted_piece_count: encodings.len(), + }); + let (result_sender, result_receiver) = mpsc::channel(); self.inner @@ -333,6 +353,13 @@ impl Plot { ) })? } + + pub fn on_progress_change( + &self, + callback: Arc, + ) -> HandlerId { + self.inner.handlers.progress_change.add(callback) + } } #[derive(Clone)]