Skip to content

Commit 1025125

Browse files
committed
dev: create torrent repo trait and extract entry
1 parent 48ce426 commit 1025125

File tree

21 files changed

+1222
-747
lines changed

21 files changed

+1222
-747
lines changed

Cargo.lock

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

cSpell.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
"Containerfile",
3737
"curr",
3838
"Cyberneering",
39+
"dashmap",
3940
"datagram",
4041
"datetime",
4142
"debuginfo",

packages/torrent-repository-benchmarks/src/benches/asyn.rs

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,17 @@ use std::time::Duration;
33

44
use clap::Parser;
55
use futures::stream::FuturesUnordered;
6-
use torrust_tracker::core::torrent::repository_asyn::{RepositoryAsync, RepositoryTokioRwLock};
7-
use torrust_tracker::core::torrent::UpdateTorrentAsync;
6+
use torrust_tracker::core::torrent::repository::tokio_sync::RepositoryTokioRwLock;
7+
use torrust_tracker::core::torrent::repository::UpdateTorrentAsync;
88
use torrust_tracker::shared::bit_torrent::info_hash::InfoHash;
99

1010
use crate::args::Args;
1111
use crate::benches::utils::{generate_unique_info_hashes, get_average_and_adjusted_average_from_results, DEFAULT_PEER};
1212

