From c4a1b68bacc1eccacb894c6dacee4a012f0e1175 Mon Sep 17 00:00:00 2001 From: Philip Jenvey Date: Thu, 25 Oct 2018 16:26:03 -0700 Subject: [PATCH] feat: plugin and cleanup various trait methods into MysqlDb: - kills get_collection_id (only used within the db layer now) - simplify uid_data! (just alias HawkIdentifier) - phrasing Issue #65 --- .travis.yml | 3 +- src/db/mock.rs | 36 +++++-------- src/db/mod.rs | 30 +++++------ src/db/mysql/models.rs | 119 ++++++++++++++++++++++++++--------------- src/db/mysql/test.rs | 71 +++++++++++++++--------- src/db/params.rs | 17 +++--- src/db/results.rs | 19 ++++--- src/server/test.rs | 12 ++--- src/web/handlers.rs | 42 +++++++-------- 9 files changed, 191 insertions(+), 158 deletions(-) diff --git a/.travis.yml b/.travis.yml index e1a5012fa5..1a13f181c2 100644 --- a/.travis.yml +++ b/.travis.yml @@ -21,7 +21,8 @@ script: - cargo fmt -- --check - cargo build - cargo test - - ./scripts/build-docs.sh + - # XXX: 1.3.0 rustdoc breakage https://github.com/rust-lang/rust/issues/54524 + - ./scripts/build-docs.sh | true notifications: email: false diff --git a/src/db/mock.rs b/src/db/mock.rs index a792e637cc..844db473ea 100644 --- a/src/db/mock.rs +++ b/src/db/mock.rs @@ -30,20 +30,14 @@ impl MockDb { macro_rules! mock_db_method { ($name:ident, $type:ident) => { - fn $name(&self, _params: ¶ms::$type) -> DbFuture { - Box::new(future::ok(results::$type::default())) + mock_db_method!($name, $type, results::$type); + }; + ($name:ident, $type:ident, $result:ty) => { + fn $name(&self, _params: params::$type) -> DbFuture<$result> { + let result: $result = Default::default(); + Box::new(future::ok(result)) } - } -} - -// XXX: temporary: takes ownership of params vs mock_db_method taking -// a reference -macro_rules! mock_db_method2 { - ($name:ident, $type:ident) => { - fn $name(&self, _params: params::$type) -> DbFuture { - Box::new(future::ok(results::$type::default())) - } - } + }; } impl Db for MockDb { @@ -55,22 +49,20 @@ impl Db for MockDb { Box::new(future::ok(())) } - mock_db_method2!(lock_for_read, LockCollection); - mock_db_method2!(lock_for_write, LockCollection); - - mock_db_method!(get_collection_id, GetCollectionId); - mock_db_method!(get_collections, GetCollections); + mock_db_method!(lock_for_read, LockCollection); + mock_db_method!(lock_for_write, LockCollection); + mock_db_method!(get_collection_modifieds, GetCollectionModifieds); mock_db_method!(get_collection_counts, GetCollectionCounts); mock_db_method!(get_collection_usage, GetCollectionUsage); mock_db_method!(get_storage_usage, GetStorageUsage); - mock_db_method!(delete_all, DeleteAll); + mock_db_method!(delete_storage, DeleteStorage); mock_db_method!(delete_collection, DeleteCollection); mock_db_method!(get_collection, GetCollection); mock_db_method!(post_collection, PostCollection); + mock_db_method!(delete_bsos, DeleteBsos); mock_db_method!(delete_bso, DeleteBso); - mock_db_method!(get_bso, GetBso); - - mock_db_method2!(put_bso, PutBso); + mock_db_method!(get_bso, GetBso, Option); + mock_db_method!(put_bso, PutBso); } unsafe impl Send for MockDb {} diff --git a/src/db/mod.rs b/src/db/mod.rs index 7aa9c894e9..058561e75b 100644 --- a/src/db/mod.rs +++ b/src/db/mod.rs @@ -51,44 +51,42 @@ pub trait Db: Send { fn rollback(&self) -> DbFuture<()>; - fn get_collection_id( + fn get_collection_modifieds( &self, - params: ¶ms::GetCollectionId, - ) -> DbFuture; - - fn get_collections(&self, params: ¶ms::GetCollections) - -> DbFuture; + params: params::GetCollectionModifieds, + ) -> DbFuture; fn get_collection_counts( &self, - params: ¶ms::GetCollectionCounts, + params: params::GetCollectionCounts, ) -> DbFuture; fn get_collection_usage( &self, - params: ¶ms::GetCollectionUsage, + params: params::GetCollectionUsage, ) -> DbFuture; fn get_storage_usage( &self, - params: ¶ms::GetStorageUsage, + params: params::GetStorageUsage, ) -> DbFuture; - fn delete_all(&self, params: ¶ms::DeleteAll) -> DbFuture; + fn delete_storage(&self, params: params::DeleteStorage) -> DbFuture; fn delete_collection( &self, - params: ¶ms::DeleteCollection, + params: params::DeleteCollection, ) -> DbFuture; - fn get_collection(&self, params: ¶ms::GetCollection) -> DbFuture; + fn delete_bsos(&self, params: params::DeleteBsos) -> DbFuture; + + fn delete_bso(&self, params: params::DeleteBso) -> DbFuture; - fn post_collection(&self, params: ¶ms::PostCollection) - -> DbFuture; + fn get_collection(&self, params: params::GetCollection) -> DbFuture; - fn delete_bso(&self, params: ¶ms::DeleteBso) -> DbFuture; + fn post_collection(&self, params: params::PostCollection) -> DbFuture; - fn get_bso(&self, params: ¶ms::GetBso) -> DbFuture; + fn get_bso(&self, params: params::GetBso) -> DbFuture>; fn put_bso(&self, params: params::PutBso) -> DbFuture; } diff --git a/src/db/mysql/models.rs b/src/db/mysql/models.rs index a99cfe8adc..418ef6a666 100644 --- a/src/db/mysql/models.rs +++ b/src/db/mysql/models.rs @@ -249,7 +249,8 @@ impl MysqlDb { .rollback_transaction(&self.conn)?) } - pub fn delete_storage_sync(&self, user_id: u32) -> Result<()> { + pub fn delete_storage_sync(&self, user_id: HawkIdentifier) -> Result<()> { + let user_id = user_id.legacy_id; delete(bso::table) .filter(bso::user_id.eq(user_id as i32)) .execute(&self.conn)?; @@ -259,8 +260,9 @@ impl MysqlDb { Ok(()) } - pub fn delete_collection_sync(&self, user_id: u32, collection: &str) -> Result { - let collection_id = self.get_collection_id(collection)?; + pub fn delete_collection_sync(&self, params: params::DeleteCollection) -> Result { + let user_id = params.user_id.legacy_id; + let collection_id = self.get_collection_id(¶ms.collection)?; let mut count = delete(bso::table) .filter(bso::user_id.eq(user_id as i32)) .filter(bso::collection_id.eq(&collection_id)) @@ -272,7 +274,7 @@ impl MysqlDb { if count == 0 { Err(DbErrorKind::CollectionNotFound)? } - self.get_storage_modified_sync(user_id) + self.get_storage_modified_sync(user_id as u32) } pub(super) fn create_collection(&self, name: &str) -> Result { @@ -401,8 +403,13 @@ impl MysqlDb { // 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: + .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)) @@ -444,33 +451,39 @@ impl MysqlDb { }) } - pub fn get_bso_sync(&self, params: ¶ms::GetBso) -> Result> { - let collection_id = self.get_collection_id(¶ms.collection)?; + pub fn get_bso_sync(&self, params: params::GetBso) -> Result> { 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::(user_id as i32) // XXX: - .bind::(&collection_id) - .bind::(¶ms.id) - .bind::(&self.timestamp()) - .get_result::(&self.conn) - .optional()?) + let collection_id = self.get_collection_id(¶ms.collection)?; + let q = r#" + SELECT id, modified, payload, sortindex, expiry FROM bso + WHERE user_id = ? AND collection_id = ? AND id = ? AND expiry >= ? + "#; + Ok(sql_query(q) + .bind::(user_id as i32) // XXX: + .bind::(&collection_id) + .bind::(¶ms.id) + .bind::(&self.timestamp()) + .get_result::(&self.conn) + .optional()?) } - pub fn delete_bso_sync(&self, user_id: u32, collection: &str, bso_id: &str) -> Result { - self.delete_bsos_sync(user_id, collection, &[bso_id]) + pub fn delete_bso_sync(&self, params: params::DeleteBso) -> Result { + self.delete_bsos_sync(params::DeleteBsos { + user_id: params.user_id, + collection: params.collection, + ids: vec![params.id], + }) } - pub fn delete_bsos_sync(&self, user_id: u32, collection: &str, bso_id: &[&str]) -> Result { - let collection_id = self.get_collection_id(collection)?; + pub fn delete_bsos_sync(&self, params: params::DeleteBsos) -> Result { + let user_id = params.user_id.legacy_id; + let collection_id = self.get_collection_id(¶ms.collection)?; 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)) + .filter(bso::id.eq_any(params.ids)) .execute(&self.conn)?; - self.touch_collection(user_id, collection_id) + self.touch_collection(user_id as u32, collection_id) } pub fn post_bsos_sync( @@ -551,13 +564,13 @@ impl MysqlDb { .ok_or(DbErrorKind::ItemNotFound.into()) } - pub fn get_collections_modified_sync( + pub fn get_collection_modifieds_sync( &self, - params: ¶ms::GetCollections, - ) -> Result { + user_id: HawkIdentifier, + ) -> Result { let modifieds = sql_query("SELECT collection_id, modified FROM user_collections WHERE user_id = ?") - .bind::(params.user_id.legacy_id as i32) + .bind::(user_id.legacy_id as i32) .load::(&self.conn)? .into_iter() .map(|cr| (cr.collection_id, cr.modified)) @@ -566,7 +579,7 @@ impl MysqlDb { } fn map_collection_names(&self, by_id: HashMap) -> Result> { - let names = self.load_collection_names(&by_id.keys().cloned().collect::>())?; + let names = self.load_collection_names(by_id.keys())?; by_id .into_iter() .map(|(id, value)| { @@ -577,7 +590,10 @@ impl MysqlDb { }).collect() } - fn load_collection_names(&self, collection_ids: &[i32]) -> Result> { + fn load_collection_names<'a>( + &self, + collection_ids: impl Iterator, + ) -> Result> { let mut names = HashMap::new(); let mut uncached = Vec::new(); for &id in collection_ids { @@ -615,7 +631,7 @@ impl MysqlDb { Ok(self.timestamp()) } - pub fn get_storage_size_sync( + pub fn get_storage_usage_sync( &self, user_id: HawkIdentifier, ) -> Result { @@ -627,10 +643,10 @@ impl MysqlDb { Ok(total_size as u64) } - pub fn get_collection_sizes_sync( + pub fn get_collection_usage_sync( &self, user_id: HawkIdentifier, - ) -> Result { + ) -> Result { let counts = bso::table .select((bso::collection_id, sql::("SUM(LENGTH(payload))"))) .filter(bso::user_id.eq(user_id.legacy_id as i32)) @@ -669,14 +685,17 @@ impl MysqlDb { macro_rules! sync_db_method { ($name:ident, $sync_name:ident, $type:ident) => { - fn $name(&self, params: params::$type) -> DbFuture { + sync_db_method!($name, $sync_name, $type, results::$type); + }; + ($name:ident, $sync_name:ident, $type:ident, $result:ty) => { + fn $name(&self, params: params::$type) -> DbFuture<$result> { let db = self.clone(); Box::new( self.thread_pool .spawn_handle(lazy(move || future::result(db.$sync_name(params)))), ) } - } + }; } impl Db for MysqlDb { @@ -698,19 +717,31 @@ impl Db for MysqlDb { sync_db_method!(lock_for_read, lock_for_read_sync, LockCollection); sync_db_method!(lock_for_write, lock_for_write_sync, LockCollection); + sync_db_method!( + get_collection_modifieds, + get_collection_modifieds_sync, + GetCollectionModifieds + ); + sync_db_method!( + get_collection_counts, + get_collection_counts_sync, + GetCollectionCounts + ); + sync_db_method!( + get_collection_usage, + get_collection_usage_sync, + GetCollectionUsage + ); + sync_db_method!(get_storage_usage, get_storage_usage_sync, GetStorageUsage); + sync_db_method!(delete_storage, delete_storage_sync, DeleteStorage); + sync_db_method!(delete_collection, delete_collection_sync, DeleteCollection); - 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_storage_usage, GetStorageUsage); - 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); + sync_db_method!(delete_bsos, delete_bsos_sync, DeleteBsos); + sync_db_method!(delete_bso, delete_bso_sync, DeleteBso); + sync_db_method!(get_bso, get_bso_sync, GetBso, Option); sync_db_method!(put_bso, put_bso_sync, PutBso); } diff --git a/src/db/mysql/test.rs b/src/db/mysql/test.rs index c0e0fb1e30..743adfb6c1 100644 --- a/src/db/mysql/test.rs +++ b/src/db/mysql/test.rs @@ -88,6 +88,22 @@ fn gbso(user_id: u32, coll: &str, bid: &str) -> params::GetBso { } } +fn dbso(user_id: u32, coll: &str, bid: &str) -> params::DeleteBso { + params::DeleteBso { + user_id: HawkIdentifier::new_legacy(user_id as u64), + collection: coll.to_owned(), + id: bid.to_owned(), + } +} + +fn dbsos(user_id: u32, coll: &str, bids: &[&str]) -> params::DeleteBsos { + params::DeleteBsos { + user_id: HawkIdentifier::new_legacy(user_id as u64), + collection: coll.to_owned(), + ids: bids.into_iter().map(|id| id.to_owned().into()).collect(), + } +} + fn hid(user_id: u32) -> HawkIdentifier { HawkIdentifier::new_legacy(user_id as u64) } @@ -156,7 +172,7 @@ fn bso_successfully_updates_single_values() -> Result<()> { let bso2 = pbso(uid, coll, bid, Some(payload), None, None); db.put_bso_sync(bso2)?; - let bso = db.get_bso_sync(&gbso(uid, coll, bid))?.unwrap(); + let bso = db.get_bso_sync(gbso(uid, coll, bid))?.unwrap(); assert_eq!(bso.modified, db.timestamp()); assert_eq!(bso.payload, payload); assert_eq!(bso.sortindex, Some(sortindex)); @@ -167,7 +183,7 @@ fn bso_successfully_updates_single_values() -> Result<()> { let sortindex = 2; let bso2 = pbso(uid, coll, bid, None, Some(sortindex), None); db.put_bso_sync(bso2)?; - let bso = db.get_bso_sync(&gbso(uid, coll, bid))?.unwrap(); + let bso = db.get_bso_sync(gbso(uid, coll, bid))?.unwrap(); assert_eq!(bso.modified, db.timestamp()); assert_eq!(bso.payload, payload); assert_eq!(bso.sortindex, Some(sortindex)); @@ -194,7 +210,7 @@ fn bso_modified_not_changed_on_ttl_touch() -> Result<()> { let bso2 = pbso(uid, coll, bid, None, None, Some(15)); db.put_bso_sync(bso2)?; - let bso = db.get_bso_sync(&gbso(uid, coll, bid))?.unwrap(); + let bso = db.get_bso_sync(gbso(uid, coll, bid))?.unwrap(); // ttl has changed assert_eq!(bso.expiry, timestamp + 15); // modified has not changed @@ -217,7 +233,7 @@ fn put_bso_updates() -> Result<()> { let bso2 = pbso(uid, coll, bid, Some(payload), Some(sortindex), None); db.put_bso_sync(bso2)?; - let bso = db.get_bso_sync(&gbso(uid, coll, bid))?.unwrap(); + let bso = db.get_bso_sync(gbso(uid, coll, bid))?.unwrap(); assert_eq!(Some(bso.payload), Some(payload.to_owned())); assert_eq!(bso.sortindex, Some(sortindex)); assert_eq!(bso.modified, db.timestamp()); @@ -447,8 +463,8 @@ fn delete_bsos_in_correct_collection() -> Result<()> { let payload = "data"; db.put_bso_sync(pbso(uid, "clients", "b1", Some(payload), None, None))?; db.put_bso_sync(pbso(uid, "crypto", "b1", Some(payload), None, None))?; - db.delete_bsos_sync(uid, "clients", &["b1"])?; - let bso = db.get_bso_sync(&gbso(uid, "crypto", "b1"))?; + db.delete_bsos_sync(dbsos(uid, "clients", &["b1"]))?; + let bso = db.get_bso_sync(gbso(uid, "crypto", "b1"))?; assert!(bso.is_some()); Ok(()) } @@ -507,13 +523,16 @@ fn delete_collection() -> Result<()> { for bid in 1..=3 { db.put_bso_sync(pbso(uid, coll, &bid.to_string(), Some("test"), None, None))?; } - let modified = db.delete_collection_sync(uid, coll)?; + let modified = db.delete_collection_sync(params::DeleteCollection { + user_id: hid(uid), + collection: coll.to_owned(), + })?; let modified2 = db.get_storage_modified_sync(uid)?; assert_eq!(modified2, modified); // make sure BSOs are deleted for bid in 1..=3 { - let result = db.get_bso_sync(&gbso(uid, coll, &bid.to_string()))?; + let result = db.get_bso_sync(gbso(uid, coll, &bid.to_string()))?; assert!(result.is_none()); } @@ -533,7 +552,7 @@ fn get_collections_modified() -> Result<()> { let coll = "test"; let cid = db.create_collection(coll)?; db.touch_collection(uid, cid)?; - let cols = db.get_collections_modified_sync(¶ms::GetCollections { user_id: hid(uid) })?; + let cols = db.get_collection_modifieds_sync(hid(uid))?; assert!(cols.contains_key(coll)); assert_eq!(cols.get(coll), Some(&db.timestamp())); @@ -543,7 +562,7 @@ fn get_collections_modified() -> Result<()> { } #[test] -fn get_collection_sizes() -> Result<()> { +fn get_collection_usage() -> Result<()> { let db = db()?; let uid = 1; @@ -569,9 +588,9 @@ fn get_collection_sizes() -> Result<()> { } } - let sizes = db.get_collection_sizes_sync(hid(uid))?; + let sizes = db.get_collection_usage_sync(hid(uid))?; assert_eq!(sizes, expected); - let total = db.get_storage_size_sync(hid(uid))?; + let total = db.get_storage_usage_sync(hid(uid))?; assert_eq!(total, expected.values().sum::() as u64); Ok(()) } @@ -609,7 +628,7 @@ fn put_bso() -> Result<()> { let modified = db.get_collection_modified_sync(uid, coll)?; assert_eq!(modified, db.timestamp()); - let bso = db.get_bso_sync(&gbso(uid, coll, bid))?.unwrap(); + let bso = db.get_bso_sync(gbso(uid, coll, bid))?.unwrap(); assert_eq!(&bso.payload, "foo"); assert_eq!(bso.sortindex, Some(1)); @@ -619,7 +638,7 @@ fn put_bso() -> Result<()> { let modified = db.get_collection_modified_sync(uid, coll)?; assert_eq!(modified, db.timestamp()); - let bso = db.get_bso_sync(&gbso(uid, coll, bid))?.unwrap(); + let bso = db.get_bso_sync(gbso(uid, coll, bid))?.unwrap(); assert_eq!(&bso.payload, "bar"); assert_eq!(bso.sortindex, Some(2)); Ok(()) @@ -666,10 +685,10 @@ fn post_bsos() -> Result<()> { assert!(result2.success.contains(&"b0".to_owned())); assert!(result2.success.contains(&"b2".to_owned())); - let bso = db.get_bso_sync(&gbso(uid, coll, "b0"))?.unwrap(); + let bso = db.get_bso_sync(gbso(uid, coll, "b0"))?.unwrap(); assert_eq!(bso.sortindex, Some(11)); assert_eq!(bso.payload, "updated 0"); - let bso = db.get_bso_sync(&gbso(uid, coll, "b2"))?.unwrap(); + let bso = db.get_bso_sync(gbso(uid, coll, "b2"))?.unwrap(); assert_eq!(bso.sortindex, Some(22)); assert_eq!(bso.payload, "updated 2"); @@ -688,11 +707,11 @@ fn get_bso() -> Result<()> { let payload = "a"; db.put_bso_sync(pbso(uid, coll, bid, Some(payload), None, None))?; - let bso = db.get_bso_sync(&gbso(uid, coll, bid))?.unwrap(); + let bso = db.get_bso_sync(gbso(uid, coll, bid))?.unwrap(); assert_eq!(bso.id, bid); assert_eq!(bso.payload, payload); - let result = db.get_bso_sync(&gbso(uid, coll, "nope"))?; + let result = db.get_bso_sync(gbso(uid, coll, "nope"))?; assert!(result.is_none()); Ok(()) } @@ -766,8 +785,8 @@ fn delete_bso() -> Result<()> { let coll = "clients"; let bid = "b0"; db.put_bso_sync(pbso(uid, coll, bid, Some("a"), None, None))?; - db.delete_bso_sync(uid, coll, bid)?; - let bso = db.get_bso_sync(&gbso(uid, coll, bid))?; + db.delete_bso_sync(dbso(uid, coll, bid))?; + let bso = db.get_bso_sync(gbso(uid, coll, bid))?; assert!(bso.is_none()); Ok(()) } @@ -789,12 +808,12 @@ fn delete_bsos() -> Result<()> { Some(DEFAULT_BSO_TTL), ))?; } - db.delete_bso_sync(uid, coll, "b0")?; + db.delete_bso_sync(dbso(uid, coll, "b0"))?; // deleting non existant bid returns no errors - db.delete_bso_sync(uid, coll, "bxi0")?; - db.delete_bsos_sync(uid, coll, &["b1", "b2"])?; + db.delete_bso_sync(dbso(uid, coll, "bxi0"))?; + db.delete_bsos_sync(dbsos(uid, coll, &["b1", "b2"]))?; for bid in bids { - let bso = db.get_bso_sync(&gbso(uid, coll, &bid))?; + let bso = db.get_bso_sync(gbso(uid, coll, &bid))?; assert!(bso.is_none()); } Ok(()) @@ -830,8 +849,8 @@ fn delete_storage() -> Result<()> { let cid = db.create_collection(coll)?; db.put_bso_sync(pbso(uid, coll, bid, Some("test"), None, None))?; - db.delete_storage_sync(uid)?; - let result = db.get_bso_sync(&gbso(uid, coll, bid))?; + db.delete_storage_sync(hid(uid))?; + let result = db.get_bso_sync(gbso(uid, coll, bid))?; assert!(result.is_none()); // collection data sticks around diff --git a/src/db/params.rs b/src/db/params.rs index 10b908afeb..fde9abd7d7 100644 --- a/src/db/params.rs +++ b/src/db/params.rs @@ -12,11 +12,7 @@ macro_rules! data { macro_rules! uid_data { ($($name:ident,)+) => ($( - data! { - $name { - user_id: HawkIdentifier, - } - } + pub type $name = HawkIdentifier; )+) } @@ -46,19 +42,18 @@ macro_rules! bso_data { } uid_data! { - GetCollections, + GetCollectionModifieds, GetCollectionCounts, GetCollectionUsage, GetStorageUsage, - DeleteAll, + DeleteStorage, } -pub type GetCollectionId = str; - collection_data! { LockCollection {}, - DeleteCollection { - bso_ids: Vec, + DeleteCollection {}, + DeleteBsos { + ids: Vec, }, GetCollection {}, PostCollection { diff --git a/src/db/results.rs b/src/db/results.rs index 65c229a0b9..8000214424 100644 --- a/src/db/results.rs +++ b/src/db/results.rs @@ -7,15 +7,15 @@ use std::collections::HashMap; use diesel::sql_types::{BigInt, Integer, Nullable, Text}; pub type LockCollection = (); -pub type GetCollectionId = i32; -pub type GetCollections = HashMap; +pub type GetCollectionModifieds = HashMap; pub type GetCollectionCounts = HashMap; -pub type GetCollectionUsage = HashMap; +pub type GetCollectionUsage = HashMap; pub type GetStorageUsage = u64; -pub type DeleteAll = (); +pub type DeleteStorage = (); pub type GetCollection = Vec; -pub type DeleteCollection = Option; -pub type DeleteBso = (); +pub type DeleteCollection = i64; +pub type DeleteBsos = i64; +pub type DeleteBso = i64; pub type PutBso = u64; #[derive(Debug, Deserialize, Serialize)] @@ -49,10 +49,13 @@ pub struct BSOs { pub offset: i64, // XXX: i64? } -#[derive(Debug, Serialize)] -pub struct DeleteBsos { +// XXX: ideally only used by the handlers (could use json! instead?) +/* +#[derive(Debug, Default, Serialize)] +pub struct DeleteBso { modified: u64, } +*/ #[derive(Debug, Default, Deserialize, Serialize)] pub struct PostCollection { diff --git a/src/server/test.rs b/src/server/test.rs index 8a8410779c..6a32a8adac 100644 --- a/src/server/test.rs +++ b/src/server/test.rs @@ -177,13 +177,9 @@ fn delete_all() { #[test] fn delete_collection() { - test_endpoint(http::Method::DELETE, "/42/storage/bookmarks", "null"); - test_endpoint(http::Method::DELETE, "/42/storage/bookmarks?ids=1,", "null"); - test_endpoint( - http::Method::DELETE, - "/42/storage/bookmarks?ids=1,2,3", - "null", - ); + test_endpoint(http::Method::DELETE, "/42/storage/bookmarks", "0"); + test_endpoint(http::Method::DELETE, "/42/storage/bookmarks?ids=1,", "0"); + test_endpoint(http::Method::DELETE, "/42/storage/bookmarks?ids=1,2,3", "0"); } #[test] @@ -216,7 +212,7 @@ fn post_collection() { #[test] fn delete_bso() { - test_endpoint(http::Method::DELETE, "/42/storage/bookmarks/wibble", "null"); + test_endpoint(http::Method::DELETE, "/42/storage/bookmarks/wibble", "0"); } #[test] diff --git a/src/web/handlers.rs b/src/web/handlers.rs index aebfbba189..0de4e9291c 100644 --- a/src/web/handlers.rs +++ b/src/web/handlers.rs @@ -16,9 +16,8 @@ pub fn get_collections(meta: MetaRequest) -> FutureResponse { Box::new( meta.state .db - .get_collections(¶ms::GetCollections { - user_id: meta.user_id, - }).map_err(From::from) + .get_collection_modifieds(meta.user_id) + .map_err(From::from) .map(|result| HttpResponse::Ok().json(result)), ) } @@ -27,9 +26,8 @@ pub fn get_collection_counts(meta: MetaRequest) -> FutureResponse Box::new( meta.state .db - .get_collection_counts(¶ms::GetCollectionCounts { - user_id: meta.user_id, - }).map_err(From::from) + .get_collection_counts(meta.user_id) + .map_err(From::from) .map(|result| HttpResponse::Ok().json(result)), ) } @@ -38,9 +36,8 @@ pub fn get_collection_usage(meta: MetaRequest) -> FutureResponse { Box::new( meta.state .db - .get_collection_usage(¶ms::GetCollectionUsage { - user_id: meta.user_id, - }).map_err(From::from) + .get_collection_usage(meta.user_id) + .map_err(From::from) .map(|result| HttpResponse::Ok().json(result)), ) } @@ -49,9 +46,8 @@ pub fn get_quota(meta: MetaRequest) -> FutureResponse { Box::new( meta.state .db - .get_storage_usage(¶ms::GetStorageUsage { - user_id: meta.user_id, - }).map_err(From::from) + .get_storage_usage(meta.user_id) + .map_err(From::from) .map(|result| HttpResponse::Ok().json(vec![Some(result), None])), ) } @@ -60,9 +56,8 @@ pub fn delete_all(meta: MetaRequest) -> FutureResponse { Box::new( meta.state .db - .delete_all(¶ms::DeleteAll { - user_id: meta.user_id, - }).map_err(From::from) + .delete_storage(meta.user_id) + .map_err(From::from) .map(|result| HttpResponse::Ok().json(result)), ) } @@ -71,14 +66,17 @@ pub fn delete_collection(coll: CollectionRequest) -> FutureResponse FutureResponse { Box::new( coll.state .db - .get_collection(¶ms::GetCollection { + .get_collection(params::GetCollection { user_id: coll.user_id, collection: coll.collection, }).map_err(From::from) @@ -107,7 +105,7 @@ pub fn post_collection( Box::new( state .db - .post_collection(¶ms::PostCollection { + .post_collection(params::PostCollection { user_id: auth, collection: "tabs".to_owned(), bsos: body.into_inner().into_iter().map(From::from).collect(), @@ -121,7 +119,7 @@ pub fn delete_bso(bso_req: BsoRequest) -> FutureResponse { bso_req .state .db - .delete_bso(¶ms::DeleteBso { + .delete_bso(params::DeleteBso { user_id: bso_req.user_id, collection: bso_req.collection, id: bso_req.bso.clone(), @@ -135,12 +133,12 @@ pub fn get_bso(bso_req: BsoRequest) -> FutureResponse { bso_req .state .db - .get_bso(¶ms::GetBso { + .get_bso(params::GetBso { user_id: bso_req.user_id, collection: bso_req.collection, id: bso_req.bso.clone(), }).map_err(From::from) - .map(|result| HttpResponse::Ok().json(result)), + .map(|_result| HttpResponse::Ok().json(::db::results::GetBso::default())), ) }