|
| 1 | +use std::collections::BTreeMap; |
| 2 | +use std::sync::Arc; |
| 3 | + |
| 4 | +use crossbeam_skiplist::SkipMap; |
| 5 | +use torrust_tracker_configuration::TrackerPolicy; |
| 6 | +use torrust_tracker_primitives::info_hash::InfoHash; |
| 7 | +use torrust_tracker_primitives::pagination::Pagination; |
| 8 | +use torrust_tracker_primitives::swarm_metadata::SwarmMetadata; |
| 9 | +use torrust_tracker_primitives::torrent_metrics::TorrentsMetrics; |
| 10 | +use torrust_tracker_primitives::{peer, DurationSinceUnixEpoch, PersistentTorrents}; |
| 11 | + |
| 12 | +use super::Repository; |
| 13 | +use crate::entry::{Entry, EntrySync}; |
| 14 | +use crate::{EntryMutexStd, EntrySingle}; |
| 15 | + |
| 16 | +#[derive(Default, Debug)] |
| 17 | +pub struct CrossbeamSkipList<T> { |
| 18 | + pub torrents: SkipMap<InfoHash, T>, |
| 19 | +} |
| 20 | + |
| 21 | +impl Repository<EntryMutexStd> for CrossbeamSkipList<EntryMutexStd> |
| 22 | +where |
| 23 | + EntryMutexStd: EntrySync, |
| 24 | + EntrySingle: Entry, |
| 25 | +{ |
| 26 | + fn update_torrent_with_peer_and_get_stats(&self, info_hash: &InfoHash, peer: &peer::Peer) -> (bool, SwarmMetadata) { |
| 27 | + let entry = self.torrents.get_or_insert(*info_hash, Arc::default()); |
| 28 | + entry.value().insert_or_update_peer_and_get_stats(peer) |
| 29 | + } |
| 30 | + |
| 31 | + fn get(&self, key: &InfoHash) -> Option<EntryMutexStd> { |
| 32 | + let maybe_entry = self.torrents.get(key); |
| 33 | + maybe_entry.map(|entry| entry.value().clone()) |
| 34 | + } |
| 35 | + |
| 36 | + fn get_metrics(&self) -> TorrentsMetrics { |
| 37 | + let mut metrics = TorrentsMetrics::default(); |
| 38 | + |
| 39 | + for entry in &self.torrents { |
| 40 | + let stats = entry.value().lock().expect("it should get a lock").get_stats(); |
| 41 | + metrics.complete += u64::from(stats.complete); |
| 42 | + metrics.downloaded += u64::from(stats.downloaded); |
| 43 | + metrics.incomplete += u64::from(stats.incomplete); |
| 44 | + metrics.torrents += 1; |
| 45 | + } |
| 46 | + |
| 47 | + metrics |
| 48 | + } |
| 49 | + |
| 50 | + fn get_paginated(&self, pagination: Option<&Pagination>) -> Vec<(InfoHash, EntryMutexStd)> { |
| 51 | + match pagination { |
| 52 | + Some(pagination) => self |
| 53 | + .torrents |
| 54 | + .iter() |
| 55 | + .skip(pagination.offset as usize) |
| 56 | + .take(pagination.limit as usize) |
| 57 | + .map(|entry| (*entry.key(), entry.value().clone())) |
| 58 | + .collect(), |
| 59 | + None => self |
| 60 | + .torrents |
| 61 | + .iter() |
| 62 | + .map(|entry| (*entry.key(), entry.value().clone())) |
| 63 | + .collect(), |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | + fn import_persistent(&self, persistent_torrents: &PersistentTorrents) { |
| 68 | + for (info_hash, completed) in persistent_torrents { |
| 69 | + if self.torrents.contains_key(info_hash) { |
| 70 | + continue; |
| 71 | + } |
| 72 | + |
| 73 | + let entry = EntryMutexStd::new( |
| 74 | + EntrySingle { |
| 75 | + peers: BTreeMap::default(), |
| 76 | + downloaded: *completed, |
| 77 | + } |
| 78 | + .into(), |
| 79 | + ); |
| 80 | + |
| 81 | + // Since SkipMap is lock-free the torrent could have been inserted |
| 82 | + // after checking if it exists. |
| 83 | + self.torrents.get_or_insert(*info_hash, entry); |
| 84 | + } |
| 85 | + } |
| 86 | + |
| 87 | + fn remove(&self, key: &InfoHash) -> Option<EntryMutexStd> { |
| 88 | + self.torrents.remove(key).map(|entry| entry.value().clone()) |
| 89 | + } |
| 90 | + |
| 91 | + fn remove_inactive_peers(&self, current_cutoff: DurationSinceUnixEpoch) { |
| 92 | + for entry in &self.torrents { |
| 93 | + entry.value().remove_inactive_peers(current_cutoff); |
| 94 | + } |
| 95 | + } |
| 96 | + |
| 97 | + fn remove_peerless_torrents(&self, policy: &TrackerPolicy) { |
| 98 | + for entry in &self.torrents { |
| 99 | + if entry.value().is_good(policy) { |
| 100 | + continue; |
| 101 | + } |
| 102 | + |
| 103 | + entry.remove(); |
| 104 | + } |
| 105 | + } |
| 106 | +} |
0 commit comments