Skip to content
This repository has been archived by the owner on Feb 21, 2024. It is now read-only.

Commit

Permalink
on_progress_change for plotting (paritytech#249)
Browse files Browse the repository at this point in the history
  • Loading branch information
ozgunozerk authored Feb 10, 2022
1 parent c963750 commit 1d71239
Showing 1 changed file with 27 additions and 0 deletions.
27 changes: 27 additions & 0 deletions crates/subspace-farmer/src/plot.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#[cfg(test)]
mod tests;

use event_listener_primitives::{Bag, HandlerId};
use log::error;
use rocksdb::DB;
use std::fs::OpenOptions;
Expand All @@ -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<Arc<dyn Fn(&PlottedPieces) + Send + Sync + 'static>, PlottedPieces>,
}

#[derive(Debug)]
enum ReadRequests {
ReadEncoding {
Expand All @@ -48,6 +59,7 @@ enum WriteRequests {

struct Inner {
any_requests_sender: Option<mpsc::SyncSender<()>>,
handlers: Handlers,
read_requests_sender: Option<mpsc::SyncSender<ReadRequests>>,
write_requests_sender: Option<mpsc::SyncSender<WriteRequests>>,
plot_metadata_db: Option<Arc<DB>>,
Expand Down Expand Up @@ -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)),
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -333,6 +353,13 @@ impl Plot {
)
})?
}

pub fn on_progress_change(
&self,
callback: Arc<dyn Fn(&PlottedPieces) + Send + Sync + 'static>,
) -> HandlerId {
self.inner.handlers.progress_change.add(callback)
}
}

#[derive(Clone)]
Expand Down

0 comments on commit 1d71239

Please sign in to comment.