Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ r2d2_sqlite = { version = "0", features = ["bundled"] }
rand = "0"
reqwest = { version = "0", features = ["json"] }
ringbuf = "0"
rustc-hash = "1.1.0"
serde = { version = "1", features = ["derive"] }
serde_bencode = "0"
serde_bytes = "0"
Expand Down
2 changes: 2 additions & 0 deletions cSpell.json
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@
"Pando",
"peekable",
"peerlist",
"prefixfree",
"proot",
"proto",
"Quickstart",
Expand All @@ -125,6 +126,7 @@
"routable",
"rstest",
"rusqlite",
"rustc",
"RUSTDOCFLAGS",
"RUSTFLAGS",
"rustfmt",
Expand Down
1 change: 1 addition & 0 deletions packages/torrent-repository/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ version.workspace = true
crossbeam-skiplist = "0.1"
dashmap = "5.5.3"
futures = "0.3.29"
rustc-hash = "1.1.0"
tokio = { version = "1", features = ["macros", "net", "rt-multi-thread", "signal", "sync"] }
torrust-tracker-clock = { version = "3.0.0-alpha.12-develop", path = "../clock" }
torrust-tracker-configuration = { version = "3.0.0-alpha.12-develop", path = "../configuration" }
Expand Down
18 changes: 15 additions & 3 deletions packages/torrent-repository/src/entry/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use std::fmt::Debug;
use std::net::SocketAddr;
use std::sync::Arc;

//use serde::{Deserialize, Serialize};
use rustc_hash::FxHashMap;
use torrust_tracker_configuration::TrackerPolicy;
use torrust_tracker_primitives::swarm_metadata::SwarmMetadata;
use torrust_tracker_primitives::{peer, DurationSinceUnixEpoch};
Expand Down Expand Up @@ -79,11 +79,23 @@ pub trait EntryAsync {
/// This is the tracker entry for a given torrent and contains the swarm data,
/// that's the list of all the peers trying to download the same torrent.
/// The tracker keeps one entry like this for every torrent.
#[derive(Clone, Debug, Default, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[derive(Clone, Debug, Default, PartialEq, Eq)]
pub struct Torrent {
/// The swarm: a network of peers that are all trying to download the torrent associated to this entry
// #[serde(skip)]
pub(crate) peers: std::collections::BTreeMap<peer::Id, Arc<peer::Peer>>,
pub(crate) peers: FxHashMap<peer::Id, Arc<peer::Peer>>,
/// The number of peers that have ever completed downloading the torrent associated to this entry
pub(crate) downloaded: u32,
}

impl std::hash::Hash for Torrent {
fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
state.write_length_prefix(self.peers.len());

for peer in &self.peers {
peer.hash(state);
}

self.downloaded.hash(state);
}
}
2 changes: 2 additions & 0 deletions packages/torrent-repository/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#![feature(hasher_prefixfree_extras)]

use std::sync::Arc;

use repository::dash_map_mutex_std::XacrimonDashMap;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::collections::BTreeMap;
use std::collections::HashMap;
use std::sync::Arc;

use dashmap::DashMap;
Expand Down Expand Up @@ -82,7 +82,7 @@ where

let entry = EntryMutexStd::new(
EntrySingle {
peers: BTreeMap::default(),
peers: HashMap::default(),
downloaded: *completed,
}
.into(),
Expand Down
16 changes: 8 additions & 8 deletions packages/torrent-repository/src/repository/rw_lock_std.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::collections::BTreeMap;
use std::collections::HashMap;

use torrust_tracker_configuration::TrackerPolicy;
use torrust_tracker_primitives::info_hash::InfoHash;
Expand All @@ -13,7 +13,7 @@ use crate::{EntrySingle, TorrentsRwLockStd};

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

impl<T> RwLockStd<T> {
Expand All @@ -22,22 +22,22 @@ impl<T> RwLockStd<T> {
/// Panics if unable to get a lock.
pub fn write(
&self,
) -> std::sync::RwLockWriteGuard<'_, std::collections::BTreeMap<torrust_tracker_primitives::info_hash::InfoHash, T>> {
) -> std::sync::RwLockWriteGuard<'_, std::collections::HashMap<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>>
fn get_torrents<'a>(&'a self) -> std::sync::RwLockReadGuard<'a, std::collections::HashMap<InfoHash, EntrySingle>>
where
std::collections::BTreeMap<InfoHash, EntrySingle>: 'a,
std::collections::HashMap<InfoHash, EntrySingle>: 'a,
{
self.torrents.read().expect("it should get the read lock")
}

fn get_torrents_mut<'a>(&'a self) -> std::sync::RwLockWriteGuard<'a, std::collections::BTreeMap<InfoHash, EntrySingle>>
fn get_torrents_mut<'a>(&'a self) -> std::sync::RwLockWriteGuard<'a, std::collections::HashMap<InfoHash, EntrySingle>>
where
std::collections::BTreeMap<InfoHash, EntrySingle>: 'a,
std::collections::HashMap<InfoHash, EntrySingle>: 'a,
{
self.torrents.write().expect("it should get the write lock")
}
Expand Down Expand Up @@ -102,7 +102,7 @@ where
}

