Skip to content

Commit

Permalink
feat: plugin and cleanup various trait methods into MysqlDb:
Browse files Browse the repository at this point in the history
- kills get_collection_id (only used within the db layer now)
- simplify uid_data! (just alias HawkIdentifier)
- phrasing

Issue #65
  • Loading branch information
pjenvey committed Oct 26, 2018
1 parent 0eba8ae commit c4a1b68
Show file tree
Hide file tree
Showing 9 changed files with 191 additions and 158 deletions.
3 changes: 2 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
36 changes: 14 additions & 22 deletions src/db/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,20 +30,14 @@ impl MockDb {

macro_rules! mock_db_method {
($name:ident, $type:ident) => {
fn $name(&self, _params: &params::$type) -> DbFuture<results::$type> {
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<results::$type> {
Box::new(future::ok(results::$type::default()))
}
}
};
}

impl Db for MockDb {
Expand All @@ -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<results::GetBso>);
mock_db_method!(put_bso, PutBso);
}

unsafe impl Send for MockDb {}
30 changes: 14 additions & 16 deletions src/db/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,44 +51,42 @@ pub trait Db: Send {

fn rollback(&self) -> DbFuture<()>;

fn get_collection_id(
fn get_collection_modifieds(
&self,
params: &params::GetCollectionId,
) -> DbFuture<results::GetCollectionId>;

fn get_collections(&self, params: &params::GetCollections)
-> DbFuture<results::GetCollections>;
params: params::GetCollectionModifieds,
) -> DbFuture<results::GetCollectionModifieds>;

fn get_collection_counts(
&self,
params: &params::GetCollectionCounts,
params: params::GetCollectionCounts,
) -> DbFuture<results::GetCollectionCounts>;

fn get_collection_usage(
&self,
params: &params::GetCollectionUsage,
params: params::GetCollectionUsage,
) -> DbFuture<results::GetCollectionUsage>;

fn get_storage_usage(
&self,
params: &params::GetStorageUsage,
params: params::GetStorageUsage,
) -> DbFuture<results::GetStorageUsage>;

fn delete_all(&self, params: &params::DeleteAll) -> DbFuture<results::DeleteAll>;
fn delete_storage(&self, params: params::DeleteStorage) -> DbFuture<results::DeleteStorage>;

fn delete_collection(
&self,
params: &params::DeleteCollection,
params: params::DeleteCollection,
) -> DbFuture<results::DeleteCollection>;

fn get_collection(&self, params: &params::GetCollection) -> DbFuture<results::GetCollection>;
fn delete_bsos(&self, params: params::DeleteBsos) -> DbFuture<results::DeleteBsos>;

fn delete_bso(&self, params: params::DeleteBso) -> DbFuture<results::DeleteBso>;

fn post_collection(&self, params: &params::PostCollection)
-> DbFuture<results::PostCollection>;
fn get_collection(&self, params: params::GetCollection) -> DbFuture<results::GetCollection>;

fn delete_bso(&self, params: &params::DeleteBso) -> DbFuture<results::DeleteBso>;
fn post_collection(&self, params: params::PostCollection) -> DbFuture<results::PostCollection>;

fn get_bso(&self, params: &params::GetBso) -> DbFuture<results::GetBso>;
fn get_bso(&self, params: params::GetBso) -> DbFuture<Option<results::GetBso>>;

fn put_bso(&self, params: params::PutBso) -> DbFuture<results::PutBso>;
}
Expand Down
119 changes: 75 additions & 44 deletions src/db/mysql/models.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)?;
Expand All @@ -259,8 +260,9 @@ impl MysqlDb {
Ok(())
}

pub fn delete_collection_sync(&self, user_id: u32, collection: &str) -> Result<i64> {
let collection_id = self.get_collection_id(collection)?;
pub fn delete_collection_sync(&self, params: params::DeleteCollection) -> Result<i64> {
let user_id = params.user_id.legacy_id;
let collection_id = self.get_collection_id(&params.collection)?;
let mut count = delete(bso::table)
.filter(bso::user_id.eq(user_id as i32))
.filter(bso::collection_id.eq(&collection_id))
Expand All @@ -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<i32> {
Expand Down Expand Up @@ -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))
Expand Down Expand Up @@ -444,33 +451,39 @@ impl MysqlDb {
})
}

pub fn get_bso_sync(&self, params: &params::GetBso) -> Result<Option<results::GetBso>> {
let collection_id = self.get_collection_id(&params.collection)?;
pub fn get_bso_sync(&self, params: params::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, _>(&collection_id)
.bind::<Text, _>(&params.id)
.bind::<BigInt, _>(&self.timestamp())
.get_result::<results::GetBso>(&self.conn)
.optional()?)
let collection_id = self.get_collection_id(&params.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::<Integer, _>(user_id as i32) // XXX:
.bind::<Integer, _>(&collection_id)
.bind::<Text, _>(&params.id)
.bind::<BigInt, _>(&self.timestamp())
.get_result::<results::GetBso>(&self.conn)
.optional()?)
}

pub fn delete_bso_sync(&self, user_id: u32, collection: &str, bso_id: &str) -> Result<i64> {
self.delete_bsos_sync(user_id, collection, &[bso_id])
pub fn delete_bso_sync(&self, params: params::DeleteBso) -> Result<results::DeleteBso> {
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<i64> {
let collection_id = self.get_collection_id(collection)?;
pub fn delete_bsos_sync(&self, params: params::DeleteBsos) -> Result<results::DeleteBsos> {
let user_id = params.user_id.legacy_id;
let collection_id = self.get_collection_id(&params.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(
Expand Down Expand Up @@ -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: &params::GetCollections,
) -> Result<results::GetCollections> {
user_id: HawkIdentifier,
) -> Result<results::GetCollectionModifieds> {
let modifieds =
sql_query("SELECT collection_id, modified FROM user_collections WHERE user_id = ?")
.bind::<Integer, _>(params.user_id.legacy_id as i32)
.bind::<Integer, _>(user_id.legacy_id as i32)
.load::<UserCollectionsResult>(&self.conn)?
.into_iter()
.map(|cr| (cr.collection_id, cr.modified))
Expand All @@ -566,7 +579,7 @@ impl MysqlDb {
}

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<_>>())?;
let names = self.load_collection_names(by_id.keys())?;
by_id
.into_iter()
.map(|(id, value)| {
Expand All @@ -577,7 +590,10 @@ impl MysqlDb {
}).collect()
}

fn load_collection_names(&self, collection_ids: &[i32]) -> Result<HashMap<i32, String>> {
fn load_collection_names<'a>(
&self,
collection_ids: impl Iterator<Item = &'a i32>,
) -> Result<HashMap<i32, String>> {
let mut names = HashMap::new();
let mut uncached = Vec::new();
for &id in collection_ids {
Expand Down Expand Up @@ -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<results::GetStorageUsage> {
Expand All @@ -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<results::GetCollectionCounts> {
) -> Result<results::GetCollectionUsage> {
let counts = bso::table
.select((bso::collection_id, sql::<BigInt>("SUM(LENGTH(payload))")))
.filter(bso::user_id.eq(user_id.legacy_id as i32))
Expand Down Expand Up @@ -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<results::$type> {
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 {
Expand All @@ -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<results::GetBso>);
sync_db_method!(put_bso, put_bso_sync, PutBso);
}

Expand Down
Loading

0 comments on commit c4a1b68

Please sign in to comment.