Skip to content

Commit 42f1b30

Browse files
committed
refactor: extract mod peer_list
1 parent 922afda commit 42f1b30

File tree

10 files changed

+93
-74
lines changed

10 files changed

+93
-74
lines changed

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

Lines changed: 3 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,15 @@ use std::fmt::Debug;
22
use std::net::SocketAddr;
33
use std::sync::Arc;
44

5-
//use serde::{Deserialize, Serialize};
65
use torrust_tracker_configuration::TrackerPolicy;
76
use torrust_tracker_primitives::swarm_metadata::SwarmMetadata;
87
use torrust_tracker_primitives::{peer, DurationSinceUnixEpoch};
98

9+
use self::peer_list::PeerList;
10+
1011
pub mod mutex_std;
1112
pub mod mutex_tokio;
13+
pub mod peer_list;
1214
pub mod single;
1315

1416
pub trait Entry {
@@ -86,68 +88,3 @@ pub struct Torrent {
8688
/// The number of peers that have ever completed downloading the torrent associated to this entry
8789
pub(crate) downloaded: u32,
8890
}
89-
90-
#[derive(Clone, Debug, Default, PartialEq, Eq, PartialOrd, Ord, Hash)]
91-
pub struct PeerList {
92-
peers: std::collections::BTreeMap<peer::Id, Arc<peer::Peer>>,
93-
}
94-
95-
impl PeerList {
96-
fn len(&self) -> usize {
97-
self.peers.len()
98-
}
99-
100-
fn is_empty(&self) -> bool {
101-
self.peers.is_empty()
102-
}
103-
104-
fn insert(&mut self, key: peer::Id, value: Arc<peer::Peer>) -> Option<Arc<peer::Peer>> {
105-
self.peers.insert(key, value)
106-
}
107-
108-
fn remove(&mut self, key: &peer::Id) -> Option<Arc<peer::Peer>> {
109-
self.peers.remove(key)
110-
}
111-
112-
fn retain<F>(&mut self, f: F)
113-
where
114-
F: FnMut(&peer::Id, &mut Arc<peer::Peer>) -> bool,
115-
{
116-
self.peers.retain(f);
117-
}
118-
119-
fn seeders_and_leechers(&self) -> (usize, usize) {
120-
let seeders = self.peers.values().filter(|peer| peer.is_seeder()).count();
121-
let leechers = self.len() - seeders;
122-
123-
(seeders, leechers)
124-
}
125-
126-
fn get_peers(&self, limit: Option<usize>) -> Vec<Arc<peer::Peer>> {
127-
match limit {
128-
Some(limit) => self.peers.values().take(limit).cloned().collect(),
129-
None => self.peers.values().cloned().collect(),
130-
}
131-
}
132-
133-
fn get_peers_for_client(&self, client: &SocketAddr, limit: Option<usize>) -> Vec<Arc<peer::Peer>> {
134-
match limit {
135-
Some(limit) => self
136-
.peers
137-
.values()
138-
// Take peers which are not the client peer
139-
.filter(|peer| peer::ReadInfo::get_address(peer.as_ref()) != *client)
140-
// Limit the number of peers on the result
141-
.take(limit)
142-
.cloned()
143-
.collect(),
144-
None => self
145-
.peers
146-
.values()
147-
// Take peers which are not the client peer
148-
.filter(|peer| peer::ReadInfo::get_address(peer.as_ref()) != *client)
149-
.cloned()
150-
.collect(),
151-
}
152-
}
153-
}
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
use std::net::SocketAddr;
2+
use std::sync::Arc;
3+
4+
use torrust_tracker_primitives::peer;
5+
6+
#[derive(Clone, Debug, Default, PartialEq, Eq, PartialOrd, Ord, Hash)]
7+
pub struct PeerList {
8+
peers: std::collections::BTreeMap<peer::Id, Arc<peer::Peer>>,
9+
}
10+
11+
impl PeerList {
12+
#[must_use]
13+
pub fn len(&self) -> usize {
14+
self.peers.len()
15+
}
16+
17+
#[must_use]
18+
pub fn is_empty(&self) -> bool {
19+
self.peers.is_empty()
20+
}
21+
22+
pub fn insert(&mut self, key: peer::Id, value: Arc<peer::Peer>) -> Option<Arc<peer::Peer>> {
23+
self.peers.insert(key, value)
24+
}
25+
26+
pub fn remove(&mut self, key: &peer::Id) -> Option<Arc<peer::Peer>> {
27+
self.peers.remove(key)
28+
}
29+
30+
pub fn retain<F>(&mut self, f: F)
31+
where
32+
F: FnMut(&peer::Id, &mut Arc<peer::Peer>) -> bool,
33+
{
34+
self.peers.retain(f);
35+
}
36+
37+
#[must_use]
38+
pub fn seeders_and_leechers(&self) -> (usize, usize) {
39+
let seeders = self.peers.values().filter(|peer| peer.is_seeder()).count();
40+
let leechers = self.len() - seeders;
41+
42+
(seeders, leechers)
43+
}
44+
45+
#[must_use]
46+
pub fn get_peers(&self, limit: Option<usize>) -> Vec<Arc<peer::Peer>> {
47+
match limit {
48+
Some(limit) => self.peers.values().take(limit).cloned().collect(),
49+
None => self.peers.values().cloned().collect(),
50+
}
51+
}
52+
53+
#[must_use]
54+
pub fn get_peers_for_client(&self, client: &SocketAddr, limit: Option<usize>) -> Vec<Arc<peer::Peer>> {
55+
match limit {
56+
Some(limit) => self
57+
.peers
58+
.values()
59+
// Take peers which are not the client peer
60+
.filter(|peer| peer::ReadInfo::get_address(peer.as_ref()) != *client)
61+
// Limit the number of peers on the result
62+
.take(limit)
63+
.cloned()
64+
.collect(),
65+
None => self
66+
.peers
67+
.values()
68+
// Take peers which are not the client peer
69+
.filter(|peer| peer::ReadInfo::get_address(peer.as_ref()) != *client)
70+
.cloned()
71+
.collect(),
72+
}
73+
}
74+
}

packages/torrent-repository/src/repository/dash_map_mutex_std.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@ use torrust_tracker_primitives::torrent_metrics::TorrentsMetrics;
99
use torrust_tracker_primitives::{peer, DurationSinceUnixEpoch, PersistentTorrents};
1010

1111
use super::Repository;
12-
use crate::entry::{Entry, EntrySync, PeerList};
12+
use crate::entry::peer_list::PeerList;
13+
use crate::entry::{Entry, EntrySync};
1314
use crate::{EntryMutexStd, EntrySingle};
1415

1516
#[derive(Default, Debug)]

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@ use torrust_tracker_primitives::torrent_metrics::TorrentsMetrics;
66
use torrust_tracker_primitives::{peer, DurationSinceUnixEpoch, PersistentTorrents};
77

88
use super::Repository;
9-
use crate::entry::{Entry, PeerList};
9+
use crate::entry::peer_list::PeerList;
10+
use crate::entry::Entry;
1011
use crate::{EntrySingle, TorrentsRwLockStd};
1112

1213
#[derive(Default, Debug)]

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@ use torrust_tracker_primitives::torrent_metrics::TorrentsMetrics;
88
use torrust_tracker_primitives::{peer, DurationSinceUnixEpoch, PersistentTorrents};
99

1010
use super::Repository;
11-
use crate::entry::{Entry, EntrySync, PeerList};
11+
use crate::entry::peer_list::PeerList;
12+
use crate::entry::{Entry, EntrySync};
1213
use crate::{EntryMutexStd, EntrySingle, TorrentsRwLockStdMutexStd};
1314

1415
impl TorrentsRwLockStdMutexStd {

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@ use torrust_tracker_primitives::torrent_metrics::TorrentsMetrics;
1212
use torrust_tracker_primitives::{peer, DurationSinceUnixEpoch, PersistentTorrents};
1313

1414
use super::RepositoryAsync;
15-
use crate::entry::{Entry, EntryAsync, PeerList};
15+
use crate::entry::peer_list::PeerList;
16+
use crate::entry::{Entry, EntryAsync};
1617
use crate::{EntryMutexTokio, EntrySingle, TorrentsRwLockStdMutexTokio};
1718

1819
impl TorrentsRwLockStdMutexTokio {

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@ use torrust_tracker_primitives::torrent_metrics::TorrentsMetrics;
66
use torrust_tracker_primitives::{peer, DurationSinceUnixEpoch, PersistentTorrents};
77

88
use super::RepositoryAsync;
9-
use crate::entry::{Entry, PeerList};
9+
use crate::entry::peer_list::PeerList;
10+
use crate::entry::Entry;
1011
use crate::{EntrySingle, TorrentsRwLockTokio};
1112

1213
#[derive(Default, Debug)]

packages/torrent-repository/src/repository/rw_lock_tokio_mutex_std.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@ use torrust_tracker_primitives::torrent_metrics::TorrentsMetrics;
88
use torrust_tracker_primitives::{peer, DurationSinceUnixEpoch, PersistentTorrents};
99

1010
use super::RepositoryAsync;
11-
use crate::entry::{Entry, EntrySync, PeerList};
11+
use crate::entry::peer_list::PeerList;
12+
use crate::entry::{Entry, EntrySync};
1213
use crate::{EntryMutexStd, EntrySingle, TorrentsRwLockTokioMutexStd};
1314

1415
impl TorrentsRwLockTokioMutexStd {

packages/torrent-repository/src/repository/rw_lock_tokio_mutex_tokio.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@ use torrust_tracker_primitives::torrent_metrics::TorrentsMetrics;
88
use torrust_tracker_primitives::{peer, DurationSinceUnixEpoch, PersistentTorrents};
99

1010
use super::RepositoryAsync;
11-
use crate::entry::{Entry, EntryAsync, PeerList};
11+
use crate::entry::peer_list::PeerList;
12+
use crate::entry::{Entry, EntryAsync};
1213
use crate::{EntryMutexTokio, EntrySingle, TorrentsRwLockTokioMutexTokio};
1314

1415
impl TorrentsRwLockTokioMutexTokio {

packages/torrent-repository/src/repository/skip_map_mutex_std.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@ use torrust_tracker_primitives::torrent_metrics::TorrentsMetrics;
99
use torrust_tracker_primitives::{peer, DurationSinceUnixEpoch, PersistentTorrents};
1010

1111
use super::Repository;
12-
use crate::entry::{Entry, EntrySync, PeerList};
12+
use crate::entry::peer_list::PeerList;
13+
use crate::entry::{Entry, EntrySync};
1314
use crate::{EntryMutexStd, EntrySingle};
1415

1516
#[derive(Default, Debug)]

0 commit comments

Comments
 (0)