Skip to content

Commit

Permalink
refactor: extract mod peer_list
Browse files Browse the repository at this point in the history
  • Loading branch information
josecelano committed Apr 15, 2024
1 parent 922afda commit 42f1b30
Show file tree
Hide file tree
Showing 10 changed files with 93 additions and 74 deletions.
69 changes: 3 additions & 66 deletions packages/torrent-repository/src/entry/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@ use std::fmt::Debug;
use std::net::SocketAddr;
use std::sync::Arc;

//use serde::{Deserialize, Serialize};
use torrust_tracker_configuration::TrackerPolicy;
use torrust_tracker_primitives::swarm_metadata::SwarmMetadata;
use torrust_tracker_primitives::{peer, DurationSinceUnixEpoch};

use self::peer_list::PeerList;

pub mod mutex_std;
pub mod mutex_tokio;
pub mod peer_list;
pub mod single;

pub trait Entry {
Expand Down Expand Up @@ -86,68 +88,3 @@ pub struct Torrent {
/// The number of peers that have ever completed downloading the torrent associated to this entry
pub(crate) downloaded: u32,
}

#[derive(Clone, Debug, Default, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct PeerList {
peers: std::collections::BTreeMap<peer::Id, Arc<peer::Peer>>,
}

impl PeerList {
fn len(&self) -> usize {
self.peers.len()
}

fn is_empty(&self) -> bool {
self.peers.is_empty()
}

fn insert(&mut self, key: peer::Id, value: Arc<peer::Peer>) -> Option<Arc<peer::Peer>> {
self.peers.insert(key, value)
}

fn remove(&mut self, key: &peer::Id) -> Option<Arc<peer::Peer>> {
self.peers.remove(key)
}

fn retain<F>(&mut self, f: F)
where
F: FnMut(&peer::Id, &mut Arc<peer::Peer>) -> bool,
{
self.peers.retain(f);
}

fn seeders_and_leechers(&self) -> (usize, usize) {
let seeders = self.peers.values().filter(|peer| peer.is_seeder()).count();
let leechers = self.len() - seeders;

(seeders, leechers)
}

fn get_peers(&self, limit: Option<usize>) -> Vec<Arc<peer::Peer>> {
match limit {
Some(limit) => self.peers.values().take(limit).cloned().collect(),
None => self.peers.values().cloned().collect(),
}
}

fn get_peers_for_client(&self, client: &SocketAddr, limit: Option<usize>) -> Vec<Arc<peer::Peer>> {
match limit {
Some(limit) => self
.peers
.values()
// Take peers which are not the client peer
.filter(|peer| peer::ReadInfo::get_address(peer.as_ref()) != *client)
// Limit the number of peers on the result
.take(limit)
.cloned()
.collect(),
None => self
.peers
.values()
// Take peers which are not the client peer
.filter(|peer| peer::ReadInfo::get_address(peer.as_ref()) != *client)
.cloned()
.collect(),
}
}
}
74 changes: 74 additions & 0 deletions packages/torrent-repository/src/entry/peer_list.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
use std::net::SocketAddr;
use std::sync::Arc;

use torrust_tracker_primitives::peer;

#[derive(Clone, Debug, Default, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct PeerList {
peers: std::collections::BTreeMap<peer::Id, Arc<peer::Peer>>,
}

