Skip to content

Commit f4dcb51

Browse files
committed
refactor: [torrust#1201] rename tracker field
1 parent 9b5f776 commit f4dcb51

File tree

2 files changed

+25
-17
lines changed

2 files changed

+25
-17
lines changed

src/core/mod.rs

+18-14
Original file line numberDiff line numberDiff line change
@@ -487,7 +487,7 @@ pub struct Tracker {
487487
pub whitelist_authorization: Arc<whitelist::authorization::Authorization>,
488488

489489
/// The in-memory torrents repository.
490-
torrents: Arc<InMemoryTorrentRepository>,
490+
in_memory_torrent_repository: Arc<InMemoryTorrentRepository>,
491491

492492
/// The persistent torrents repository.
493493
db_torrent_repository: Arc<DatabasePersistentTorrentRepository>,
@@ -550,7 +550,7 @@ impl Tracker {
550550
config: config.clone(),
551551
database: database.clone(),
552552
whitelist_authorization: whitelist_authorization.clone(),
553-
torrents: Arc::new(InMemoryTorrentRepository::default()),
553+
in_memory_torrent_repository: Arc::new(InMemoryTorrentRepository::default()),
554554
db_torrent_repository: Arc::new(DatabasePersistentTorrentRepository::new(database)),
555555
})
556556
}
@@ -658,7 +658,7 @@ impl Tracker {
658658

659659
/// It returns the data for a `scrape` response.
660660
fn get_swarm_metadata(&self, info_hash: &InfoHash) -> SwarmMetadata {
661-
self.torrents.get_swarm_metadata(info_hash)
661+
self.in_memory_torrent_repository.get_swarm_metadata(info_hash)
662662
}
663663

664664
/// It loads the torrents from database into memory. It only loads the torrent entry list with the number of seeders for each torrent.
@@ -672,7 +672,7 @@ impl Tracker {
672672
pub fn load_torrents_from_database(&self) -> Result<(), databases::error::Error> {
673673
let persistent_torrents = self.db_torrent_repository.load_all()?;
674674

675-
self.torrents.import_persistent(&persistent_torrents);
675+
self.in_memory_torrent_repository.import_persistent(&persistent_torrents);
676676

677677
Ok(())
678678
}
@@ -683,15 +683,15 @@ impl Tracker {
683683
///
684684
/// It filters out the client making the request.
685685
fn get_peers_for(&self, info_hash: &InfoHash, peer: &peer::Peer, limit: usize) -> Vec<Arc<peer::Peer>> {
686-
self.torrents.get_peers_for(info_hash, peer, limit)
686+
self.in_memory_torrent_repository.get_peers_for(info_hash, peer, limit)
687687
}
688688

689689
/// # Context: Tracker
690690
///
691691
/// Get torrent peers for a given torrent.
692692
#[must_use]
693693
pub fn get_torrent_peers(&self, info_hash: &InfoHash) -> Vec<Arc<peer::Peer>> {
694-
self.torrents.get_torrent_peers(info_hash)
694+
self.in_memory_torrent_repository.get_torrent_peers(info_hash)
695695
}
696696

697697
/// It updates the torrent entry in memory, it also stores in the database
@@ -701,14 +701,14 @@ impl Tracker {
701701
/// # Context: Tracker
702702
#[must_use]
703703
pub fn upsert_peer_and_get_stats(&self, info_hash: &InfoHash, peer: &peer::Peer) -> SwarmMetadata {
704-
let swarm_metadata_before = match self.torrents.get_opt_swarm_metadata(info_hash) {
704+
let swarm_metadata_before = match self.in_memory_torrent_repository.get_opt_swarm_metadata(info_hash) {
705705
Some(swarm_metadata) => swarm_metadata,
706706
None => SwarmMetadata::zeroed(),
707707
};
708708

709-
self.torrents.upsert_peer(info_hash, peer);
709+
self.in_memory_torrent_repository.upsert_peer(info_hash, peer);
710710

711-
let swarm_metadata_after = match self.torrents.get_opt_swarm_metadata(info_hash) {
711+
let swarm_metadata_after = match self.in_memory_torrent_repository.get_opt_swarm_metadata(info_hash) {
712712
Some(swarm_metadata) => swarm_metadata,
713713
None => SwarmMetadata::zeroed(),
714714
};
@@ -741,7 +741,7 @@ impl Tracker {
741741
/// Panics if unable to get the torrent metrics.
742742
#[must_use]
743743
pub fn get_torrents_metrics(&self) -> TorrentsMetrics {
744-
self.torrents.get_torrents_metrics()
744+
self.in_memory_torrent_repository.get_torrents_metrics()
745745
}
746746

747747
/// Remove inactive peers and (optionally) peerless torrents.
@@ -751,10 +751,11 @@ impl Tracker {
751751
let current_cutoff = CurrentClock::now_sub(&Duration::from_secs(u64::from(self.config.tracker_policy.max_peer_timeout)))
752752
.unwrap_or_default();
753753

754-
self.torrents.remove_inactive_peers(current_cutoff);
754+
self.in_memory_torrent_repository.remove_inactive_peers(current_cutoff);
755755

756756
if self.config.tracker_policy.remove_peerless_torrents {
757-
self.torrents.remove_peerless_torrents(&self.config.tracker_policy);
757+
self.in_memory_torrent_repository
758+
.remove_peerless_torrents(&self.config.tracker_policy);
758759
}
759760
}
760761

@@ -1505,11 +1506,14 @@ mod tests {
15051506
assert_eq!(swarm_stats.downloaded, 1);
15061507

15071508
// Remove the newly updated torrent from memory
1508-
let _unused = tracker.torrents.remove(&info_hash);
1509+
let _unused = tracker.in_memory_torrent_repository.remove(&info_hash);
15091510

15101511
tracker.load_torrents_from_database().unwrap();
15111512

1512-
let torrent_entry = tracker.torrents.get(&info_hash).expect("it should be able to get entry");
1513+
let torrent_entry = tracker
1514+
.in_memory_torrent_repository
1515+
.get(&info_hash)
1516+
.expect("it should be able to get entry");
15131517

15141518
// It persists the number of completed peers.
15151519
assert_eq!(torrent_entry.get_swarm_metadata().downloaded, 1);

src/core/services/torrent.rs

+7-3
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ pub struct BasicInfo {
4545

4646
/// It returns all the information the tracker has about one torrent in a [Info] struct.
4747
pub async fn get_torrent_info(tracker: Arc<Tracker>, info_hash: &InfoHash) -> Option<Info> {
48-
let torrent_entry_option = tracker.torrents.get(info_hash);
48+
let torrent_entry_option = tracker.in_memory_torrent_repository.get(info_hash);
4949

5050
let torrent_entry = torrent_entry_option?;
5151

@@ -68,7 +68,7 @@ pub async fn get_torrent_info(tracker: Arc<Tracker>, info_hash: &InfoHash) -> Op
6868
pub async fn get_torrents_page(tracker: Arc<Tracker>, pagination: Option<&Pagination>) -> Vec<BasicInfo> {
6969
let mut basic_infos: Vec<BasicInfo> = vec![];
7070

71-
for (info_hash, torrent_entry) in tracker.torrents.get_paginated(pagination) {
71+
for (info_hash, torrent_entry) in tracker.in_memory_torrent_repository.get_paginated(pagination) {
7272
let stats = torrent_entry.get_swarm_metadata();
7373

7474
basic_infos.push(BasicInfo {
@@ -87,7 +87,11 @@ pub async fn get_torrents(tracker: Arc<Tracker>, info_hashes: &[InfoHash]) -> Ve
8787
let mut basic_infos: Vec<BasicInfo> = vec![];
8888

8989
for info_hash in info_hashes {
90-
if let Some(stats) = tracker.torrents.get(info_hash).map(|t| t.get_swarm_metadata()) {
90+
if let Some(stats) = tracker
91+
.in_memory_torrent_repository
92+
.get(info_hash)
93+
.map(|t| t.get_swarm_metadata())
94+
{
9195
basic_infos.push(BasicInfo {
9296
info_hash: *info_hash,
9397
seeders: u64::from(stats.complete),

0 commit comments

Comments
 (0)