From 5ac8a30991eb674cebf8d57214a607081156789c Mon Sep 17 00:00:00 2001 From: Cameron Garnham Date: Sun, 17 Mar 2024 03:32:19 +0800 Subject: [PATCH] dev: refactor entry traits --- packages/torrent-repository/src/entry/mod.rs | 65 +++++++------------ .../torrent-repository/src/entry/mutex_std.rs | 8 +-- .../src/entry/mutex_tokio.rs | 9 +-- .../torrent-repository/src/entry/single.rs | 9 +-- packages/torrent-repository/src/lib.rs | 6 +- .../src/repository/rw_lock_std.rs | 8 ++- .../src/repository/rw_lock_std_mutex_std.rs | 8 ++- .../src/repository/rw_lock_std_mutex_tokio.rs | 10 ++- .../src/repository/rw_lock_tokio.rs | 7 +- .../src/repository/rw_lock_tokio_mutex_std.rs | 8 ++- .../repository/rw_lock_tokio_mutex_tokio.rs | 8 ++- src/core/mod.rs | 4 +- src/core/services/torrent.rs | 2 +- src/core/torrent/mod.rs | 2 +- 14 files changed, 73 insertions(+), 81 deletions(-) diff --git a/packages/torrent-repository/src/entry/mod.rs b/packages/torrent-repository/src/entry/mod.rs index 1d3c39a93..04aa597df 100644 --- a/packages/torrent-repository/src/entry/mod.rs +++ b/packages/torrent-repository/src/entry/mod.rs @@ -10,7 +10,7 @@ pub mod mutex_std; pub mod mutex_tokio; pub mod single; -pub trait ReadInfo { +pub trait Entry { /// It returns the swarm metadata (statistics) as a struct: /// /// `(seeders, completed, leechers)` @@ -24,27 +24,7 @@ pub trait ReadInfo { /// Returns the number of Peers fn get_peers_len(&self) -> usize; -} - -/// Same as [`ReadInfo`], but async. -pub trait ReadInfoAsync { - /// It returns the swarm metadata (statistics) as a struct: - /// - /// `(seeders, completed, leechers)` - fn get_stats(self) -> impl std::future::Future + Send; - - /// Returns True if Still a Valid Entry according to the Tracker Policy - #[allow(clippy::wrong_self_convention)] - fn is_not_zombie(self, policy: &TrackerPolicy) -> impl std::future::Future + Send; - - /// Returns True if the Peers is Empty - fn peers_is_empty(self) -> impl std::future::Future + Send; - - /// Returns the number of Peers - fn get_peers_len(self) -> impl std::future::Future + Send; -} -pub trait ReadPeers { /// Get all swarm peers, optionally limiting the result. fn get_peers(&self, limit: Option) -> Vec>; @@ -54,20 +34,7 @@ pub trait ReadPeers { /// It filters out the input peer, typically because we want to return this /// list of peers to that client peer. fn get_peers_for_peer(&self, client: &peer::Peer, limit: Option) -> Vec>; -} -/// Same as [`ReadPeers`], but async. -pub trait ReadPeersAsync { - fn get_peers(self, limit: Option) -> impl std::future::Future>> + Send; - - fn get_peers_for_peer( - self, - client: &peer::Peer, - limit: Option, - ) -> impl std::future::Future>> + Send; -} - -pub trait Update { /// It updates a peer and returns true if the number of complete downloads have increased. /// /// The number of peers that have complete downloading is synchronously updated when peers are updated. @@ -81,22 +48,38 @@ pub trait Update { fn remove_inactive_peers(&mut self, current_cutoff: DurationSinceUnixEpoch); } -/// Same as [`Update`], except not `mut`. -pub trait UpdateSync { +#[allow(clippy::module_name_repetitions)] +pub trait EntrySync { + fn get_stats(&self) -> SwarmMetadata; + fn is_not_zombie(&self, policy: &TrackerPolicy) -> bool; + fn peers_is_empty(&self) -> bool; + fn get_peers_len(&self) -> usize; + fn get_peers(&self, limit: Option) -> Vec>; + fn get_peers_for_peer(&self, client: &peer::Peer, limit: Option) -> Vec>; fn insert_or_update_peer(&self, peer: &peer::Peer) -> bool; fn insert_or_update_peer_and_get_stats(&self, peer: &peer::Peer) -> (bool, SwarmMetadata); fn remove_inactive_peers(&self, current_cutoff: DurationSinceUnixEpoch); } -/// Same as [`Update`], except not `mut` and async. -pub trait UpdateAsync { - fn insert_or_update_peer(self, peer: &peer::Peer) -> impl std::future::Future + Send; +#[allow(clippy::module_name_repetitions)] +pub trait EntryAsync { + fn get_stats(self) -> impl std::future::Future + Send; + #[allow(clippy::wrong_self_convention)] + fn is_not_zombie(self, policy: &TrackerPolicy) -> impl std::future::Future + Send; + fn peers_is_empty(self) -> impl std::future::Future + Send; + fn get_peers_len(self) -> impl std::future::Future + Send; + fn get_peers(self, limit: Option) -> impl std::future::Future>> + Send; + fn get_peers_for_peer( + self, + client: &peer::Peer, + limit: Option, + ) -> impl std::future::Future>> + Send; + fn insert_or_update_peer(self, peer: &peer::Peer) -> impl std::future::Future + Send; fn insert_or_update_peer_and_get_stats( self, peer: &peer::Peer, ) -> impl std::future::Future + std::marker::Send; - fn remove_inactive_peers(self, current_cutoff: DurationSinceUnixEpoch) -> impl std::future::Future + Send; } @@ -106,7 +89,7 @@ pub trait UpdateAsync { /// that's the list of all the peers trying to download the same torrent. /// The tracker keeps one entry like this for every torrent. #[derive(Serialize, Deserialize, Clone, Debug, Default)] -pub struct Entry { +pub struct Torrent { /// The swarm: a network of peers that are all trying to download the torrent associated to this entry #[serde(skip)] pub(crate) peers: std::collections::BTreeMap>, diff --git a/packages/torrent-repository/src/entry/mutex_std.rs b/packages/torrent-repository/src/entry/mutex_std.rs index 9a4ee6b07..df6228317 100644 --- a/packages/torrent-repository/src/entry/mutex_std.rs +++ b/packages/torrent-repository/src/entry/mutex_std.rs @@ -4,10 +4,10 @@ use torrust_tracker_configuration::TrackerPolicy; use torrust_tracker_primitives::swarm_metadata::SwarmMetadata; use torrust_tracker_primitives::{peer, DurationSinceUnixEpoch}; -use super::{ReadInfo, ReadPeers, Update, UpdateSync}; +use super::{Entry, EntrySync}; use crate::EntryMutexStd; -impl ReadInfo for EntryMutexStd { +impl EntrySync for EntryMutexStd { fn get_stats(&self) -> SwarmMetadata { self.lock().expect("it should get a lock").get_stats() } @@ -23,9 +23,7 @@ impl ReadInfo for EntryMutexStd { fn get_peers_len(&self) -> usize { self.lock().expect("it should get a lock").get_peers_len() } -} -impl ReadPeers for EntryMutexStd { fn get_peers(&self, limit: Option) -> Vec> { self.lock().expect("it should get lock").get_peers(limit) } @@ -33,9 +31,7 @@ impl ReadPeers for EntryMutexStd { fn get_peers_for_peer(&self, client: &peer::Peer, limit: Option) -> Vec> { self.lock().expect("it should get lock").get_peers_for_peer(client, limit) } -} -impl UpdateSync for EntryMutexStd { fn insert_or_update_peer(&self, peer: &peer::Peer) -> bool { self.lock().expect("it should lock the entry").insert_or_update_peer(peer) } diff --git a/packages/torrent-repository/src/entry/mutex_tokio.rs b/packages/torrent-repository/src/entry/mutex_tokio.rs index 8ef0756c0..c4d13fb43 100644 --- a/packages/torrent-repository/src/entry/mutex_tokio.rs +++ b/packages/torrent-repository/src/entry/mutex_tokio.rs @@ -4,11 +4,10 @@ use torrust_tracker_configuration::TrackerPolicy; use torrust_tracker_primitives::swarm_metadata::SwarmMetadata; use torrust_tracker_primitives::{peer, DurationSinceUnixEpoch}; -use super::{ReadInfo, ReadInfoAsync, ReadPeers, ReadPeersAsync, Update}; -use crate::entry::UpdateAsync; +use super::{Entry, EntryAsync}; use crate::EntryMutexTokio; -impl ReadInfoAsync for EntryMutexTokio { +impl EntryAsync for EntryMutexTokio { async fn get_stats(self) -> SwarmMetadata { self.lock().await.get_stats() } @@ -24,9 +23,7 @@ impl ReadInfoAsync for EntryMutexTokio { async fn get_peers_len(self) -> usize { self.lock().await.get_peers_len() } -} -impl ReadPeersAsync for EntryMutexTokio { async fn get_peers(self, limit: Option) -> Vec> { self.lock().await.get_peers(limit) } @@ -34,9 +31,7 @@ impl ReadPeersAsync for EntryMutexTokio { async fn get_peers_for_peer(self, client: &peer::Peer, limit: Option) -> Vec> { self.lock().await.get_peers_for_peer(client, limit) } -} -impl UpdateAsync for EntryMutexTokio { async fn insert_or_update_peer(self, peer: &peer::Peer) -> bool { self.lock().await.insert_or_update_peer(peer) } diff --git a/packages/torrent-repository/src/entry/single.rs b/packages/torrent-repository/src/entry/single.rs index 5046b803e..7a5cf6240 100644 --- a/packages/torrent-repository/src/entry/single.rs +++ b/packages/torrent-repository/src/entry/single.rs @@ -6,10 +6,10 @@ use torrust_tracker_primitives::peer::{self}; use torrust_tracker_primitives::swarm_metadata::SwarmMetadata; use torrust_tracker_primitives::DurationSinceUnixEpoch; -use super::{ReadInfo, ReadPeers, Update}; +use super::Entry; use crate::EntrySingle; -impl ReadInfo for EntrySingle { +impl Entry for EntrySingle { #[allow(clippy::cast_possible_truncation)] fn get_stats(&self) -> SwarmMetadata { let complete: u32 = self.peers.values().filter(|peer| peer.is_seeder()).count() as u32; @@ -41,9 +41,6 @@ impl ReadInfo for EntrySingle { fn get_peers_len(&self) -> usize { self.peers.len() } -} - -impl ReadPeers for EntrySingle { fn get_peers(&self, limit: Option) -> Vec> { match limit { Some(limit) => self.peers.values().take(limit).cloned().collect(), @@ -71,9 +68,7 @@ impl ReadPeers for EntrySingle { .collect(), } } -} -impl Update for EntrySingle { fn insert_or_update_peer(&mut self, peer: &peer::Peer) -> bool { let mut did_torrent_stats_change: bool = false; diff --git a/packages/torrent-repository/src/lib.rs b/packages/torrent-repository/src/lib.rs index 9a626dc6a..903e1405e 100644 --- a/packages/torrent-repository/src/lib.rs +++ b/packages/torrent-repository/src/lib.rs @@ -3,9 +3,9 @@ use std::sync::Arc; pub mod entry; pub mod repository; -pub type EntrySingle = entry::Entry; -pub type EntryMutexStd = Arc>; -pub type EntryMutexTokio = Arc>; +pub type EntrySingle = entry::Torrent; +pub type EntryMutexStd = Arc>; +pub type EntryMutexTokio = Arc>; pub type TorrentsRwLockStd = repository::RwLockStd; pub type TorrentsRwLockStdMutexStd = repository::RwLockStd; diff --git a/packages/torrent-repository/src/repository/rw_lock_std.rs b/packages/torrent-repository/src/repository/rw_lock_std.rs index 69f2b5298..bacef623d 100644 --- a/packages/torrent-repository/src/repository/rw_lock_std.rs +++ b/packages/torrent-repository/src/repository/rw_lock_std.rs @@ -8,7 +8,7 @@ use torrust_tracker_primitives::torrent_metrics::TorrentsMetrics; use torrust_tracker_primitives::{peer, DurationSinceUnixEpoch, PersistentTorrents}; use super::Repository; -use crate::entry::{ReadInfo, Update}; +use crate::entry::Entry; use crate::{EntrySingle, TorrentsRwLockStd}; impl TorrentsRwLockStd { @@ -27,7 +27,10 @@ impl TorrentsRwLockStd { } } -impl Repository for TorrentsRwLockStd { +impl Repository for TorrentsRwLockStd +where + EntrySingle: Entry, +{ fn update_torrent_with_peer_and_get_stats(&self, info_hash: &InfoHash, peer: &peer::Peer) -> (bool, SwarmMetadata) { let mut db = self.get_torrents_mut(); @@ -35,6 +38,7 @@ impl Repository for TorrentsRwLockStd { entry.insert_or_update_peer_and_get_stats(peer) } + fn get(&self, key: &InfoHash) -> Option { let db = self.get_torrents(); db.get(key).cloned() diff --git a/packages/torrent-repository/src/repository/rw_lock_std_mutex_std.rs b/packages/torrent-repository/src/repository/rw_lock_std_mutex_std.rs index 506952919..9fca82ba8 100644 --- a/packages/torrent-repository/src/repository/rw_lock_std_mutex_std.rs +++ b/packages/torrent-repository/src/repository/rw_lock_std_mutex_std.rs @@ -9,7 +9,7 @@ use torrust_tracker_primitives::torrent_metrics::TorrentsMetrics; use torrust_tracker_primitives::{peer, DurationSinceUnixEpoch, PersistentTorrents}; use super::Repository; -use crate::entry::{ReadInfo, UpdateSync}; +use crate::entry::{Entry, EntrySync}; use crate::{EntryMutexStd, EntrySingle, TorrentsRwLockStdMutexStd}; impl TorrentsRwLockStdMutexStd { @@ -28,7 +28,11 @@ impl TorrentsRwLockStdMutexStd { } } -impl Repository for TorrentsRwLockStdMutexStd { +impl Repository for TorrentsRwLockStdMutexStd +where + EntryMutexStd: EntrySync, + EntrySingle: Entry, +{ fn update_torrent_with_peer_and_get_stats(&self, info_hash: &InfoHash, peer: &peer::Peer) -> (bool, SwarmMetadata) { let maybe_entry = self.get_torrents().get(info_hash).cloned(); diff --git a/packages/torrent-repository/src/repository/rw_lock_std_mutex_tokio.rs b/packages/torrent-repository/src/repository/rw_lock_std_mutex_tokio.rs index 13b7f7e2e..b9fb54469 100644 --- a/packages/torrent-repository/src/repository/rw_lock_std_mutex_tokio.rs +++ b/packages/torrent-repository/src/repository/rw_lock_std_mutex_tokio.rs @@ -12,7 +12,7 @@ use torrust_tracker_primitives::torrent_metrics::TorrentsMetrics; use torrust_tracker_primitives::{peer, DurationSinceUnixEpoch, PersistentTorrents}; use super::RepositoryAsync; -use crate::entry::{self, ReadInfo, UpdateAsync}; +use crate::entry::{Entry, EntryAsync}; use crate::{EntryMutexTokio, EntrySingle, TorrentsRwLockStdMutexTokio}; impl TorrentsRwLockStdMutexTokio { @@ -31,7 +31,11 @@ impl TorrentsRwLockStdMutexTokio { } } -impl RepositoryAsync for TorrentsRwLockStdMutexTokio { +impl RepositoryAsync for TorrentsRwLockStdMutexTokio +where + EntryMutexTokio: EntryAsync, + EntrySingle: Entry, +{ async fn update_torrent_with_peer_and_get_stats(&self, info_hash: &InfoHash, peer: &peer::Peer) -> (bool, SwarmMetadata) { let maybe_entry = self.get_torrents().get(info_hash).cloned(); @@ -67,7 +71,7 @@ impl RepositoryAsync for TorrentsRwLockStdMutexTokio { async fn get_metrics(&self) -> TorrentsMetrics { let mut metrics = TorrentsMetrics::default(); - let entries: Vec>> = self.get_torrents().values().cloned().collect(); + let entries: Vec<_> = self.get_torrents().values().cloned().collect(); for entry in entries { let stats = entry.lock().await.get_stats(); diff --git a/packages/torrent-repository/src/repository/rw_lock_tokio.rs b/packages/torrent-repository/src/repository/rw_lock_tokio.rs index ef643469a..d0b7ec751 100644 --- a/packages/torrent-repository/src/repository/rw_lock_tokio.rs +++ b/packages/torrent-repository/src/repository/rw_lock_tokio.rs @@ -8,7 +8,7 @@ use torrust_tracker_primitives::torrent_metrics::TorrentsMetrics; use torrust_tracker_primitives::{peer, DurationSinceUnixEpoch, PersistentTorrents}; use super::RepositoryAsync; -use crate::entry::{ReadInfo, Update}; +use crate::entry::Entry; use crate::{EntrySingle, TorrentsRwLockTokio}; impl TorrentsRwLockTokio { @@ -29,7 +29,10 @@ impl TorrentsRwLockTokio { } } -impl RepositoryAsync for TorrentsRwLockTokio { +impl RepositoryAsync for TorrentsRwLockTokio +where + EntrySingle: Entry, +{ async fn update_torrent_with_peer_and_get_stats(&self, info_hash: &InfoHash, peer: &peer::Peer) -> (bool, SwarmMetadata) { let mut db = self.get_torrents_mut().await; diff --git a/packages/torrent-repository/src/repository/rw_lock_tokio_mutex_std.rs b/packages/torrent-repository/src/repository/rw_lock_tokio_mutex_std.rs index 1385a9263..f800d2001 100644 --- a/packages/torrent-repository/src/repository/rw_lock_tokio_mutex_std.rs +++ b/packages/torrent-repository/src/repository/rw_lock_tokio_mutex_std.rs @@ -9,7 +9,7 @@ use torrust_tracker_primitives::torrent_metrics::TorrentsMetrics; use torrust_tracker_primitives::{peer, DurationSinceUnixEpoch, PersistentTorrents}; use super::RepositoryAsync; -use crate::entry::{ReadInfo, UpdateSync}; +use crate::entry::{Entry, EntrySync}; use crate::{EntryMutexStd, EntrySingle, TorrentsRwLockTokioMutexStd}; impl TorrentsRwLockTokioMutexStd { @@ -30,7 +30,11 @@ impl TorrentsRwLockTokioMutexStd { } } -impl RepositoryAsync for TorrentsRwLockTokioMutexStd { +impl RepositoryAsync for TorrentsRwLockTokioMutexStd +where + EntryMutexStd: EntrySync, + EntrySingle: Entry, +{ async fn update_torrent_with_peer_and_get_stats(&self, info_hash: &InfoHash, peer: &peer::Peer) -> (bool, SwarmMetadata) { let maybe_entry = self.get_torrents().await.get(info_hash).cloned(); diff --git a/packages/torrent-repository/src/repository/rw_lock_tokio_mutex_tokio.rs b/packages/torrent-repository/src/repository/rw_lock_tokio_mutex_tokio.rs index 059742cf5..7ce2cc74c 100644 --- a/packages/torrent-repository/src/repository/rw_lock_tokio_mutex_tokio.rs +++ b/packages/torrent-repository/src/repository/rw_lock_tokio_mutex_tokio.rs @@ -9,7 +9,7 @@ use torrust_tracker_primitives::torrent_metrics::TorrentsMetrics; use torrust_tracker_primitives::{peer, DurationSinceUnixEpoch, PersistentTorrents}; use super::RepositoryAsync; -use crate::entry::{ReadInfo, ReadInfoAsync, UpdateAsync}; +use crate::entry::{Entry, EntryAsync}; use crate::{EntryMutexTokio, EntrySingle, TorrentsRwLockTokioMutexTokio}; impl TorrentsRwLockTokioMutexTokio { @@ -30,7 +30,11 @@ impl TorrentsRwLockTokioMutexTokio { } } -impl RepositoryAsync for TorrentsRwLockTokioMutexTokio { +impl RepositoryAsync for TorrentsRwLockTokioMutexTokio +where + EntryMutexTokio: EntryAsync, + EntrySingle: Entry, +{ async fn update_torrent_with_peer_and_get_stats(&self, info_hash: &InfoHash, peer: &peer::Peer) -> (bool, SwarmMetadata) { let maybe_entry = self.get_torrents().await.get(info_hash).cloned(); diff --git a/src/core/mod.rs b/src/core/mod.rs index 8f632cfc4..f94c46543 100644 --- a/src/core/mod.rs +++ b/src/core/mod.rs @@ -449,7 +449,7 @@ use torrust_tracker_primitives::info_hash::InfoHash; use torrust_tracker_primitives::swarm_metadata::SwarmMetadata; use torrust_tracker_primitives::torrent_metrics::TorrentsMetrics; use torrust_tracker_primitives::{peer, TrackerMode}; -use torrust_tracker_torrent_repository::entry::{ReadInfo, ReadPeers}; +use torrust_tracker_torrent_repository::entry::EntrySync; use torrust_tracker_torrent_repository::repository::Repository; use self::auth::Key; @@ -1689,7 +1689,7 @@ mod tests { mod handling_torrent_persistence { use torrust_tracker_primitives::announce_event::AnnounceEvent; - use torrust_tracker_torrent_repository::entry::ReadInfo; + use torrust_tracker_torrent_repository::entry::EntrySync; use torrust_tracker_torrent_repository::repository::Repository; use crate::core::tests::the_tracker::{sample_info_hash, sample_peer, tracker_persisting_torrents_in_database}; diff --git a/src/core/services/torrent.rs b/src/core/services/torrent.rs index 7a81c4887..ce44af3a8 100644 --- a/src/core/services/torrent.rs +++ b/src/core/services/torrent.rs @@ -9,7 +9,7 @@ use std::sync::Arc; use torrust_tracker_primitives::info_hash::InfoHash; use torrust_tracker_primitives::pagination::Pagination; use torrust_tracker_primitives::peer; -use torrust_tracker_torrent_repository::entry::{ReadInfo, ReadPeers}; +use torrust_tracker_torrent_repository::entry::EntrySync; use torrust_tracker_torrent_repository::repository::Repository; use crate::core::Tracker; diff --git a/src/core/torrent/mod.rs b/src/core/torrent/mod.rs index b90a842a0..b5a2b4c07 100644 --- a/src/core/torrent/mod.rs +++ b/src/core/torrent/mod.rs @@ -42,7 +42,7 @@ mod tests { use torrust_tracker_primitives::announce_event::AnnounceEvent; use torrust_tracker_primitives::{peer, DurationSinceUnixEpoch, NumberOfBytes}; - use torrust_tracker_torrent_repository::entry::{ReadInfo, ReadPeers, Update}; + use torrust_tracker_torrent_repository::entry::Entry; use torrust_tracker_torrent_repository::EntrySingle; use crate::core::TORRENT_PEERS_LIMIT;