impl PeerList {
#[must_use]
pub fn len(&self) -> usize {
self.peers.len()
}

#[must_use]
pub fn is_empty(&self) -> bool {
self.peers.is_empty()
}

pub fn insert(&mut self, key: peer::Id, value: Arc<peer::Peer>) -> Option<Arc<peer::Peer>> {
self.peers.insert(key, value)
}

pub fn remove(&mut self, key: &peer::Id) -> Option<Arc<peer::Peer>> {
self.peers.remove(key)
}

pub fn retain<F>(&mut self, f: F)
where
F: FnMut(&peer::Id, &mut Arc<peer::Peer>) -> bool,
{
self.peers.retain(f);
}

#[must_use]
pub fn seeders_and_leechers(&self) -> (usize, usize) {
let seeders = self.peers.values().filter(|peer| peer.is_seeder()).count();
let leechers = self.len() - seeders;

(seeders, leechers)
}

#[must_use]
pub fn get_peers(&self, limit: Option<usize>) -> Vec<Arc<peer::Peer>> {
match limit {
Some(limit) => self.peers.values().take(limit).cloned().collect(),
None => self.peers.values().cloned().collect(),
}
}

#[must_use]
pub fn get_peers_for_client(&self, client: &SocketAddr, limit: Option<usize>) -> Vec<Arc<peer::Peer>> {
match limit {
Some(limit) => self
.peers
.values()
// Take peers which are not the client peer
.filter(|peer| peer::ReadInfo::get_address(peer.as_ref()) != *client)
// Limit the number of peers on the result
.take(limit)
.cloned()
.collect(),
None => self
.peers
.values()
// Take peers which are not the client peer
.filter(|peer| peer::ReadInfo::get_address(peer.as_ref()) != *client)
.cloned()
.collect(),
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ use torrust_tracker_primitives::torrent_metrics::TorrentsMetrics;
use torrust_tracker_primitives::{peer, DurationSinceUnixEpoch, PersistentTorrents};

use super::Repository;
use crate::entry::{Entry, EntrySync, PeerList};
use crate::entry::peer_list::PeerList;
use crate::entry::{Entry, EntrySync};
use crate::{EntryMutexStd, EntrySingle};

#[derive(Default, Debug)]
Expand Down
3 changes: 2 additions & 1 deletion packages/torrent-repository/src/repository/rw_lock_std.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ use torrust_tracker_primitives::torrent_metrics::TorrentsMetrics;
use torrust_tracker_primitives::{peer, DurationSinceUnixEpoch, PersistentTorrents};

use super::Repository;
use crate::entry::{Entry, PeerList};
use crate::entry::peer_list::PeerList;
use crate::entry::Entry;
use crate::{EntrySingle, TorrentsRwLockStd};

#[derive(Default, Debug)]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ use torrust_tracker_primitives::torrent_metrics::TorrentsMetrics;
use torrust_tracker_primitives::{peer, DurationSinceUnixEpoch, PersistentTorrents};

use super::Repository;
use crate::entry::{Entry, EntrySync, PeerList};
use crate::entry::peer_list::PeerList;
use crate::entry::{Entry, EntrySync};
use crate::{EntryMutexStd, EntrySingle, TorrentsRwLockStdMutexStd};

impl TorrentsRwLockStdMutexStd {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ use torrust_tracker_primitives::torrent_metrics::TorrentsMetrics;
use torrust_tracker_primitives::{peer, DurationSinceUnixEpoch, PersistentTorrents};

use super::RepositoryAsync;
use crate::entry::{Entry, EntryAsync, PeerList};
use crate::entry::peer_list::PeerList;
use crate::entry::{Entry, EntryAsync};
use crate::{EntryMutexTokio, EntrySingle, TorrentsRwLockStdMutexTokio};

impl TorrentsRwLockStdMutexTokio {
Expand Down
3 changes: 2 additions & 1 deletion packages/torrent-repository/src/repository/rw_lock_tokio.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ use torrust_tracker_primitives::torrent_metrics::TorrentsMetrics;
use torrust_tracker_primitives::{peer, DurationSinceUnixEpoch, PersistentTorrents};

use super::RepositoryAsync;
use crate::entry::{Entry, PeerList};
use crate::entry::peer_list::PeerList;
use crate::entry::Entry;
use crate::{EntrySingle, TorrentsRwLockTokio};

#[derive(Default, Debug)]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ use torrust_tracker_primitives::torrent_metrics::TorrentsMetrics;
use torrust_tracker_primitives::{peer, DurationSinceUnixEpoch, PersistentTorrents};

use super::RepositoryAsync;
use crate::entry::{Entry, EntrySync, PeerList};
use crate::entry::peer_list::PeerList;
use crate::entry::{Entry, EntrySync};
use crate::{EntryMutexStd, EntrySingle, TorrentsRwLockTokioMutexStd};

impl TorrentsRwLockTokioMutexStd {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ use torrust_tracker_primitives::torrent_metrics::TorrentsMetrics;
use torrust_tracker_primitives::{peer, DurationSinceUnixEpoch, PersistentTorrents};

use super::RepositoryAsync;
use crate::entry::{Entry, EntryAsync, PeerList};
use crate::entry::peer_list::PeerList;
use crate::entry::{Entry, EntryAsync};
use crate::{EntryMutexTokio, EntrySingle, TorrentsRwLockTokioMutexTokio};

impl TorrentsRwLockTokioMutexTokio {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ use torrust_tracker_primitives::torrent_metrics::TorrentsMetrics;
use torrust_tracker_primitives::{peer, DurationSinceUnixEpoch, PersistentTorrents};

use super::Repository;
use crate::entry::{Entry, EntrySync, PeerList};
use crate::entry::peer_list::PeerList;
use crate::entry::{Entry, EntrySync};
use crate::{EntryMutexStd, EntrySingle};

#[derive(Default, Debug)]
Expand Down

0 comments on commit 42f1b30

Please sign in to comment.