Skip to content

Commit 0989285

Browse files
committedApr 5, 2024··
refactor: separate torrent repository trait from implementations
There are now more implementations.
1 parent eec2024 commit 0989285

File tree

6 files changed

+44
-43
lines changed

6 files changed

+44
-43
lines changed
 

‎packages/torrent-repository/src/lib.rs

+8-6
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
use std::sync::Arc;
22

3+
use repository::rw_lock_std::RwLockStd;
4+
use repository::rw_lock_tokio::RwLockTokio;
35
use repository::skip_map_mutex_std::CrossbeamSkipList;
46
use torrust_tracker_clock::clock;
57

@@ -10,12 +12,12 @@ pub type EntrySingle = entry::Torrent;
1012
pub type EntryMutexStd = Arc<std::sync::Mutex<entry::Torrent>>;
1113
pub type EntryMutexTokio = Arc<tokio::sync::Mutex<entry::Torrent>>;
1214

13-
pub type TorrentsRwLockStd = repository::RwLockStd<EntrySingle>;
14-
pub type TorrentsRwLockStdMutexStd = repository::RwLockStd<EntryMutexStd>;
15-
pub type TorrentsRwLockStdMutexTokio = repository::RwLockStd<EntryMutexTokio>;
16-
pub type TorrentsRwLockTokio = repository::RwLockTokio<EntrySingle>;
17-
pub type TorrentsRwLockTokioMutexStd = repository::RwLockTokio<EntryMutexStd>;
18-
pub type TorrentsRwLockTokioMutexTokio = repository::RwLockTokio<EntryMutexTokio>;
15+
pub type TorrentsRwLockStd = RwLockStd<EntrySingle>;
16+
pub type TorrentsRwLockStdMutexStd = RwLockStd<EntryMutexStd>;
17+
pub type TorrentsRwLockStdMutexTokio = RwLockStd<EntryMutexTokio>;
18+
pub type TorrentsRwLockTokio = RwLockTokio<EntrySingle>;
19+
pub type TorrentsRwLockTokioMutexStd = RwLockTokio<EntryMutexStd>;
20+
pub type TorrentsRwLockTokioMutexTokio = RwLockTokio<EntryMutexTokio>;
1921

2022
pub type TorrentsSkipMapMutexStd = CrossbeamSkipList<EntryMutexStd>;
2123

‎packages/torrent-repository/src/repository/mod.rs

-34
Original file line numberDiff line numberDiff line change
@@ -41,37 +41,3 @@ pub trait RepositoryAsync<T>: Debug + Default + Sized + 'static {
4141
peer: &peer::Peer,
4242
) -> impl std::future::Future<Output = (bool, SwarmMetadata)> + Send;
4343
}
44-
45-
#[derive(Default, Debug)]
46-
pub struct RwLockStd<T> {
47-
torrents: std::sync::RwLock<std::collections::BTreeMap<InfoHash, T>>,
48-
}
49-
50-
#[derive(Default, Debug)]
51-
pub struct RwLockTokio<T> {
52-
torrents: tokio::sync::RwLock<std::collections::BTreeMap<InfoHash, T>>,
53-
}
54-
55-
impl<T> RwLockStd<T> {
56-
/// # Panics
57-
///
58-
/// Panics if unable to get a lock.
59-
pub fn write(
60-
&self,
61-
) -> std::sync::RwLockWriteGuard<'_, std::collections::BTreeMap<torrust_tracker_primitives::info_hash::InfoHash, T>> {
62-
self.torrents.write().expect("it should get lock")
63-
}
64-
}
65-
66-
impl<T> RwLockTokio<T> {
67-
pub fn write(
68-
&self,
69-
) -> impl std::future::Future<
70-
Output = tokio::sync::RwLockWriteGuard<
71-
'_,
72-
std::collections::BTreeMap<torrust_tracker_primitives::info_hash::InfoHash, T>,
73-
>,
74-
> {
75-
self.torrents.write()
76-
}
77-
}

‎packages/torrent-repository/src/repository/rw_lock_std.rs

+16
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,22 @@ use super::Repository;
1111
use crate::entry::Entry;
1212
use crate::{EntrySingle, TorrentsRwLockStd};
1313

14+
#[derive(Default, Debug)]
15+
pub struct RwLockStd<T> {
16+
pub(crate) torrents: std::sync::RwLock<std::collections::BTreeMap<InfoHash, T>>,
17+
}
18+
19+
impl<T> RwLockStd<T> {
20+
/// # Panics
21+
///
22+
/// Panics if unable to get a lock.
23+
pub fn write(
24+
&self,
25+
) -> std::sync::RwLockWriteGuard<'_, std::collections::BTreeMap<torrust_tracker_primitives::info_hash::InfoHash, T>> {
26+
self.torrents.write().expect("it should get lock")
27+
}
28+
}
29+
1430
impl TorrentsRwLockStd {
1531
fn get_torrents<'a>(&'a self) -> std::sync::RwLockReadGuard<'a, std::collections::BTreeMap<InfoHash, EntrySingle>>
1632
where

‎packages/torrent-repository/src/repository/rw_lock_tokio.rs

+18
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,24 @@ use super::RepositoryAsync;
1111
use crate::entry::Entry;
1212
use crate::{EntrySingle, TorrentsRwLockTokio};
1313

14+
#[derive(Default, Debug)]
15+
pub struct RwLockTokio<T> {
16+
pub(crate) torrents: tokio::sync::RwLock<std::collections::BTreeMap<InfoHash, T>>,
17+
}
18+
19+
impl<T> RwLockTokio<T> {
20+
pub fn write(
21+
&self,
22+
) -> impl std::future::Future<
23+
Output = tokio::sync::RwLockWriteGuard<
24+
'_,
25+
std::collections::BTreeMap<torrust_tracker_primitives::info_hash::InfoHash, T>,
26+
>,
27+
> {
28+
self.torrents.write()
29+
}
30+
}
31+
1432
impl TorrentsRwLockTokio {
1533
async fn get_torrents<'a>(&'a self) -> tokio::sync::RwLockReadGuard<'a, std::collections::BTreeMap<InfoHash, EntrySingle>>
1634
where

‎packages/torrent-repository/tests/repository/mod.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@ use torrust_tracker_primitives::info_hash::InfoHash;
88
use torrust_tracker_primitives::pagination::Pagination;
99
use torrust_tracker_primitives::{NumberOfBytes, PersistentTorrents};
1010
use torrust_tracker_torrent_repository::entry::Entry as _;
11-
use torrust_tracker_torrent_repository::repository::{RwLockStd, RwLockTokio};
11+
use torrust_tracker_torrent_repository::repository::rw_lock_std::RwLockStd;
12+
use torrust_tracker_torrent_repository::repository::rw_lock_tokio::RwLockTokio;
1213
use torrust_tracker_torrent_repository::EntrySingle;
1314

1415
use crate::common::repo::Repo;

‎src/core/torrent/mod.rs

-2
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,6 @@
2525
//! - The number of peers that have NOT completed downloading the torrent and are still active, that means they are actively participating in the network.
2626
//! Peer that don not have a full copy of the torrent data are called "leechers".
2727
//!
28-
2928
use torrust_tracker_torrent_repository::TorrentsSkipMapMutexStd;
3029

31-
//pub type Torrents = TorrentsRwLockStdMutexStd; // Currently Used
3230
pub type Torrents = TorrentsSkipMapMutexStd; // Currently Used

0 commit comments

Comments
 (0)
Please sign in to comment.