@@ -4,12 +4,7 @@ use crate::core::peer;
4
4
use crate :: core:: torrent:: { Entry , SwarmStats } ;
5
5
use crate :: shared:: bit_torrent:: info_hash:: InfoHash ;
6
6
7
- pub trait Repository {
8
- fn new ( ) -> Self ;
9
- fn update_torrent_with_peer_and_get_stats ( & self , info_hash : & InfoHash , peer : & peer:: Peer ) -> ( SwarmStats , bool ) ;
10
- }
11
-
12
- pub trait TRepositoryAsync {
7
+ pub trait RepositoryAsync {
13
8
fn new ( ) -> Self ;
14
9
fn update_torrent_with_peer_and_get_stats (
15
10
& self ,
@@ -18,135 +13,13 @@ pub trait TRepositoryAsync {
18
13
) -> impl std:: future:: Future < Output = ( SwarmStats , bool ) > + Send ;
19
14
}
20
15
21
- /// Structure that holds all torrents. Using `std::sync` locks.
22
- pub struct Sync {
23
- torrents : std:: sync:: RwLock < std:: collections:: BTreeMap < InfoHash , Arc < std:: sync:: Mutex < Entry > > > > ,
24
- }
25
-
26
- impl Sync {
27
- /// Returns the get torrents of this [`Sync`].
28
- ///
29
- /// # Panics
30
- ///
31
- /// Panics if unable to read the torrent.
32
- pub fn get_torrents (
33
- & self ,
34
- ) -> std:: sync:: RwLockReadGuard < ' _ , std:: collections:: BTreeMap < InfoHash , Arc < std:: sync:: Mutex < Entry > > > > {
35
- self . torrents . read ( ) . expect ( "unable to get torrent list" )
36
- }
37
-
38
- /// Returns the mutable get torrents of this [`Sync`].
39
- ///
40
- /// # Panics
41
- ///
42
- /// Panics if unable to write to the torrents list.
43
- pub fn get_torrents_mut (
44
- & self ,
45
- ) -> std:: sync:: RwLockWriteGuard < ' _ , std:: collections:: BTreeMap < InfoHash , Arc < std:: sync:: Mutex < Entry > > > > {
46
- self . torrents . write ( ) . expect ( "unable to get writable torrent list" )
47
- }
48
- }
49
-
50
- impl Repository for Sync {
51
- fn new ( ) -> Self {
52
- Self {
53
- torrents : std:: sync:: RwLock :: new ( std:: collections:: BTreeMap :: new ( ) ) ,
54
- }
55
- }
56
-
57
- fn update_torrent_with_peer_and_get_stats ( & self , info_hash : & InfoHash , peer : & peer:: Peer ) -> ( SwarmStats , bool ) {
58
- let maybe_existing_torrent_entry = self . get_torrents ( ) . get ( info_hash) . cloned ( ) ;
59
-
60
- let torrent_entry: Arc < std:: sync:: Mutex < Entry > > = if let Some ( existing_torrent_entry) = maybe_existing_torrent_entry {
61
- existing_torrent_entry
62
- } else {
63
- let mut torrents_lock = self . get_torrents_mut ( ) ;
64
- let entry = torrents_lock
65
- . entry ( * info_hash)
66
- . or_insert ( Arc :: new ( std:: sync:: Mutex :: new ( Entry :: new ( ) ) ) ) ;
67
- entry. clone ( )
68
- } ;
69
-
70
- let ( stats, stats_updated) = {
71
- let mut torrent_entry_lock = torrent_entry. lock ( ) . unwrap ( ) ;
72
- let stats_updated = torrent_entry_lock. insert_or_update_peer ( peer) ;
73
- let stats = torrent_entry_lock. get_stats ( ) ;
74
-
75
- ( stats, stats_updated)
76
- } ;
77
-
78
- (
79
- SwarmStats {
80
- downloaded : stats. 1 ,
81
- complete : stats. 0 ,
82
- incomplete : stats. 2 ,
83
- } ,
84
- stats_updated,
85
- )
86
- }
87
- }
88
-
89
- /// Structure that holds all torrents. Using `std::sync` locks.
90
- pub struct SyncSingle {
91
- torrents : std:: sync:: RwLock < std:: collections:: BTreeMap < InfoHash , Entry > > ,
92
- }
93
-
94
- impl SyncSingle {
95
- /// Returns the get torrents of this [`SyncSingle`].
96
- ///
97
- /// # Panics
98
- ///
99
- /// Panics if unable to get torrent list.
100
- pub fn get_torrents ( & self ) -> std:: sync:: RwLockReadGuard < ' _ , std:: collections:: BTreeMap < InfoHash , Entry > > {
101
- self . torrents . read ( ) . expect ( "unable to get torrent list" )
102
- }
103
-
104
- /// Returns the get torrents of this [`SyncSingle`].
105
- ///
106
- /// # Panics
107
- ///
108
- /// Panics if unable to get writable torrent list.
109
- pub fn get_torrents_mut ( & self ) -> std:: sync:: RwLockWriteGuard < ' _ , std:: collections:: BTreeMap < InfoHash , Entry > > {
110
- self . torrents . write ( ) . expect ( "unable to get writable torrent list" )
111
- }
112
- }
113
-
114
- impl Repository for SyncSingle {
115
- fn new ( ) -> Self {
116
- Self {
117
- torrents : std:: sync:: RwLock :: new ( std:: collections:: BTreeMap :: new ( ) ) ,
118
- }
119
- }
120
-
121
- fn update_torrent_with_peer_and_get_stats ( & self , info_hash : & InfoHash , peer : & peer:: Peer ) -> ( SwarmStats , bool ) {
122
- let mut torrents = self . torrents . write ( ) . unwrap ( ) ;
123
-
124
- let torrent_entry = match torrents. entry ( * info_hash) {
125
- std:: collections:: btree_map:: Entry :: Vacant ( vacant) => vacant. insert ( Entry :: new ( ) ) ,
126
- std:: collections:: btree_map:: Entry :: Occupied ( entry) => entry. into_mut ( ) ,
127
- } ;
128
-
129
- let stats_updated = torrent_entry. insert_or_update_peer ( peer) ;
130
- let stats = torrent_entry. get_stats ( ) ;
131
-
132
- (
133
- SwarmStats {
134
- downloaded : stats. 1 ,
135
- complete : stats. 0 ,
136
- incomplete : stats. 2 ,
137
- } ,
138
- stats_updated,
139
- )
140
- }
141
- }
142
-
143
16
/// Structure that holds all torrents. Using `tokio::sync` locks.
144
17
#[ allow( clippy:: module_name_repetitions) ]
145
- pub struct RepositoryAsync {
18
+ pub struct Async {
146
19
torrents : tokio:: sync:: RwLock < std:: collections:: BTreeMap < InfoHash , Arc < tokio:: sync:: Mutex < Entry > > > > ,
147
20
}
148
21
149
- impl TRepositoryAsync for RepositoryAsync {
22
+ impl RepositoryAsync for Async {
150
23
fn new ( ) -> Self {
151
24
Self {
152
25
torrents : tokio:: sync:: RwLock :: new ( std:: collections:: BTreeMap :: new ( ) ) ,
@@ -185,7 +58,7 @@ impl TRepositoryAsync for RepositoryAsync {
185
58
}
186
59
}
187
60
188
- impl RepositoryAsync {
61
+ impl Async {
189
62
pub async fn get_torrents (
190
63
& self ,
191
64
) -> tokio:: sync:: RwLockReadGuard < ' _ , std:: collections:: BTreeMap < InfoHash , Arc < tokio:: sync:: Mutex < Entry > > > > {
@@ -204,7 +77,7 @@ pub struct AsyncSync {
204
77
torrents : tokio:: sync:: RwLock < std:: collections:: BTreeMap < InfoHash , Arc < std:: sync:: Mutex < Entry > > > > ,
205
78
}
206
79
207
- impl TRepositoryAsync for AsyncSync {
80
+ impl RepositoryAsync for AsyncSync {
208
81
fn new ( ) -> Self {
209
82
Self {
210
83
torrents : tokio:: sync:: RwLock :: new ( std:: collections:: BTreeMap :: new ( ) ) ,
@@ -262,7 +135,7 @@ pub struct RepositoryAsyncSingle {
262
135
torrents : tokio:: sync:: RwLock < std:: collections:: BTreeMap < InfoHash , Entry > > ,
263
136
}
264
137
265
- impl TRepositoryAsync for RepositoryAsyncSingle {
138
+ impl RepositoryAsync for RepositoryAsyncSingle {
266
139
fn new ( ) -> Self {
267
140
Self {
268
141
torrents : tokio:: sync:: RwLock :: new ( std:: collections:: BTreeMap :: new ( ) ) ,
0 commit comments