diff --git a/packages/axum-http-tracker-server/src/v1/handlers/announce.rs b/packages/axum-http-tracker-server/src/v1/handlers/announce.rs index 7d7a0b386..16ff83f81 100644 --- a/packages/axum-http-tracker-server/src/v1/handlers/announce.rs +++ b/packages/axum-http-tracker-server/src/v1/handlers/announce.rs @@ -119,8 +119,8 @@ mod tests { use bittorrent_tracker_core::authentication::key::repository::in_memory::InMemoryKeyRepository; use bittorrent_tracker_core::authentication::service::AuthenticationService; use bittorrent_tracker_core::databases::setup::initialize_database; + use bittorrent_tracker_core::statistics::persisted::downloads::DatabaseDownloadsMetricRepository; use bittorrent_tracker_core::torrent::repository::in_memory::InMemoryTorrentRepository; - use bittorrent_tracker_core::torrent::repository::persisted::DatabasePersistentTorrentRepository; use bittorrent_tracker_core::whitelist::authorization::WhitelistAuthorization; use bittorrent_tracker_core::whitelist::repository::in_memory::InMemoryWhitelist; use torrust_tracker_configuration::Configuration; @@ -156,12 +156,12 @@ mod tests { let in_memory_key_repository = Arc::new(InMemoryKeyRepository::default()); let authentication_service = Arc::new(AuthenticationService::new(&config.core, &in_memory_key_repository)); let in_memory_torrent_repository = Arc::new(InMemoryTorrentRepository::default()); - let db_torrent_repository = Arc::new(DatabasePersistentTorrentRepository::new(&database)); + let db_downloads_metric_repository = Arc::new(DatabaseDownloadsMetricRepository::new(&database)); let announce_handler = Arc::new(AnnounceHandler::new( &config.core, &whitelist_authorization, &in_memory_torrent_repository, - &db_torrent_repository, + &db_downloads_metric_repository, )); // HTTP core stats diff --git a/packages/http-tracker-core/benches/helpers/util.rs b/packages/http-tracker-core/benches/helpers/util.rs index cfb3f745f..414d3b40e 100644 --- a/packages/http-tracker-core/benches/helpers/util.rs +++ b/packages/http-tracker-core/benches/helpers/util.rs @@ -14,8 +14,8 @@ use bittorrent_tracker_core::announce_handler::AnnounceHandler; use bittorrent_tracker_core::authentication::key::repository::in_memory::InMemoryKeyRepository; use bittorrent_tracker_core::authentication::service::AuthenticationService; use bittorrent_tracker_core::databases::setup::initialize_database; +use bittorrent_tracker_core::statistics::persisted::downloads::DatabaseDownloadsMetricRepository; use bittorrent_tracker_core::torrent::repository::in_memory::InMemoryTorrentRepository; -use bittorrent_tracker_core::torrent::repository::persisted::DatabasePersistentTorrentRepository; use bittorrent_tracker_core::whitelist::authorization::WhitelistAuthorization; use bittorrent_tracker_core::whitelist::repository::in_memory::InMemoryWhitelist; use futures::future::BoxFuture; @@ -45,7 +45,7 @@ pub fn initialize_core_tracker_services_with_config(config: &Configuration) -> ( let core_config = Arc::new(config.core.clone()); let database = initialize_database(&config.core); let in_memory_torrent_repository = Arc::new(InMemoryTorrentRepository::default()); - let db_torrent_repository = Arc::new(DatabasePersistentTorrentRepository::new(&database)); + let db_downloads_metric_repository = Arc::new(DatabaseDownloadsMetricRepository::new(&database)); let in_memory_whitelist = Arc::new(InMemoryWhitelist::default()); let whitelist_authorization = Arc::new(WhitelistAuthorization::new(&config.core, &in_memory_whitelist.clone())); let in_memory_key_repository = Arc::new(InMemoryKeyRepository::default()); @@ -55,7 +55,7 @@ pub fn initialize_core_tracker_services_with_config(config: &Configuration) -> ( &config.core, &whitelist_authorization, &in_memory_torrent_repository, - &db_torrent_repository, + &db_downloads_metric_repository, )); // HTTP core stats diff --git a/packages/http-tracker-core/src/services/announce.rs b/packages/http-tracker-core/src/services/announce.rs index 0ad5ed143..23d589bce 100644 --- a/packages/http-tracker-core/src/services/announce.rs +++ b/packages/http-tracker-core/src/services/announce.rs @@ -212,8 +212,8 @@ mod tests { use bittorrent_tracker_core::authentication::key::repository::in_memory::InMemoryKeyRepository; use bittorrent_tracker_core::authentication::service::AuthenticationService; use bittorrent_tracker_core::databases::setup::initialize_database; + use bittorrent_tracker_core::statistics::persisted::downloads::DatabaseDownloadsMetricRepository; use bittorrent_tracker_core::torrent::repository::in_memory::InMemoryTorrentRepository; - use bittorrent_tracker_core::torrent::repository::persisted::DatabasePersistentTorrentRepository; use bittorrent_tracker_core::whitelist::authorization::WhitelistAuthorization; use bittorrent_tracker_core::whitelist::repository::in_memory::InMemoryWhitelist; use torrust_tracker_configuration::{Configuration, Core}; @@ -239,7 +239,7 @@ mod tests { let core_config = Arc::new(config.core.clone()); let database = initialize_database(&config.core); let in_memory_torrent_repository = Arc::new(InMemoryTorrentRepository::default()); - let db_torrent_repository = Arc::new(DatabasePersistentTorrentRepository::new(&database)); + let db_downloads_metric_repository = Arc::new(DatabaseDownloadsMetricRepository::new(&database)); let in_memory_whitelist = Arc::new(InMemoryWhitelist::default()); let whitelist_authorization = Arc::new(WhitelistAuthorization::new(&config.core, &in_memory_whitelist.clone())); let in_memory_key_repository = Arc::new(InMemoryKeyRepository::default()); @@ -249,7 +249,7 @@ mod tests { &config.core, &whitelist_authorization, &in_memory_torrent_repository, - &db_torrent_repository, + &db_downloads_metric_repository, )); // HTTP core stats diff --git a/packages/http-tracker-core/src/services/scrape.rs b/packages/http-tracker-core/src/services/scrape.rs index f22f2f632..1445ffcfe 100644 --- a/packages/http-tracker-core/src/services/scrape.rs +++ b/packages/http-tracker-core/src/services/scrape.rs @@ -176,8 +176,8 @@ mod tests { use bittorrent_tracker_core::authentication::service::AuthenticationService; use bittorrent_tracker_core::databases::setup::initialize_database; use bittorrent_tracker_core::scrape_handler::ScrapeHandler; + use bittorrent_tracker_core::statistics::persisted::downloads::DatabaseDownloadsMetricRepository; use bittorrent_tracker_core::torrent::repository::in_memory::InMemoryTorrentRepository; - use bittorrent_tracker_core::torrent::repository::persisted::DatabasePersistentTorrentRepository; use bittorrent_tracker_core::whitelist::authorization::WhitelistAuthorization; use bittorrent_tracker_core::whitelist::repository::in_memory::InMemoryWhitelist; use futures::future::BoxFuture; @@ -200,7 +200,7 @@ mod tests { let in_memory_whitelist = Arc::new(InMemoryWhitelist::default()); let whitelist_authorization = Arc::new(WhitelistAuthorization::new(&config.core, &in_memory_whitelist.clone())); let in_memory_torrent_repository = Arc::new(InMemoryTorrentRepository::default()); - let db_torrent_repository = Arc::new(DatabasePersistentTorrentRepository::new(&database)); + let db_downloads_metric_repository = Arc::new(DatabaseDownloadsMetricRepository::new(&database)); let in_memory_key_repository = Arc::new(InMemoryKeyRepository::default()); let authentication_service = Arc::new(AuthenticationService::new(&config.core, &in_memory_key_repository)); @@ -208,7 +208,7 @@ mod tests { &config.core, &whitelist_authorization, &in_memory_torrent_repository, - &db_torrent_repository, + &db_downloads_metric_repository, )); let scrape_handler = Arc::new(ScrapeHandler::new(&whitelist_authorization, &in_memory_torrent_repository)); diff --git a/packages/primitives/src/lib.rs b/packages/primitives/src/lib.rs index c901e5276..ec2edda97 100644 --- a/packages/primitives/src/lib.rs +++ b/packages/primitives/src/lib.rs @@ -18,5 +18,5 @@ use bittorrent_primitives::info_hash::InfoHash; /// Duration since the Unix Epoch. pub type DurationSinceUnixEpoch = Duration; -pub type PersistentTorrent = u32; -pub type PersistentTorrents = BTreeMap; +pub type NumberOfDownloads = u32; +pub type NumberOfDownloadsBTreeMap = BTreeMap; diff --git a/packages/torrent-repository-benchmarking/src/repository/dash_map_mutex_std.rs b/packages/torrent-repository-benchmarking/src/repository/dash_map_mutex_std.rs index d4a84caa0..192777b32 100644 --- a/packages/torrent-repository-benchmarking/src/repository/dash_map_mutex_std.rs +++ b/packages/torrent-repository-benchmarking/src/repository/dash_map_mutex_std.rs @@ -5,7 +5,7 @@ use dashmap::DashMap; use torrust_tracker_configuration::TrackerPolicy; use torrust_tracker_primitives::pagination::Pagination; use torrust_tracker_primitives::swarm_metadata::{AggregateSwarmMetadata, SwarmMetadata}; -use torrust_tracker_primitives::{peer, DurationSinceUnixEpoch, PersistentTorrent, PersistentTorrents}; +use torrust_tracker_primitives::{peer, DurationSinceUnixEpoch, NumberOfDownloads, NumberOfDownloadsBTreeMap}; use super::Repository; use crate::entry::peer_list::PeerList; @@ -22,7 +22,7 @@ where EntryMutexStd: EntrySync, EntrySingle: Entry, { - fn upsert_peer(&self, info_hash: &InfoHash, peer: &peer::Peer, _opt_persistent_torrent: Option) -> bool { + fn upsert_peer(&self, info_hash: &InfoHash, peer: &peer::Peer, _opt_persistent_torrent: Option) -> bool { // todo: load persistent torrent data if provided if let Some(entry) = self.torrents.get(info_hash) { @@ -77,7 +77,7 @@ where } } - fn import_persistent(&self, persistent_torrents: &PersistentTorrents) { + fn import_persistent(&self, persistent_torrents: &NumberOfDownloadsBTreeMap) { for (info_hash, completed) in persistent_torrents { if self.torrents.contains_key(info_hash) { continue; diff --git a/packages/torrent-repository-benchmarking/src/repository/mod.rs b/packages/torrent-repository-benchmarking/src/repository/mod.rs index 9284ff6e6..890088ea7 100644 --- a/packages/torrent-repository-benchmarking/src/repository/mod.rs +++ b/packages/torrent-repository-benchmarking/src/repository/mod.rs @@ -2,7 +2,7 @@ use bittorrent_primitives::info_hash::InfoHash; use torrust_tracker_configuration::TrackerPolicy; use torrust_tracker_primitives::pagination::Pagination; use torrust_tracker_primitives::swarm_metadata::{AggregateSwarmMetadata, SwarmMetadata}; -use torrust_tracker_primitives::{peer, DurationSinceUnixEpoch, PersistentTorrent, PersistentTorrents}; +use torrust_tracker_primitives::{peer, DurationSinceUnixEpoch, NumberOfDownloads, NumberOfDownloadsBTreeMap}; pub mod dash_map_mutex_std; pub mod rw_lock_std; @@ -19,11 +19,11 @@ pub trait Repository: Debug + Default + Sized + 'static { fn get(&self, key: &InfoHash) -> Option; fn get_metrics(&self) -> AggregateSwarmMetadata; fn get_paginated(&self, pagination: Option<&Pagination>) -> Vec<(InfoHash, T)>; - fn import_persistent(&self, persistent_torrents: &PersistentTorrents); + fn import_persistent(&self, persistent_torrents: &NumberOfDownloadsBTreeMap); fn remove(&self, key: &InfoHash) -> Option; fn remove_inactive_peers(&self, current_cutoff: DurationSinceUnixEpoch); fn remove_peerless_torrents(&self, policy: &TrackerPolicy); - fn upsert_peer(&self, info_hash: &InfoHash, peer: &peer::Peer, opt_persistent_torrent: Option) -> bool; + fn upsert_peer(&self, info_hash: &InfoHash, peer: &peer::Peer, opt_persistent_torrent: Option) -> bool; fn get_swarm_metadata(&self, info_hash: &InfoHash) -> Option; } @@ -32,7 +32,7 @@ pub trait RepositoryAsync: Debug + Default + Sized + 'static { fn get(&self, key: &InfoHash) -> impl std::future::Future> + Send; fn get_metrics(&self) -> impl std::future::Future + Send; fn get_paginated(&self, pagination: Option<&Pagination>) -> impl std::future::Future> + Send; - fn import_persistent(&self, persistent_torrents: &PersistentTorrents) -> impl std::future::Future + Send; + fn import_persistent(&self, persistent_torrents: &NumberOfDownloadsBTreeMap) -> impl std::future::Future + Send; fn remove(&self, key: &InfoHash) -> impl std::future::Future> + Send; fn remove_inactive_peers(&self, current_cutoff: DurationSinceUnixEpoch) -> impl std::future::Future + Send; fn remove_peerless_torrents(&self, policy: &TrackerPolicy) -> impl std::future::Future + Send; @@ -40,7 +40,7 @@ pub trait RepositoryAsync: Debug + Default + Sized + 'static { &self, info_hash: &InfoHash, peer: &peer::Peer, - opt_persistent_torrent: Option, + opt_persistent_torrent: Option, ) -> impl std::future::Future + Send; fn get_swarm_metadata(&self, info_hash: &InfoHash) -> impl std::future::Future> + Send; } diff --git a/packages/torrent-repository-benchmarking/src/repository/rw_lock_std.rs b/packages/torrent-repository-benchmarking/src/repository/rw_lock_std.rs index d190718af..074725674 100644 --- a/packages/torrent-repository-benchmarking/src/repository/rw_lock_std.rs +++ b/packages/torrent-repository-benchmarking/src/repository/rw_lock_std.rs @@ -2,7 +2,7 @@ use bittorrent_primitives::info_hash::InfoHash; use torrust_tracker_configuration::TrackerPolicy; use torrust_tracker_primitives::pagination::Pagination; use torrust_tracker_primitives::swarm_metadata::{AggregateSwarmMetadata, SwarmMetadata}; -use torrust_tracker_primitives::{peer, DurationSinceUnixEpoch, PersistentTorrent, PersistentTorrents}; +use torrust_tracker_primitives::{peer, DurationSinceUnixEpoch, NumberOfDownloads, NumberOfDownloadsBTreeMap}; use super::Repository; use crate::entry::peer_list::PeerList; @@ -45,7 +45,7 @@ impl Repository for TorrentsRwLockStd where EntrySingle: Entry, { - fn upsert_peer(&self, info_hash: &InfoHash, peer: &peer::Peer, _opt_persistent_torrent: Option) -> bool { + fn upsert_peer(&self, info_hash: &InfoHash, peer: &peer::Peer, _opt_persistent_torrent: Option) -> bool { // todo: load persistent torrent data if provided let mut db = self.get_torrents_mut(); @@ -92,7 +92,7 @@ where } } - fn import_persistent(&self, persistent_torrents: &PersistentTorrents) { + fn import_persistent(&self, persistent_torrents: &NumberOfDownloadsBTreeMap) { let mut torrents = self.get_torrents_mut(); for (info_hash, downloaded) in persistent_torrents { diff --git a/packages/torrent-repository-benchmarking/src/repository/rw_lock_std_mutex_std.rs b/packages/torrent-repository-benchmarking/src/repository/rw_lock_std_mutex_std.rs index 1764b94e8..9577a42e1 100644 --- a/packages/torrent-repository-benchmarking/src/repository/rw_lock_std_mutex_std.rs +++ b/packages/torrent-repository-benchmarking/src/repository/rw_lock_std_mutex_std.rs @@ -4,7 +4,7 @@ use bittorrent_primitives::info_hash::InfoHash; use torrust_tracker_configuration::TrackerPolicy; use torrust_tracker_primitives::pagination::Pagination; use torrust_tracker_primitives::swarm_metadata::{AggregateSwarmMetadata, SwarmMetadata}; -use torrust_tracker_primitives::{peer, DurationSinceUnixEpoch, PersistentTorrent, PersistentTorrents}; +use torrust_tracker_primitives::{peer, DurationSinceUnixEpoch, NumberOfDownloads, NumberOfDownloadsBTreeMap}; use super::Repository; use crate::entry::peer_list::PeerList; @@ -32,7 +32,7 @@ where EntryMutexStd: EntrySync, EntrySingle: Entry, { - fn upsert_peer(&self, info_hash: &InfoHash, peer: &peer::Peer, _opt_persistent_torrent: Option) -> bool { + fn upsert_peer(&self, info_hash: &InfoHash, peer: &peer::Peer, _opt_persistent_torrent: Option) -> bool { // todo: load persistent torrent data if provided let maybe_entry = self.get_torrents().get(info_hash).cloned(); @@ -87,7 +87,7 @@ where } } - fn import_persistent(&self, persistent_torrents: &PersistentTorrents) { + fn import_persistent(&self, persistent_torrents: &NumberOfDownloadsBTreeMap) { let mut torrents = self.get_torrents_mut(); for (info_hash, completed) in persistent_torrents { diff --git a/packages/torrent-repository-benchmarking/src/repository/rw_lock_std_mutex_tokio.rs b/packages/torrent-repository-benchmarking/src/repository/rw_lock_std_mutex_tokio.rs index 116c1ff87..73cb64a08 100644 --- a/packages/torrent-repository-benchmarking/src/repository/rw_lock_std_mutex_tokio.rs +++ b/packages/torrent-repository-benchmarking/src/repository/rw_lock_std_mutex_tokio.rs @@ -8,7 +8,7 @@ use futures::{Future, FutureExt}; use torrust_tracker_configuration::TrackerPolicy; use torrust_tracker_primitives::pagination::Pagination; use torrust_tracker_primitives::swarm_metadata::{AggregateSwarmMetadata, SwarmMetadata}; -use torrust_tracker_primitives::{peer, DurationSinceUnixEpoch, PersistentTorrent, PersistentTorrents}; +use torrust_tracker_primitives::{peer, DurationSinceUnixEpoch, NumberOfDownloads, NumberOfDownloadsBTreeMap}; use super::RepositoryAsync; use crate::entry::peer_list::PeerList; @@ -40,7 +40,7 @@ where &self, info_hash: &InfoHash, peer: &peer::Peer, - _opt_persistent_torrent: Option, + _opt_persistent_torrent: Option, ) -> bool { // todo: load persistent torrent data if provided @@ -101,7 +101,7 @@ where metrics } - async fn import_persistent(&self, persistent_torrents: &PersistentTorrents) { + async fn import_persistent(&self, persistent_torrents: &NumberOfDownloadsBTreeMap) { let mut db = self.get_torrents_mut(); for (info_hash, completed) in persistent_torrents { diff --git a/packages/torrent-repository-benchmarking/src/repository/rw_lock_tokio.rs b/packages/torrent-repository-benchmarking/src/repository/rw_lock_tokio.rs index 53838023d..9d7d591fc 100644 --- a/packages/torrent-repository-benchmarking/src/repository/rw_lock_tokio.rs +++ b/packages/torrent-repository-benchmarking/src/repository/rw_lock_tokio.rs @@ -2,7 +2,7 @@ use bittorrent_primitives::info_hash::InfoHash; use torrust_tracker_configuration::TrackerPolicy; use torrust_tracker_primitives::pagination::Pagination; use torrust_tracker_primitives::swarm_metadata::{AggregateSwarmMetadata, SwarmMetadata}; -use torrust_tracker_primitives::{peer, DurationSinceUnixEpoch, PersistentTorrent, PersistentTorrents}; +use torrust_tracker_primitives::{peer, DurationSinceUnixEpoch, NumberOfDownloads, NumberOfDownloadsBTreeMap}; use super::RepositoryAsync; use crate::entry::peer_list::PeerList; @@ -50,7 +50,7 @@ where &self, info_hash: &InfoHash, peer: &peer::Peer, - _opt_persistent_torrent: Option, + _opt_persistent_torrent: Option, ) -> bool { // todo: load persistent torrent data if provided @@ -98,7 +98,7 @@ where metrics } - async fn import_persistent(&self, persistent_torrents: &PersistentTorrents) { + async fn import_persistent(&self, persistent_torrents: &NumberOfDownloadsBTreeMap) { let mut torrents = self.get_torrents_mut().await; for (info_hash, completed) in persistent_torrents { diff --git a/packages/torrent-repository-benchmarking/src/repository/rw_lock_tokio_mutex_std.rs b/packages/torrent-repository-benchmarking/src/repository/rw_lock_tokio_mutex_std.rs index eb7e300fd..6ad7ade98 100644 --- a/packages/torrent-repository-benchmarking/src/repository/rw_lock_tokio_mutex_std.rs +++ b/packages/torrent-repository-benchmarking/src/repository/rw_lock_tokio_mutex_std.rs @@ -4,7 +4,7 @@ use bittorrent_primitives::info_hash::InfoHash; use torrust_tracker_configuration::TrackerPolicy; use torrust_tracker_primitives::pagination::Pagination; use torrust_tracker_primitives::swarm_metadata::{AggregateSwarmMetadata, SwarmMetadata}; -use torrust_tracker_primitives::{peer, DurationSinceUnixEpoch, PersistentTorrent, PersistentTorrents}; +use torrust_tracker_primitives::{peer, DurationSinceUnixEpoch, NumberOfDownloads, NumberOfDownloadsBTreeMap}; use super::RepositoryAsync; use crate::entry::peer_list::PeerList; @@ -38,7 +38,7 @@ where &self, info_hash: &InfoHash, peer: &peer::Peer, - _opt_persistent_torrent: Option, + _opt_persistent_torrent: Option, ) -> bool { // todo: load persistent torrent data if provided @@ -92,7 +92,7 @@ where metrics } - async fn import_persistent(&self, persistent_torrents: &PersistentTorrents) { + async fn import_persistent(&self, persistent_torrents: &NumberOfDownloadsBTreeMap) { let mut torrents = self.get_torrents_mut().await; for (info_hash, completed) in persistent_torrents { diff --git a/packages/torrent-repository-benchmarking/src/repository/rw_lock_tokio_mutex_tokio.rs b/packages/torrent-repository-benchmarking/src/repository/rw_lock_tokio_mutex_tokio.rs index c8ebaf4d6..6ce6c3f58 100644 --- a/packages/torrent-repository-benchmarking/src/repository/rw_lock_tokio_mutex_tokio.rs +++ b/packages/torrent-repository-benchmarking/src/repository/rw_lock_tokio_mutex_tokio.rs @@ -4,7 +4,7 @@ use bittorrent_primitives::info_hash::InfoHash; use torrust_tracker_configuration::TrackerPolicy; use torrust_tracker_primitives::pagination::Pagination; use torrust_tracker_primitives::swarm_metadata::{AggregateSwarmMetadata, SwarmMetadata}; -use torrust_tracker_primitives::{peer, DurationSinceUnixEpoch, PersistentTorrent, PersistentTorrents}; +use torrust_tracker_primitives::{peer, DurationSinceUnixEpoch, NumberOfDownloads, NumberOfDownloadsBTreeMap}; use super::RepositoryAsync; use crate::entry::peer_list::PeerList; @@ -38,7 +38,7 @@ where &self, info_hash: &InfoHash, peer: &peer::Peer, - _opt_persistent_torrent: Option, + _opt_persistent_torrent: Option, ) -> bool { // todo: load persistent torrent data if provided @@ -95,7 +95,7 @@ where metrics } - async fn import_persistent(&self, persistent_torrents: &PersistentTorrents) { + async fn import_persistent(&self, persistent_torrents: &NumberOfDownloadsBTreeMap) { let mut db = self.get_torrents_mut().await; for (info_hash, completed) in persistent_torrents { diff --git a/packages/torrent-repository-benchmarking/src/repository/skip_map_mutex_std.rs b/packages/torrent-repository-benchmarking/src/repository/skip_map_mutex_std.rs index 8a15a9442..81fc1c05a 100644 --- a/packages/torrent-repository-benchmarking/src/repository/skip_map_mutex_std.rs +++ b/packages/torrent-repository-benchmarking/src/repository/skip_map_mutex_std.rs @@ -5,7 +5,7 @@ use crossbeam_skiplist::SkipMap; use torrust_tracker_configuration::TrackerPolicy; use torrust_tracker_primitives::pagination::Pagination; use torrust_tracker_primitives::swarm_metadata::{AggregateSwarmMetadata, SwarmMetadata}; -use torrust_tracker_primitives::{peer, DurationSinceUnixEpoch, PersistentTorrent, PersistentTorrents}; +use torrust_tracker_primitives::{peer, DurationSinceUnixEpoch, NumberOfDownloads, NumberOfDownloadsBTreeMap}; use super::Repository; use crate::entry::peer_list::PeerList; @@ -38,7 +38,7 @@ where /// /// Returns `true` if the number of downloads was increased because the peer /// completed the download. - fn upsert_peer(&self, info_hash: &InfoHash, peer: &peer::Peer, opt_persistent_torrent: Option) -> bool { + fn upsert_peer(&self, info_hash: &InfoHash, peer: &peer::Peer, opt_persistent_torrent: Option) -> bool { if let Some(existing_entry) = self.torrents.get(info_hash) { existing_entry.value().upsert_peer(peer) } else { @@ -100,7 +100,7 @@ where } } - fn import_persistent(&self, persistent_torrents: &PersistentTorrents) { + fn import_persistent(&self, persistent_torrents: &NumberOfDownloadsBTreeMap) { for (info_hash, completed) in persistent_torrents { if self.torrents.contains_key(info_hash) { continue; @@ -146,7 +146,7 @@ where EntryRwLockParkingLot: EntrySync, EntrySingle: Entry, { - fn upsert_peer(&self, info_hash: &InfoHash, peer: &peer::Peer, _opt_persistent_torrent: Option) -> bool { + fn upsert_peer(&self, info_hash: &InfoHash, peer: &peer::Peer, _opt_persistent_torrent: Option) -> bool { // todo: load persistent torrent data if provided let entry = self.torrents.get_or_insert(*info_hash, Arc::default()); @@ -193,7 +193,7 @@ where } } - fn import_persistent(&self, persistent_torrents: &PersistentTorrents) { + fn import_persistent(&self, persistent_torrents: &NumberOfDownloadsBTreeMap) { for (info_hash, completed) in persistent_torrents { if self.torrents.contains_key(info_hash) { continue; @@ -239,7 +239,7 @@ where EntryMutexParkingLot: EntrySync, EntrySingle: Entry, { - fn upsert_peer(&self, info_hash: &InfoHash, peer: &peer::Peer, _opt_persistent_torrent: Option) -> bool { + fn upsert_peer(&self, info_hash: &InfoHash, peer: &peer::Peer, _opt_persistent_torrent: Option) -> bool { // todo: load persistent torrent data if provided let entry = self.torrents.get_or_insert(*info_hash, Arc::default()); @@ -286,7 +286,7 @@ where } } - fn import_persistent(&self, persistent_torrents: &PersistentTorrents) { + fn import_persistent(&self, persistent_torrents: &NumberOfDownloadsBTreeMap) { for (info_hash, completed) in persistent_torrents { if self.torrents.contains_key(info_hash) { continue; diff --git a/packages/torrent-repository-benchmarking/tests/common/repo.rs b/packages/torrent-repository-benchmarking/tests/common/repo.rs index 6c5c6ff77..e5037d641 100644 --- a/packages/torrent-repository-benchmarking/tests/common/repo.rs +++ b/packages/torrent-repository-benchmarking/tests/common/repo.rs @@ -2,7 +2,7 @@ use bittorrent_primitives::info_hash::InfoHash; use torrust_tracker_configuration::TrackerPolicy; use torrust_tracker_primitives::pagination::Pagination; use torrust_tracker_primitives::swarm_metadata::{AggregateSwarmMetadata, SwarmMetadata}; -use torrust_tracker_primitives::{peer, DurationSinceUnixEpoch, PersistentTorrent, PersistentTorrents}; +use torrust_tracker_primitives::{peer, DurationSinceUnixEpoch, NumberOfDownloads, NumberOfDownloadsBTreeMap}; use torrust_tracker_torrent_repository_benchmarking::repository::{Repository as _, RepositoryAsync as _}; use torrust_tracker_torrent_repository_benchmarking::{ EntrySingle, TorrentsDashMapMutexStd, TorrentsRwLockStd, TorrentsRwLockStdMutexStd, TorrentsRwLockStdMutexTokio, @@ -29,7 +29,7 @@ impl Repo { &self, info_hash: &InfoHash, peer: &peer::Peer, - opt_persistent_torrent: Option, + opt_persistent_torrent: Option, ) -> bool { match self { Repo::RwLockStd(repo) => repo.upsert_peer(info_hash, peer, opt_persistent_torrent), @@ -144,7 +144,7 @@ impl Repo { } } - pub(crate) async fn import_persistent(&self, persistent_torrents: &PersistentTorrents) { + pub(crate) async fn import_persistent(&self, persistent_torrents: &NumberOfDownloadsBTreeMap) { match self { Repo::RwLockStd(repo) => repo.import_persistent(persistent_torrents), Repo::RwLockStdMutexStd(repo) => repo.import_persistent(persistent_torrents), diff --git a/packages/torrent-repository-benchmarking/tests/repository/mod.rs b/packages/torrent-repository-benchmarking/tests/repository/mod.rs index 6973f38bd..141faa8a9 100644 --- a/packages/torrent-repository-benchmarking/tests/repository/mod.rs +++ b/packages/torrent-repository-benchmarking/tests/repository/mod.rs @@ -7,7 +7,7 @@ use rstest::{fixture, rstest}; use torrust_tracker_configuration::TrackerPolicy; use torrust_tracker_primitives::pagination::Pagination; use torrust_tracker_primitives::swarm_metadata::SwarmMetadata; -use torrust_tracker_primitives::PersistentTorrents; +use torrust_tracker_primitives::NumberOfDownloadsBTreeMap; use torrust_tracker_torrent_repository_benchmarking::entry::Entry as _; use torrust_tracker_torrent_repository_benchmarking::repository::dash_map_mutex_std::XacrimonDashMap; use torrust_tracker_torrent_repository_benchmarking::repository::rw_lock_std::RwLockStd; @@ -167,12 +167,12 @@ fn many_hashed_in_order() -> Entries { } #[fixture] -fn persistent_empty() -> PersistentTorrents { - PersistentTorrents::default() +fn persistent_empty() -> NumberOfDownloadsBTreeMap { + NumberOfDownloadsBTreeMap::default() } #[fixture] -fn persistent_single() -> PersistentTorrents { +fn persistent_single() -> NumberOfDownloadsBTreeMap { let hash = &mut DefaultHasher::default(); hash.write_u8(1); @@ -182,7 +182,7 @@ fn persistent_single() -> PersistentTorrents { } #[fixture] -fn persistent_three() -> PersistentTorrents { +fn persistent_three() -> NumberOfDownloadsBTreeMap { let hash = &mut DefaultHasher::default(); hash.write_u8(1); @@ -445,7 +445,7 @@ async fn it_should_import_persistent_torrents( )] repo: Repo, #[case] entries: Entries, - #[values(persistent_empty(), persistent_single(), persistent_three())] persistent_torrents: PersistentTorrents, + #[values(persistent_empty(), persistent_single(), persistent_three())] persistent_torrents: NumberOfDownloadsBTreeMap, ) { make(&repo, &entries).await; diff --git a/packages/torrent-repository/src/swarms.rs b/packages/torrent-repository/src/swarms.rs index 1504ac1f4..ba8a80a62 100644 --- a/packages/torrent-repository/src/swarms.rs +++ b/packages/torrent-repository/src/swarms.rs @@ -7,7 +7,7 @@ use torrust_tracker_clock::conv::convert_from_timestamp_to_datetime_utc; use torrust_tracker_configuration::TrackerPolicy; use torrust_tracker_primitives::pagination::Pagination; use torrust_tracker_primitives::swarm_metadata::{AggregateSwarmMetadata, SwarmMetadata}; -use torrust_tracker_primitives::{peer, DurationSinceUnixEpoch, PersistentTorrent, PersistentTorrents}; +use torrust_tracker_primitives::{peer, DurationSinceUnixEpoch, NumberOfDownloads, NumberOfDownloadsBTreeMap}; use crate::event::sender::Sender; use crate::event::Event; @@ -53,7 +53,7 @@ impl Swarms { &self, info_hash: &InfoHash, peer: &peer::Peer, - opt_persistent_torrent: Option, + opt_persistent_torrent: Option, ) -> Result<(), Error> { let swarm_handle = match self.swarms.get(info_hash) { None => { @@ -356,7 +356,7 @@ impl Swarms { /// This method takes a set of persisted torrent entries (e.g., from a /// database) and imports them into the in-memory repository for immediate /// access. - pub fn import_persistent(&self, persistent_torrents: &PersistentTorrents) -> u64 { + pub fn import_persistent(&self, persistent_torrents: &NumberOfDownloadsBTreeMap) -> u64 { tracing::info!("Importing persisted info about torrents ..."); let mut torrents_imported = 0; @@ -1271,7 +1271,7 @@ mod tests { use std::sync::Arc; - use torrust_tracker_primitives::PersistentTorrents; + use torrust_tracker_primitives::NumberOfDownloadsBTreeMap; use crate::swarms::Swarms; use crate::tests::{leecher, sample_info_hash}; @@ -1282,7 +1282,7 @@ mod tests { let infohash = sample_info_hash(); - let mut persistent_torrents = PersistentTorrents::default(); + let mut persistent_torrents = NumberOfDownloadsBTreeMap::default(); persistent_torrents.insert(infohash, 1); @@ -1302,7 +1302,7 @@ mod tests { let infohash = sample_info_hash(); - let mut persistent_torrents = PersistentTorrents::default(); + let mut persistent_torrents = NumberOfDownloadsBTreeMap::default(); persistent_torrents.insert(infohash, 1); persistent_torrents.insert(infohash, 2); @@ -1327,7 +1327,7 @@ mod tests { // Try to import the torrent entry let new_number_of_downloads = initial_number_of_downloads + 1; - let mut persistent_torrents = PersistentTorrents::default(); + let mut persistent_torrents = NumberOfDownloadsBTreeMap::default(); persistent_torrents.insert(infohash, new_number_of_downloads); swarms.import_persistent(&persistent_torrents); diff --git a/packages/tracker-core/src/announce_handler.rs b/packages/tracker-core/src/announce_handler.rs index ffd244f2a..a6614361a 100644 --- a/packages/tracker-core/src/announce_handler.rs +++ b/packages/tracker-core/src/announce_handler.rs @@ -99,8 +99,8 @@ use torrust_tracker_primitives::core::AnnounceData; use torrust_tracker_primitives::peer; use super::torrent::repository::in_memory::InMemoryTorrentRepository; -use super::torrent::repository::persisted::DatabasePersistentTorrentRepository; use crate::error::AnnounceError; +use crate::statistics::persisted::downloads::DatabaseDownloadsMetricRepository; use crate::whitelist::authorization::WhitelistAuthorization; /// Handles `announce` requests from `BitTorrent` clients. @@ -115,7 +115,7 @@ pub struct AnnounceHandler { in_memory_torrent_repository: Arc, /// Repository for persistent torrent data (database). - db_torrent_repository: Arc, + db_downloads_metric_repository: Arc, } impl AnnounceHandler { @@ -125,13 +125,13 @@ impl AnnounceHandler { config: &Core, whitelist_authorization: &Arc, in_memory_torrent_repository: &Arc, - db_torrent_repository: &Arc, + db_downloads_metric_repository: &Arc, ) -> Self { Self { whitelist_authorization: whitelist_authorization.clone(), config: config.clone(), in_memory_torrent_repository: in_memory_torrent_repository.clone(), - db_torrent_repository: db_torrent_repository.clone(), + db_downloads_metric_repository: db_downloads_metric_repository.clone(), } } @@ -169,7 +169,7 @@ impl AnnounceHandler { // downloads across all torrents. The in-memory metric will count only // the number of downloads during the current tracker uptime. let opt_persistent_torrent = if self.config.tracker_policy.persistent_torrent_completed_stat { - self.db_torrent_repository.load(info_hash)? + self.db_downloads_metric_repository.load_torrent_downloads(info_hash)? } else { None }; diff --git a/packages/tracker-core/src/container.rs b/packages/tracker-core/src/container.rs index ed56fb106..02af67118 100644 --- a/packages/tracker-core/src/container.rs +++ b/packages/tracker-core/src/container.rs @@ -11,9 +11,9 @@ use crate::authentication::service::AuthenticationService; use crate::databases::setup::initialize_database; use crate::databases::Database; use crate::scrape_handler::ScrapeHandler; +use crate::statistics::persisted::downloads::DatabaseDownloadsMetricRepository; use crate::torrent::manager::TorrentsManager; use crate::torrent::repository::in_memory::InMemoryTorrentRepository; -use crate::torrent::repository::persisted::DatabasePersistentTorrentRepository; use crate::whitelist::authorization::WhitelistAuthorization; use crate::whitelist::manager::WhitelistManager; use crate::whitelist::repository::in_memory::InMemoryWhitelist; @@ -31,7 +31,7 @@ pub struct TrackerCoreContainer { pub whitelist_authorization: Arc, pub whitelist_manager: Arc, pub in_memory_torrent_repository: Arc, - pub db_torrent_repository: Arc, + pub db_downloads_metric_repository: Arc, pub torrents_manager: Arc, pub stats_repository: Arc, } @@ -51,12 +51,12 @@ impl TrackerCoreContainer { &in_memory_key_repository.clone(), )); let in_memory_torrent_repository = Arc::new(InMemoryTorrentRepository::new(torrent_repository_container.swarms.clone())); - let db_torrent_repository = Arc::new(DatabasePersistentTorrentRepository::new(&database)); + let db_downloads_metric_repository = Arc::new(DatabaseDownloadsMetricRepository::new(&database)); let torrents_manager = Arc::new(TorrentsManager::new( core_config, &in_memory_torrent_repository, - &db_torrent_repository, + &db_downloads_metric_repository, )); let stats_repository = Arc::new(statistics::repository::Repository::new()); @@ -65,7 +65,7 @@ impl TrackerCoreContainer { core_config, &whitelist_authorization, &in_memory_torrent_repository, - &db_torrent_repository, + &db_downloads_metric_repository, )); let scrape_handler = Arc::new(ScrapeHandler::new(&whitelist_authorization, &in_memory_torrent_repository)); @@ -81,7 +81,7 @@ impl TrackerCoreContainer { whitelist_authorization, whitelist_manager, in_memory_torrent_repository, - db_torrent_repository, + db_downloads_metric_repository, torrents_manager, stats_repository, } diff --git a/packages/tracker-core/src/databases/driver/mod.rs b/packages/tracker-core/src/databases/driver/mod.rs index e8f0ecbfb..6c849bb70 100644 --- a/packages/tracker-core/src/databases/driver/mod.rs +++ b/packages/tracker-core/src/databases/driver/mod.rs @@ -169,9 +169,9 @@ pub(crate) mod tests { let number_of_downloads = 1; - driver.save_persistent_torrent(&infohash, number_of_downloads).unwrap(); + driver.save_torrent_downloads(&infohash, number_of_downloads).unwrap(); - let number_of_downloads = driver.load_persistent_torrent(&infohash).unwrap().unwrap(); + let number_of_downloads = driver.load_torrent_downloads(&infohash).unwrap().unwrap(); assert_eq!(number_of_downloads, 1); } @@ -181,9 +181,9 @@ pub(crate) mod tests { let number_of_downloads = 1; - driver.save_persistent_torrent(&infohash, number_of_downloads).unwrap(); + driver.save_torrent_downloads(&infohash, number_of_downloads).unwrap(); - let torrents = driver.load_persistent_torrents().unwrap(); + let torrents = driver.load_all_torrents_downloads().unwrap(); assert_eq!(torrents.len(), 1); assert_eq!(torrents.get(&infohash), Some(number_of_downloads).as_ref()); @@ -194,11 +194,11 @@ pub(crate) mod tests { let number_of_downloads = 1; - driver.save_persistent_torrent(&infohash, number_of_downloads).unwrap(); + driver.save_torrent_downloads(&infohash, number_of_downloads).unwrap(); - driver.increase_number_of_downloads(&infohash).unwrap(); + driver.increase_downloads_for_torrent(&infohash).unwrap(); - let number_of_downloads = driver.load_persistent_torrent(&infohash).unwrap().unwrap(); + let number_of_downloads = driver.load_torrent_downloads(&infohash).unwrap().unwrap(); assert_eq!(number_of_downloads, 2); } @@ -208,9 +208,9 @@ pub(crate) mod tests { pub fn it_should_save_and_load_the_global_number_of_downloads(driver: &Arc>) { let number_of_downloads = 1; - driver.save_global_number_of_downloads(number_of_downloads).unwrap(); + driver.save_global_downloads(number_of_downloads).unwrap(); - let number_of_downloads = driver.load_global_number_of_downloads().unwrap().unwrap(); + let number_of_downloads = driver.load_global_downloads().unwrap().unwrap(); assert_eq!(number_of_downloads, 1); } @@ -218,9 +218,9 @@ pub(crate) mod tests { pub fn it_should_load_the_global_number_of_downloads(driver: &Arc>) { let number_of_downloads = 1; - driver.save_global_number_of_downloads(number_of_downloads).unwrap(); + driver.save_global_downloads(number_of_downloads).unwrap(); - let number_of_downloads = driver.load_global_number_of_downloads().unwrap().unwrap(); + let number_of_downloads = driver.load_global_downloads().unwrap().unwrap(); assert_eq!(number_of_downloads, 1); } @@ -228,11 +228,11 @@ pub(crate) mod tests { pub fn it_should_increase_the_global_number_of_downloads(driver: &Arc>) { let number_of_downloads = 1; - driver.save_global_number_of_downloads(number_of_downloads).unwrap(); + driver.save_global_downloads(number_of_downloads).unwrap(); - driver.increase_global_number_of_downloads().unwrap(); + driver.increase_global_downloads().unwrap(); - let number_of_downloads = driver.load_global_number_of_downloads().unwrap().unwrap(); + let number_of_downloads = driver.load_global_downloads().unwrap().unwrap(); assert_eq!(number_of_downloads, 2); } diff --git a/packages/tracker-core/src/databases/driver/mysql.rs b/packages/tracker-core/src/databases/driver/mysql.rs index bfbc47ebd..da2f86ce8 100644 --- a/packages/tracker-core/src/databases/driver/mysql.rs +++ b/packages/tracker-core/src/databases/driver/mysql.rs @@ -13,7 +13,7 @@ use r2d2::Pool; use r2d2_mysql::mysql::prelude::Queryable; use r2d2_mysql::mysql::{params, Opts, OptsBuilder}; use r2d2_mysql::MySqlConnectionManager; -use torrust_tracker_primitives::{PersistentTorrent, PersistentTorrents}; +use torrust_tracker_primitives::{NumberOfDownloads, NumberOfDownloadsBTreeMap}; use super::{Database, Driver, Error, TORRENTS_DOWNLOADS_TOTAL}; use crate::authentication::key::AUTH_KEY_LENGTH; @@ -47,7 +47,7 @@ impl Mysql { Ok(Self { pool }) } - fn load_torrent_aggregate_metric(&self, metric_name: &str) -> Result, Error> { + fn load_torrent_aggregate_metric(&self, metric_name: &str) -> Result, Error> { let mut conn = self.pool.get().map_err(|e| (e, DRIVER))?; let query = conn.exec_first::( @@ -60,7 +60,7 @@ impl Mysql { Ok(persistent_torrent) } - fn save_torrent_aggregate_metric(&self, metric_name: &str, completed: PersistentTorrent) -> Result<(), Error> { + fn save_torrent_aggregate_metric(&self, metric_name: &str, completed: NumberOfDownloads) -> Result<(), Error> { const COMMAND : &str = "INSERT INTO torrent_aggregate_metrics (metric_name, value) VALUES (:metric_name, :completed) ON DUPLICATE KEY UPDATE value = VALUES(value)"; let mut conn = self.pool.get().map_err(|e| (e, DRIVER))?; @@ -146,7 +146,7 @@ impl Database for Mysql { } /// Refer to [`databases::Database::load_persistent_torrents`](crate::core::databases::Database::load_persistent_torrents). - fn load_persistent_torrents(&self) -> Result { + fn load_all_torrents_downloads(&self) -> Result { let mut conn = self.pool.get().map_err(|e| (e, DRIVER))?; let torrents = conn.query_map( @@ -161,7 +161,7 @@ impl Database for Mysql { } /// Refer to [`databases::Database::load_persistent_torrent`](crate::core::databases::Database::load_persistent_torrent). - fn load_persistent_torrent(&self, info_hash: &InfoHash) -> Result, Error> { + fn load_torrent_downloads(&self, info_hash: &InfoHash) -> Result, Error> { let mut conn = self.pool.get().map_err(|e| (e, DRIVER))?; let query = conn.exec_first::( @@ -175,7 +175,7 @@ impl Database for Mysql { } /// Refer to [`databases::Database::save_persistent_torrent`](crate::core::databases::Database::save_persistent_torrent). - fn save_persistent_torrent(&self, info_hash: &InfoHash, completed: u32) -> Result<(), Error> { + fn save_torrent_downloads(&self, info_hash: &InfoHash, completed: u32) -> Result<(), Error> { const COMMAND : &str = "INSERT INTO torrents (info_hash, completed) VALUES (:info_hash_str, :completed) ON DUPLICATE KEY UPDATE completed = VALUES(completed)"; let mut conn = self.pool.get().map_err(|e| (e, DRIVER))?; @@ -186,7 +186,7 @@ impl Database for Mysql { } /// Refer to [`databases::Database::increase_number_of_downloads`](crate::core::databases::Database::increase_number_of_downloads). - fn increase_number_of_downloads(&self, info_hash: &InfoHash) -> Result<(), Error> { + fn increase_downloads_for_torrent(&self, info_hash: &InfoHash) -> Result<(), Error> { let mut conn = self.pool.get().map_err(|e| (e, DRIVER))?; let info_hash_str = info_hash.to_string(); @@ -200,17 +200,17 @@ impl Database for Mysql { } /// Refer to [`databases::Database::load_global_number_of_downloads`](crate::core::databases::Database::load_global_number_of_downloads). - fn load_global_number_of_downloads(&self) -> Result, Error> { + fn load_global_downloads(&self) -> Result, Error> { self.load_torrent_aggregate_metric(TORRENTS_DOWNLOADS_TOTAL) } /// Refer to [`databases::Database::save_global_number_of_downloads`](crate::core::databases::Database::save_global_number_of_downloads). - fn save_global_number_of_downloads(&self, downloaded: PersistentTorrent) -> Result<(), Error> { + fn save_global_downloads(&self, downloaded: NumberOfDownloads) -> Result<(), Error> { self.save_torrent_aggregate_metric(TORRENTS_DOWNLOADS_TOTAL, downloaded) } /// Refer to [`databases::Database::increase_global_number_of_downloads`](crate::core::databases::Database::increase_global_number_of_downloads). - fn increase_global_number_of_downloads(&self) -> Result<(), Error> { + fn increase_global_downloads(&self) -> Result<(), Error> { let mut conn = self.pool.get().map_err(|e| (e, DRIVER))?; let metric_name = TORRENTS_DOWNLOADS_TOTAL; diff --git a/packages/tracker-core/src/databases/driver/sqlite.rs b/packages/tracker-core/src/databases/driver/sqlite.rs index 91e969233..d08351aa8 100644 --- a/packages/tracker-core/src/databases/driver/sqlite.rs +++ b/packages/tracker-core/src/databases/driver/sqlite.rs @@ -13,7 +13,7 @@ use r2d2::Pool; use r2d2_sqlite::rusqlite::params; use r2d2_sqlite::rusqlite::types::Null; use r2d2_sqlite::SqliteConnectionManager; -use torrust_tracker_primitives::{DurationSinceUnixEpoch, PersistentTorrent, PersistentTorrents}; +use torrust_tracker_primitives::{DurationSinceUnixEpoch, NumberOfDownloads, NumberOfDownloadsBTreeMap}; use super::{Database, Driver, Error, TORRENTS_DOWNLOADS_TOTAL}; use crate::authentication::{self, Key}; @@ -50,7 +50,7 @@ impl Sqlite { Ok(Self { pool }) } - fn load_torrent_aggregate_metric(&self, metric_name: &str) -> Result, Error> { + fn load_torrent_aggregate_metric(&self, metric_name: &str) -> Result, Error> { let conn = self.pool.get().map_err(|e| (e, DRIVER))?; let mut stmt = conn.prepare("SELECT value FROM torrent_aggregate_metrics WHERE metric_name = ?")?; @@ -65,7 +65,7 @@ impl Sqlite { })) } - fn save_torrent_aggregate_metric(&self, metric_name: &str, completed: PersistentTorrent) -> Result<(), Error> { + fn save_torrent_aggregate_metric(&self, metric_name: &str, completed: NumberOfDownloads) -> Result<(), Error> { let conn = self.pool.get().map_err(|e| (e, DRIVER))?; let insert = conn.execute( @@ -152,7 +152,7 @@ impl Database for Sqlite { } /// Refer to [`databases::Database::load_persistent_torrents`](crate::core::databases::Database::load_persistent_torrents). - fn load_persistent_torrents(&self) -> Result { + fn load_all_torrents_downloads(&self) -> Result { let conn = self.pool.get().map_err(|e| (e, DRIVER))?; let mut stmt = conn.prepare("SELECT info_hash, completed FROM torrents")?; @@ -168,7 +168,7 @@ impl Database for Sqlite { } /// Refer to [`databases::Database::load_persistent_torrent`](crate::core::databases::Database::load_persistent_torrent). - fn load_persistent_torrent(&self, info_hash: &InfoHash) -> Result, Error> { + fn load_torrent_downloads(&self, info_hash: &InfoHash) -> Result, Error> { let conn = self.pool.get().map_err(|e| (e, DRIVER))?; let mut stmt = conn.prepare("SELECT completed FROM torrents WHERE info_hash = ?")?; @@ -184,7 +184,7 @@ impl Database for Sqlite { } /// Refer to [`databases::Database::save_persistent_torrent`](crate::core::databases::Database::save_persistent_torrent). - fn save_persistent_torrent(&self, info_hash: &InfoHash, completed: u32) -> Result<(), Error> { + fn save_torrent_downloads(&self, info_hash: &InfoHash, completed: u32) -> Result<(), Error> { let conn = self.pool.get().map_err(|e| (e, DRIVER))?; let insert = conn.execute( @@ -203,7 +203,7 @@ impl Database for Sqlite { } /// Refer to [`databases::Database::increase_number_of_downloads`](crate::core::databases::Database::increase_number_of_downloads). - fn increase_number_of_downloads(&self, info_hash: &InfoHash) -> Result<(), Error> { + fn increase_downloads_for_torrent(&self, info_hash: &InfoHash) -> Result<(), Error> { let conn = self.pool.get().map_err(|e| (e, DRIVER))?; let _ = conn.execute( @@ -215,17 +215,17 @@ impl Database for Sqlite { } /// Refer to [`databases::Database::load_global_number_of_downloads`](crate::core::databases::Database::load_global_number_of_downloads). - fn load_global_number_of_downloads(&self) -> Result, Error> { + fn load_global_downloads(&self) -> Result, Error> { self.load_torrent_aggregate_metric(TORRENTS_DOWNLOADS_TOTAL) } /// Refer to [`databases::Database::save_global_number_of_downloads`](crate::core::databases::Database::save_global_number_of_downloads). - fn save_global_number_of_downloads(&self, downloaded: PersistentTorrent) -> Result<(), Error> { + fn save_global_downloads(&self, downloaded: NumberOfDownloads) -> Result<(), Error> { self.save_torrent_aggregate_metric(TORRENTS_DOWNLOADS_TOTAL, downloaded) } /// Refer to [`databases::Database::increase_global_number_of_downloads`](crate::core::databases::Database::increase_global_number_of_downloads). - fn increase_global_number_of_downloads(&self) -> Result<(), Error> { + fn increase_global_downloads(&self) -> Result<(), Error> { let conn = self.pool.get().map_err(|e| (e, DRIVER))?; let metric_name = TORRENTS_DOWNLOADS_TOTAL; diff --git a/packages/tracker-core/src/databases/mod.rs b/packages/tracker-core/src/databases/mod.rs index a9d6b2a22..c9d89769a 100644 --- a/packages/tracker-core/src/databases/mod.rs +++ b/packages/tracker-core/src/databases/mod.rs @@ -52,7 +52,7 @@ pub mod setup; use bittorrent_primitives::info_hash::InfoHash; use mockall::automock; -use torrust_tracker_primitives::{PersistentTorrent, PersistentTorrents}; +use torrust_tracker_primitives::{NumberOfDownloads, NumberOfDownloadsBTreeMap}; use self::error::Error; use crate::authentication::{self, Key}; @@ -101,7 +101,7 @@ pub trait Database: Sync + Send { /// # Errors /// /// Returns an [`Error`] if the metrics cannot be loaded. - fn load_persistent_torrents(&self) -> Result; + fn load_all_torrents_downloads(&self) -> Result; /// Loads torrent metrics data from the database for one torrent. /// @@ -110,7 +110,7 @@ pub trait Database: Sync + Send { /// # Errors /// /// Returns an [`Error`] if the metrics cannot be loaded. - fn load_persistent_torrent(&self, info_hash: &InfoHash) -> Result, Error>; + fn load_torrent_downloads(&self, info_hash: &InfoHash) -> Result, Error>; /// Saves torrent metrics data into the database. /// @@ -124,7 +124,7 @@ pub trait Database: Sync + Send { /// # Errors /// /// Returns an [`Error`] if the metrics cannot be saved. - fn save_persistent_torrent(&self, info_hash: &InfoHash, downloaded: u32) -> Result<(), Error>; + fn save_torrent_downloads(&self, info_hash: &InfoHash, downloaded: u32) -> Result<(), Error>; /// Increases the number of downloads for a given torrent. /// @@ -140,7 +140,7 @@ pub trait Database: Sync + Send { /// # Errors /// /// Returns an [`Error`] if the query failed. - fn increase_number_of_downloads(&self, info_hash: &InfoHash) -> Result<(), Error>; + fn increase_downloads_for_torrent(&self, info_hash: &InfoHash) -> Result<(), Error>; /// Loads the total number of downloads for all torrents from the database. /// @@ -149,7 +149,7 @@ pub trait Database: Sync + Send { /// # Errors /// /// Returns an [`Error`] if the total downloads cannot be loaded. - fn load_global_number_of_downloads(&self) -> Result, Error>; + fn load_global_downloads(&self) -> Result, Error>; /// Saves the total number of downloads for all torrents into the database. /// @@ -163,7 +163,7 @@ pub trait Database: Sync + Send { /// # Errors /// /// Returns an [`Error`] if the total downloads cannot be saved. - fn save_global_number_of_downloads(&self, downloaded: PersistentTorrent) -> Result<(), Error>; + fn save_global_downloads(&self, downloaded: NumberOfDownloads) -> Result<(), Error>; /// Increases the total number of downloads for all torrents. /// @@ -172,7 +172,7 @@ pub trait Database: Sync + Send { /// # Errors /// /// Returns an [`Error`] if the query failed. - fn increase_global_number_of_downloads(&self) -> Result<(), Error>; + fn increase_global_downloads(&self) -> Result<(), Error>; // Whitelist diff --git a/packages/tracker-core/src/statistics/event/handler.rs b/packages/tracker-core/src/statistics/event/handler.rs index 4002053e2..0909dc184 100644 --- a/packages/tracker-core/src/statistics/event/handler.rs +++ b/packages/tracker-core/src/statistics/event/handler.rs @@ -5,14 +5,14 @@ use torrust_tracker_metrics::metric_name; use torrust_tracker_primitives::DurationSinceUnixEpoch; use torrust_tracker_torrent_repository::event::Event; +use crate::statistics::persisted::downloads::DatabaseDownloadsMetricRepository; use crate::statistics::repository::Repository; use crate::statistics::TRACKER_CORE_PERSISTENT_TORRENTS_DOWNLOADS_TOTAL; -use crate::torrent::repository::persisted::DatabasePersistentTorrentRepository; pub async fn handle_event( event: Event, stats_repository: &Arc, - db_torrent_repository: &Arc, + db_downloads_metric_repository: &Arc, persistent_torrent_completed_stat: bool, now: DurationSinceUnixEpoch, ) { @@ -53,7 +53,7 @@ pub async fn handle_event( if persistent_torrent_completed_stat { // Increment the number of downloads for the torrent in the database - match db_torrent_repository.increase_number_of_downloads(&info_hash) { + match db_downloads_metric_repository.increase_downloads_for_torrent(&info_hash) { Ok(()) => { tracing::debug!(info_hash = ?info_hash, "Number of torrent downloads increased"); } @@ -63,7 +63,7 @@ pub async fn handle_event( } // Increment the global number of downloads (for all torrents) in the database - match db_torrent_repository.increase_global_number_of_downloads() { + match db_downloads_metric_repository.increase_global_downloads() { Ok(()) => { tracing::debug!("Global number of downloads increased"); } diff --git a/packages/tracker-core/src/statistics/event/listener.rs b/packages/tracker-core/src/statistics/event/listener.rs index cf6d35d6e..2702aa858 100644 --- a/packages/tracker-core/src/statistics/event/listener.rs +++ b/packages/tracker-core/src/statistics/event/listener.rs @@ -6,19 +6,19 @@ use torrust_tracker_events::receiver::RecvError; use torrust_tracker_torrent_repository::event::receiver::Receiver; use super::handler::handle_event; +use crate::statistics::persisted::downloads::DatabaseDownloadsMetricRepository; use crate::statistics::repository::Repository; -use crate::torrent::repository::persisted::DatabasePersistentTorrentRepository; use crate::{CurrentClock, TRACKER_CORE_LOG_TARGET}; #[must_use] pub fn run_event_listener( receiver: Receiver, repository: &Arc, - db_torrent_repository: &Arc, + db_downloads_metric_repository: &Arc, persistent_torrent_completed_stat: bool, ) -> JoinHandle<()> { let stats_repository = repository.clone(); - let db_torrent_repository: Arc = db_torrent_repository.clone(); + let db_downloads_metric_repository: Arc = db_downloads_metric_repository.clone(); tracing::info!(target: TRACKER_CORE_LOG_TARGET, "Starting torrent repository event listener"); @@ -26,7 +26,7 @@ pub fn run_event_listener( dispatch_events( receiver, stats_repository, - db_torrent_repository, + db_downloads_metric_repository, persistent_torrent_completed_stat, ) .await; @@ -38,7 +38,7 @@ pub fn run_event_listener( async fn dispatch_events( mut receiver: Receiver, stats_repository: Arc, - db_torrent_repository: Arc, + db_downloads_metric_repository: Arc, persistent_torrent_completed_stat: bool, ) { let shutdown_signal = tokio::signal::ctrl_c(); @@ -59,7 +59,7 @@ async fn dispatch_events( Ok(event) => handle_event( event, &stats_repository, - &db_torrent_repository, + &db_downloads_metric_repository, persistent_torrent_completed_stat, CurrentClock::now()).await, Err(e) => { diff --git a/packages/tracker-core/src/statistics/mod.rs b/packages/tracker-core/src/statistics/mod.rs index 89d6b79d5..ff8187379 100644 --- a/packages/tracker-core/src/statistics/mod.rs +++ b/packages/tracker-core/src/statistics/mod.rs @@ -1,6 +1,6 @@ pub mod event; pub mod metrics; -pub mod persisted_metrics; +pub mod persisted; pub mod repository; use metrics::Metrics; diff --git a/packages/tracker-core/src/torrent/repository/persisted.rs b/packages/tracker-core/src/statistics/persisted/downloads.rs similarity index 70% rename from packages/tracker-core/src/torrent/repository/persisted.rs rename to packages/tracker-core/src/statistics/persisted/downloads.rs index 1818065fd..4d3bdf9a3 100644 --- a/packages/tracker-core/src/torrent/repository/persisted.rs +++ b/packages/tracker-core/src/statistics/persisted/downloads.rs @@ -2,7 +2,7 @@ use std::sync::Arc; use bittorrent_primitives::info_hash::InfoHash; -use torrust_tracker_primitives::{PersistentTorrent, PersistentTorrents}; +use torrust_tracker_primitives::{NumberOfDownloads, NumberOfDownloadsBTreeMap}; use crate::databases::error::Error; use crate::databases::Database; @@ -19,7 +19,7 @@ use crate::databases::Database; /// /// Not all in-memory torrent data is persisted; only the aggregate metrics are /// stored. -pub struct DatabasePersistentTorrentRepository { +pub struct DatabaseDownloadsMetricRepository { /// A shared reference to the database driver implementation. /// /// The driver must implement the [`Database`] trait. This allows for @@ -28,7 +28,7 @@ pub struct DatabasePersistentTorrentRepository { database: Arc>, } -impl DatabasePersistentTorrentRepository { +impl DatabaseDownloadsMetricRepository { /// Creates a new instance of `DatabasePersistentTorrentRepository`. /// /// # Arguments @@ -41,7 +41,7 @@ impl DatabasePersistentTorrentRepository { /// A new `DatabasePersistentTorrentRepository` instance with a cloned /// reference to the provided database. #[must_use] - pub fn new(database: &Arc>) -> DatabasePersistentTorrentRepository { + pub fn new(database: &Arc>) -> DatabaseDownloadsMetricRepository { Self { database: database.clone(), } @@ -60,12 +60,12 @@ impl DatabasePersistentTorrentRepository { /// # Errors /// /// Returns an [`Error`] if the database operation fails. - pub(crate) fn increase_number_of_downloads(&self, info_hash: &InfoHash) -> Result<(), Error> { - let torrent = self.load(info_hash)?; + pub(crate) fn increase_downloads_for_torrent(&self, info_hash: &InfoHash) -> Result<(), Error> { + let torrent = self.load_torrent_downloads(info_hash)?; match torrent { - Some(_number_of_downloads) => self.database.increase_number_of_downloads(info_hash), - None => self.save(info_hash, 1), + Some(_number_of_downloads) => self.database.increase_downloads_for_torrent(info_hash), + None => self.save_torrent_downloads(info_hash, 1), } } @@ -77,8 +77,8 @@ impl DatabasePersistentTorrentRepository { /// # Errors /// /// Returns an [`Error`] if the underlying database query fails. - pub(crate) fn load_all(&self) -> Result { - self.database.load_persistent_torrents() + pub(crate) fn load_all_torrents_downloads(&self) -> Result { + self.database.load_all_torrents_downloads() } /// Loads one persistent torrent metrics from the database. @@ -89,8 +89,8 @@ impl DatabasePersistentTorrentRepository { /// # Errors /// /// Returns an [`Error`] if the underlying database query fails. - pub(crate) fn load(&self, info_hash: &InfoHash) -> Result, Error> { - self.database.load_persistent_torrent(info_hash) + pub(crate) fn load_torrent_downloads(&self, info_hash: &InfoHash) -> Result, Error> { + self.database.load_torrent_downloads(info_hash) } /// Saves the persistent torrent metric into the database. @@ -106,8 +106,8 @@ impl DatabasePersistentTorrentRepository { /// # Errors /// /// Returns an [`Error`] if the database operation fails. - pub(crate) fn save(&self, info_hash: &InfoHash, downloaded: u32) -> Result<(), Error> { - self.database.save_persistent_torrent(info_hash, downloaded) + pub(crate) fn save_torrent_downloads(&self, info_hash: &InfoHash, downloaded: u32) -> Result<(), Error> { + self.database.save_torrent_downloads(info_hash, downloaded) } // Aggregate Metrics @@ -119,12 +119,12 @@ impl DatabasePersistentTorrentRepository { /// # Errors /// /// Returns an [`Error`] if the database operation fails. - pub(crate) fn increase_global_number_of_downloads(&self) -> Result<(), Error> { - let torrent = self.database.load_global_number_of_downloads()?; + pub(crate) fn increase_global_downloads(&self) -> Result<(), Error> { + let torrent = self.database.load_global_downloads()?; match torrent { - Some(_number_of_downloads) => self.database.increase_global_number_of_downloads(), - None => self.database.save_global_number_of_downloads(1), + Some(_number_of_downloads) => self.database.increase_global_downloads(), + None => self.database.save_global_downloads(1), } } @@ -133,24 +133,24 @@ impl DatabasePersistentTorrentRepository { /// # Errors /// /// Returns an [`Error`] if the underlying database query fails. - pub(crate) fn load_global_number_of_downloads(&self) -> Result, Error> { - self.database.load_global_number_of_downloads() + pub(crate) fn load_global_downloads(&self) -> Result, Error> { + self.database.load_global_downloads() } } #[cfg(test)] mod tests { - use torrust_tracker_primitives::PersistentTorrents; + use torrust_tracker_primitives::NumberOfDownloadsBTreeMap; - use super::DatabasePersistentTorrentRepository; + use super::DatabaseDownloadsMetricRepository; use crate::databases::setup::initialize_database; use crate::test_helpers::tests::{ephemeral_configuration, sample_info_hash, sample_info_hash_one, sample_info_hash_two}; - fn initialize_db_persistent_torrent_repository() -> DatabasePersistentTorrentRepository { + fn initialize_db_persistent_torrent_repository() -> DatabaseDownloadsMetricRepository { let config = ephemeral_configuration(); let database = initialize_database(&config); - DatabasePersistentTorrentRepository::new(&database) + DatabaseDownloadsMetricRepository::new(&database) } #[test] @@ -159,9 +159,9 @@ mod tests { let infohash = sample_info_hash(); - repository.save(&infohash, 1).unwrap(); + repository.save_torrent_downloads(&infohash, 1).unwrap(); - let torrents = repository.load_all().unwrap(); + let torrents = repository.load_all_torrents_downloads().unwrap(); assert_eq!(torrents.get(&infohash), Some(1).as_ref()); } @@ -172,9 +172,9 @@ mod tests { let infohash = sample_info_hash(); - repository.increase_number_of_downloads(&infohash).unwrap(); + repository.increase_downloads_for_torrent(&infohash).unwrap(); - let torrents = repository.load_all().unwrap(); + let torrents = repository.load_all_torrents_downloads().unwrap(); assert_eq!(torrents.get(&infohash), Some(1).as_ref()); } @@ -186,12 +186,12 @@ mod tests { let infohash_one = sample_info_hash_one(); let infohash_two = sample_info_hash_two(); - repository.save(&infohash_one, 1).unwrap(); - repository.save(&infohash_two, 2).unwrap(); + repository.save_torrent_downloads(&infohash_one, 1).unwrap(); + repository.save_torrent_downloads(&infohash_two, 2).unwrap(); - let torrents = repository.load_all().unwrap(); + let torrents = repository.load_all_torrents_downloads().unwrap(); - let mut expected_torrents = PersistentTorrents::new(); + let mut expected_torrents = NumberOfDownloadsBTreeMap::new(); expected_torrents.insert(infohash_one, 1); expected_torrents.insert(infohash_two, 2); diff --git a/packages/tracker-core/src/statistics/persisted_metrics.rs b/packages/tracker-core/src/statistics/persisted/mod.rs similarity index 85% rename from packages/tracker-core/src/statistics/persisted_metrics.rs rename to packages/tracker-core/src/statistics/persisted/mod.rs index 4d53236a5..86c28370d 100644 --- a/packages/tracker-core/src/statistics/persisted_metrics.rs +++ b/packages/tracker-core/src/statistics/persisted/mod.rs @@ -1,3 +1,5 @@ +pub mod downloads; + use std::sync::Arc; use thiserror::Error; @@ -8,7 +10,7 @@ use torrust_tracker_primitives::DurationSinceUnixEpoch; use super::repository::Repository; use super::TRACKER_CORE_PERSISTENT_TORRENTS_DOWNLOADS_TOTAL; use crate::databases; -use crate::torrent::repository::persisted::DatabasePersistentTorrentRepository; +use crate::statistics::persisted::downloads::DatabaseDownloadsMetricRepository; /// Loads persisted metrics from the database and sets them in the stats repository. /// @@ -18,10 +20,10 @@ use crate::torrent::repository::persisted::DatabasePersistentTorrentRepository; /// metric collection fails to set the initial metric values. pub async fn load_persisted_metrics( stats_repository: &Arc, - db_torrent_repository: &Arc, + db_downloads_metric_repository: &Arc, now: DurationSinceUnixEpoch, ) -> Result<(), Error> { - if let Some(downloads) = db_torrent_repository.load_global_number_of_downloads()? { + if let Some(downloads) = db_downloads_metric_repository.load_global_downloads()? { stats_repository .set_counter( &metric_name!(TRACKER_CORE_PERSISTENT_TORRENTS_DOWNLOADS_TOTAL), diff --git a/packages/tracker-core/src/test_helpers.rs b/packages/tracker-core/src/test_helpers.rs index 04fe4133b..62649cd22 100644 --- a/packages/tracker-core/src/test_helpers.rs +++ b/packages/tracker-core/src/test_helpers.rs @@ -19,8 +19,8 @@ pub(crate) mod tests { use crate::announce_handler::AnnounceHandler; use crate::databases::setup::initialize_database; use crate::scrape_handler::ScrapeHandler; + use crate::statistics::persisted::downloads::DatabaseDownloadsMetricRepository; use crate::torrent::repository::in_memory::InMemoryTorrentRepository; - use crate::torrent::repository::persisted::DatabasePersistentTorrentRepository; use crate::whitelist::repository::in_memory::InMemoryWhitelist; use crate::whitelist::{self}; @@ -137,13 +137,13 @@ pub(crate) mod tests { &in_memory_whitelist.clone(), )); let in_memory_torrent_repository = Arc::new(InMemoryTorrentRepository::default()); - let db_torrent_repository = Arc::new(DatabasePersistentTorrentRepository::new(&database)); + let db_downloads_metric_repository = Arc::new(DatabaseDownloadsMetricRepository::new(&database)); let announce_handler = Arc::new(AnnounceHandler::new( &config.core, &whitelist_authorization, &in_memory_torrent_repository, - &db_torrent_repository, + &db_downloads_metric_repository, )); let scrape_handler = Arc::new(ScrapeHandler::new(&whitelist_authorization, &in_memory_torrent_repository)); diff --git a/packages/tracker-core/src/torrent/manager.rs b/packages/tracker-core/src/torrent/manager.rs index d9997c4ad..766fa5c4a 100644 --- a/packages/tracker-core/src/torrent/manager.rs +++ b/packages/tracker-core/src/torrent/manager.rs @@ -7,7 +7,7 @@ use torrust_tracker_configuration::Core; use torrust_tracker_primitives::DurationSinceUnixEpoch; use super::repository::in_memory::InMemoryTorrentRepository; -use super::repository::persisted::DatabasePersistentTorrentRepository; +use crate::statistics::persisted::downloads::DatabaseDownloadsMetricRepository; use crate::{databases, CurrentClock}; /// The `TorrentsManager` is responsible for managing torrent entries by @@ -29,9 +29,8 @@ pub struct TorrentsManager { /// The in-memory torrents repository. in_memory_torrent_repository: Arc, - /// The persistent torrents repository. - #[allow(dead_code)] - db_torrent_repository: Arc, + /// The download metrics repository. + db_downloads_metric_repository: Arc, } impl TorrentsManager { @@ -42,7 +41,7 @@ impl TorrentsManager { /// * `config` - A reference to the tracker configuration. /// * `in_memory_torrent_repository` - A shared reference to the in-memory /// repository of torrents. - /// * `db_torrent_repository` - A shared reference to the persistent + /// * `db_downloads_metric_repository` - A shared reference to the persistent /// repository for torrent metrics. /// /// # Returns @@ -52,12 +51,12 @@ impl TorrentsManager { pub fn new( config: &Core, in_memory_torrent_repository: &Arc, - db_torrent_repository: &Arc, + db_downloads_metric_repository: &Arc, ) -> Self { Self { config: config.clone(), in_memory_torrent_repository: in_memory_torrent_repository.clone(), - db_torrent_repository: db_torrent_repository.clone(), + db_downloads_metric_repository: db_downloads_metric_repository.clone(), } } @@ -72,9 +71,7 @@ impl TorrentsManager { /// Returns a `databases::error::Error` if unable to load the persistent /// torrent data. pub fn load_torrents_from_database(&self) -> Result<(), databases::error::Error> { - let persistent_torrents = self.db_torrent_repository.load_all()?; - - println!("Loaded {} persistent torrents from the database", persistent_torrents.len()); + let persistent_torrents = self.db_downloads_metric_repository.load_all_torrents_downloads()?; self.in_memory_torrent_repository.import_persistent(&persistent_torrents); @@ -153,7 +150,7 @@ mod tests { use torrust_tracker_configuration::Core; use torrust_tracker_torrent_repository::Swarms; - use super::{DatabasePersistentTorrentRepository, TorrentsManager}; + use super::{DatabaseDownloadsMetricRepository, TorrentsManager}; use crate::databases::setup::initialize_database; use crate::test_helpers::tests::{ephemeral_configuration, sample_info_hash}; use crate::torrent::repository::in_memory::InMemoryTorrentRepository; @@ -161,7 +158,7 @@ mod tests { struct TorrentsManagerDeps { config: Arc, in_memory_torrent_repository: Arc, - database_persistent_torrent_repository: Arc, + database_persistent_torrent_repository: Arc, } fn initialize_torrents_manager() -> (Arc, Arc) { @@ -173,7 +170,7 @@ mod tests { let swarms = Arc::new(Swarms::default()); let in_memory_torrent_repository = Arc::new(InMemoryTorrentRepository::new(swarms)); let database = initialize_database(&config); - let database_persistent_torrent_repository = Arc::new(DatabasePersistentTorrentRepository::new(&database)); + let database_persistent_torrent_repository = Arc::new(DatabaseDownloadsMetricRepository::new(&database)); let torrents_manager = Arc::new(TorrentsManager::new( &config, @@ -197,7 +194,10 @@ mod tests { let infohash = sample_info_hash(); - services.database_persistent_torrent_repository.save(&infohash, 1).unwrap(); + services + .database_persistent_torrent_repository + .save_torrent_downloads(&infohash, 1) + .unwrap(); torrents_manager.load_torrents_from_database().unwrap(); diff --git a/packages/tracker-core/src/torrent/repository/in_memory.rs b/packages/tracker-core/src/torrent/repository/in_memory.rs index 5c8a335b6..164f46c69 100644 --- a/packages/tracker-core/src/torrent/repository/in_memory.rs +++ b/packages/tracker-core/src/torrent/repository/in_memory.rs @@ -6,7 +6,7 @@ use bittorrent_primitives::info_hash::InfoHash; use torrust_tracker_configuration::{TrackerPolicy, TORRENT_PEERS_LIMIT}; use torrust_tracker_primitives::pagination::Pagination; use torrust_tracker_primitives::swarm_metadata::{AggregateSwarmMetadata, SwarmMetadata}; -use torrust_tracker_primitives::{peer, DurationSinceUnixEpoch, PersistentTorrent, PersistentTorrents}; +use torrust_tracker_primitives::{peer, DurationSinceUnixEpoch, NumberOfDownloads, NumberOfDownloadsBTreeMap}; use torrust_tracker_torrent_repository::{SwarmHandle, Swarms}; /// In-memory repository for torrent entries. @@ -52,7 +52,7 @@ impl InMemoryTorrentRepository { &self, info_hash: &InfoHash, peer: &peer::Peer, - opt_persistent_torrent: Option, + opt_persistent_torrent: Option, ) { self.swarms .handle_announcement(info_hash, peer, opt_persistent_torrent) @@ -264,7 +264,7 @@ impl InMemoryTorrentRepository { /// # Arguments /// /// * `persistent_torrents` - A reference to the persisted torrent data. - pub fn import_persistent(&self, persistent_torrents: &PersistentTorrents) { + pub fn import_persistent(&self, persistent_torrents: &NumberOfDownloadsBTreeMap) { self.swarms.import_persistent(persistent_torrents); } } diff --git a/packages/tracker-core/src/torrent/repository/mod.rs b/packages/tracker-core/src/torrent/repository/mod.rs index ae789e5e9..d8325dec5 100644 --- a/packages/tracker-core/src/torrent/repository/mod.rs +++ b/packages/tracker-core/src/torrent/repository/mod.rs @@ -1,3 +1,2 @@ //! Torrent repository implementations. pub mod in_memory; -pub mod persisted; diff --git a/packages/tracker-core/tests/common/test_env.rs b/packages/tracker-core/tests/common/test_env.rs index 11a4d400a..2aafbbbad 100644 --- a/packages/tracker-core/tests/common/test_env.rs +++ b/packages/tracker-core/tests/common/test_env.rs @@ -5,7 +5,7 @@ use aquatic_udp_protocol::AnnounceEvent; use bittorrent_primitives::info_hash::InfoHash; use bittorrent_tracker_core::announce_handler::PeersWanted; use bittorrent_tracker_core::container::TrackerCoreContainer; -use bittorrent_tracker_core::statistics::persisted_metrics::load_persisted_metrics; +use bittorrent_tracker_core::statistics::persisted::load_persisted_metrics; use tokio::task::yield_now; use torrust_tracker_configuration::Core; use torrust_tracker_metrics::label::LabelSet; @@ -57,7 +57,7 @@ impl TestEnv { async fn load_persisted_metrics(&self, now: DurationSinceUnixEpoch) { load_persisted_metrics( &self.tracker_core_container.stats_repository, - &self.tracker_core_container.db_torrent_repository, + &self.tracker_core_container.db_downloads_metric_repository, now, ) .await @@ -77,7 +77,7 @@ impl TestEnv { let job = bittorrent_tracker_core::statistics::event::listener::run_event_listener( self.torrent_repository_container.event_bus.receiver(), &self.tracker_core_container.stats_repository, - &self.tracker_core_container.db_torrent_repository, + &self.tracker_core_container.db_downloads_metric_repository, self.tracker_core_container .core_config .tracker_policy diff --git a/packages/udp-tracker-server/src/handlers/announce.rs b/packages/udp-tracker-server/src/handlers/announce.rs index e2ca6821e..2fc3f6e63 100644 --- a/packages/udp-tracker-server/src/handlers/announce.rs +++ b/packages/udp-tracker-server/src/handlers/announce.rs @@ -835,8 +835,8 @@ mod tests { use aquatic_udp_protocol::{InfoHash as AquaticInfoHash, PeerId as AquaticPeerId}; use bittorrent_tracker_core::announce_handler::AnnounceHandler; use bittorrent_tracker_core::databases::setup::initialize_database; + use bittorrent_tracker_core::statistics::persisted::downloads::DatabaseDownloadsMetricRepository; use bittorrent_tracker_core::torrent::repository::in_memory::InMemoryTorrentRepository; - use bittorrent_tracker_core::torrent::repository::persisted::DatabasePersistentTorrentRepository; use bittorrent_tracker_core::whitelist::authorization::WhitelistAuthorization; use bittorrent_tracker_core::whitelist::repository::in_memory::InMemoryWhitelist; use bittorrent_udp_tracker_core::connection_cookie::{gen_remote_fingerprint, make}; @@ -885,7 +885,7 @@ mod tests { let whitelist_authorization = Arc::new(WhitelistAuthorization::new(&config.core, &in_memory_whitelist.clone())); let in_memory_torrent_repository = Arc::new(InMemoryTorrentRepository::default()); - let db_torrent_repository = Arc::new(DatabasePersistentTorrentRepository::new(&database)); + let db_downloads_metric_repository = Arc::new(DatabaseDownloadsMetricRepository::new(&database)); let mut udp_core_stats_event_sender_mock = MockUdpCoreStatsEventSender::new(); udp_core_stats_event_sender_mock @@ -923,7 +923,7 @@ mod tests { &config.core, &whitelist_authorization, &in_memory_torrent_repository, - &db_torrent_repository, + &db_downloads_metric_repository, )); let request = AnnounceRequestBuilder::default() diff --git a/packages/udp-tracker-server/src/handlers/mod.rs b/packages/udp-tracker-server/src/handlers/mod.rs index 831073333..df550ab72 100644 --- a/packages/udp-tracker-server/src/handlers/mod.rs +++ b/packages/udp-tracker-server/src/handlers/mod.rs @@ -211,8 +211,8 @@ pub(crate) mod tests { use bittorrent_tracker_core::announce_handler::AnnounceHandler; use bittorrent_tracker_core::databases::setup::initialize_database; use bittorrent_tracker_core::scrape_handler::ScrapeHandler; + use bittorrent_tracker_core::statistics::persisted::downloads::DatabaseDownloadsMetricRepository; use bittorrent_tracker_core::torrent::repository::in_memory::InMemoryTorrentRepository; - use bittorrent_tracker_core::torrent::repository::persisted::DatabasePersistentTorrentRepository; use bittorrent_tracker_core::whitelist; use bittorrent_tracker_core::whitelist::authorization::WhitelistAuthorization; use bittorrent_tracker_core::whitelist::repository::in_memory::InMemoryWhitelist; @@ -275,12 +275,12 @@ pub(crate) mod tests { let in_memory_whitelist = Arc::new(InMemoryWhitelist::default()); let whitelist_authorization = Arc::new(WhitelistAuthorization::new(&config.core, &in_memory_whitelist.clone())); let in_memory_torrent_repository = Arc::new(InMemoryTorrentRepository::default()); - let db_torrent_repository = Arc::new(DatabasePersistentTorrentRepository::new(&database)); + let db_downloads_metric_repository = Arc::new(DatabaseDownloadsMetricRepository::new(&database)); let announce_handler = Arc::new(AnnounceHandler::new( &config.core, &whitelist_authorization, &in_memory_torrent_repository, - &db_torrent_repository, + &db_downloads_metric_repository, )); let scrape_handler = Arc::new(ScrapeHandler::new(&whitelist_authorization, &in_memory_torrent_repository)); diff --git a/src/app.rs b/src/app.rs index 571e034f5..ccc2e8bcb 100644 --- a/src/app.rs +++ b/src/app.rs @@ -66,13 +66,6 @@ async fn load_data_from_database(config: &Configuration, app_container: &Arc) -> JobManager { @@ -127,23 +120,11 @@ async fn load_whitelisted_torrents(config: &Configuration, app_container: &Arc) { - if config.core.tracker_policy.persistent_torrent_completed_stat { - app_container - .tracker_core_container - .torrents_manager - .load_torrents_from_database() - .expect("Could not load torrents from database."); - } -} - -#[allow(dead_code)] async fn load_torrent_metrics(config: &Configuration, app_container: &Arc) { if config.core.tracker_policy.persistent_torrent_completed_stat { - bittorrent_tracker_core::statistics::persisted_metrics::load_persisted_metrics( + bittorrent_tracker_core::statistics::persisted::load_persisted_metrics( &app_container.tracker_core_container.stats_repository, - &app_container.tracker_core_container.db_torrent_repository, + &app_container.tracker_core_container.db_downloads_metric_repository, CurrentClock::now(), ) .await diff --git a/src/bootstrap/jobs/tracker_core.rs b/src/bootstrap/jobs/tracker_core.rs index 161e69aad..fd5cacbda 100644 --- a/src/bootstrap/jobs/tracker_core.rs +++ b/src/bootstrap/jobs/tracker_core.rs @@ -10,7 +10,7 @@ pub fn start_event_listener(config: &Configuration, app_container: &Arc