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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
pub mod driver;
pub mod error;
pub mod mysql;
pub mod sqlite;
use std::marker::PhantomData;
use async_trait::async_trait;
use self::error::Error;
use crate::shared::bit_torrent::info_hash::InfoHash;
use crate::tracker::auth::{self, Key};
pub(self) struct Builder<T>
where
T: Database,
{
phantom: PhantomData<T>,
}
impl<T> Builder<T>
where
T: Database + 'static,
{
pub(self) fn build(db_path: &str) -> Result<Box<dyn Database>, Error> {
Ok(Box::new(T::new(db_path)?))
}
}
#[async_trait]
pub trait Database: Sync + Send {
fn new(db_path: &str) -> Result<Self, Error>
where
Self: std::marker::Sized;
fn create_database_tables(&self) -> Result<(), Error>;
fn drop_database_tables(&self) -> Result<(), Error>;
async fn load_persistent_torrents(&self) -> Result<Vec<(InfoHash, u32)>, Error>;
async fn load_keys(&self) -> Result<Vec<auth::ExpiringKey>, Error>;
async fn load_whitelist(&self) -> Result<Vec<InfoHash>, Error>;
async fn save_persistent_torrent(&self, info_hash: &InfoHash, completed: u32) -> Result<(), Error>;
async fn get_info_hash_from_whitelist(&self, info_hash: &InfoHash) -> Result<Option<InfoHash>, Error>;
async fn add_info_hash_to_whitelist(&self, info_hash: InfoHash) -> Result<usize, Error>;
async fn remove_info_hash_from_whitelist(&self, info_hash: InfoHash) -> Result<usize, Error>;
async fn get_key_from_keys(&self, key: &Key) -> Result<Option<auth::ExpiringKey>, Error>;
async fn add_key_to_keys(&self, auth_key: &auth::ExpiringKey) -> Result<usize, Error>;
async fn remove_key_from_keys(&self, key: &Key) -> Result<usize, Error>;
async fn is_info_hash_whitelisted(&self, info_hash: &InfoHash) -> Result<bool, Error> {
Ok(self.get_info_hash_from_whitelist(info_hash).await?.is_some())
}
}