let entry = EntrySingle {
peers: BTreeMap::default(),
peers: HashMap::default(),
downloaded: *downloaded,
};

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::collections::BTreeMap;
use std::collections::HashMap;
use std::sync::Arc;

use torrust_tracker_configuration::TrackerPolicy;
Expand All @@ -13,16 +13,16 @@ use crate::entry::{Entry, EntrySync};
use crate::{EntryMutexStd, EntrySingle, TorrentsRwLockStdMutexStd};

impl TorrentsRwLockStdMutexStd {
fn get_torrents<'a>(&'a self) -> std::sync::RwLockReadGuard<'a, std::collections::BTreeMap<InfoHash, EntryMutexStd>>
fn get_torrents<'a>(&'a self) -> std::sync::RwLockReadGuard<'a, std::collections::HashMap<InfoHash, EntryMutexStd>>
where
std::collections::BTreeMap<InfoHash, crate::EntryMutexStd>: 'a,
std::collections::HashMap<InfoHash, crate::EntryMutexStd>: 'a,
{
self.torrents.read().expect("unable to get torrent list")
}

fn get_torrents_mut<'a>(&'a self) -> std::sync::RwLockWriteGuard<'a, std::collections::BTreeMap<InfoHash, EntryMutexStd>>
fn get_torrents_mut<'a>(&'a self) -> std::sync::RwLockWriteGuard<'a, std::collections::HashMap<InfoHash, EntryMutexStd>>
where
std::collections::BTreeMap<InfoHash, EntryMutexStd>: 'a,
std::collections::HashMap<InfoHash, EntryMutexStd>: 'a,
{
self.torrents.write().expect("unable to get writable torrent list")
}
Expand Down Expand Up @@ -97,7 +97,7 @@ where

let entry = EntryMutexStd::new(
EntrySingle {
peers: BTreeMap::default(),
peers: HashMap::default(),
downloaded: *completed,
}
.into(),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::collections::BTreeMap;
use std::collections::HashMap;
use std::iter::zip;
use std::pin::Pin;
use std::sync::Arc;
Expand All @@ -17,16 +17,16 @@ use crate::entry::{Entry, EntryAsync};
use crate::{EntryMutexTokio, EntrySingle, TorrentsRwLockStdMutexTokio};

impl TorrentsRwLockStdMutexTokio {
fn get_torrents<'a>(&'a self) -> std::sync::RwLockReadGuard<'a, std::collections::BTreeMap<InfoHash, EntryMutexTokio>>
fn get_torrents<'a>(&'a self) -> std::sync::RwLockReadGuard<'a, std::collections::HashMap<InfoHash, EntryMutexTokio>>
where
std::collections::BTreeMap<InfoHash, EntryMutexTokio>: 'a,
std::collections::HashMap<InfoHash, EntryMutexTokio>: 'a,
{
self.torrents.read().expect("unable to get torrent list")
}

fn get_torrents_mut<'a>(&'a self) -> std::sync::RwLockWriteGuard<'a, std::collections::BTreeMap<InfoHash, EntryMutexTokio>>
fn get_torrents_mut<'a>(&'a self) -> std::sync::RwLockWriteGuard<'a, std::collections::HashMap<InfoHash, EntryMutexTokio>>
where
std::collections::BTreeMap<InfoHash, EntryMutexTokio>: 'a,
std::collections::HashMap<InfoHash, EntryMutexTokio>: 'a,
{
self.torrents.write().expect("unable to get writable torrent list")
}
Expand Down Expand Up @@ -106,7 +106,7 @@ where

let entry = EntryMutexTokio::new(
EntrySingle {
peers: BTreeMap::default(),
peers: HashMap::default(),
downloaded: *completed,
}
.into(),
Expand Down
16 changes: 8 additions & 8 deletions packages/torrent-repository/src/repository/rw_lock_tokio.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::collections::BTreeMap;
use std::collections::HashMap;

use torrust_tracker_configuration::TrackerPolicy;
use torrust_tracker_primitives::info_hash::InfoHash;
Expand All @@ -13,7 +13,7 @@ use crate::{EntrySingle, TorrentsRwLockTokio};

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

impl<T> RwLockTokio<T> {
Expand All @@ -22,26 +22,26 @@ impl<T> RwLockTokio<T> {
) -> impl std::future::Future<
Output = tokio::sync::RwLockWriteGuard<
'_,
std::collections::BTreeMap<torrust_tracker_primitives::info_hash::InfoHash, T>,
std::collections::HashMap<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>>
async fn get_torrents<'a>(&'a self) -> tokio::sync::RwLockReadGuard<'a, std::collections::HashMap<InfoHash, EntrySingle>>
where
std::collections::BTreeMap<InfoHash, EntrySingle>: 'a,
std::collections::HashMap<InfoHash, EntrySingle>: 'a,
{
self.torrents.read().await
}

async fn get_torrents_mut<'a>(
&'a self,
) -> tokio::sync::RwLockWriteGuard<'a, std::collections::BTreeMap<InfoHash, EntrySingle>>
) -> tokio::sync::RwLockWriteGuard<'a, std::collections::HashMap<InfoHash, EntrySingle>>
where
std::collections::BTreeMap<InfoHash, EntrySingle>: 'a,
std::collections::HashMap<InfoHash, EntrySingle>: 'a,
{
self.torrents.write().await
}
Expand Down Expand Up @@ -106,7 +106,7 @@ where
}

