-
Notifications
You must be signed in to change notification settings - Fork 46
/
models.rs
530 lines (478 loc) · 17.9 KB
/
models.rs
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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
#![allow(proc_macro_derive_resolution_fallback)]
use std::{self, collections::HashMap, ops::Deref, sync::Arc};
use diesel::{
delete,
dsl::max,
expression::sql_literal::sql,
insert_into,
mysql::MysqlConnection,
r2d2::{ConnectionManager, PooledConnection},
sql_query,
sql_types::{BigInt, Integer, Text},
update, Connection, ExpressionMethods, GroupByDsl, OptionalExtension, QueryDsl, RunQueryDsl,
};
#[cfg(test)]
use diesel_logger::LoggingConnection;
use futures::future;
use super::{
pool::CollectionCache,
schema::{bso, collections, user_collections},
};
use db::{
error::{DbError, DbErrorKind},
params, results,
util::ms_since_epoch,
Db, DbFuture, Sorting,
};
use settings::Settings;
embed_migrations!();
no_arg_sql_function!(last_insert_id, Integer);
pub type Result<T> = std::result::Result<T, DbError>;
type Conn = PooledConnection<ConnectionManager<MysqlConnection>>;
// The ttl to use for rows that are never supposed to expire (in seconds)
pub const DEFAULT_BSO_TTL: u32 = 2100000000;
/// Run the diesel embedded migrations
///
/// Mysql DDL statements implicitly commit which could disrupt MysqlPool's
/// begin_test_transaction during tests. So this runs on its own separate conn.
pub fn run_embedded_migrations(settings: &Settings) -> Result<()> {
let conn = MysqlConnection::establish(&settings.database_url).unwrap();
Ok(embedded_migrations::run(&conn)?)
}
pub struct MysqlDb {
#[cfg(not(test))]
pub(super) conn: Conn,
#[cfg(test)]
pub(super) conn: LoggingConnection<Conn>,
/// Pool level cache of collection_ids and their names
coll_cache: Arc<CollectionCache>,
/// Per session cache of modified timestamps per (user_id, collection_id)
coll_modified_cache: HashMap<(u32, i32), i64>,
}
impl MysqlDb {
pub fn new(conn: Conn, coll_cache: Arc<CollectionCache>) -> Self {
MysqlDb {
#[cfg(not(test))]
conn,
#[cfg(test)]
conn: LoggingConnection::new(conn),
coll_cache,
coll_modified_cache: Default::default(),
}
}
pub(super) fn create_collection(&self, name: &str) -> Result<i32> {
// XXX: handle concurrent attempts at inserts
let collection_id = self.conn.transaction(|| {
sql_query("INSERT INTO collections (name) VALUES (?)")
.bind::<Text, _>(name)
.execute(&self.conn)?;
collections::table.select(last_insert_id).first(&self.conn)
})?;
Ok(collection_id)
}
pub fn delete_storage_sync(&self, user_id: u32) -> Result<()> {
delete(bso::table)
.filter(bso::user_id.eq(user_id as i32))
.execute(&self.conn)?;
delete(user_collections::table)
.filter(user_collections::user_id.eq(user_id as i32))
.execute(&self.conn)?;
Ok(())
}
pub fn delete_collection_sync(&self, user_id: u32, collection_id: i32) -> Result<i64> {
let mut count = delete(bso::table)
.filter(bso::user_id.eq(user_id as i32))
.filter(bso::collection_id.eq(&collection_id))
.execute(&self.conn)?;
count += delete(user_collections::table)
.filter(user_collections::user_id.eq(user_id as i32))
.filter(user_collections::collection_id.eq(&collection_id))
.execute(&self.conn)?;
if count == 0 {
Err(DbErrorKind::CollectionNotFound)?
}
self.get_storage_modified_sync(user_id)
}
pub(super) fn get_collection_id(&self, name: &str) -> Result<i32> {
let id = if let Some(id) = self.coll_cache.get_id(name)? {
id
} else {
sql_query("SELECT id FROM collections WHERE name = ?")
.bind::<Text, _>(name)
.get_result::<IdResult>(&self.conn)
.optional()?
.ok_or(DbErrorKind::CollectionNotFound)?
.id
};
Ok(id)
}
fn _get_collection_name(&self, id: i32) -> Result<String> {
let name = if let Some(name) = self.coll_cache.get_name(id)? {
name
} else {
sql_query("SELECT name FROM collections where id = ?")
.bind::<Integer, _>(&id)
.get_result::<NameResult>(&self.conn)
.optional()?
.ok_or(DbErrorKind::CollectionNotFound)?
.name
};
Ok(name)
}
pub fn put_bso_sync(&self, bso: ¶ms::PutBso) -> Result<results::PutBso> {
/*
if bso.payload.is_none() && bso.sortindex.is_none() && bso.ttl.is_none() {
// XXX: go returns an error here (ErrNothingToDo), and is treated
// as other errors
return Ok(());
}
*/
// XXX: this should auto create collections when they're not found
let user_id: u64 = bso.user_id.legacy_id;
// XXX: consider mysql ON DUPLICATE KEY UPDATE?
self.conn.transaction(|| {
let q = r#"
SELECT 1 as count FROM bso
WHERE user_id = ? AND collection_id = ? AND id = ?
"#;
let exists = sql_query(q)
.bind::<Integer, _>(user_id as i32) // XXX:
.bind::<Integer, _>(&bso.collection_id)
.bind::<Text, _>(&bso.id)
.get_result::<Count>(&self.conn)
.optional()?
.is_some();
if exists {
update(bso::table)
.filter(bso::user_id.eq(user_id as i32)) // XXX:
.filter(bso::collection_id.eq(&bso.collection_id))
.filter(bso::id.eq(&bso.id))
.set(put_bso_as_changeset(&bso))
.execute(&self.conn)?;
} else {
let payload = bso.payload.as_ref().map(Deref::deref).unwrap_or_default();
let sortindex = bso.sortindex;
let ttl = bso.ttl.map_or(DEFAULT_BSO_TTL, |ttl| ttl);
insert_into(bso::table)
.values((
bso::user_id.eq(user_id as i32), // XXX:
bso::collection_id.eq(&bso.collection_id),
bso::id.eq(&bso.id),
bso::sortindex.eq(sortindex),
bso::payload.eq(payload),
bso::payload_size.eq(payload.len() as i32), // XXX:
bso::modified.eq(bso.modified),
bso::expiry.eq(bso.modified + ttl as i64),
)).execute(&self.conn)?;
}
self.touch_collection(user_id as u32, bso.collection_id, bso.modified)?;
// XXX:
Ok(bso.modified as u64)
})
}
// XXX: limit/offset i64?
pub fn get_bsos_sync(
&self,
user_id: u32,
collection_id: i32,
mut ids: &[&str],
older: u64,
newer: u64,
sort: Sorting,
limit: i64,
offset: i64,
) -> Result<results::BSOs> {
// XXX: ensure offset/limit/newer are valid
// XXX: should error out (400 Bad Request) when more than 100
// are provided (move to validation layer)
if ids.len() > 100 {
// spec says only 100 ids at a time
ids = &ids[0..100];
}
// XXX: convert to raw SQL for use by other backends
let mut query = bso::table
//.select(bso::table::all_columns())
.select((bso::id, bso::modified, bso::payload, bso::sortindex, bso::expiry))
.filter(bso::user_id.eq(user_id as i32)) // XXX:
.filter(bso::collection_id.eq(collection_id as i32)) // XXX:
.filter(bso::modified.lt(older as i64))
.filter(bso::modified.gt(newer as i64))
.filter(bso::expiry.gt(ms_since_epoch()))
.into_boxed();
if !ids.is_empty() {
query = query.filter(bso::id.eq_any(ids));
}
query = match sort {
Sorting::Index => query.order(bso::sortindex.desc()),
Sorting::Newest => query.order(bso::modified.desc()),
Sorting::Oldest => query.order(bso::modified.asc()),
_ => query,
};
// fetch an extra row to detect if there are more rows that
// match the query conditions
query = query.limit(if limit >= 0 { limit + 1 } else { limit });
if offset != 0 {
// XXX: copy over this optimization:
// https://github.com/mozilla-services/server-syncstorage/blob/a0f8117/syncstorage/storage/sql/__init__.py#L404
query = query.offset(offset);
}
let mut bsos = query.load::<results::GetBso>(&self.conn)?;
let (more, next_offset) = if limit >= 0 && bsos.len() > limit as usize {
bsos.pop();
(true, limit + offset)
} else {
(false, 0)
};
Ok(results::BSOs {
bsos,
more,
offset: next_offset,
})
}
pub fn get_bso_sync(&self, params: ¶ms::GetBso) -> Result<Option<results::GetBso>> {
let user_id = params.user_id.legacy_id;
Ok(sql_query(r#"
SELECT id, modified, payload, sortindex, expiry FROM bso
WHERE user_id = ? AND collection_id = ? AND id = ? AND expiry >= ?
"#)
.bind::<Integer, _>(user_id as i32) // XXX:
.bind::<Integer, _>(¶ms.collection_id)
.bind::<Text, _>(¶ms.id)
.bind::<BigInt, _>(ms_since_epoch())
.get_result::<results::GetBso>(&self.conn)
.optional()?)
}
pub fn delete_bso_sync(&self, user_id: u32, collection_id: i32, bso_id: &str) -> Result<i64> {
self.delete_bsos_sync(user_id, collection_id, &[bso_id])
}
pub fn delete_bsos_sync(
&self,
user_id: u32,
collection_id: i32,
bso_id: &[&str],
) -> Result<i64> {
delete(bso::table)
.filter(bso::user_id.eq(user_id as i32))
.filter(bso::collection_id.eq(&collection_id))
.filter(bso::id.eq_any(bso_id))
.execute(&self.conn)?;
let modified = ms_since_epoch();
self.touch_collection(user_id, collection_id, modified)?;
Ok(modified)
}
pub fn post_bsos_sync(
&self,
input: ¶ms::PostCollection,
) -> Result<results::PostCollection> {
let modified = ms_since_epoch();
let mut result = results::PostCollection {
modified: modified as u64,
success: Default::default(),
failed: Default::default(),
};
for pbso in &input.bsos {
let put_result = self.put_bso_sync(¶ms::PutBso {
user_id: input.user_id.clone(),
collection_id: input.collection_id,
id: pbso.id.clone(),
payload: pbso.payload.as_ref().map(Into::into),
sortindex: pbso.sortindex,
ttl: pbso.ttl,
modified,
});
// XXX: python version doesn't report failures from db layer..
// XXX: sanitize to.to_string()?
match put_result {
Ok(_) => result.success.push(pbso.id.clone()),
Err(e) => {
result.failed.insert(pbso.id.clone(), e.to_string());
}
}
}
self.touch_collection(
input.user_id.legacy_id as u32,
input.collection_id,
modified,
)?;
Ok(result)
}
pub fn get_storage_modified_sync(&self, user_id: u32) -> Result<i64> {
Ok(user_collections::table
.select(max(user_collections::modified))
.filter(user_collections::user_id.eq(user_id as i32))
.first::<Option<i64>>(&self.conn)?
.unwrap_or_default())
}
pub fn get_collection_modified_sync(&self, user_id: u32, collection_id: i32) -> Result<i64> {
if let Some(modified) = self.coll_modified_cache.get(&(user_id, collection_id)) {
return Ok(*modified);
}
user_collections::table
.select(user_collections::modified)
.filter(user_collections::user_id.eq(user_id as i32))
.filter(user_collections::collection_id.eq(collection_id))
.first(&self.conn)
.optional()?
.ok_or(DbErrorKind::CollectionNotFound.into())
}
pub fn get_bso_modified_sync(
&self,
user_id: u32,
collection_id: i32,
bso_id: &str,
) -> Result<i64> {
bso::table
.select(bso::modified)
.filter(bso::user_id.eq(user_id as i32))
.filter(bso::collection_id.eq(&collection_id))
.filter(bso::id.eq(&bso_id))
.first(&self.conn)
.optional()?
.ok_or(DbErrorKind::ItemNotFound.into())
}
pub fn get_collections_modified_sync(
&self,
params: ¶ms::GetCollections,
) -> Result<results::GetCollections> {
let modifieds =
sql_query("SELECT collection_id, modified FROM user_collections WHERE user_id = ?")
.bind::<Integer, _>(params.user_id.legacy_id as i32)
.load::<UserCollectionsResult>(&self.conn)?
.into_iter()
.map(|cr| (cr.collection_id, cr.modified))
.collect();
self.map_collection_names(modifieds)
}
fn map_collection_names<T>(&self, by_id: HashMap<i32, T>) -> Result<HashMap<String, T>> {
let names = self.load_collection_names(&by_id.keys().cloned().collect::<Vec<_>>())?;
by_id
.into_iter()
.map(|(id, value)| {
names
.get(&id)
.map(|name| (name.to_owned(), value))
.ok_or(DbError::internal("load_collection_names get"))
}).collect()
}
fn load_collection_names(&self, collection_ids: &[i32]) -> Result<HashMap<i32, String>> {
let mut names = HashMap::new();
let mut uncached = Vec::new();
for &id in collection_ids {
if let Some(name) = self.coll_cache.get_name(id)? {
names.insert(id, name);
} else {
uncached.push(id);
}
}
let result = collections::table
.select((collections::id, collections::name))
.filter(collections::id.eq_any(uncached))
.load::<(i32, String)>(&self.conn)?;
for (id, name) in result {
names.insert(id, name.clone());
self.coll_cache.put(id, name)?;
}
Ok(names)
}
pub(super) fn touch_collection(
&self,
user_id: u32,
collection_id: i32,
modified: i64,
) -> Result<()> {
let upsert = r#"
INSERT INTO user_collections (user_id, collection_id, modified)
VALUES (?, ?, ?)
ON DUPLICATE KEY UPDATE modified = ?
"#;
sql_query(upsert)
.bind::<Integer, _>(user_id as i32)
.bind::<Integer, _>(&collection_id)
.bind::<BigInt, _>(&modified)
.bind::<BigInt, _>(&modified)
.execute(&self.conn)?;
Ok(())
}
pub fn get_collection_counts_sync(&self, user_id: u32) -> Result<results::GetCollectionCounts> {
let counts = bso::table
.select((bso::collection_id, sql::<BigInt>("COUNT(collection_id)")))
.filter(bso::user_id.eq(user_id as i32))
.filter(bso::expiry.gt(&ms_since_epoch()))
.group_by(bso::collection_id)
.load(&self.conn)?
.into_iter()
.collect();
self.map_collection_names(counts)
}
pub fn get_collection_sizes_sync(&self, user_id: u32) -> Result<results::GetCollectionCounts> {
let counts = bso::table
.select((bso::collection_id, sql::<BigInt>("SUM(payload_size)")))
.filter(bso::user_id.eq(user_id as i32))
.filter(bso::expiry.gt(&ms_since_epoch()))
.group_by(bso::collection_id)
.load(&self.conn)?
.into_iter()
.collect();
self.map_collection_names(counts)
}
}
impl Db for MysqlDb {
mock_db_method!(get_collection_id, GetCollectionId);
mock_db_method!(get_collections, GetCollections);
mock_db_method!(get_collection_counts, GetCollectionCounts);
mock_db_method!(get_collection_usage, GetCollectionUsage);
mock_db_method!(get_quota, GetQuota);
mock_db_method!(delete_all, DeleteAll);
mock_db_method!(delete_collection, DeleteCollection);
mock_db_method!(get_collection, GetCollection);
mock_db_method!(post_collection, PostCollection);
mock_db_method!(delete_bso, DeleteBso);
mock_db_method!(get_bso, GetBso);
mock_db_method!(put_bso, PutBso);
}
#[derive(Debug, QueryableByName)]
struct IdResult {
#[sql_type = "Integer"]
id: i32,
}
#[allow(dead_code)] // Not really dead, Rust can't see the use above
#[derive(Debug, QueryableByName)]
struct NameResult {
#[sql_type = "Text"]
name: String,
}
#[derive(Debug, QueryableByName)]
struct UserCollectionsResult {
#[sql_type = "Integer"]
collection_id: i32,
#[sql_type = "BigInt"]
modified: i64,
}
#[derive(Debug, QueryableByName)]
struct Count {
#[sql_type = "BigInt"]
count: i64,
}
/// Formats a BSO for UPDATEs
#[derive(AsChangeset)]
#[table_name = "bso"]
struct UpdateBSO<'a> {
pub sortindex: Option<i32>,
pub payload: Option<&'a str>,
pub payload_size: Option<i32>,
pub modified: Option<i64>,
pub expiry: Option<i64>,
}
fn put_bso_as_changeset<'a>(bso: &'a params::PutBso) -> UpdateBSO<'a> {
UpdateBSO {
sortindex: bso.sortindex,
expiry: bso.ttl.map(|ttl| bso.modified + ttl as i64),
payload: bso.payload.as_ref().map(|payload| &**payload),
payload_size: bso.payload.as_ref().map(|payload| payload.len() as i32), // XXX:
modified: if bso.payload.is_some() || bso.sortindex.is_some() {
Some(bso.modified)
} else {
None
},
}
}