Skip to content

Commit ac02657

Browse files
committed
dev: refactor torrent/repository
1 parent d0f3806 commit ac02657

File tree

6 files changed

+32
-47
lines changed

6 files changed

+32
-47
lines changed

src/core/torrent/repository/std_single.rs

+3-6
Original file line numberDiff line numberDiff line change
@@ -30,14 +30,11 @@ impl TorrentsStdRwLock<Entry> {
3030

3131
impl UpdateTorrentSync for TorrentsStdRwLock<Entry> {
3232
fn update_torrent_with_peer_and_get_stats(&self, info_hash: &InfoHash, peer: &peer::Peer) -> (bool, SwarmMetadata) {
33-
let mut torrents = self.torrents.write().unwrap();
33+
let mut db = self.get_torrents_mut();
3434

35-
let torrent_entry = match torrents.entry(*info_hash) {
36-
std::collections::btree_map::Entry::Vacant(vacant) => vacant.insert(Entry::default()),
37-
std::collections::btree_map::Entry::Occupied(entry) => entry.into_mut(),
38-
};
35+
let entry = db.entry(*info_hash).or_insert(Entry::default());
3936

40-
torrent_entry.insert_or_update_peer_and_get_stats(peer)
37+
entry.insert_or_update_peer_and_get_stats(peer)
4138
}
4239
}
4340
impl Repository<Entry> for TorrentsStdRwLock<Entry> {

src/core/torrent/repository/std_std.rs

+6-8
Original file line numberDiff line numberDiff line change
@@ -30,19 +30,17 @@ impl TorrentsStdRwLock<entry::MutexStd> {
3030

3131
impl UpdateTorrentSync for TorrentsStdRwLock<entry::MutexStd> {
3232
fn update_torrent_with_peer_and_get_stats(&self, info_hash: &InfoHash, peer: &peer::Peer) -> (bool, SwarmMetadata) {
33-
let maybe_existing_torrent_entry = self.get_torrents().get(info_hash).cloned();
33+
let maybe_entry = self.get_torrents().get(info_hash).cloned();
3434

35-
let torrent_entry: Arc<std::sync::Mutex<Entry>> = if let Some(existing_torrent_entry) = maybe_existing_torrent_entry {
36-
existing_torrent_entry
35+
let entry = if let Some(entry) = maybe_entry {
36+
entry
3737
} else {
38-
let mut torrents_lock = self.get_torrents_mut();
39-
let entry = torrents_lock
40-
.entry(*info_hash)
41-
.or_insert(Arc::new(std::sync::Mutex::new(Entry::default())));
38+
let mut db = self.get_torrents_mut();
39+
let entry = db.entry(*info_hash).or_insert(Arc::default());
4240
entry.clone()
4341
};
4442

45-
torrent_entry.insert_or_update_peer_and_get_stats(peer)
43+
entry.insert_or_update_peer_and_get_stats(peer)
4644
}
4745
}
4846
impl Repository<entry::MutexStd> for TorrentsStdRwLock<entry::MutexStd> {

src/core/torrent/repository/std_tokio.rs

+6-6
Original file line numberDiff line numberDiff line change
@@ -30,17 +30,17 @@ impl TorrentsStdRwLock<entry::MutexTokio> {
3030

3131
impl UpdateTorrentAsync for TorrentsStdRwLock<entry::MutexTokio> {
3232
async fn update_torrent_with_peer_and_get_stats(&self, info_hash: &InfoHash, peer: &peer::Peer) -> (bool, SwarmMetadata) {
33-
let maybe_existing_torrent_entry = self.get_torrents().get(info_hash).cloned();
33+
let maybe_entry = self.get_torrents().get(info_hash).cloned();
3434

35-
let torrent_entry = if let Some(existing_torrent_entry) = maybe_existing_torrent_entry {
36-
existing_torrent_entry
35+
let entry = if let Some(entry) = maybe_entry {
36+
entry
3737
} else {
38-
let mut torrents_lock = self.get_torrents_mut();
39-
let entry = torrents_lock.entry(*info_hash).or_insert(Arc::default());
38+
let mut db = self.get_torrents_mut();
39+
let entry = db.entry(*info_hash).or_insert(Arc::default());
4040
entry.clone()
4141
};
4242

43-
torrent_entry.insert_or_update_peer_and_get_stats(peer).await
43+
entry.insert_or_update_peer_and_get_stats(peer).await
4444
}
4545
}
4646
impl Repository<entry::MutexTokio> for TorrentsStdRwLock<entry::MutexTokio> {

src/core/torrent/repository/tokio_single.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,11 @@ impl TorrentsTokioRwLock<Entry> {
2929

3030
impl UpdateTorrentAsync for TorrentsTokioRwLock<Entry> {
3131
async fn update_torrent_with_peer_and_get_stats(&self, info_hash: &InfoHash, peer: &peer::Peer) -> (bool, SwarmMetadata) {
32-
let mut db = self.torrents.write().await;
32+
let mut db = self.get_torrents_mut().await;
3333

34-
let torrent = db.entry(*info_hash).or_insert(Entry::default());
34+
let entry = db.entry(*info_hash).or_insert(Entry::default());
3535

36-
torrent.insert_or_update_peer_and_get_stats(peer)
36+
entry.insert_or_update_peer_and_get_stats(peer)
3737
}
3838
}
3939

src/core/torrent/repository/tokio_std.rs

+7-12
Original file line numberDiff line numberDiff line change
@@ -31,22 +31,17 @@ impl TorrentsTokioRwLock<entry::MutexStd> {
3131

3232
impl UpdateTorrentAsync for TorrentsTokioRwLock<entry::MutexStd> {
3333
async fn update_torrent_with_peer_and_get_stats(&self, info_hash: &InfoHash, peer: &peer::Peer) -> (bool, SwarmMetadata) {
34-
let maybe_torrent;
35-
{
36-
let db = self.torrents.read().await;
37-
maybe_torrent = db.get(info_hash).cloned();
38-
}
34+
let maybe_entry = self.get_torrents().await.get(info_hash).cloned();
3935

40-
let torrent = if let Some(torrent) = maybe_torrent {
41-
torrent
42-
} else {
43-
let entry = entry::MutexStd::default();
44-
let mut db = self.torrents.write().await;
45-
db.insert(*info_hash, entry.clone());
36+
let entry = if let Some(entry) = maybe_entry {
4637
entry
38+
} else {
39+
let mut db = self.get_torrents_mut().await;
40+
let entry = db.entry(*info_hash).or_insert(Arc::default());
41+
entry.clone()
4742
};
4843

49-
torrent.insert_or_update_peer_and_get_stats(peer)
44+
entry.insert_or_update_peer_and_get_stats(peer)
5045
}
5146
}
5247

src/core/torrent/repository/tokio_tokio.rs

+7-12
Original file line numberDiff line numberDiff line change
@@ -33,22 +33,17 @@ impl TorrentsTokioRwLock<entry::MutexTokio> {
3333

3434
impl UpdateTorrentAsync for TorrentsTokioRwLock<entry::MutexTokio> {
3535
async fn update_torrent_with_peer_and_get_stats(&self, info_hash: &InfoHash, peer: &peer::Peer) -> (bool, SwarmMetadata) {
36-
let maybe_torrent;
37-
{
38-
let db = self.torrents.read().await;
39-
maybe_torrent = db.get(info_hash).cloned();
40-
}
36+
let maybe_entry = self.get_torrents().await.get(info_hash).cloned();
4137

42-
let torrent = if let Some(torrent) = maybe_torrent {
43-
torrent
44-
} else {
45-
let entry = entry::MutexTokio::default();
46-
let mut db = self.torrents.write().await;
47-
db.insert(*info_hash, entry.clone());
38+
let entry = if let Some(entry) = maybe_entry {
4839
entry
40+
} else {
41+
let mut db = self.get_torrents_mut().await;
42+
let entry = db.entry(*info_hash).or_insert(Arc::default());
43+
entry.clone()
4944
};
5045

51-
torrent.insert_or_update_peer_and_get_stats(peer).await
46+
entry.insert_or_update_peer_and_get_stats(peer).await
5247
}
5348
}
5449

0 commit comments

Comments
 (0)