Skip to content

Commit 9b5f776

Browse files
committed
refactor: [#1201] exatrct DatabasePersistentTorrentRepository
1 parent 03ef7f6 commit 9b5f776

File tree

3 files changed

+53
-3
lines changed

3 files changed

+53
-3
lines changed

src/core/mod.rs

+8-3
Original file line numberDiff line numberDiff line change
@@ -455,6 +455,7 @@ use std::time::Duration;
455455

456456
use bittorrent_primitives::info_hash::InfoHash;
457457
use torrent::repository::in_memory::InMemoryTorrentRepository;
458+
use torrent::repository::persisted::DatabasePersistentTorrentRepository;
458459
use torrust_tracker_clock::clock::Time;
459460
use torrust_tracker_configuration::{AnnouncePolicy, Core, TORRENT_PEERS_LIMIT};
460461
use torrust_tracker_primitives::core::{AnnounceData, ScrapeData};
@@ -487,6 +488,9 @@ pub struct Tracker {
487488

488489
/// The in-memory torrents repository.
489490
torrents: Arc<InMemoryTorrentRepository>,
491+
492+
/// The persistent torrents repository.
493+
db_torrent_repository: Arc<DatabasePersistentTorrentRepository>,
490494
}
491495

492496
/// How many peers the peer announcing wants in the announce response.
@@ -547,6 +551,7 @@ impl Tracker {
547551
database: database.clone(),
548552
whitelist_authorization: whitelist_authorization.clone(),
549553
torrents: Arc::new(InMemoryTorrentRepository::default()),
554+
db_torrent_repository: Arc::new(DatabasePersistentTorrentRepository::new(database)),
550555
})
551556
}
552557

@@ -665,7 +670,7 @@ impl Tracker {
665670
///
666671
/// Will return a `database::Error` if unable to load the list of `persistent_torrents` from the database.
667672
pub fn load_torrents_from_database(&self) -> Result<(), databases::error::Error> {
668-
let persistent_torrents = self.database.load_persistent_torrents()?;
673+
let persistent_torrents = self.db_torrent_repository.load_all()?;
669674

670675
self.torrents.import_persistent(&persistent_torrents);
671676

@@ -723,7 +728,7 @@ impl Tracker {
723728
let completed = swarm_metadata.downloaded;
724729
let info_hash = *info_hash;
725730

726-
drop(self.database.save_persistent_torrent(&info_hash, completed));
731+
drop(self.db_torrent_repository.save(&info_hash, completed));
727732
}
728733
}
729734

@@ -759,7 +764,7 @@ impl Tracker {
759764
///
760765
/// Will return `Err` if unable to drop tables.
761766
pub fn drop_database_tables(&self) -> Result<(), databases::error::Error> {
762-
// todo: this is only used for testing. WE have to pass the database
767+
// todo: this is only used for testing. We have to pass the database
763768
// reference directly to the tests instead of via the tracker.
764769
self.database.drop_database_tables()
765770
}

src/core/torrent/repository/mod.rs

+1
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
pub mod in_memory;
2+
pub mod persisted;
+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
use std::sync::Arc;
2+
3+
use bittorrent_primitives::info_hash::InfoHash;
4+
use torrust_tracker_primitives::PersistentTorrents;
5+
6+
use crate::core::databases::error::Error;
7+
use crate::core::databases::Database;
8+
9+
/// Torrent repository implementation that persists the torrents in a database.
10+
///
11+
/// Not all the torrent in-memory data is persisted. For now only some of the
12+
/// torrent metrics are persisted.
13+
pub struct DatabasePersistentTorrentRepository {
14+
/// A database driver implementation: [`Sqlite3`](crate::core::databases::sqlite)
15+
/// or [`MySQL`](crate::core::databases::mysql)
16+
database: Arc<Box<dyn Database>>,
17+
}
18+
19+
impl DatabasePersistentTorrentRepository {
20+
#[must_use]
21+
pub fn new(database: &Arc<Box<dyn Database>>) -> DatabasePersistentTorrentRepository {
22+
Self {
23+
database: database.clone(),
24+
}
25+
}
26+
27+
/// It loads the persistent torrents from the database.
28+
///
29+
/// # Errors
30+
///
31+
/// Will return a database `Err` if unable to load.
32+
pub fn load_all(&self) -> Result<PersistentTorrents, Error> {
33+
self.database.load_persistent_torrents()
34+
}
35+
36+
/// It saves the persistent torrent into the database.
37+
///
38+
/// # Errors
39+
///
40+
/// Will return a database `Err` if unable to save.
41+
pub fn save(&self, info_hash: &InfoHash, downloaded: u32) -> Result<(), Error> {
42+
self.database.save_persistent_torrent(info_hash, downloaded)
43+
}
44+
}

0 commit comments

Comments
 (0)