@@ -7,141 +7,172 @@ use torrust_tracker_primitives::{peer, DurationSinceUnixEpoch, PersistentTorrent
7
7
use torrust_tracker_torrent_repository:: repository:: { Repository as _, RepositoryAsync as _} ;
8
8
use torrust_tracker_torrent_repository:: {
9
9
EntrySingle , TorrentsRwLockStd , TorrentsRwLockStdMutexStd , TorrentsRwLockStdMutexTokio , TorrentsRwLockTokio ,
10
- TorrentsRwLockTokioMutexStd , TorrentsRwLockTokioMutexTokio ,
10
+ TorrentsRwLockTokioMutexStd , TorrentsRwLockTokioMutexTokio , TorrentsSkipMapMutexStd ,
11
11
} ;
12
12
13
13
#[ derive( Debug ) ]
14
14
pub ( crate ) enum Repo {
15
- Std ( TorrentsRwLockStd ) ,
16
- StdMutexStd ( TorrentsRwLockStdMutexStd ) ,
17
- StdMutexTokio ( TorrentsRwLockStdMutexTokio ) ,
18
- Tokio ( TorrentsRwLockTokio ) ,
19
- TokioMutexStd ( TorrentsRwLockTokioMutexStd ) ,
20
- TokioMutexTokio ( TorrentsRwLockTokioMutexTokio ) ,
15
+ RwLockStd ( TorrentsRwLockStd ) ,
16
+ RwLockStdMutexStd ( TorrentsRwLockStdMutexStd ) ,
17
+ RwLockStdMutexTokio ( TorrentsRwLockStdMutexTokio ) ,
18
+ RwLockTokio ( TorrentsRwLockTokio ) ,
19
+ RwLockTokioMutexStd ( TorrentsRwLockTokioMutexStd ) ,
20
+ RwLockTokioMutexTokio ( TorrentsRwLockTokioMutexTokio ) ,
21
+ SkipMapMutexStd ( TorrentsSkipMapMutexStd ) ,
21
22
}
22
23
23
24
impl Repo {
24
25
pub ( crate ) async fn get ( & self , key : & InfoHash ) -> Option < EntrySingle > {
25
26
match self {
26
- Repo :: Std ( repo) => repo. get ( key) ,
27
- Repo :: StdMutexStd ( repo) => Some ( repo. get ( key) ?. lock ( ) . unwrap ( ) . clone ( ) ) ,
28
- Repo :: StdMutexTokio ( repo) => Some ( repo. get ( key) . await ?. lock ( ) . await . clone ( ) ) ,
29
- Repo :: Tokio ( repo) => repo. get ( key) . await ,
30
- Repo :: TokioMutexStd ( repo) => Some ( repo. get ( key) . await ?. lock ( ) . unwrap ( ) . clone ( ) ) ,
31
- Repo :: TokioMutexTokio ( repo) => Some ( repo. get ( key) . await ?. lock ( ) . await . clone ( ) ) ,
27
+ Repo :: RwLockStd ( repo) => repo. get ( key) ,
28
+ Repo :: RwLockStdMutexStd ( repo) => Some ( repo. get ( key) ?. lock ( ) . unwrap ( ) . clone ( ) ) ,
29
+ Repo :: RwLockStdMutexTokio ( repo) => Some ( repo. get ( key) . await ?. lock ( ) . await . clone ( ) ) ,
30
+ Repo :: RwLockTokio ( repo) => repo. get ( key) . await ,
31
+ Repo :: RwLockTokioMutexStd ( repo) => Some ( repo. get ( key) . await ?. lock ( ) . unwrap ( ) . clone ( ) ) ,
32
+ Repo :: RwLockTokioMutexTokio ( repo) => Some ( repo. get ( key) . await ?. lock ( ) . await . clone ( ) ) ,
33
+ Repo :: SkipMapMutexStd ( repo) => Some ( repo. get ( key) ?. lock ( ) . unwrap ( ) . clone ( ) ) ,
32
34
}
33
35
}
36
+
34
37
pub ( crate ) async fn get_metrics ( & self ) -> TorrentsMetrics {
35
38
match self {
36
- Repo :: Std ( repo) => repo. get_metrics ( ) ,
37
- Repo :: StdMutexStd ( repo) => repo. get_metrics ( ) ,
38
- Repo :: StdMutexTokio ( repo) => repo. get_metrics ( ) . await ,
39
- Repo :: Tokio ( repo) => repo. get_metrics ( ) . await ,
40
- Repo :: TokioMutexStd ( repo) => repo. get_metrics ( ) . await ,
41
- Repo :: TokioMutexTokio ( repo) => repo. get_metrics ( ) . await ,
39
+ Repo :: RwLockStd ( repo) => repo. get_metrics ( ) ,
40
+ Repo :: RwLockStdMutexStd ( repo) => repo. get_metrics ( ) ,
41
+ Repo :: RwLockStdMutexTokio ( repo) => repo. get_metrics ( ) . await ,
42
+ Repo :: RwLockTokio ( repo) => repo. get_metrics ( ) . await ,
43
+ Repo :: RwLockTokioMutexStd ( repo) => repo. get_metrics ( ) . await ,
44
+ Repo :: RwLockTokioMutexTokio ( repo) => repo. get_metrics ( ) . await ,
45
+ Repo :: SkipMapMutexStd ( repo) => repo. get_metrics ( ) ,
42
46
}
43
47
}
48
+
44
49
pub ( crate ) async fn get_paginated ( & self , pagination : Option < & Pagination > ) -> Vec < ( InfoHash , EntrySingle ) > {
45
50
match self {
46
- Repo :: Std ( repo) => repo. get_paginated ( pagination) ,
47
- Repo :: StdMutexStd ( repo) => repo
51
+ Repo :: RwLockStd ( repo) => repo. get_paginated ( pagination) ,
52
+ Repo :: RwLockStdMutexStd ( repo) => repo
48
53
. get_paginated ( pagination)
49
54
. iter ( )
50
55
. map ( |( i, t) | ( * i, t. lock ( ) . expect ( "it should get a lock" ) . clone ( ) ) )
51
56
. collect ( ) ,
52
- Repo :: StdMutexTokio ( repo) => {
57
+ Repo :: RwLockStdMutexTokio ( repo) => {
53
58
let mut v: Vec < ( InfoHash , EntrySingle ) > = vec ! [ ] ;
54
59
55
60
for ( i, t) in repo. get_paginated ( pagination) . await {
56
61
v. push ( ( i, t. lock ( ) . await . clone ( ) ) ) ;
57
62
}
58
63
v
59
64
}
60
- Repo :: Tokio ( repo) => repo. get_paginated ( pagination) . await ,
61
- Repo :: TokioMutexStd ( repo) => repo
65
+ Repo :: RwLockTokio ( repo) => repo. get_paginated ( pagination) . await ,
66
+ Repo :: RwLockTokioMutexStd ( repo) => repo
62
67
. get_paginated ( pagination)
63
68
. await
64
69
. iter ( )
65
70
. map ( |( i, t) | ( * i, t. lock ( ) . expect ( "it should get a lock" ) . clone ( ) ) )
66
71
. collect ( ) ,
67
- Repo :: TokioMutexTokio ( repo) => {
72
+ Repo :: RwLockTokioMutexTokio ( repo) => {
68
73
let mut v: Vec < ( InfoHash , EntrySingle ) > = vec ! [ ] ;
69
74
70
75
for ( i, t) in repo. get_paginated ( pagination) . await {
71
76
v. push ( ( i, t. lock ( ) . await . clone ( ) ) ) ;
72
77
}
73
78
v
74
79
}
80
+ Repo :: SkipMapMutexStd ( repo) => repo
81
+ . get_paginated ( pagination)
82
+ . iter ( )
83
+ . map ( |( i, t) | ( * i, t. lock ( ) . expect ( "it should get a lock" ) . clone ( ) ) )
84
+ . collect ( ) ,
75
85
}
76
86
}
87
+
77
88
pub ( crate ) async fn import_persistent ( & self , persistent_torrents : & PersistentTorrents ) {
78
89
match self {
79
- Repo :: Std ( repo) => repo. import_persistent ( persistent_torrents) ,
80
- Repo :: StdMutexStd ( repo) => repo. import_persistent ( persistent_torrents) ,
81
- Repo :: StdMutexTokio ( repo) => repo. import_persistent ( persistent_torrents) . await ,
82
- Repo :: Tokio ( repo) => repo. import_persistent ( persistent_torrents) . await ,
83
- Repo :: TokioMutexStd ( repo) => repo. import_persistent ( persistent_torrents) . await ,
84
- Repo :: TokioMutexTokio ( repo) => repo. import_persistent ( persistent_torrents) . await ,
90
+ Repo :: RwLockStd ( repo) => repo. import_persistent ( persistent_torrents) ,
91
+ Repo :: RwLockStdMutexStd ( repo) => repo. import_persistent ( persistent_torrents) ,
92
+ Repo :: RwLockStdMutexTokio ( repo) => repo. import_persistent ( persistent_torrents) . await ,
93
+ Repo :: RwLockTokio ( repo) => repo. import_persistent ( persistent_torrents) . await ,
94
+ Repo :: RwLockTokioMutexStd ( repo) => repo. import_persistent ( persistent_torrents) . await ,
95
+ Repo :: RwLockTokioMutexTokio ( repo) => repo. import_persistent ( persistent_torrents) . await ,
96
+ Repo :: SkipMapMutexStd ( repo) => repo. import_persistent ( persistent_torrents) ,
85
97
}
86
98
}
99
+
87
100
pub ( crate ) async fn remove ( & self , key : & InfoHash ) -> Option < EntrySingle > {
88
101
match self {
89
- Repo :: Std ( repo) => repo. remove ( key) ,
90
- Repo :: StdMutexStd ( repo) => Some ( repo. remove ( key) ?. lock ( ) . unwrap ( ) . clone ( ) ) ,
91
- Repo :: StdMutexTokio ( repo) => Some ( repo. remove ( key) . await ?. lock ( ) . await . clone ( ) ) ,
92
- Repo :: Tokio ( repo) => repo. remove ( key) . await ,
93
- Repo :: TokioMutexStd ( repo) => Some ( repo. remove ( key) . await ?. lock ( ) . unwrap ( ) . clone ( ) ) ,
94
- Repo :: TokioMutexTokio ( repo) => Some ( repo. remove ( key) . await ?. lock ( ) . await . clone ( ) ) ,
102
+ Repo :: RwLockStd ( repo) => repo. remove ( key) ,
103
+ Repo :: RwLockStdMutexStd ( repo) => Some ( repo. remove ( key) ?. lock ( ) . unwrap ( ) . clone ( ) ) ,
104
+ Repo :: RwLockStdMutexTokio ( repo) => Some ( repo. remove ( key) . await ?. lock ( ) . await . clone ( ) ) ,
105
+ Repo :: RwLockTokio ( repo) => repo. remove ( key) . await ,
106
+ Repo :: RwLockTokioMutexStd ( repo) => Some ( repo. remove ( key) . await ?. lock ( ) . unwrap ( ) . clone ( ) ) ,
107
+ Repo :: RwLockTokioMutexTokio ( repo) => Some ( repo. remove ( key) . await ?. lock ( ) . await . clone ( ) ) ,
108
+ Repo :: SkipMapMutexStd ( repo) => Some ( repo. remove ( key) ?. lock ( ) . unwrap ( ) . clone ( ) ) ,
95
109
}
96
110
}
111
+
97
112
pub ( crate ) async fn remove_inactive_peers ( & self , current_cutoff : DurationSinceUnixEpoch ) {
98
113
match self {
99
- Repo :: Std ( repo) => repo. remove_inactive_peers ( current_cutoff) ,
100
- Repo :: StdMutexStd ( repo) => repo. remove_inactive_peers ( current_cutoff) ,
101
- Repo :: StdMutexTokio ( repo) => repo. remove_inactive_peers ( current_cutoff) . await ,
102
- Repo :: Tokio ( repo) => repo. remove_inactive_peers ( current_cutoff) . await ,
103
- Repo :: TokioMutexStd ( repo) => repo. remove_inactive_peers ( current_cutoff) . await ,
104
- Repo :: TokioMutexTokio ( repo) => repo. remove_inactive_peers ( current_cutoff) . await ,
114
+ Repo :: RwLockStd ( repo) => repo. remove_inactive_peers ( current_cutoff) ,
115
+ Repo :: RwLockStdMutexStd ( repo) => repo. remove_inactive_peers ( current_cutoff) ,
116
+ Repo :: RwLockStdMutexTokio ( repo) => repo. remove_inactive_peers ( current_cutoff) . await ,
117
+ Repo :: RwLockTokio ( repo) => repo. remove_inactive_peers ( current_cutoff) . await ,
118
+ Repo :: RwLockTokioMutexStd ( repo) => repo. remove_inactive_peers ( current_cutoff) . await ,
119
+ Repo :: RwLockTokioMutexTokio ( repo) => repo. remove_inactive_peers ( current_cutoff) . await ,
120
+ Repo :: SkipMapMutexStd ( repo) => repo. remove_inactive_peers ( current_cutoff) ,
105
121
}
106
122
}
123
+
107
124
pub ( crate ) async fn remove_peerless_torrents ( & self , policy : & TrackerPolicy ) {
108
125
match self {
109
- Repo :: Std ( repo) => repo. remove_peerless_torrents ( policy) ,
110
- Repo :: StdMutexStd ( repo) => repo. remove_peerless_torrents ( policy) ,
111
- Repo :: StdMutexTokio ( repo) => repo. remove_peerless_torrents ( policy) . await ,
112
- Repo :: Tokio ( repo) => repo. remove_peerless_torrents ( policy) . await ,
113
- Repo :: TokioMutexStd ( repo) => repo. remove_peerless_torrents ( policy) . await ,
114
- Repo :: TokioMutexTokio ( repo) => repo. remove_peerless_torrents ( policy) . await ,
126
+ Repo :: RwLockStd ( repo) => repo. remove_peerless_torrents ( policy) ,
127
+ Repo :: RwLockStdMutexStd ( repo) => repo. remove_peerless_torrents ( policy) ,
128
+ Repo :: RwLockStdMutexTokio ( repo) => repo. remove_peerless_torrents ( policy) . await ,
129
+ Repo :: RwLockTokio ( repo) => repo. remove_peerless_torrents ( policy) . await ,
130
+ Repo :: RwLockTokioMutexStd ( repo) => repo. remove_peerless_torrents ( policy) . await ,
131
+ Repo :: RwLockTokioMutexTokio ( repo) => repo. remove_peerless_torrents ( policy) . await ,
132
+ Repo :: SkipMapMutexStd ( repo) => repo. remove_peerless_torrents ( policy) ,
115
133
}
116
134
}
135
+
117
136
pub ( crate ) async fn update_torrent_with_peer_and_get_stats (
118
137
& self ,
119
138
info_hash : & InfoHash ,
120
139
peer : & peer:: Peer ,
121
140
) -> ( bool , SwarmMetadata ) {
122
141
match self {
123
- Repo :: Std ( repo) => repo. update_torrent_with_peer_and_get_stats ( info_hash, peer) ,
124
- Repo :: StdMutexStd ( repo) => repo. update_torrent_with_peer_and_get_stats ( info_hash, peer) ,
125
- Repo :: StdMutexTokio ( repo) => repo. update_torrent_with_peer_and_get_stats ( info_hash, peer) . await ,
126
- Repo :: Tokio ( repo) => repo. update_torrent_with_peer_and_get_stats ( info_hash, peer) . await ,
127
- Repo :: TokioMutexStd ( repo) => repo. update_torrent_with_peer_and_get_stats ( info_hash, peer) . await ,
128
- Repo :: TokioMutexTokio ( repo) => repo. update_torrent_with_peer_and_get_stats ( info_hash, peer) . await ,
142
+ Repo :: RwLockStd ( repo) => repo. update_torrent_with_peer_and_get_stats ( info_hash, peer) ,
143
+ Repo :: RwLockStdMutexStd ( repo) => repo. update_torrent_with_peer_and_get_stats ( info_hash, peer) ,
144
+ Repo :: RwLockStdMutexTokio ( repo) => repo. update_torrent_with_peer_and_get_stats ( info_hash, peer) . await ,
145
+ Repo :: RwLockTokio ( repo) => repo. update_torrent_with_peer_and_get_stats ( info_hash, peer) . await ,
146
+ Repo :: RwLockTokioMutexStd ( repo) => repo. update_torrent_with_peer_and_get_stats ( info_hash, peer) . await ,
147
+ Repo :: RwLockTokioMutexTokio ( repo) => repo. update_torrent_with_peer_and_get_stats ( info_hash, peer) . await ,
148
+ Repo :: SkipMapMutexStd ( repo) => repo. update_torrent_with_peer_and_get_stats ( info_hash, peer) ,
129
149
}
130
150
}
151
+
131
152
pub ( crate ) async fn insert ( & self , info_hash : & InfoHash , torrent : EntrySingle ) -> Option < EntrySingle > {
132
153
match self {
133
- Repo :: Std ( repo) => repo. write ( ) . insert ( * info_hash, torrent) ,
134
- Repo :: StdMutexStd ( repo) => Some ( repo. write ( ) . insert ( * info_hash, torrent. into ( ) ) ?. lock ( ) . unwrap ( ) . clone ( ) ) ,
135
- Repo :: StdMutexTokio ( repo) => {
136
- let r = repo. write ( ) . insert ( * info_hash, torrent. into ( ) ) ;
137
- match r {
138
- Some ( t) => Some ( t. lock ( ) . await . clone ( ) ) ,
139
- None => None ,
140
- }
154
+ Repo :: RwLockStd ( repo) => {
155
+ repo. write ( ) . insert ( * info_hash, torrent) ;
141
156
}
142
- Repo :: Tokio ( repo) => repo. write ( ) . await . insert ( * info_hash, torrent) ,
143
- Repo :: TokioMutexStd ( repo) => Some ( repo. write ( ) . await . insert ( * info_hash, torrent. into ( ) ) ?. lock ( ) . unwrap ( ) . clone ( ) ) ,
144
- Repo :: TokioMutexTokio ( repo) => Some ( repo. write ( ) . await . insert ( * info_hash, torrent. into ( ) ) ?. lock ( ) . await . clone ( ) ) ,
145
- }
157
+ Repo :: RwLockStdMutexStd ( repo) => {
158
+ repo. write ( ) . insert ( * info_hash, torrent. into ( ) ) ;
159
+ }
160
+ Repo :: RwLockStdMutexTokio ( repo) => {
161
+ repo. write ( ) . insert ( * info_hash, torrent. into ( ) ) ;
162
+ }
163
+ Repo :: RwLockTokio ( repo) => {
164
+ repo. write ( ) . await . insert ( * info_hash, torrent) ;
165
+ }
166
+ Repo :: RwLockTokioMutexStd ( repo) => {
167
+ repo. write ( ) . await . insert ( * info_hash, torrent. into ( ) ) ;
168
+ }
169
+ Repo :: RwLockTokioMutexTokio ( repo) => {
170
+ repo. write ( ) . await . insert ( * info_hash, torrent. into ( ) ) ;
171
+ }
172
+ Repo :: SkipMapMutexStd ( repo) => {
173
+ repo. torrents . insert ( * info_hash, torrent. into ( ) ) ;
174
+ }
175
+ } ;
176
+ self . get ( info_hash) . await
146
177
}
147
178
}
0 commit comments