1313
pub async fn add_one_torrent<T>(samples: usize) -> (Duration, Duration)
1414
where
15-
RepositoryTokioRwLock<T>: RepositoryAsync<T> + UpdateTorrentAsync,
15+
T: Default,
16+
RepositoryTokioRwLock<T>: UpdateTorrentAsync + Default,
1617
{
1718
let mut results: Vec<Duration> = Vec::with_capacity(samples);
1819

@@ -38,8 +39,8 @@ where
3839
// Add one torrent ten thousand times in parallel (depending on the set worker threads)
3940
pub async fn update_one_torrent_in_parallel<T>(runtime: &tokio::runtime::Runtime, samples: usize) -> (Duration, Duration)
4041
where
41-
T: Send + Sync + 'static,
42-
RepositoryTokioRwLock<T>: RepositoryAsync<T> + UpdateTorrentAsync,
42+
T: Default + Send + Sync + 'static,
43+
RepositoryTokioRwLock<T>: UpdateTorrentAsync + Default,
4344
{
4445
let args = Args::parse();
4546
let mut results: Vec<Duration> = Vec::with_capacity(samples);
@@ -88,8 +89,8 @@ where
8889
// Add ten thousand torrents in parallel (depending on the set worker threads)
8990
pub async fn add_multiple_torrents_in_parallel<T>(runtime: &tokio::runtime::Runtime, samples: usize) -> (Duration, Duration)
9091
where
91-
T: Send + Sync + 'static,
92-
RepositoryTokioRwLock<T>: RepositoryAsync<T> + UpdateTorrentAsync,
92+
T: Default + Send + Sync + 'static,
93+
RepositoryTokioRwLock<T>: UpdateTorrentAsync + Default,
9394
{
9495
let args = Args::parse();
9596
let mut results: Vec<Duration> = Vec::with_capacity(samples);
@@ -133,8 +134,8 @@ where
133134
// Async update ten thousand torrents in parallel (depending on the set worker threads)
134135
pub async fn update_multiple_torrents_in_parallel<T>(runtime: &tokio::runtime::Runtime, samples: usize) -> (Duration, Duration)
135136
where
136-
T: Send + Sync + 'static,
137-
RepositoryTokioRwLock<T>: RepositoryAsync<T> + UpdateTorrentAsync,
137+
T: Default + Send + Sync + 'static,
138+
RepositoryTokioRwLock<T>: UpdateTorrentAsync + Default,
138139
{
139140
let args = Args::parse();
140141
let mut results: Vec<Duration> = Vec::with_capacity(samples);

packages/torrent-repository-benchmarks/src/benches/sync.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ use std::time::Duration;
33

44
use clap::Parser;
55
use futures::stream::FuturesUnordered;
6-
use torrust_tracker::core::torrent::repository_sync::{RepositoryStdRwLock, RepositorySync};
7-
use torrust_tracker::core::torrent::UpdateTorrentSync;
6+
use torrust_tracker::core::torrent::repository::std_sync::RepositoryStdRwLock;
7+
use torrust_tracker::core::torrent::repository::UpdateTorrentSync;
88
use torrust_tracker::shared::bit_torrent::info_hash::InfoHash;
99

1010
use crate::args::Args;
@@ -14,7 +14,7 @@ use crate::benches::utils::{generate_unique_info_hashes, get_average_and_adjuste
1414
#[must_use]
1515
pub fn add_one_torrent<T>(samples: usize) -> (Duration, Duration)
1616
where
17-
RepositoryStdRwLock<T>: RepositorySync<T> + UpdateTorrentSync,
17+
RepositoryStdRwLock<T>: UpdateTorrentSync + Default,
1818
{
1919
let mut results: Vec<Duration> = Vec::with_capacity(samples);
2020

@@ -39,7 +39,7 @@ where
3939
pub async fn update_one_torrent_in_parallel<T>(runtime: &tokio::runtime::Runtime, samples: usize) -> (Duration, Duration)
4040
where
4141
T: Send + Sync + 'static,
42-
RepositoryStdRwLock<T>: RepositorySync<T> + UpdateTorrentSync,
42+
RepositoryStdRwLock<T>: UpdateTorrentSync + Default,
4343
{
4444
let args = Args::parse();
4545
let mut results: Vec<Duration> = Vec::with_capacity(samples);
@@ -85,7 +85,7 @@ where
8585
pub async fn add_multiple_torrents_in_parallel<T>(runtime: &tokio::runtime::Runtime, samples: usize) -> (Duration, Duration)
8686
where
8787
T: Send + Sync + 'static,
88-
RepositoryStdRwLock<T>: RepositorySync<T> + UpdateTorrentSync,
88+
RepositoryStdRwLock<T>: UpdateTorrentSync + Default,
8989
{
9090
let args = Args::parse();
9191
let mut results: Vec<Duration> = Vec::with_capacity(samples);
@@ -128,7 +128,7 @@ where
128128
pub async fn update_multiple_torrents_in_parallel<T>(runtime: &tokio::runtime::Runtime, samples: usize) -> (Duration, Duration)
129129
where
130130
T: Send + Sync + 'static,
131-
RepositoryStdRwLock<T>: RepositorySync<T> + UpdateTorrentSync,
131+
RepositoryStdRwLock<T>: UpdateTorrentSync + Default,
132132
{
133133
let args = Args::parse();
134134
let mut results: Vec<Duration> = Vec::with_capacity(samples);

packages/torrent-repository-benchmarks/src/benches/sync_asyn.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ use std::time::Duration;
33

44
use clap::Parser;
55
use futures::stream::FuturesUnordered;
6-
use torrust_tracker::core::torrent::repository_sync::{RepositoryStdRwLock, RepositorySync};
7-
use torrust_tracker::core::torrent::UpdateTorrentAsync;
6+
use torrust_tracker::core::torrent::repository::std_sync::RepositoryStdRwLock;
7+
use torrust_tracker::core::torrent::repository::UpdateTorrentAsync;
88
use torrust_tracker::shared::bit_torrent::info_hash::InfoHash;
99

1010
use crate::args::Args;
@@ -14,7 +14,7 @@ use crate::benches::utils::{generate_unique_info_hashes, get_average_and_adjuste
1414
#[must_use]
1515
pub async fn add_one_torrent<T>(samples: usize) -> (Duration, Duration)
1616
where
17-
RepositoryStdRwLock<T>: RepositorySync<T> + UpdateTorrentAsync,
17+
RepositoryStdRwLock<T>: UpdateTorrentAsync + Default,
1818
{
1919
let mut results: Vec<Duration> = Vec::with_capacity(samples);
2020

@@ -41,7 +41,7 @@ where
4141
pub async fn update_one_torrent_in_parallel<T>(runtime: &tokio::runtime::Runtime, samples: usize) -> (Duration, Duration)
4242
where
4343
T: Send + Sync + 'static,
44-
RepositoryStdRwLock<T>: RepositorySync<T> + UpdateTorrentAsync,
44+
RepositoryStdRwLock<T>: UpdateTorrentAsync + Default,
4545
{
4646
let args = Args::parse();
4747
let mut results: Vec<Duration> = Vec::with_capacity(samples);
@@ -91,7 +91,7 @@ where
9191
pub async fn add_multiple_torrents_in_parallel<T>(runtime: &tokio::runtime::Runtime, samples: usize) -> (Duration, Duration)
9292
where
9393
T: Send + Sync + 'static,
94-
RepositoryStdRwLock<T>: RepositorySync<T> + UpdateTorrentAsync,
94+
RepositoryStdRwLock<T>: UpdateTorrentAsync + Default,
9595
{
9696
let args = Args::parse();
9797
let mut results: Vec<Duration> = Vec::with_capacity(samples);
@@ -136,7 +136,7 @@ where
136136
pub async fn update_multiple_torrents_in_parallel<T>(runtime: &tokio::runtime::Runtime, samples: usize) -> (Duration, Duration)
137137
where
138138
T: Send + Sync + 'static,
139-
RepositoryStdRwLock<T>: RepositorySync<T> + UpdateTorrentAsync,
139+
RepositoryStdRwLock<T>: UpdateTorrentAsync + Default,
140140
{
141141
let args = Args::parse();
142142
let mut results: Vec<Duration> = Vec::with_capacity(samples);

packages/torrent-repository-benchmarks/src/main.rs

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use clap::Parser;
22
use torrust_torrent_repository_benchmarks::args::Args;
33
use torrust_torrent_repository_benchmarks::benches::{asyn, sync, sync_asyn};
4-
use torrust_tracker::core::torrent::{Entry, EntryMutexStd, EntryMutexTokio};
4+
use torrust_tracker::core::torrent::entry::{Entry, MutexStd, MutexTokio};
55

66
#[allow(clippy::too_many_lines)]
77
#[allow(clippy::print_literal)]
@@ -68,22 +68,22 @@ fn main() {
6868
println!(
6969
"{}: Avg/AdjAvg: {:?}",
7070
"add_one_torrent",
71-
sync::add_one_torrent::<EntryMutexStd>(1_000_000)
71+
sync::add_one_torrent::<MutexStd>(1_000_000)
7272
);
7373
println!(
7474
"{}: Avg/AdjAvg: {:?}",
7575
"update_one_torrent_in_parallel",
76-
rt.block_on(sync::update_one_torrent_in_parallel::<EntryMutexStd>(&rt, 10))
76+
rt.block_on(sync::update_one_torrent_in_parallel::<MutexStd>(&rt, 10))
7777
);
7878
println!(
7979
"{}: Avg/AdjAvg: {:?}",
8080
"add_multiple_torrents_in_parallel",
81-
rt.block_on(sync::add_multiple_torrents_in_parallel::<EntryMutexStd>(&rt, 10))
81+
rt.block_on(sync::add_multiple_torrents_in_parallel::<MutexStd>(&rt, 10))
8282
);
8383
println!(
8484
"{}: Avg/AdjAvg: {:?}",
8585
"update_multiple_torrents_in_parallel",
86-
rt.block_on(sync::update_multiple_torrents_in_parallel::<EntryMutexStd>(&rt, 10))
86+
rt.block_on(sync::update_multiple_torrents_in_parallel::<MutexStd>(&rt, 10))
8787
);
8888

8989
println!();
@@ -92,22 +92,22 @@ fn main() {
9292
println!(
9393
"{}: Avg/AdjAvg: {:?}",
9494
"add_one_torrent",
95-
rt.block_on(sync_asyn::add_one_torrent::<EntryMutexTokio>(1_000_000))
95+
rt.block_on(sync_asyn::add_one_torrent::<MutexTokio>(1_000_000))
9696
);
9797
println!(
9898
"{}: Avg/AdjAvg: {:?}",
9999
"update_one_torrent_in_parallel",
100-
rt.block_on(sync_asyn::update_one_torrent_in_parallel::<EntryMutexTokio>(&rt, 10))
100+
rt.block_on(sync_asyn::update_one_torrent_in_parallel::<MutexTokio>(&rt, 10))
101101
);
102102
println!(
103103
"{}: Avg/AdjAvg: {:?}",
104104
"add_multiple_torrents_in_parallel",
105-
rt.block_on(sync_asyn::add_multiple_torrents_in_parallel::<EntryMutexTokio>(&rt, 10))
105+
rt.block_on(sync_asyn::add_multiple_torrents_in_parallel::<MutexTokio>(&rt, 10))
106106
);
107107
println!(
108108
"{}: Avg/AdjAvg: {:?}",
109109
"update_multiple_torrents_in_parallel",
110-
rt.block_on(sync_asyn::update_multiple_torrents_in_parallel::<EntryMutexTokio>(&rt, 10))
110+
rt.block_on(sync_asyn::update_multiple_torrents_in_parallel::<MutexTokio>(&rt, 10))
111111
);
112112

113113
println!();
@@ -116,22 +116,22 @@ fn main() {
116116
println!(
117117
"{}: Avg/AdjAvg: {:?}",
118118
"add_one_torrent",
119-
rt.block_on(asyn::add_one_torrent::<EntryMutexStd>(1_000_000))
119+
rt.block_on(asyn::add_one_torrent::<MutexStd>(1_000_000))
120120
);
121121
println!(
122122
"{}: Avg/AdjAvg: {:?}",
123123
"update_one_torrent_in_parallel",
124-
rt.block_on(asyn::update_one_torrent_in_parallel::<EntryMutexStd>(&rt, 10))
124+
rt.block_on(asyn::update_one_torrent_in_parallel::<MutexStd>(&rt, 10))
125125
);
126126
println!(
127127
"{}: Avg/AdjAvg: {:?}",
128128
"add_multiple_torrents_in_parallel",
129-
rt.block_on(asyn::add_multiple_torrents_in_parallel::<EntryMutexStd>(&rt, 10))
129+
rt.block_on(asyn::add_multiple_torrents_in_parallel::<MutexStd>(&rt, 10))
130130
);
131131
println!(
132132
"{}: Avg/AdjAvg: {:?}",
133133
"update_multiple_torrents_in_parallel",
134-
rt.block_on(asyn::update_multiple_torrents_in_parallel::<EntryMutexStd>(&rt, 10))
134+
rt.block_on(asyn::update_multiple_torrents_in_parallel::<MutexStd>(&rt, 10))
135135
);
136136

137137
println!();
@@ -140,22 +140,22 @@ fn main() {
140140
println!(
141141
"{}: Avg/AdjAvg: {:?}",
142142
"add_one_torrent",
143-
rt.block_on(asyn::add_one_torrent::<EntryMutexTokio>(1_000_000))
143+
rt.block_on(asyn::add_one_torrent::<MutexTokio>(1_000_000))
144144
);
145145
println!(
146146
"{}: Avg/AdjAvg: {:?}",
147147
"update_one_torrent_in_parallel",
148-
rt.block_on(asyn::update_one_torrent_in_parallel::<EntryMutexTokio>(&rt, 10))
148+
rt.block_on(asyn::update_one_torrent_in_parallel::<MutexTokio>(&rt, 10))
149149
);
150150
println!(
151151
"{}: Avg/AdjAvg: {:?}",
152152
"add_multiple_torrents_in_parallel",
153-
rt.block_on(asyn::add_multiple_torrents_in_parallel::<EntryMutexTokio>(&rt, 10))
153+
rt.block_on(asyn::add_multiple_torrents_in_parallel::<MutexTokio>(&rt, 10))
154154
);
155155
println!(
156156
"{}: Avg/AdjAvg: {:?}",
157157
"update_multiple_torrents_in_parallel",
158-
rt.block_on(asyn::update_multiple_torrents_in_parallel::<EntryMutexTokio>(&rt, 10))
158+
rt.block_on(asyn::update_multiple_torrents_in_parallel::<MutexTokio>(&rt, 10))
159159
);
160160
}
161161
}

src/core/databases/mod.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,8 @@ use self::error::Error;
5656
use crate::core::auth::{self, Key};
5757
use crate::shared::bit_torrent::info_hash::InfoHash;
5858

59+
pub type PersistentTorrents = Vec<(InfoHash, u32)>;
60+
5961
struct Builder<T>
6062
where
6163
T: Database,
@@ -125,7 +127,7 @@ pub trait Database: Sync + Send {
125127
/// # Errors
126128
///
127129
/// Will return `Err` if unable to load.
128-
async fn load_persistent_torrents(&self) -> Result<Vec<(InfoHash, u32)>, Error>;
130+
async fn load_persistent_torrents(&self) -> Result<PersistentTorrents, Error>;
129131

130132
/// It saves the torrent metrics data into the database.
131133
///

0 commit comments

Comments
 (0)