1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
//! Sharded torrent storage for concurrent access.
use crateInfoHash;
use crateTorrentEntry;
use RwLock;
use BTreeMap;
use Arc;
/// Sharded storage for torrent entries with 256 shards.
///
/// `TorrentSharding` distributes torrents across 256 separate shards to minimize
/// lock contention in high-concurrency scenarios. Each shard is independently
/// locked, allowing multiple threads to access different torrents simultaneously.
///
/// # Sharding Strategy
///
/// Torrents are assigned to shards based on the first byte of their info hash:
/// - `shard_index = info_hash[0]` (0-255)
///
/// This provides uniform distribution since info hashes are SHA-1 hashes with
/// pseudo-random byte distribution.
///
/// # Thread Safety
///
/// Each shard is wrapped in `Arc<RwLock<...>>` using `parking_lot`:
/// - Multiple readers can access the same shard concurrently
/// - Writers get exclusive access to their shard only
/// - Other shards remain accessible during writes
///
/// # Performance
///
/// With 256 shards, the probability of lock contention is reduced by ~256x
/// compared to a single lock. For a tracker handling millions of torrents,
/// this significantly improves throughput.
///
/// # Example
///
/// ```rust,ignore
/// use torrust_actix::tracker::structs::torrent_sharding::TorrentSharding;
/// use torrust_actix::tracker::structs::info_hash::InfoHash;
///
/// let sharding = TorrentSharding::new();
///
/// // Check if a torrent exists
/// let info_hash = InfoHash([0u8; 20]);
/// let exists = sharding.contains_torrent(info_hash);
///
/// // Get total torrent count across all shards
/// let total = sharding.get_torrents_amount();
/// ```