Skip to content

Commit

Permalink
refactor: separate torrent repository trait from implementations
Browse files Browse the repository at this point in the history
There are now more implementations.
  • Loading branch information
josecelano committed Apr 5, 2024
1 parent eec2024 commit 0989285
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 43 deletions.
14 changes: 8 additions & 6 deletions packages/torrent-repository/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
use std::sync::Arc;

use repository::rw_lock_std::RwLockStd;
use repository::rw_lock_tokio::RwLockTokio;
use repository::skip_map_mutex_std::CrossbeamSkipList;
use torrust_tracker_clock::clock;

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

pub type TorrentsRwLockStd = repository::RwLockStd<EntrySingle>;
pub type TorrentsRwLockStdMutexStd = repository::RwLockStd<EntryMutexStd>;
pub type TorrentsRwLockStdMutexTokio = repository::RwLockStd<EntryMutexTokio>;
pub type TorrentsRwLockTokio = repository::RwLockTokio<EntrySingle>;
pub type TorrentsRwLockTokioMutexStd = repository::RwLockTokio<EntryMutexStd>;
pub type TorrentsRwLockTokioMutexTokio = repository::RwLockTokio<EntryMutexTokio>;
pub type TorrentsRwLockStd = RwLockStd<EntrySingle>;
pub type TorrentsRwLockStdMutexStd = RwLockStd<EntryMutexStd>;
pub type TorrentsRwLockStdMutexTokio = RwLockStd<EntryMutexTokio>;
pub type TorrentsRwLockTokio = RwLockTokio<EntrySingle>;
pub type TorrentsRwLockTokioMutexStd = RwLockTokio<EntryMutexStd>;
pub type TorrentsRwLockTokioMutexTokio = RwLockTokio<EntryMutexTokio>;

pub type TorrentsSkipMapMutexStd = CrossbeamSkipList<EntryMutexStd>;

Expand Down
34 changes: 0 additions & 34 deletions packages/torrent-repository/src/repository/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,37 +41,3 @@ pub trait RepositoryAsync<T>: Debug + Default + Sized + 'static {
peer: &peer::Peer,
) -> impl std::future::Future<Output = (bool, SwarmMetadata)> + Send;
}

#[derive(Default, Debug)]
pub struct RwLockStd<T> {
torrents: std::sync::RwLock<std::collections::BTreeMap<InfoHash, T>>,
}

#[derive(Default, Debug)]
pub struct RwLockTokio<T> {
torrents: tokio::sync::RwLock<std::collections::BTreeMap<InfoHash, T>>,
}

impl<T> RwLockStd<T> {
/// # Panics
///
/// Panics if unable to get a lock.
pub fn write(
&self,
) -> std::sync::RwLockWriteGuard<'_, std::collections::BTreeMap<torrust_tracker_primitives::info_hash::InfoHash, T>> {
self.torrents.write().expect("it should get lock")
}
}

impl<T> RwLockTokio<T> {
pub fn write(
&self,
) -> impl std::future::Future<
Output = tokio::sync::RwLockWriteGuard<
'_,
std::collections::BTreeMap<torrust_tracker_primitives::info_hash::InfoHash, T>,
>,
> {
self.torrents.write()
}
}
16 changes: 16 additions & 0 deletions packages/torrent-repository/src/repository/rw_lock_std.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,22 @@ use super::Repository;
use crate::entry::Entry;
use crate::{EntrySingle, TorrentsRwLockStd};

#[derive(Default, Debug)]
pub struct RwLockStd<T> {
pub(crate) torrents: std::sync::RwLock<std::collections::BTreeMap<InfoHash, T>>,
}

impl<T> RwLockStd<T> {
/// # Panics
///
/// Panics if unable to get a lock.
pub fn write(
&self,
) -> std::sync::RwLockWriteGuard<'_, std::collections::BTreeMap<torrust_tracker_primitives::info_hash::InfoHash, T>> {
self.torrents.write().expect("it should get lock")
}
}

impl TorrentsRwLockStd {
fn get_torrents<'a>(&'a self) -> std::sync::RwLockReadGuard<'a, std::collections::BTreeMap<InfoHash, EntrySingle>>
where
Expand Down
18 changes: 18 additions & 0 deletions packages/torrent-repository/src/repository/rw_lock_tokio.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,24 @@ use super::RepositoryAsync;
use crate::entry::Entry;
use crate::{EntrySingle, TorrentsRwLockTokio};

#[derive(Default, Debug)]
pub struct RwLockTokio<T> {
pub(crate) torrents: tokio::sync::RwLock<std::collections::BTreeMap<InfoHash, T>>,
}

impl<T> RwLockTokio<T> {
pub fn write(
&self,
) -> impl std::future::Future<
Output = tokio::sync::RwLockWriteGuard<
'_,
std::collections::BTreeMap<torrust_tracker_primitives::info_hash::InfoHash, T>,
>,
> {
self.torrents.write()
}
}

impl TorrentsRwLockTokio {
async fn get_torrents<'a>(&'a self) -> tokio::sync::RwLockReadGuard<'a, std::collections::BTreeMap<InfoHash, EntrySingle>>
where
Expand Down
3 changes: 2 additions & 1 deletion packages/torrent-repository/tests/repository/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ use torrust_tracker_primitives::info_hash::InfoHash;
use torrust_tracker_primitives::pagination::Pagination;
use torrust_tracker_primitives::{NumberOfBytes, PersistentTorrents};
use torrust_tracker_torrent_repository::entry::Entry as _;
use torrust_tracker_torrent_repository::repository::{RwLockStd, RwLockTokio};
use torrust_tracker_torrent_repository::repository::rw_lock_std::RwLockStd;
use torrust_tracker_torrent_repository::repository::rw_lock_tokio::RwLockTokio;
use torrust_tracker_torrent_repository::EntrySingle;

use crate::common::repo::Repo;
Expand Down
2 changes: 0 additions & 2 deletions src/core/torrent/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,6 @@
//! - The number of peers that have NOT completed downloading the torrent and are still active, that means they are actively participating in the network.
//! Peer that don not have a full copy of the torrent data are called "leechers".
//!
use torrust_tracker_torrent_repository::TorrentsSkipMapMutexStd;

//pub type Torrents = TorrentsRwLockStdMutexStd; // Currently Used
pub type Torrents = TorrentsSkipMapMutexStd; // Currently Used

0 comments on commit 0989285

Please sign in to comment.