@@ -51,6 +51,9 @@ use state::*;
5151mod actor;
5252use actor:: * ;
5353
54+ mod content_discovery;
55+ pub use content_discovery:: * ;
56+
5457#[ derive(
5558 Debug , Clone , Copy , Serialize , Deserialize , PartialEq , Eq , PartialOrd , Ord , derive_more:: From ,
5659) ]
@@ -76,35 +79,6 @@ struct PeerDownloadId(u64);
7679) ]
7780struct BitfieldSubscriptionId ( u64 ) ;
7881
79- /// Announce kind
80- #[ derive( Debug , Clone , Copy , Serialize , Deserialize , PartialEq , Eq , PartialOrd , Ord , Default ) ]
81- pub enum AnnounceKind {
82- /// The peer supposedly has some of the data.
83- Partial = 0 ,
84- /// The peer supposedly has the complete data.
85- #[ default]
86- Complete ,
87- }
88-
89- /// Options for finding peers
90- #[ derive( Debug , Default ) ]
91- pub struct FindPeersOpts {
92- /// Kind of announce
93- pub kind : AnnounceKind ,
94- }
95-
96- /// A pluggable content discovery mechanism
97- pub trait ContentDiscovery : std:: fmt:: Debug + Send + ' static {
98- /// Find peers that have the given blob.
99- ///
100- /// The returned stream is a handle for the discovery task. It should be an
101- /// infinite stream that only stops when it is dropped.
102- fn find_peers ( & mut self , hash : Hash , opts : FindPeersOpts ) -> BoxStream < ' static , NodeId > ;
103- }
104-
105- /// A boxed content discovery
106- pub type BoxedContentDiscovery = Box < dyn ContentDiscovery > ;
107-
10882/// A pluggable bitfield subscription mechanism
10983pub trait BitfieldSubscription : std:: fmt:: Debug + Send + ' static {
11084 /// Subscribe to a bitfield
@@ -265,7 +239,7 @@ impl<S> DownloaderBuilder<S> {
265239 {
266240 let store = self . store ;
267241 let discovery = self . discovery . expect ( "discovery not set" ) ;
268- let local_pool = self . local_pool . unwrap_or_else ( || LocalPool :: single ( ) ) ;
242+ let local_pool = self . local_pool . unwrap_or_else ( LocalPool :: single) ;
269243 let planner = self
270244 . planner
271245 . unwrap_or_else ( || Box :: new ( StripePlanner2 :: new ( 0 , 10 ) ) ) ;
@@ -350,33 +324,6 @@ impl Downloader {
350324 }
351325}
352326
353- /// A simple static content discovery mechanism
354- #[ derive( Debug ) ]
355- pub struct StaticContentDiscovery {
356- info : BTreeMap < Hash , Vec < NodeId > > ,
357- default : Vec < NodeId > ,
358- }
359-
360- impl StaticContentDiscovery {
361- /// Create a new static content discovery mechanism
362- pub fn new ( mut info : BTreeMap < Hash , Vec < NodeId > > , mut default : Vec < NodeId > ) -> Self {
363- default. sort ( ) ;
364- default. dedup ( ) ;
365- for ( _, peers) in info. iter_mut ( ) {
366- peers. sort ( ) ;
367- peers. dedup ( ) ;
368- }
369- Self { info, default }
370- }
371- }
372-
373- impl ContentDiscovery for StaticContentDiscovery {
374- fn find_peers ( & mut self , hash : Hash , _opts : FindPeersOpts ) -> BoxStream < ' static , NodeId > {
375- let peers = self . info . get ( & hash) . unwrap_or ( & self . default ) . clone ( ) ;
376- Box :: pin ( futures_lite:: stream:: iter ( peers) . chain ( futures_lite:: stream:: pending ( ) ) )
377- }
378- }
379-
380327/// A bitfield subscription that just returns nothing for local and everything(*) for remote
381328///
382329/// * Still need to figure out how to deal with open ended chunk ranges.
@@ -423,7 +370,7 @@ impl<S> SimpleBitfieldSubscription<S> {
423370}
424371
425372async fn get_valid_ranges_local < S : Store > ( hash : & Hash , store : S ) -> anyhow:: Result < ChunkRanges > {
426- if let Some ( entry) = store. get_mut ( & hash) . await ? {
373+ if let Some ( entry) = store. get_mut ( hash) . await ? {
427374 crate :: get:: db:: valid_ranges :: < S > ( & entry) . await
428375 } else {
429376 Ok ( ChunkRanges :: empty ( ) )
@@ -436,7 +383,7 @@ async fn get_valid_ranges_remote(
436383 hash : & Hash ,
437384) -> anyhow:: Result < ChunkRanges > {
438385 let conn = endpoint. connect ( id, crate :: ALPN ) . await ?;
439- let ( size, _) = crate :: get:: request:: get_verified_size ( & conn, & hash) . await ?;
386+ let ( size, _) = crate :: get:: request:: get_verified_size ( & conn, hash) . await ?;
440387 let chunks = ( size + 1023 ) / 1024 ;
441388 Ok ( ChunkRanges :: from ( ChunkNum ( 0 ) ..ChunkNum ( chunks) ) )
442389}
@@ -491,6 +438,7 @@ impl<S: Store> BitfieldSubscription for SimpleBitfieldSubscription<S> {
491438
492439#[ cfg( test) ]
493440mod tests {
441+ #![ allow( clippy:: single_range_in_vec_init) ]
494442 use std:: ops:: Range ;
495443
496444 use crate :: net_protocol:: Blobs ;
@@ -534,7 +482,7 @@ mod tests {
534482 let mut planner = StripePlanner2 :: new ( 0 , 4 ) ;
535483 let hash = Hash :: new ( b"test" ) ;
536484 let mut ranges = make_range_map ( & [ chunk_ranges ( [ 0 ..50 ] ) , chunk_ranges ( [ 50 ..100 ] ) ] ) ;
537- println ! ( "" ) ;
485+ println ! ( ) ;
538486 print_range_map ( & ranges) ;
539487 println ! ( "planning" ) ;
540488 planner. plan ( hash, & mut ranges) ;
@@ -550,7 +498,7 @@ mod tests {
550498 chunk_ranges ( [ 0 ..100 ] ) ,
551499 chunk_ranges ( [ 0 ..100 ] ) ,
552500 ] ) ;
553- println ! ( "" ) ;
501+ println ! ( ) ;
554502 print_range_map ( & ranges) ;
555503 println ! ( "planning" ) ;
556504 planner. plan ( hash, & mut ranges) ;
@@ -567,7 +515,7 @@ mod tests {
567515 chunk_ranges ( [ 0 ..120 ] ) ,
568516 chunk_ranges ( [ 0 ..50 ] ) ,
569517 ] ) ;
570- println ! ( "" ) ;
518+ println ! ( ) ;
571519 print_range_map ( & ranges) ;
572520 println ! ( "planning" ) ;
573521 planner. plan ( hash, & mut ranges) ;
@@ -656,10 +604,7 @@ mod tests {
656604 . discovery_n0 ( )
657605 . bind ( )
658606 . await ?;
659- let discovery = StaticContentDiscovery {
660- info : BTreeMap :: new ( ) ,
661- default : vec ! [ peer] ,
662- } ;
607+ let discovery = StaticContentDiscovery :: new ( BTreeMap :: new ( ) , vec ! [ peer] ) ;
663608 let bitfield_subscription = TestBitfieldSubscription ;
664609 let downloader = Downloader :: builder ( endpoint, store)
665610 . discovery ( discovery)
@@ -697,10 +642,7 @@ mod tests {
697642 . discovery_n0 ( )
698643 . bind ( )
699644 . await ?;
700- let discovery = StaticContentDiscovery {
701- info : BTreeMap :: new ( ) ,
702- default : peers,
703- } ;
645+ let discovery = StaticContentDiscovery :: new ( BTreeMap :: new ( ) , peers) ;
704646 let downloader = Downloader :: builder ( endpoint, store)
705647 . discovery ( discovery)
706648 . planner ( StripePlanner2 :: new ( 0 , 8 ) )
0 commit comments