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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
use std::collections::{BTreeSet, HashMap, HashSet};
use crate::{
error::table::TableError,
tables::table::{
Cacheable, Deduplicate, Diagnostic, Table, CHAT_HANDLE_JOIN, CHAT_MESSAGE_JOIN,
},
util::output::{done_processing, processing},
};
use rusqlite::{Connection, Error, Result, Row, Statement};
pub struct ChatToHandle {
chat_id: i32,
handle_id: i32,
}
impl Table for ChatToHandle {
fn from_row(row: &Row) -> Result<ChatToHandle> {
Ok(ChatToHandle {
chat_id: row.get(0)?,
handle_id: row.get(1)?,
})
}
fn get(db: &Connection) -> Statement {
db.prepare(&format!("SELECT * FROM {}", CHAT_HANDLE_JOIN))
.unwrap()
}
fn extract(chat_to_handle: Result<Result<Self, Error>, Error>) -> Result<Self, TableError> {
match chat_to_handle {
Ok(chat_to_handle) => match chat_to_handle {
Ok(c2h) => Ok(c2h),
Err(why) => Err(TableError::ChatToHandle(why)),
},
Err(why) => Err(TableError::ChatToHandle(why)),
}
}
}
impl Cacheable for ChatToHandle {
type K = i32;
type V = BTreeSet<i32>;
fn cache(db: &Connection) -> Result<HashMap<Self::K, Self::V>, TableError> {
let mut cache: HashMap<i32, BTreeSet<i32>> = HashMap::new();
let mut rows = ChatToHandle::get(db);
let mappings = rows
.query_map([], |row| Ok(ChatToHandle::from_row(row)))
.unwrap();
for mapping in mappings {
let joiner = ChatToHandle::extract(mapping)?;
match cache.get_mut(&joiner.chat_id) {
Some(handles) => {
handles.insert(joiner.handle_id);
}
None => {
let mut data_to_cache = BTreeSet::new();
data_to_cache.insert(joiner.handle_id);
cache.insert(joiner.chat_id, data_to_cache);
}
}
}
Ok(cache)
}
}
impl Deduplicate for ChatToHandle {
type T = BTreeSet<i32>;
fn dedupe(duplicated_data: &HashMap<i32, Self::T>) -> HashMap<i32, i32> {
let mut deduplicated_chats: HashMap<i32, i32> = HashMap::new();
let mut participants_to_unique_chat_id: HashMap<Self::T, i32> = HashMap::new();
let mut unique_chat_identifier = 0;
for (chat_id, participants) in duplicated_data {
match participants_to_unique_chat_id.get(participants) {
Some(id) => {
deduplicated_chats.insert(chat_id.to_owned(), id.to_owned());
}
None => {
participants_to_unique_chat_id
.insert(participants.to_owned(), unique_chat_identifier);
deduplicated_chats.insert(chat_id.to_owned(), unique_chat_identifier);
unique_chat_identifier += 1;
}
}
}
deduplicated_chats
}
}
impl Diagnostic for ChatToHandle {
fn run_diagnostic(db: &Connection) {
processing();
let mut statement_message_chats = db
.prepare(&format!("SELECT DISTINCT chat_id from {CHAT_MESSAGE_JOIN}"))
.unwrap();
let statement_message_chat_rows = statement_message_chats
.query_map([], |row: &Row| -> Result<i32> { row.get(0) })
.unwrap();
let mut unique_chats_from_messages: HashSet<i32> = HashSet::new();
statement_message_chat_rows.into_iter().for_each(|row| {
unique_chats_from_messages.insert(row.unwrap());
});
let mut statement_handle_chats = db
.prepare(&format!("SELECT DISTINCT chat_id from {CHAT_HANDLE_JOIN}"))
.unwrap();
let statement_handle_chat_rows = statement_handle_chats
.query_map([], |row: &Row| -> Result<i32> { row.get(0) })
.unwrap();
let mut unique_chats_from_handles: HashSet<i32> = HashSet::new();
statement_handle_chat_rows.into_iter().for_each(|row| {
unique_chats_from_handles.insert(row.unwrap());
});
done_processing();
let chats_with_no_handles = unique_chats_from_messages
.difference(&unique_chats_from_handles)
.count();
if chats_with_no_handles > 0 {
println!("\rChats with no handles: {chats_with_no_handles:?}");
}
}
}