diff --git a/syncserver/src/web/extractors/bso_query_params.rs b/syncserver/src/web/extractors/bso_query_params.rs index b497c292..245f543a 100644 --- a/syncserver/src/web/extractors/bso_query_params.rs +++ b/syncserver/src/web/extractors/bso_query_params.rs @@ -1,4 +1,4 @@ -use std::{num::ParseIntError, str::FromStr}; +use std::str::FromStr; use actix_web::{Error, FromRequest, HttpRequest, dev::Payload, web::Query}; use futures::future::{LocalBoxFuture, TryFutureExt}; @@ -13,45 +13,6 @@ use syncstorage_db::{Sorting, SyncTimestamp, params}; use super::{BATCH_MAX_IDS, RequestErrorLocation, VALID_ID_REGEX, request_error}; use crate::web::error::ValidationErrorKind; -#[derive(Debug, Default, Clone, Copy, Deserialize, Eq, PartialEq, Validate)] -#[serde(default)] -pub struct Offset { - pub timestamp: Option, - pub offset: u64, -} - -impl From for params::Offset { - fn from(offset: Offset) -> Self { - Self { - timestamp: offset.timestamp, - offset: offset.offset, - } - } -} - -impl FromStr for Offset { - type Err = ParseIntError; - fn from_str(s: &str) -> Result { - let result = match s.chars().position(|c| c == ':') { - None => Offset { - timestamp: None, - offset: s.parse::()?, - }, - Some(_colon_position) => { - let mut parts = s.split(':'); - let timestamp_string = parts.next().unwrap_or("0"); - let timestamp = SyncTimestamp::from_milliseconds(timestamp_string.parse::()?); - let offset = parts.next().unwrap_or("0").parse::()?; - Offset { - timestamp: Some(timestamp), - offset, - } - } - }; - Ok(result) - } -} - /// Verifies that the list of id's is not too long and that the ids are valid pub fn validate_qs_ids(ids: &[String]) -> Result<(), ValidationError> { if ids.len() > BATCH_MAX_IDS { @@ -87,13 +48,15 @@ where } } -pub fn deserialize_offset<'de, D>(deserializer: D) -> Result, D::Error> +pub fn deserialize_offset<'de, D>(deserializer: D) -> Result, D::Error> where D: Deserializer<'de>, { let maybe_str: Option = Deserialize::deserialize(deserializer)?; if let Some(val) = maybe_str { - return Ok(Some(Offset::from_str(&val).map_err(SerdeError::custom)?)); + return Ok(Some( + params::Offset::from_str(&val).map_err(SerdeError::custom)?, + )); } Ok(None) } @@ -154,7 +117,7 @@ pub struct BsoQueryParams { /// position at which to restart search (string) #[serde(deserialize_with = "deserialize_offset")] - pub offset: Option, + pub offset: Option, /// a comma-separated list of BSO ids (list of strings) #[serde(deserialize_with = "deserialize_comma_sep_string", default)] @@ -235,7 +198,7 @@ mod tests { use syncstorage_db::{Sorting, SyncTimestamp, params}; - use super::{BsoQueryParams, Offset}; + use super::{BsoQueryParams, params::Offset}; use crate::web::extractors::test_utils::{extract_body_as_str, make_state}; #[test] diff --git a/syncserver/src/web/handlers.rs b/syncserver/src/web/handlers.rs index 725f3a04..421ad7d6 100644 --- a/syncserver/src/web/handlers.rs +++ b/syncserver/src/web/handlers.rs @@ -311,7 +311,7 @@ pub async fn get_collection( older: coll.query.older, sort: coll.query.sort, limit: coll.query.limit, - offset: coll.query.offset.map(Into::into), + offset: coll.query.offset, ids: coll.query.ids.clone(), full: coll.query.full, collection: coll.collection.clone(), diff --git a/syncstorage-db-common/src/params.rs b/syncstorage-db-common/src/params.rs index ed104848..4d2a1152 100644 --- a/syncstorage-db-common/src/params.rs +++ b/syncstorage-db-common/src/params.rs @@ -60,7 +60,7 @@ uid_data! { DeleteStorage, } -#[derive(Debug, Default, Clone)] +#[derive(Debug, Default, Clone, Copy, Eq, PartialEq)] pub struct Offset { pub timestamp: Option, pub offset: u64, diff --git a/syncstorage-spanner/src/db/db_impl.rs b/syncstorage-spanner/src/db/db_impl.rs index 10266fb0..7ad14a44 100644 --- a/syncstorage-spanner/src/db/db_impl.rs +++ b/syncstorage-spanner/src/db/db_impl.rs @@ -662,7 +662,7 @@ impl Db for SpannerDb { AND collection_id = @collection_id AND expiry > CURRENT_TIMESTAMP()"; let limit = params.limit.map(i64::from).unwrap_or(-1); - let params::Offset { offset, timestamp } = params.offset.clone().unwrap_or_default(); + let params::Offset { offset, timestamp } = params.offset.unwrap_or_default(); let sort = params.sort; let mut streaming = self.bsos_query(query, params).await?; @@ -694,7 +694,7 @@ impl Db for SpannerDb { async fn get_bso_ids(&mut self, params: params::GetBsos) -> DbResult { let limit = params.limit.map(i64::from).unwrap_or(-1); - let params::Offset { offset, timestamp } = params.offset.clone().unwrap_or_default(); + let params::Offset { offset, timestamp } = params.offset.unwrap_or_default(); let sort = params.sort; let query = "\