Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
110 changes: 106 additions & 4 deletions syncstorage-postgres/src/db/db_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,17 @@ use diesel::{
};
use diesel_async::{AsyncConnection, RunQueryDsl, TransactionManager};
use futures::TryStreamExt;
use std::collections::HashMap;
use syncstorage_db_common::{
error::DbErrorIntrospect, params, results, util::SyncTimestamp, Db, Sorting,
error::DbErrorIntrospect, params, results, util::SyncTimestamp, Db, Sorting, DEFAULT_BSO_TTL,
};
use syncstorage_settings::Quota;

use super::PgDb;
use crate::{
bsos_query,
db::CollectionLock,
orm_models::BsoChangeset,
pool::Conn,
schema::{bsos, user_collections},
DbError, DbResult,
Expand Down Expand Up @@ -353,7 +355,28 @@ impl Db for PgDb {
}

async fn post_bsos(&mut self, params: params::PostBsos) -> DbResult<results::PostBsos> {
todo!()
let collection_id = self.get_or_create_collection_id(&params.collection).await?;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since we have the collection id here, let's pass it to put_bso instead of repeatedly call get_or_create_collection there.

let modified = self.timestamp();

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why couldn't we have a quota check here and error out early?

for pbso in params.bsos {
self.put_bso(params::PutBso {
user_id: params.user_id.clone(),
collection: params.collection.clone(),
id: pbso.id.clone(),
payload: pbso.payload,
sortindex: pbso.sortindex,
ttl: pbso.ttl,
})
.await?;
}
self.update_collection(params::UpdateCollection {
user_id: params.user_id,
collection_id,
collection: params.collection,
})
.await?;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is already called in put_bso.


Ok(modified)
}

async fn delete_bso(&mut self, params: params::DeleteBso) -> DbResult<results::DeleteBso> {
Expand Down Expand Up @@ -413,8 +436,87 @@ impl Db for PgDb {
Ok(modified)
}

async fn put_bso(&mut self, params: params::PutBso) -> DbResult<results::PutBso> {
todo!()
async fn put_bso(&mut self, bso: params::PutBso) -> DbResult<results::PutBso> {
let collection_id = self.get_or_create_collection_id(&bso.collection).await?;
let user_id: u64 = bso.user_id.legacy_id;
let timestamp = self.timestamp().as_i64();
if self.quota.enabled {
let usage = self
.get_quota_usage(params::GetQuotaUsage {
user_id: bso.user_id.clone(),
collection: bso.collection.clone(),
collection_id,
})
.await?;
if usage.total_bytes >= self.quota.size {
let mut tags = HashMap::default();
tags.insert("collection".to_owned(), bso.collection.clone());
self.metrics.incr_with_tags("storage.quota.at_limit", tags);
if self.quota.enforced {
return Err(DbError::quota());
} else {
warn!("Quota at limit for user's collection ({} bytes)", usage.total_bytes; "collection"=>bso.collection.clone());
}
}
}

let payload = bso.payload.as_deref().unwrap_or_default();
let sortindex = bso.sortindex;
let ttl = bso.ttl.map_or(DEFAULT_BSO_TTL, |ttl| ttl);

// original required millisecond conversion
let expiry_ts = SyncTimestamp::from_i64(timestamp + (i64::from(ttl) * 1000))?;
let modified_ts = SyncTimestamp::from_i64(timestamp)?;

let expiry_dt = expiry_ts.as_naive_datetime()?;
let modified_dt = modified_ts.as_naive_datetime()?;
Comment on lines +467 to +472
Copy link
Member

@pjenvey pjenvey Dec 8, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is simpler/more explicit going through chrono:

Suggested change
// original required millisecond conversion
let expiry_ts = SyncTimestamp::from_i64(timestamp + (i64::from(ttl) * 1000))?;
let modified_ts = SyncTimestamp::from_i64(timestamp)?;
let expiry_dt = expiry_ts.as_naive_datetime()?;
let modified_dt = modified_ts.as_naive_datetime()?;
let modified = self.timestamp().as_naive_datetime()?;
let expiry = modified_dt + TimeDelta::seconds(ttl as i64);


// The changeset utilizes Diesel's `AsChangeset` trait.
// This allows selective updates of fields if and only if they are `Some(<T>)`
let changeset = BsoChangeset {
sortindex: if bso.sortindex.is_some() {
Some(sortindex)
} else {
None
},
payload: if bso.payload.is_some() {
Some(payload)
} else {
None
},
Comment on lines +482 to +486
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
payload: if bso.payload.is_some() {
Some(payload)
} else {
None
},
payload: bso.payload.as_deref(),

expiry: if bso.ttl.is_some() {
Some(expiry_dt)
} else {
None
},
modified: if bso.payload.is_some() || bso.sortindex.is_some() {
Some(modified_dt)
} else {
None
},
Comment on lines +487 to +496
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: you could use map and/or then_some for these

Suggested change
expiry: if bso.ttl.is_some() {
Some(expiry_dt)
} else {
None
},
modified: if bso.payload.is_some() || bso.sortindex.is_some() {
Some(modified_dt)
} else {
None
},
modified: (bso.payload.is_some() || bso.sortindex.is_some()).then_some(modified),
expiry: bso.ttl.map(|_| expiry_dt),

};
diesel::insert_into(bsos::table)
.values((
bsos::user_id.eq(user_id as i64),
bsos::collection_id.eq(&collection_id),
bsos::bso_id.eq(&bso.id),
bsos::sortindex.eq(sortindex),
bsos::payload.eq(payload),
bsos::modified.eq(modified_dt),
bsos::expiry.eq(expiry_dt),
))
.on_conflict((bsos::user_id, bsos::collection_id, bsos::bso_id))
.do_update()
.set(changeset)
.execute(&mut self.conn)
.await?;

self.update_collection(params::UpdateCollection {
user_id: bso.user_id,
collection_id,
collection: bso.collection,
})
.await
}

async fn get_collection_id(&mut self, name: &str) -> DbResult<results::GetCollectionId> {
Expand Down
1 change: 1 addition & 0 deletions syncstorage-postgres/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#[macro_use]
extern crate diesel;
extern crate diesel_migrations;
#[macro_use]
extern crate slog_scope;

mod db;
Expand Down
11 changes: 10 additions & 1 deletion syncstorage-postgres/src/orm_models.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use chrono::NaiveDateTime;

use crate::schema::{batch_bsos, batches, bsos, collections, user_collections};
use diesel::{Identifiable, Queryable};
use diesel::{AsChangeset, Identifiable, Queryable};

#[allow(clippy::all)]
#[derive(Queryable, Debug, Identifiable)]
Expand Down Expand Up @@ -38,6 +38,15 @@ pub struct Bso {
pub expiry: NaiveDateTime,
}

#[derive(AsChangeset)]
#[diesel(table_name = bsos)]
pub struct BsoChangeset<'a> {
pub sortindex: Option<Option<i32>>,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think you're able to actually NULL out sortindex on update

Suggested change
pub sortindex: Option<Option<i32>>,
pub sortindex: Option<i32>,

pub payload: Option<&'a str>,
pub modified: Option<NaiveDateTime>,
pub expiry: Option<NaiveDateTime>,
}

#[derive(Queryable, Debug, Identifiable)]
#[diesel(primary_key(collection_id))]
pub struct Collection {
Expand Down