-
Notifications
You must be signed in to change notification settings - Fork 84
Expand file tree
/
Copy pathlib.rs
More file actions
294 lines (239 loc) · 8.96 KB
/
Copy pathlib.rs
File metadata and controls
294 lines (239 loc) · 8.96 KB
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
#![allow(non_local_definitions)]
pub mod diesel;
pub mod error;
pub mod params;
pub mod results;
pub mod util;
use std::fmt::Debug;
use async_trait::async_trait;
use lazy_static::lazy_static;
use serde::Deserialize;
use syncserver_db_common::GetPoolStatus;
use error::DbErrorIntrospect;
use util::SyncTimestamp;
lazy_static! {
/// For efficiency, it's possible to use fixed pre-determined IDs for
/// common collection names. This is the canonical list of such
/// names. Non-standard collections will be allocated IDs starting
/// from the highest ID in this collection.
pub static ref STD_COLLS: Vec<(i32, &'static str)> = {
vec![
(1, "clients"),
(2, "crypto"),
(3, "forms"),
(4, "history"),
(5, "keys"),
(6, "meta"),
(7, "bookmarks"),
(8, "prefs"),
(9, "tabs"),
(10, "passwords"),
(11, "addons"),
(12, "addresses"),
(13, "creditcards"),
]
};
}
/// Rough guesstimate of the maximum reasonable life span of a batch
pub const BATCH_LIFETIME: i64 = 2 * 60 * 60 * 1000; // 2 hours, in milliseconds
/// The ttl to use for rows that are never supposed to expire (in seconds)
pub const DEFAULT_BSO_TTL: u32 = 2_100_000_000;
/// Non-standard collections will be allocated IDs beginning with this value
pub const FIRST_CUSTOM_COLLECTION_ID: i32 = 101;
#[async_trait]
pub trait DbPool: Sync + Send + Debug + GetPoolStatus {
type Error;
async fn init(&mut self) -> Result<(), Self::Error> {
Ok(())
}
async fn get(&self) -> Result<Box<dyn Db<Error = Self::Error>>, Self::Error>;
fn validate_batch_id(&self, params: params::ValidateBatchId) -> Result<(), Self::Error>;
fn box_clone(&self) -> Box<dyn DbPool<Error = Self::Error>>;
}
impl<E> Clone for Box<dyn DbPool<Error = E>> {
fn clone(&self) -> Box<dyn DbPool<Error = E>> {
self.box_clone()
}
}
#[async_trait(?Send)]
pub trait Db: BatchDb {
async fn lock_for_read(&mut self, params: params::LockCollection) -> Result<(), Self::Error>;
async fn lock_for_write(&mut self, params: params::LockCollection) -> Result<(), Self::Error>;
async fn begin(&mut self, for_write: bool) -> Result<(), Self::Error>;
async fn commit(&mut self) -> Result<(), Self::Error>;
async fn rollback(&mut self) -> Result<(), Self::Error>;
async fn get_collection_timestamps(
&mut self,
params: params::GetCollectionTimestamps,
) -> Result<results::GetCollectionTimestamps, Self::Error>;
async fn get_collection_timestamp(
&mut self,
params: params::GetCollectionTimestamp,
) -> Result<results::GetCollectionTimestamp, Self::Error>;
async fn get_collection_counts(
&mut self,
params: params::GetCollectionCounts,
) -> Result<results::GetCollectionCounts, Self::Error>;
async fn get_collection_usage(
&mut self,
params: params::GetCollectionUsage,
) -> Result<results::GetCollectionUsage, Self::Error>;
async fn get_storage_timestamp(
&mut self,
params: params::GetStorageTimestamp,
) -> Result<results::GetStorageTimestamp, Self::Error>;
async fn get_storage_usage(
&mut self,
params: params::GetStorageUsage,
) -> Result<results::GetStorageUsage, Self::Error>;
async fn get_quota_usage(
&mut self,
params: params::GetQuotaUsage,
) -> Result<results::GetQuotaUsage, Self::Error>;
async fn delete_storage(
&mut self,
params: params::DeleteStorage,
) -> Result<results::DeleteStorage, Self::Error>;
async fn delete_collection(
&mut self,
params: params::DeleteCollection,
) -> Result<results::DeleteCollection, Self::Error>;
async fn delete_bsos(
&mut self,
params: params::DeleteBsos,
) -> Result<results::DeleteBsos, Self::Error>;
async fn get_bsos(&mut self, params: params::GetBsos) -> Result<results::GetBsos, Self::Error>;
async fn get_bso_ids(
&mut self,
params: params::GetBsos,
) -> Result<results::GetBsoIds, Self::Error>;
async fn post_bsos(&mut self, params: params::PostBsos) -> Result<SyncTimestamp, Self::Error>;
async fn delete_bso(
&mut self,
params: params::DeleteBso,
) -> Result<results::DeleteBso, Self::Error>;
async fn get_bso(
&mut self,
params: params::GetBso,
) -> Result<Option<results::GetBso>, Self::Error>;
async fn get_bso_timestamp(
&mut self,
params: params::GetBsoTimestamp,
) -> Result<results::GetBsoTimestamp, Self::Error>;
async fn put_bso(&mut self, params: params::PutBso) -> Result<results::PutBso, Self::Error>;
async fn check(&mut self) -> Result<results::Check, Self::Error>;
fn get_connection_info(&self) -> results::ConnectionInfo;
/// Retrieve the timestamp for an item/collection
async fn extract_resource(
&mut self,
user_id: UserIdentifier,
collection: Option<String>,
bso: Option<String>,
) -> Result<SyncTimestamp, Self::Error> {
match collection {
None => {
// No collection specified, return overall storage timestamp
self.get_storage_timestamp(user_id).await
}
Some(collection) => match bso {
None => self
.get_collection_timestamp(params::GetCollectionTimestamp {
user_id,
collection,
})
.await
.or_else(|e| {
if e.is_collection_not_found() {
Ok(SyncTimestamp::from_seconds(0f64))
} else {
Err(e)
}
}),
Some(bso) => self
.get_bso_timestamp(params::GetBsoTimestamp {
user_id,
collection,
id: bso,
})
.await
.or_else(|e| {
if e.is_collection_not_found() {
Ok(SyncTimestamp::from_seconds(0f64))
} else {
Err(e)
}
}),
},
}
}
// Internal methods used by the db tests
// TODO: should be test only but currently isn't:
// https://github.com/mozilla-services/syncstorage-rs/issues/1959
//#[cfg(debug_assertions)]
async fn get_collection_id(&mut self, name: &str) -> Result<i32, Self::Error>;
#[cfg(debug_assertions)]
async fn create_collection(&mut self, name: &str) -> Result<i32, Self::Error>;
// TODO: all impls rely on this method internally so it can't currently be
// cfg(debug_assertions). they should be refactored to not rely on the
// trait method itself
//#[cfg(debug_assertions)]
async fn update_collection(
&mut self,
params: params::UpdateCollection,
) -> Result<SyncTimestamp, Self::Error>;
#[cfg(debug_assertions)]
fn timestamp(&self) -> SyncTimestamp;
#[cfg(debug_assertions)]
fn set_timestamp(&mut self, timestamp: SyncTimestamp);
#[cfg(debug_assertions)]
async fn clear_coll_cache(&mut self) -> Result<(), Self::Error>;
#[cfg(debug_assertions)]
fn set_quota(&mut self, enabled: bool, limit: usize, enforce: bool);
}
#[async_trait(?Send)]
pub trait BatchDb: Debug {
type Error: DbErrorIntrospect + 'static;
async fn create_batch(
&mut self,
params: params::CreateBatch,
) -> Result<results::CreateBatch, Self::Error>;
async fn validate_batch(
&mut self,
params: params::ValidateBatch,
) -> Result<results::ValidateBatch, Self::Error>;
async fn append_to_batch(
&mut self,
params: params::AppendToBatch,
) -> Result<results::AppendToBatch, Self::Error>;
async fn get_batch(
&mut self,
params: params::GetBatch,
) -> Result<Option<results::GetBatch>, Self::Error>;
async fn commit_batch(
&mut self,
params: params::CommitBatch,
) -> Result<results::CommitBatch, Self::Error>;
async fn delete_batch(&mut self, params: params::DeleteBatch) -> Result<(), Self::Error>;
}
#[derive(Debug, Default, Deserialize, Clone, PartialEq, Eq, Copy)]
#[serde(rename_all = "lowercase")]
pub enum Sorting {
#[default]
None,
Newest,
Oldest,
Index,
}
#[derive(Clone, Debug, Default, Eq, Hash, PartialEq)]
pub struct UserIdentifier {
/// For MySQL/Postgres database backends as the primary key.
pub legacy_id: u64,
/// For NoSQL database backends that require randomly distributed primary keys.
pub fxa_uid: String,
/// Key identifier; part of the sync crypto context.
pub fxa_kid: String,
/// Hash of the Firefox user ID associated with a Sync account.
pub hashed_fxa_uid: String,
/// Hash of the device id metadata.
pub hashed_device_id: String,
}