Skip to content

Commit 43b9842

Browse files
committed
introduce BitfieldUpdate and BitfieldState structs
enum is just a wrapper around them now
1 parent ced9b95 commit 43b9842

File tree

5 files changed

+120
-69
lines changed

5 files changed

+120
-69
lines changed

examples/multiprovider.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ use console::Term;
66
use iroh::{NodeId, SecretKey};
77
use iroh_blobs::{
88
downloader2::{
9-
print_bitmap, BitfieldEvent, DownloadRequest, Downloader, ObserveRequest,
10-
StaticContentDiscovery,
9+
print_bitmap, BitfieldEvent, BitfieldState, BitfieldUpdate, DownloadRequest, Downloader,
10+
ObserveRequest, StaticContentDiscovery,
1111
},
1212
store::Store,
1313
util::total_bytes,
@@ -117,10 +117,10 @@ impl BlobDownloadProgress {
117117

118118
fn update(&mut self, ev: BitfieldEvent) {
119119
match ev {
120-
BitfieldEvent::State { ranges, .. } => {
120+
BitfieldEvent::State(BitfieldState { ranges, .. }) => {
121121
self.current = ranges;
122122
}
123-
BitfieldEvent::Update { added, removed, .. } => {
123+
BitfieldEvent::Update(BitfieldUpdate { added, removed, .. }) => {
124124
self.current |= added;
125125
self.current -= removed;
126126
}

src/downloader2.rs

Lines changed: 43 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@ use std::{
1717
io,
1818
marker::PhantomData,
1919
sync::Arc,
20-
time::Instant, u64,
20+
time::Instant,
21+
u64,
2122
};
2223

2324
use crate::{
@@ -89,26 +90,32 @@ pub trait BitfieldSubscription: std::fmt::Debug + Send + 'static {
8990
pub type BoxedBitfieldSubscription = Box<dyn BitfieldSubscription>;
9091

9192
/// Events from observing a local bitfield
92-
#[derive(Debug, PartialEq, Eq)]
93+
#[derive(Debug, PartialEq, Eq, derive_more::From)]
9394
pub enum BitfieldEvent {
9495
/// The full state of the bitfield
95-
State {
96-
/// The entire bitfield
97-
ranges: ChunkRanges,
98-
/// The most precise known size of the blob.
99-
///
100-
/// If I know nothing about the blob, this is u64::MAX.
101-
size: u64,
102-
},
96+
State(BitfieldState),
10397
/// An update to the bitfield
104-
Update {
105-
/// The ranges that were added
106-
added: ChunkRanges,
107-
/// The ranges that were removed
108-
removed: ChunkRanges,
109-
/// A refinement of the size of the blob.
110-
size: u64,
111-
},
98+
Update(BitfieldUpdate),
99+
}
100+
101+
/// An update to a bitfield
102+
#[derive(Debug, PartialEq, Eq)]
103+
pub struct BitfieldUpdate {
104+
/// The ranges that were added
105+
pub added: ChunkRanges,
106+
/// The ranges that were removed
107+
pub removed: ChunkRanges,
108+
/// The total size of the bitfield in bytes
109+
pub size: u64,
110+
}
111+
112+
/// The state of a bitfield
113+
#[derive(Debug, PartialEq, Eq)]
114+
pub struct BitfieldState {
115+
/// The ranges that are set
116+
pub ranges: ChunkRanges,
117+
/// The total size of the bitfield in bytes
118+
pub size: u64,
112119
}
113120

114121
/// A download request
@@ -325,8 +332,14 @@ impl BitfieldSubscription for TestBitfieldSubscription {
325332
}
326333
};
327334
Box::pin(
328-
futures_lite::stream::once(BitfieldEvent::State { ranges, size: 1024 * 1024 * 1024 * 1024 * 1024 })
329-
.chain(futures_lite::stream::pending()),
335+
futures_lite::stream::once(
336+
BitfieldState {
337+
ranges,
338+
size: 1024 * 1024 * 1024 * 1024 * 1024,
339+
}
340+
.into(),
341+
)
342+
.chain(futures_lite::stream::pending()),
330343
)
331344
}
332345
}
@@ -353,9 +366,13 @@ impl<S> SimpleBitfieldSubscription<S> {
353366
async fn get_valid_ranges_local<S: Store>(hash: &Hash, store: S) -> anyhow::Result<BitfieldEvent> {
354367
if let Some(entry) = store.get_mut(hash).await? {
355368
let (ranges, size) = crate::get::db::valid_ranges_and_size::<S>(&entry).await?;
356-
Ok(BitfieldEvent::State { ranges, size })
369+
Ok(BitfieldState { ranges, size }.into())
357370
} else {
358-
Ok(BitfieldEvent::State { ranges: ChunkRanges::empty(), size: u64::MAX })
371+
Ok(BitfieldState {
372+
ranges: ChunkRanges::empty(),
373+
size: u64::MAX,
374+
}
375+
.into())
359376
}
360377
}
361378

@@ -368,7 +385,7 @@ async fn get_valid_ranges_remote(
368385
let (size, _) = crate::get::request::get_verified_size(&conn, hash).await?;
369386
let chunks = (size + 1023) / 1024;
370387
let ranges = ChunkRanges::from(ChunkNum(0)..ChunkNum(chunks));
371-
Ok(BitfieldEvent::State { ranges, size })
388+
Ok(BitfieldState { ranges, size }.into())
372389
}
373390

374391
impl<S: Store> BitfieldSubscription for SimpleBitfieldSubscription<S> {
@@ -406,10 +423,11 @@ impl<S: Store> BitfieldSubscription for SimpleBitfieldSubscription<S> {
406423
async move {
407424
let event = match recv.await {
408425
Ok(ev) => ev,
409-
Err(_) => BitfieldEvent::State {
426+
Err(_) => BitfieldState {
410427
ranges: ChunkRanges::empty(),
411428
size: u64::MAX,
412-
},
429+
}
430+
.into(),
413431
};
414432
event
415433
}

src/downloader2/actor.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -285,11 +285,12 @@ async fn peer_download<S: Store>(
285285
.send(Command::BitfieldInfo {
286286
peer: BitfieldPeer::Local,
287287
hash,
288-
event: BitfieldEvent::Update {
288+
event: BitfieldUpdate {
289289
added,
290290
removed: ChunkRanges::empty(),
291291
size,
292-
},
292+
}
293+
.into(),
293294
})
294295
.await
295296
.ok();

0 commit comments

Comments
 (0)