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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
use std::ops::{Deref, DerefMut};
use std::path::PathBuf;
use fs_extra::dir;
use r2d2::PooledConnection;
use r2d2_sqlite::SqliteConnectionManager;
use rusqlite::NO_PARAMS;
use crate::config::LoadConfig;
use crate::error::Result;
use crate::events::{CrawlerCheckpoint, Profile, SerializedEvent};
use crate::Database;
#[derive(Serialize, Deserialize)]
pub struct DatabaseStats {
pub size: u64,
pub event_count: u64,
pub room_count: u64,
}
pub struct Connection {
pub(crate) inner: PooledConnection<SqliteConnectionManager>,
pub(crate) path: PathBuf,
}
impl Connection {
pub fn load_checkpoints(&self) -> Result<Vec<CrawlerCheckpoint>> {
let mut stmt = self.prepare(
"SELECT room_id, token, full_crawl, direction
FROM crawlercheckpoints",
)?;
let rows = stmt.query_map(NO_PARAMS, |row| {
Ok(CrawlerCheckpoint {
room_id: row.get(0)?,
token: row.get(1)?,
full_crawl: row.get(2)?,
direction: row.get(3)?,
})
})?;
let mut checkpoints = Vec::new();
for row in rows {
let checkpoint: CrawlerCheckpoint = row?;
checkpoints.push(checkpoint);
}
Ok(checkpoints)
}
pub fn is_empty(&self) -> Result<bool> {
let event_count: i64 = Database::get_event_count(&self.inner)?;
let checkpoint_count: i64 = self.query_row(
"SELECT COUNT(*) FROM crawlercheckpoints",
NO_PARAMS,
|row| row.get(0),
)?;
Ok(event_count == 0 && checkpoint_count == 0)
}
pub fn get_stats(&self) -> Result<DatabaseStats> {
let event_count = Database::get_event_count(&self.inner)? as u64;
let room_count = Database::get_room_count(&self.inner)? as u64;
let size = dir::get_size(&self.path)?;
Ok(DatabaseStats {
size,
event_count,
room_count,
})
}
pub fn load_file_events(
&self,
load_config: &LoadConfig,
) -> Result<Vec<(SerializedEvent, Profile)>> {
Ok(Database::load_file_events(
self,
&load_config.room_id,
load_config.limit,
load_config.from_event.as_ref().map(|x| &**x),
&load_config.direction,
)?)
}
}
impl Deref for Connection {
type Target = PooledConnection<SqliteConnectionManager>;
fn deref(&self) -> &Self::Target {
&self.inner
}
}
impl DerefMut for Connection {
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.inner
}
}