|
| 1 | +use torrust_tracker_configuration::TrackerPolicy; |
| 2 | +use torrust_tracker_primitives::info_hash::InfoHash; |
| 3 | +use torrust_tracker_primitives::pagination::Pagination; |
| 4 | +use torrust_tracker_primitives::swarm_metadata::SwarmMetadata; |
| 5 | +use torrust_tracker_primitives::torrent_metrics::TorrentsMetrics; |
| 6 | +use torrust_tracker_primitives::{peer, DurationSinceUnixEpoch, PersistentTorrents}; |
| 7 | +use torrust_tracker_torrent_repository::repository::{Repository as _, RepositoryAsync as _}; |
| 8 | +use torrust_tracker_torrent_repository::{ |
| 9 | + EntrySingle, TorrentsRwLockStd, TorrentsRwLockStdMutexStd, TorrentsRwLockStdMutexTokio, TorrentsRwLockTokio, |
| 10 | + TorrentsRwLockTokioMutexStd, TorrentsRwLockTokioMutexTokio, |
| 11 | +}; |
| 12 | + |
| 13 | +#[derive(Debug)] |
| 14 | +pub(crate) enum Repo { |
| 15 | + Std(TorrentsRwLockStd), |
| 16 | + StdMutexStd(TorrentsRwLockStdMutexStd), |
| 17 | + StdMutexTokio(TorrentsRwLockStdMutexTokio), |
| 18 | + Tokio(TorrentsRwLockTokio), |
| 19 | + TokioMutexStd(TorrentsRwLockTokioMutexStd), |
| 20 | + TokioMutexTokio(TorrentsRwLockTokioMutexTokio), |
| 21 | +} |
| 22 | + |
| 23 | +impl Repo { |
| 24 | + pub(crate) async fn get(&self, key: &InfoHash) -> Option<EntrySingle> { |
| 25 | + match self { |
| 26 | + Repo::Std(repo) => repo.get(key), |
| 27 | + Repo::StdMutexStd(repo) => Some(repo.get(key)?.lock().unwrap().clone()), |
| 28 | + Repo::StdMutexTokio(repo) => Some(repo.get(key).await?.lock().await.clone()), |
| 29 | + Repo::Tokio(repo) => repo.get(key).await, |
| 30 | + Repo::TokioMutexStd(repo) => Some(repo.get(key).await?.lock().unwrap().clone()), |
| 31 | + Repo::TokioMutexTokio(repo) => Some(repo.get(key).await?.lock().await.clone()), |
| 32 | + } |
| 33 | + } |
| 34 | + pub(crate) async fn get_metrics(&self) -> TorrentsMetrics { |
| 35 | + match self { |
| 36 | + Repo::Std(repo) => repo.get_metrics(), |
| 37 | + Repo::StdMutexStd(repo) => repo.get_metrics(), |
| 38 | + Repo::StdMutexTokio(repo) => repo.get_metrics().await, |
| 39 | + Repo::Tokio(repo) => repo.get_metrics().await, |
| 40 | + Repo::TokioMutexStd(repo) => repo.get_metrics().await, |
| 41 | + Repo::TokioMutexTokio(repo) => repo.get_metrics().await, |
| 42 | + } |
| 43 | + } |
| 44 | + pub(crate) async fn get_paginated(&self, pagination: Option<&Pagination>) -> Vec<(InfoHash, EntrySingle)> { |
| 45 | + match self { |
| 46 | + Repo::Std(repo) => repo.get_paginated(pagination), |
| 47 | + Repo::StdMutexStd(repo) => repo |
| 48 | + .get_paginated(pagination) |
| 49 | + .iter() |
| 50 | + .map(|(i, t)| (*i, t.lock().expect("it should get a lock").clone())) |
| 51 | + .collect(), |
| 52 | + Repo::StdMutexTokio(repo) => { |
| 53 | + let mut v: Vec<(InfoHash, EntrySingle)> = vec![]; |
| 54 | + |
| 55 | + for (i, t) in repo.get_paginated(pagination).await { |
| 56 | + v.push((i, t.lock().await.clone())); |
| 57 | + } |
| 58 | + v |
| 59 | + } |
| 60 | + Repo::Tokio(repo) => repo.get_paginated(pagination).await, |
| 61 | + Repo::TokioMutexStd(repo) => repo |
| 62 | + .get_paginated(pagination) |
| 63 | + .await |
| 64 | + .iter() |
| 65 | + .map(|(i, t)| (*i, t.lock().expect("it should get a lock").clone())) |
| 66 | + .collect(), |
| 67 | + Repo::TokioMutexTokio(repo) => { |
| 68 | + let mut v: Vec<(InfoHash, EntrySingle)> = vec![]; |
| 69 | + |
| 70 | + for (i, t) in repo.get_paginated(pagination).await { |
| 71 | + v.push((i, t.lock().await.clone())); |
| 72 | + } |
| 73 | + v |
| 74 | + } |
| 75 | + } |
| 76 | + } |
| 77 | + pub(crate) async fn import_persistent(&self, persistent_torrents: &PersistentTorrents) { |
| 78 | + match self { |
| 79 | + Repo::Std(repo) => repo.import_persistent(persistent_torrents), |
| 80 | + Repo::StdMutexStd(repo) => repo.import_persistent(persistent_torrents), |
| 81 | + Repo::StdMutexTokio(repo) => repo.import_persistent(persistent_torrents).await, |
| 82 | + Repo::Tokio(repo) => repo.import_persistent(persistent_torrents).await, |
| 83 | + Repo::TokioMutexStd(repo) => repo.import_persistent(persistent_torrents).await, |
| 84 | + Repo::TokioMutexTokio(repo) => repo.import_persistent(persistent_torrents).await, |
| 85 | + } |
| 86 | + } |
| 87 | + pub(crate) async fn remove(&self, key: &InfoHash) -> Option<EntrySingle> { |
| 88 | + match self { |
| 89 | + Repo::Std(repo) => repo.remove(key), |
| 90 | + Repo::StdMutexStd(repo) => Some(repo.remove(key)?.lock().unwrap().clone()), |
| 91 | + Repo::StdMutexTokio(repo) => Some(repo.remove(key).await?.lock().await.clone()), |
| 92 | + Repo::Tokio(repo) => repo.remove(key).await, |
| 93 | + Repo::TokioMutexStd(repo) => Some(repo.remove(key).await?.lock().unwrap().clone()), |
| 94 | + Repo::TokioMutexTokio(repo) => Some(repo.remove(key).await?.lock().await.clone()), |
| 95 | + } |
| 96 | + } |
| 97 | + pub(crate) async fn remove_inactive_peers(&self, current_cutoff: DurationSinceUnixEpoch) { |
| 98 | + match self { |
| 99 | + Repo::Std(repo) => repo.remove_inactive_peers(current_cutoff), |
| 100 | + Repo::StdMutexStd(repo) => repo.remove_inactive_peers(current_cutoff), |
| 101 | + Repo::StdMutexTokio(repo) => repo.remove_inactive_peers(current_cutoff).await, |
| 102 | + Repo::Tokio(repo) => repo.remove_inactive_peers(current_cutoff).await, |
| 103 | + Repo::TokioMutexStd(repo) => repo.remove_inactive_peers(current_cutoff).await, |
| 104 | + Repo::TokioMutexTokio(repo) => repo.remove_inactive_peers(current_cutoff).await, |
| 105 | + } |
| 106 | + } |
| 107 | + pub(crate) async fn remove_peerless_torrents(&self, policy: &TrackerPolicy) { |
| 108 | + match self { |
| 109 | + Repo::Std(repo) => repo.remove_peerless_torrents(policy), |
| 110 | + Repo::StdMutexStd(repo) => repo.remove_peerless_torrents(policy), |
| 111 | + Repo::StdMutexTokio(repo) => repo.remove_peerless_torrents(policy).await, |
| 112 | + Repo::Tokio(repo) => repo.remove_peerless_torrents(policy).await, |
| 113 | + Repo::TokioMutexStd(repo) => repo.remove_peerless_torrents(policy).await, |
| 114 | + Repo::TokioMutexTokio(repo) => repo.remove_peerless_torrents(policy).await, |
| 115 | + } |
| 116 | + } |
| 117 | + pub(crate) async fn update_torrent_with_peer_and_get_stats( |
| 118 | + &self, |
| 119 | + info_hash: &InfoHash, |
| 120 | + peer: &peer::Peer, |
| 121 | + ) -> (bool, SwarmMetadata) { |
| 122 | + match self { |
| 123 | + Repo::Std(repo) => repo.update_torrent_with_peer_and_get_stats(info_hash, peer), |
| 124 | + Repo::StdMutexStd(repo) => repo.update_torrent_with_peer_and_get_stats(info_hash, peer), |
| 125 | + Repo::StdMutexTokio(repo) => repo.update_torrent_with_peer_and_get_stats(info_hash, peer).await, |
| 126 | + Repo::Tokio(repo) => repo.update_torrent_with_peer_and_get_stats(info_hash, peer).await, |
| 127 | + Repo::TokioMutexStd(repo) => repo.update_torrent_with_peer_and_get_stats(info_hash, peer).await, |
| 128 | + Repo::TokioMutexTokio(repo) => repo.update_torrent_with_peer_and_get_stats(info_hash, peer).await, |
| 129 | + } |
| 130 | + } |
| 131 | + pub(crate) async fn insert(&self, info_hash: &InfoHash, torrent: EntrySingle) -> Option<EntrySingle> { |
| 132 | + match self { |
| 133 | + Repo::Std(repo) => repo.write().insert(*info_hash, torrent), |
| 134 | + Repo::StdMutexStd(repo) => Some(repo.write().insert(*info_hash, torrent.into())?.lock().unwrap().clone()), |
| 135 | + Repo::StdMutexTokio(repo) => { |
| 136 | + let r = repo.write().insert(*info_hash, torrent.into()); |
| 137 | + match r { |
| 138 | + Some(t) => Some(t.lock().await.clone()), |
| 139 | + None => None, |
| 140 | + } |
| 141 | + } |
| 142 | + Repo::Tokio(repo) => repo.write().await.insert(*info_hash, torrent), |
| 143 | + Repo::TokioMutexStd(repo) => Some(repo.write().await.insert(*info_hash, torrent.into())?.lock().unwrap().clone()), |
| 144 | + Repo::TokioMutexTokio(repo) => Some(repo.write().await.insert(*info_hash, torrent.into())?.lock().await.clone()), |
| 145 | + } |
| 146 | + } |
| 147 | +} |
0 commit comments