Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit fe80703

Browse files
committedMar 15, 2024
dev: refactor torrent entry defs
1 parent 4a7fa48 commit fe80703

File tree

9 files changed

+111
-107
lines changed

9 files changed

+111
-107
lines changed
 

‎packages/torrent-repository/src/entry.rs

+11-12
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ use torrust_tracker_primitives::peer::ReadInfo as _;
88
use torrust_tracker_primitives::swarm_metadata::SwarmMetadata;
99
use torrust_tracker_primitives::{peer, DurationSinceUnixEpoch};
1010

11+
use crate::{EntryMutexStd, EntryMutexTokio, EntrySingle};
12+
1113
/// A data structure containing all the information about a torrent in the tracker.
1214
///
1315
/// This is the tracker entry for a given torrent and contains the swarm data,
@@ -21,9 +23,6 @@ pub struct Entry {
2123
/// The number of peers that have ever completed downloading the torrent associated to this entry
2224
pub(crate) completed: u32,
2325
}
24-
pub type Single = Entry;
25-
pub type MutexStd = Arc<std::sync::Mutex<Entry>>;
26-
pub type MutexTokio = Arc<tokio::sync::Mutex<Entry>>;
2726

2827
pub trait ReadInfo {
2928
/// It returns the swarm metadata (statistics) as a struct:
@@ -115,7 +114,7 @@ pub trait UpdateAsync {
115114
fn remove_inactive_peers(self, current_cutoff: DurationSinceUnixEpoch) -> impl std::future::Future<Output = ()> + Send;
116115
}
117116

118-
impl ReadInfo for Single {
117+
impl ReadInfo for EntrySingle {
119118
#[allow(clippy::cast_possible_truncation)]
120119
fn get_stats(&self) -> SwarmMetadata {
121120
let complete: u32 = self.peers.values().filter(|peer| peer.is_seeder()).count() as u32;
@@ -149,7 +148,7 @@ impl ReadInfo for Single {
149148
}
150149
}
151150

152-
impl ReadInfo for MutexStd {
151+
impl ReadInfo for EntryMutexStd {
153152
fn get_stats(&self) -> SwarmMetadata {
154153
self.lock().expect("it should get a lock").get_stats()
155154
}
@@ -167,7 +166,7 @@ impl ReadInfo for MutexStd {
167166
}
168167
}
169168

170-
impl ReadInfoAsync for MutexTokio {
169+
impl ReadInfoAsync for EntryMutexTokio {
171170
async fn get_stats(self) -> SwarmMetadata {
172171
self.lock().await.get_stats()
173172
}
@@ -185,7 +184,7 @@ impl ReadInfoAsync for MutexTokio {
185184
}
186185
}
187186

188-
impl ReadPeers for Single {
187+
impl ReadPeers for EntrySingle {
189188
fn get_peers(&self, limit: Option<usize>) -> Vec<Arc<peer::Peer>> {
190189
match limit {
191190
Some(limit) => self.peers.values().take(limit).cloned().collect(),
@@ -215,7 +214,7 @@ impl ReadPeers for Single {
215214
}
216215
}
217216

218-
impl ReadPeers for MutexStd {
217+
impl ReadPeers for EntryMutexStd {
219218
fn get_peers(&self, limit: Option<usize>) -> Vec<Arc<peer::Peer>> {
220219
self.lock().expect("it should get lock").get_peers(limit)
221220
}
@@ -225,7 +224,7 @@ impl ReadPeers for MutexStd {
225224
}
226225
}
227226

228-
impl ReadPeersAsync for MutexTokio {
227+
impl ReadPeersAsync for EntryMutexTokio {
229228
async fn get_peers(self, limit: Option<usize>) -> Vec<Arc<peer::Peer>> {
230229
self.lock().await.get_peers(limit)
231230
}
@@ -235,7 +234,7 @@ impl ReadPeersAsync for MutexTokio {
235234
}
236235
}
237236

238-
impl Update for Single {
237+
impl Update for EntrySingle {
239238
fn insert_or_update_peer(&mut self, peer: &peer::Peer) -> bool {
240239
let mut did_torrent_stats_change: bool = false;
241240

@@ -270,7 +269,7 @@ impl Update for Single {
270269
}
271270
}
272271

273-
impl UpdateSync for MutexStd {
272+
impl UpdateSync for EntryMutexStd {
274273
fn insert_or_update_peer(&self, peer: &peer::Peer) -> bool {
275274
self.lock().expect("it should lock the entry").insert_or_update_peer(peer)
276275
}
@@ -288,7 +287,7 @@ impl UpdateSync for MutexStd {
288287
}
289288
}
290289

291-
impl UpdateAsync for MutexTokio {
290+
impl UpdateAsync for EntryMutexTokio {
292291
async fn insert_or_update_peer(self, peer: &peer::Peer) -> bool {
293292
self.lock().await.insert_or_update_peer(peer)
294293
}
+12-6
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,15 @@
1+
use std::sync::Arc;
2+
13
pub mod entry;
24
pub mod repository;
35

4-
pub type TorrentsRwLockStd = repository::RwLockStd<entry::Single>;
5-
pub type TorrentsRwLockStdMutexStd = repository::RwLockStd<entry::MutexStd>;
6-
pub type TorrentsRwLockStdMutexTokio = repository::RwLockStd<entry::MutexTokio>;
7-
pub type TorrentsRwLockTokio = repository::RwLockTokio<entry::Single>;
8-
pub type TorrentsRwLockTokioMutexStd = repository::RwLockTokio<entry::MutexStd>;
9-
pub type TorrentsRwLockTokioMutexTokio = repository::RwLockTokio<entry::MutexTokio>;
6+
pub type EntrySingle = entry::Entry;
7+
pub type EntryMutexStd = Arc<std::sync::Mutex<entry::Entry>>;
8+
pub type EntryMutexTokio = Arc<tokio::sync::Mutex<entry::Entry>>;
9+
10+
pub type TorrentsRwLockStd = repository::RwLockStd<EntrySingle>;
11+
pub type TorrentsRwLockStdMutexStd = repository::RwLockStd<EntryMutexStd>;
12+
pub type TorrentsRwLockStdMutexTokio = repository::RwLockStd<EntryMutexTokio>;
13+
pub type TorrentsRwLockTokio = repository::RwLockTokio<EntrySingle>;
14+
pub type TorrentsRwLockTokioMutexStd = repository::RwLockTokio<EntryMutexStd>;
15+
pub type TorrentsRwLockTokioMutexTokio = repository::RwLockTokio<EntryMutexTokio>;

‎packages/torrent-repository/src/repository/rw_lock_std.rs

+12-12
Original file line numberDiff line numberDiff line change
@@ -9,20 +9,20 @@ use torrust_tracker_primitives::torrent_metrics::TorrentsMetrics;
99
use torrust_tracker_primitives::{peer, DurationSinceUnixEpoch, PersistentTorrents};
1010

1111
use super::{Repository, UpdateTorrentSync};
12-
use crate::entry::{self, ReadInfo, Update};
13-
use crate::TorrentsRwLockStd;
12+
use crate::entry::{ReadInfo, Update};
13+
use crate::{EntrySingle, TorrentsRwLockStd};
1414

1515
impl TorrentsRwLockStd {
16-
fn get_torrents<'a>(&'a self) -> std::sync::RwLockReadGuard<'a, std::collections::BTreeMap<InfoHash, entry::Single>>
16+
fn get_torrents<'a>(&'a self) -> std::sync::RwLockReadGuard<'a, std::collections::BTreeMap<InfoHash, EntrySingle>>
1717
where
18-
std::collections::BTreeMap<InfoHash, entry::Single>: 'a,
18+
std::collections::BTreeMap<InfoHash, EntrySingle>: 'a,
1919
{
2020
self.torrents.read().expect("it should get the read lock")
2121
}
2222

23-
fn get_torrents_mut<'a>(&'a self) -> std::sync::RwLockWriteGuard<'a, std::collections::BTreeMap<InfoHash, entry::Single>>
23+
fn get_torrents_mut<'a>(&'a self) -> std::sync::RwLockWriteGuard<'a, std::collections::BTreeMap<InfoHash, EntrySingle>>
2424
where
25-
std::collections::BTreeMap<InfoHash, entry::Single>: 'a,
25+
std::collections::BTreeMap<InfoHash, EntrySingle>: 'a,
2626
{
2727
self.torrents.write().expect("it should get the write lock")
2828
}
@@ -32,7 +32,7 @@ impl UpdateTorrentSync for TorrentsRwLockStd {
3232
fn update_torrent_with_peer_and_get_stats(&self, info_hash: &InfoHash, peer: &peer::Peer) -> (bool, SwarmMetadata) {
3333
let mut db = self.get_torrents_mut();
3434

35-
let entry = db.entry(*info_hash).or_insert(entry::Single::default());
35+
let entry = db.entry(*info_hash).or_insert(EntrySingle::default());
3636

3737
entry.insert_or_update_peer_and_get_stats(peer)
3838
}
@@ -44,8 +44,8 @@ impl UpdateTorrentSync for Arc<TorrentsRwLockStd> {
4444
}
4545
}
4646

47-
impl Repository<entry::Single> for TorrentsRwLockStd {
48-
async fn get(&self, key: &InfoHash) -> Option<entry::Single> {
47+
impl Repository<EntrySingle> for TorrentsRwLockStd {
48+
async fn get(&self, key: &InfoHash) -> Option<EntrySingle> {
4949
let db = self.get_torrents();
5050
db.get(key).cloned()
5151
}
@@ -64,7 +64,7 @@ impl Repository<entry::Single> for TorrentsRwLockStd {
6464
metrics
6565
}
6666

67-
async fn get_paginated(&self, pagination: Option<&Pagination>) -> Vec<(InfoHash, entry::Single)> {
67+
async fn get_paginated(&self, pagination: Option<&Pagination>) -> Vec<(InfoHash, EntrySingle)> {
6868
let db = self.get_torrents();
6969

7070
match pagination {
@@ -87,7 +87,7 @@ impl Repository<entry::Single> for TorrentsRwLockStd {
8787
continue;
8888
}
8989

90-
let entry = entry::Single {
90+
let entry = EntrySingle {
9191
peers: BTreeMap::default(),
9292
completed: *completed,
9393
};
@@ -96,7 +96,7 @@ impl Repository<entry::Single> for TorrentsRwLockStd {
9696
}
9797
}
9898

99-
async fn remove(&self, key: &InfoHash) -> Option<entry::Single> {
99+
async fn remove(&self, key: &InfoHash) -> Option<EntrySingle> {
100100
let mut db = self.get_torrents_mut();
101101
db.remove(key)
102102
}

‎packages/torrent-repository/src/repository/rw_lock_std_mutex_std.rs

+12-12
Original file line numberDiff line numberDiff line change
@@ -9,20 +9,20 @@ use torrust_tracker_primitives::torrent_metrics::TorrentsMetrics;
99
use torrust_tracker_primitives::{peer, DurationSinceUnixEpoch, PersistentTorrents};
1010

1111
use super::{Repository, UpdateTorrentSync};
12-
use crate::entry::{self, ReadInfo, UpdateSync};
13-
use crate::TorrentsRwLockStdMutexStd;
12+
use crate::entry::{ReadInfo, UpdateSync};
13+
use crate::{EntryMutexStd, EntrySingle, TorrentsRwLockStdMutexStd};
1414

1515
impl TorrentsRwLockStdMutexStd {
16-
fn get_torrents<'a>(&'a self) -> std::sync::RwLockReadGuard<'a, std::collections::BTreeMap<InfoHash, entry::MutexStd>>
16+
fn get_torrents<'a>(&'a self) -> std::sync::RwLockReadGuard<'a, std::collections::BTreeMap<InfoHash, EntryMutexStd>>
1717
where
18-
std::collections::BTreeMap<InfoHash, entry::MutexStd>: 'a,
18+
std::collections::BTreeMap<InfoHash, crate::EntryMutexStd>: 'a,
1919
{
2020
self.torrents.read().expect("unable to get torrent list")
2121
}
2222

23-
fn get_torrents_mut<'a>(&'a self) -> std::sync::RwLockWriteGuard<'a, std::collections::BTreeMap<InfoHash, entry::MutexStd>>
23+
fn get_torrents_mut<'a>(&'a self) -> std::sync::RwLockWriteGuard<'a, std::collections::BTreeMap<InfoHash, EntryMutexStd>>
2424
where
25-
std::collections::BTreeMap<InfoHash, entry::MutexStd>: 'a,
25+
std::collections::BTreeMap<InfoHash, EntryMutexStd>: 'a,
2626
{
2727
self.torrents.write().expect("unable to get writable torrent list")
2828
}
@@ -50,8 +50,8 @@ impl UpdateTorrentSync for Arc<TorrentsRwLockStdMutexStd> {
5050
}
5151
}
5252

53-
impl Repository<entry::MutexStd> for TorrentsRwLockStdMutexStd {
54-
async fn get(&self, key: &InfoHash) -> Option<entry::MutexStd> {
53+
impl Repository<EntryMutexStd> for TorrentsRwLockStdMutexStd {
54+
async fn get(&self, key: &InfoHash) -> Option<EntryMutexStd> {
5555
let db = self.get_torrents();
5656
db.get(key).cloned()
5757
}
@@ -70,7 +70,7 @@ impl Repository<entry::MutexStd> for TorrentsRwLockStdMutexStd {
7070
metrics
7171
}
7272

73-
async fn get_paginated(&self, pagination: Option<&Pagination>) -> Vec<(InfoHash, entry::MutexStd)> {
73+
async fn get_paginated(&self, pagination: Option<&Pagination>) -> Vec<(InfoHash, EntryMutexStd)> {
7474
let db = self.get_torrents();
7575

7676
match pagination {
@@ -93,8 +93,8 @@ impl Repository<entry::MutexStd> for TorrentsRwLockStdMutexStd {
9393
continue;
9494
}
9595

96-
let entry = entry::MutexStd::new(
97-
entry::Single {
96+
let entry = EntryMutexStd::new(
97+
EntrySingle {
9898
peers: BTreeMap::default(),
9999
completed: *completed,
100100
}
@@ -105,7 +105,7 @@ impl Repository<entry::MutexStd> for TorrentsRwLockStdMutexStd {
105105
}
106106
}
107107

108-
async fn remove(&self, key: &InfoHash) -> Option<entry::MutexStd> {
108+
async fn remove(&self, key: &InfoHash) -> Option<EntryMutexStd> {
109109
let mut db = self.get_torrents_mut();
110110
db.remove(key)
111111
}

‎packages/torrent-repository/src/repository/rw_lock_std_mutex_tokio.rs

+11-11
Original file line numberDiff line numberDiff line change
@@ -13,19 +13,19 @@ use torrust_tracker_primitives::{peer, DurationSinceUnixEpoch, PersistentTorrent
1313

1414
use super::{Repository, UpdateTorrentAsync};
1515
use crate::entry::{self, ReadInfo, UpdateAsync};
16-
use crate::TorrentsRwLockStdMutexTokio;
16+
use crate::{EntryMutexTokio, EntrySingle, TorrentsRwLockStdMutexTokio};
1717

1818
impl TorrentsRwLockStdMutexTokio {
19-
fn get_torrents<'a>(&'a self) -> std::sync::RwLockReadGuard<'a, std::collections::BTreeMap<InfoHash, entry::MutexTokio>>
19+
fn get_torrents<'a>(&'a self) -> std::sync::RwLockReadGuard<'a, std::collections::BTreeMap<InfoHash, EntryMutexTokio>>
2020
where
21-
std::collections::BTreeMap<InfoHash, entry::MutexTokio>: 'a,
21+
std::collections::BTreeMap<InfoHash, EntryMutexTokio>: 'a,
2222
{
2323
self.torrents.read().expect("unable to get torrent list")
2424
}
2525

26-
fn get_torrents_mut<'a>(&'a self) -> std::sync::RwLockWriteGuard<'a, std::collections::BTreeMap<InfoHash, entry::MutexTokio>>
26+
fn get_torrents_mut<'a>(&'a self) -> std::sync::RwLockWriteGuard<'a, std::collections::BTreeMap<InfoHash, EntryMutexTokio>>
2727
where
28-
std::collections::BTreeMap<InfoHash, entry::MutexTokio>: 'a,
28+
std::collections::BTreeMap<InfoHash, EntryMutexTokio>: 'a,
2929
{
3030
self.torrents.write().expect("unable to get writable torrent list")
3131
}
@@ -53,13 +53,13 @@ impl UpdateTorrentAsync for Arc<TorrentsRwLockStdMutexTokio> {
5353
}
5454
}
5555

56-
impl Repository<entry::MutexTokio> for TorrentsRwLockStdMutexTokio {
57-
async fn get(&self, key: &InfoHash) -> Option<entry::MutexTokio> {
56+
impl Repository<EntryMutexTokio> for TorrentsRwLockStdMutexTokio {
57+
async fn get(&self, key: &InfoHash) -> Option<EntryMutexTokio> {
5858
let db = self.get_torrents();
5959
db.get(key).cloned()
6060
}
6161

62-
async fn get_paginated(&self, pagination: Option<&Pagination>) -> Vec<(InfoHash, entry::MutexTokio)> {
62+
async fn get_paginated(&self, pagination: Option<&Pagination>) -> Vec<(InfoHash, EntryMutexTokio)> {
6363
let db = self.get_torrents();
6464

6565
match pagination {
@@ -98,8 +98,8 @@ impl Repository<entry::MutexTokio> for TorrentsRwLockStdMutexTokio {
9898
continue;
9999
}
100100

101-
let entry = entry::MutexTokio::new(
102-
entry::Single {
101+
let entry = EntryMutexTokio::new(
102+
EntrySingle {
103103
peers: BTreeMap::default(),
104104
completed: *completed,
105105
}
@@ -110,7 +110,7 @@ impl Repository<entry::MutexTokio> for TorrentsRwLockStdMutexTokio {
110110
}
111111
}
112112

113-
async fn remove(&self, key: &InfoHash) -> Option<entry::MutexTokio> {
113+
async fn remove(&self, key: &InfoHash) -> Option<EntryMutexTokio> {
114114
let mut db = self.get_torrents_mut();
115115
db.remove(key)
116116
}

‎packages/torrent-repository/src/repository/rw_lock_tokio.rs

+12-12
Original file line numberDiff line numberDiff line change
@@ -9,22 +9,22 @@ use torrust_tracker_primitives::torrent_metrics::TorrentsMetrics;
99
use torrust_tracker_primitives::{peer, DurationSinceUnixEpoch, PersistentTorrents};
1010

1111
use super::{Repository, UpdateTorrentAsync};
12-
use crate::entry::{self, ReadInfo, Update};
13-
use crate::TorrentsRwLockTokio;
12+
use crate::entry::{ReadInfo, Update};
13+
use crate::{EntrySingle, TorrentsRwLockTokio};
1414

1515
impl TorrentsRwLockTokio {
16-
async fn get_torrents<'a>(&'a self) -> tokio::sync::RwLockReadGuard<'a, std::collections::BTreeMap<InfoHash, entry::Single>>
16+
async fn get_torrents<'a>(&'a self) -> tokio::sync::RwLockReadGuard<'a, std::collections::BTreeMap<InfoHash, EntrySingle>>
1717
where
18-
std::collections::BTreeMap<InfoHash, entry::Single>: 'a,
18+
std::collections::BTreeMap<InfoHash, EntrySingle>: 'a,
1919
{
2020
self.torrents.read().await
2121
}
2222

2323
async fn get_torrents_mut<'a>(
2424
&'a self,
25-
) -> tokio::sync::RwLockWriteGuard<'a, std::collections::BTreeMap<InfoHash, entry::Single>>
25+
) -> tokio::sync::RwLockWriteGuard<'a, std::collections::BTreeMap<InfoHash, EntrySingle>>
2626
where
27-
std::collections::BTreeMap<InfoHash, entry::Single>: 'a,
27+
std::collections::BTreeMap<InfoHash, EntrySingle>: 'a,
2828
{
2929
self.torrents.write().await
3030
}
@@ -34,7 +34,7 @@ impl UpdateTorrentAsync for TorrentsRwLockTokio {
3434
async fn update_torrent_with_peer_and_get_stats(&self, info_hash: &InfoHash, peer: &peer::Peer) -> (bool, SwarmMetadata) {
3535
let mut db = self.get_torrents_mut().await;
3636

37-
let entry = db.entry(*info_hash).or_insert(entry::Single::default());
37+
let entry = db.entry(*info_hash).or_insert(EntrySingle::default());
3838

3939
entry.insert_or_update_peer_and_get_stats(peer)
4040
}
@@ -46,13 +46,13 @@ impl UpdateTorrentAsync for Arc<TorrentsRwLockTokio> {
4646
}
4747
}
4848

49-
impl Repository<entry::Single> for TorrentsRwLockTokio {
50-
async fn get(&self, key: &InfoHash) -> Option<entry::Single> {
49+
impl Repository<EntrySingle> for TorrentsRwLockTokio {
50+
async fn get(&self, key: &InfoHash) -> Option<EntrySingle> {
5151
let db = self.get_torrents().await;
5252
db.get(key).cloned()
5353
}
5454

55-
async fn get_paginated(&self, pagination: Option<&Pagination>) -> Vec<(InfoHash, entry::Single)> {
55+
async fn get_paginated(&self, pagination: Option<&Pagination>) -> Vec<(InfoHash, EntrySingle)> {
5656
let db = self.get_torrents().await;
5757

5858
match pagination {
@@ -89,7 +89,7 @@ impl Repository<entry::Single> for TorrentsRwLockTokio {
8989
continue;
9090
}
9191

92-
let entry = entry::Single {
92+
let entry = EntrySingle {
9393
peers: BTreeMap::default(),
9494
completed: *completed,
9595
};
@@ -98,7 +98,7 @@ impl Repository<entry::Single> for TorrentsRwLockTokio {
9898
}
9999
}
100100

101-
async fn remove(&self, key: &InfoHash) -> Option<entry::Single> {
101+
async fn remove(&self, key: &InfoHash) -> Option<EntrySingle> {
102102
let mut db = self.get_torrents_mut().await;
103103
db.remove(key)
104104
}

0 commit comments

Comments
 (0)
Please sign in to comment.