let entry = EntrySingle {
peers: BTreeMap::default(),
peers: HashMap::default(),
downloaded: *completed,
};

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::collections::BTreeMap;
use std::collections::HashMap;
use std::sync::Arc;

use torrust_tracker_configuration::TrackerPolicy;
Expand All @@ -13,18 +13,18 @@ use crate::entry::{Entry, EntrySync};
use crate::{EntryMutexStd, EntrySingle, TorrentsRwLockTokioMutexStd};

impl TorrentsRwLockTokioMutexStd {
async fn get_torrents<'a>(&'a self) -> tokio::sync::RwLockReadGuard<'a, std::collections::BTreeMap<InfoHash, EntryMutexStd>>
async fn get_torrents<'a>(&'a self) -> tokio::sync::RwLockReadGuard<'a, std::collections::HashMap<InfoHash, EntryMutexStd>>
where
std::collections::BTreeMap<InfoHash, EntryMutexStd>: 'a,
std::collections::HashMap<InfoHash, EntryMutexStd>: 'a,
{
self.torrents.read().await
}

async fn get_torrents_mut<'a>(
&'a self,
) -> tokio::sync::RwLockWriteGuard<'a, std::collections::BTreeMap<InfoHash, EntryMutexStd>>
) -> tokio::sync::RwLockWriteGuard<'a, std::collections::HashMap<InfoHash, EntryMutexStd>>
where
std::collections::BTreeMap<InfoHash, EntryMutexStd>: 'a,
std::collections::HashMap<InfoHash, EntryMutexStd>: 'a,
{
self.torrents.write().await
}
Expand Down Expand Up @@ -97,7 +97,7 @@ where

let entry = EntryMutexStd::new(
EntrySingle {
peers: BTreeMap::default(),
peers: HashMap::default(),
downloaded: *completed,
}
.into(),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::collections::BTreeMap;
use std::collections::HashMap;
use std::sync::Arc;

use torrust_tracker_configuration::TrackerPolicy;
Expand All @@ -13,18 +13,18 @@ use crate::entry::{Entry, EntryAsync};
use crate::{EntryMutexTokio, EntrySingle, TorrentsRwLockTokioMutexTokio};

impl TorrentsRwLockTokioMutexTokio {
async fn get_torrents<'a>(&'a self) -> tokio::sync::RwLockReadGuard<'a, std::collections::BTreeMap<InfoHash, EntryMutexTokio>>
async fn get_torrents<'a>(&'a self) -> tokio::sync::RwLockReadGuard<'a, std::collections::HashMap<InfoHash, EntryMutexTokio>>
where
std::collections::BTreeMap<InfoHash, EntryMutexTokio>: 'a,
std::collections::HashMap<InfoHash, EntryMutexTokio>: 'a,
{
self.torrents.read().await
}

async fn get_torrents_mut<'a>(
&'a self,
) -> tokio::sync::RwLockWriteGuard<'a, std::collections::BTreeMap<InfoHash, EntryMutexTokio>>
) -> tokio::sync::RwLockWriteGuard<'a, std::collections::HashMap<InfoHash, EntryMutexTokio>>
where
std::collections::BTreeMap<InfoHash, EntryMutexTokio>: 'a,
std::collections::HashMap<InfoHash, EntryMutexTokio>: 'a,
{
self.torrents.write().await
}
Expand Down Expand Up @@ -100,7 +100,7 @@ where

let entry = EntryMutexTokio::new(
EntrySingle {
peers: BTreeMap::default(),
peers: HashMap::default(),
downloaded: *completed,
}
.into(),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::collections::BTreeMap;
use std::collections::HashMap;
use std::sync::Arc;

use crossbeam_skiplist::SkipMap;
Expand Down Expand Up @@ -76,7 +76,7 @@ where

let entry = EntryMutexStd::new(
EntrySingle {
peers: BTreeMap::default(),
peers: HashMap::default(),
downloaded: *completed,
}
.into(),
Expand Down
4 changes: 2 additions & 2 deletions packages/torrent-repository/tests/repository/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::collections::{BTreeMap, HashSet};
use std::collections::{HashMap, HashSet};
use std::hash::{DefaultHasher, Hash, Hasher};

use rstest::{fixture, rstest};
Expand Down Expand Up @@ -140,7 +140,7 @@ fn many_out_of_order() -> Entries {

#[fixture]
fn many_hashed_in_order() -> Entries {
let mut entries: BTreeMap<InfoHash, EntrySingle> = BTreeMap::default();
let mut entries: HashMap<InfoHash, EntrySingle> = HashMap::default();

for i in 0..408 {
let mut entry = EntrySingle::default();
Expand Down