From 7de0d3826058758f824a93a409e9856ea3b2f9dd Mon Sep 17 00:00:00 2001 From: Eugenio Tampieri Date: Sun, 22 Aug 2021 12:01:49 +0200 Subject: [PATCH 1/9] Created convert crate --- Cargo.toml | 2 +- convert/Cargo.toml | 10 ++++++++++ convert/src/lib.rs | 27 +++++++++++++++++++++++++++ convert/src/mongo.rs | 14 ++++++++++++++ convert/src/proto.rs | 13 +++++++++++++ 5 files changed, 65 insertions(+), 1 deletion(-) create mode 100644 convert/Cargo.toml create mode 100644 convert/src/lib.rs create mode 100644 convert/src/mongo.rs create mode 100644 convert/src/proto.rs diff --git a/Cargo.toml b/Cargo.toml index c435115..3f03220 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,2 +1,2 @@ [workspace] -members = ["protos", "contest_service", "rpctest-client", "rpctest-server"] +members = ["protos", "contest_service", "rpctest-client", "rpctest-server", "convert"] diff --git a/convert/Cargo.toml b/convert/Cargo.toml new file mode 100644 index 0000000..1c46bf0 --- /dev/null +++ b/convert/Cargo.toml @@ -0,0 +1,10 @@ +[package] +name = "convert" +version = "0.1.0" +edition = "2018" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] +prost-types = "0.8" +bson = "2.0.0-beta.3" diff --git a/convert/src/lib.rs b/convert/src/lib.rs new file mode 100644 index 0000000..6cd3597 --- /dev/null +++ b/convert/src/lib.rs @@ -0,0 +1,27 @@ +//! Convertion utilities between protobuf and mongodb +pub mod mongo; +pub mod proto; + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn cycle_test_mongo() { + let now = std::time::UNIX_EPOCH + + std::time::Duration::from_secs( + std::time::SystemTime::now() + .duration_since(std::time::UNIX_EPOCH) + .unwrap() + .as_secs(), + ); //This is needed because we need to discard anything below the second + let mongo_now = mongo::systime_to_timestamp(now); + assert_eq!(now, mongo::timestamp_to_systime(mongo_now)); + } + #[test] + fn cycle_test_proto() { + let now = std::time::SystemTime::now(); + let proto_now = proto::systime_to_timestamp(now); + assert_eq!(now, proto::timestamp_to_systime(proto_now)); + } +} diff --git a/convert/src/mongo.rs b/convert/src/mongo.rs new file mode 100644 index 0000000..6f21d3b --- /dev/null +++ b/convert/src/mongo.rs @@ -0,0 +1,14 @@ +/// Convert MongoDB timestamps to Rust's SystemTime +pub fn timestamp_to_systime(ts: bson::Timestamp) -> std::time::SystemTime { + std::time::UNIX_EPOCH + std::time::Duration::from_secs(ts.time.into()) +} + +/// Convert a SystemTime to a MongoDB timestamp. Note that MongoDB sucks and stores unix timestamps in 32 bits +pub fn systime_to_timestamp(st: std::time::SystemTime) -> bson::Timestamp { + let nano_duration = st.duration_since(std::time::UNIX_EPOCH).unwrap().as_nanos(); + let seconds = (nano_duration / 1_000_000_000) as u32; // TODO This is bad, because we will have an overflow in 2106 + bson::Timestamp { + time: seconds, + increment: 0, + } +} diff --git a/convert/src/proto.rs b/convert/src/proto.rs new file mode 100644 index 0000000..a9d8cc0 --- /dev/null +++ b/convert/src/proto.rs @@ -0,0 +1,13 @@ +/// Convert a SystemTime to a protobuf timestamp. Not sure about the correctness of nanoseconds calculation +pub fn systime_to_timestamp(t: std::time::SystemTime) -> prost_types::Timestamp { + let nano_duration = t.duration_since(std::time::UNIX_EPOCH).unwrap().as_nanos(); + let seconds = (nano_duration / 1_000_000_000) as i64; + let nanos = (nano_duration % 1_000_000_000) as i32; // TODO Check correctness of this + prost_types::Timestamp { seconds, nanos } +} + +/// Convert a protobuf timestamp to a Rust SystemTime +pub fn timestamp_to_systime(t: prost_types::Timestamp) -> std::time::SystemTime { + let nano_duration = t.nanos as u64 + t.seconds as u64 * 1_000_000_000; + std::time::UNIX_EPOCH + std::time::Duration::from_nanos(nano_duration) +} From 04f50afc465d39291b996af939c7d470022b518e Mon Sep 17 00:00:00 2001 From: Giacomo Aloisi <14826807+GiackAloZ@users.noreply.github.com> Date: Sun, 22 Aug 2021 12:32:13 +0200 Subject: [PATCH 2/9] Fix Dockerfile --- contest_service/Dockerfile | 1 + 1 file changed, 1 insertion(+) diff --git a/contest_service/Dockerfile b/contest_service/Dockerfile index 43b634f..5819358 100644 --- a/contest_service/Dockerfile +++ b/contest_service/Dockerfile @@ -1,6 +1,7 @@ FROM rustlang/rust:nightly as base WORKDIR /src COPY protos protos +COPY convert convert WORKDIR /src/server COPY contest_service/dummy.rs . COPY contest_service/Cargo.toml . From fef55d5fa255c881b880f2109e0ba9d497095384 Mon Sep 17 00:00:00 2001 From: Eugenio Tampieri Date: Sun, 22 Aug 2021 13:41:19 +0200 Subject: [PATCH 3/9] Separated tests --- convert/src/lib.rs | 23 +---------------------- convert/src/tests.rs | 20 ++++++++++++++++++++ 2 files changed, 21 insertions(+), 22 deletions(-) create mode 100644 convert/src/tests.rs diff --git a/convert/src/lib.rs b/convert/src/lib.rs index 6cd3597..99c3d59 100644 --- a/convert/src/lib.rs +++ b/convert/src/lib.rs @@ -3,25 +3,4 @@ pub mod mongo; pub mod proto; #[cfg(test)] -mod tests { - use super::*; - - #[test] - fn cycle_test_mongo() { - let now = std::time::UNIX_EPOCH - + std::time::Duration::from_secs( - std::time::SystemTime::now() - .duration_since(std::time::UNIX_EPOCH) - .unwrap() - .as_secs(), - ); //This is needed because we need to discard anything below the second - let mongo_now = mongo::systime_to_timestamp(now); - assert_eq!(now, mongo::timestamp_to_systime(mongo_now)); - } - #[test] - fn cycle_test_proto() { - let now = std::time::SystemTime::now(); - let proto_now = proto::systime_to_timestamp(now); - assert_eq!(now, proto::timestamp_to_systime(proto_now)); - } -} +mod tests; diff --git a/convert/src/tests.rs b/convert/src/tests.rs new file mode 100644 index 0000000..45e1bb4 --- /dev/null +++ b/convert/src/tests.rs @@ -0,0 +1,20 @@ +use super::*; + +#[test] +fn cycle_test_mongo() { + let now = std::time::UNIX_EPOCH + + std::time::Duration::from_secs( + std::time::SystemTime::now() + .duration_since(std::time::UNIX_EPOCH) + .unwrap() + .as_secs(), + ); //This is needed because we need to discard anything below the second + let mongo_now = mongo::systime_to_timestamp(now); + assert_eq!(now, mongo::timestamp_to_systime(mongo_now)); +} +#[test] +fn cycle_test_proto() { + let now = std::time::SystemTime::now(); + let proto_now = proto::systime_to_timestamp(now); + assert_eq!(now, proto::timestamp_to_systime(proto_now)); +} From 861467b06324fcb3df7bb967e265f8913cc19723 Mon Sep 17 00:00:00 2001 From: Eugenio Tampieri Date: Sun, 22 Aug 2021 13:45:43 +0200 Subject: [PATCH 4/9] Renamed tests --- convert/src/tests.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/convert/src/tests.rs b/convert/src/tests.rs index 45e1bb4..c91ea2f 100644 --- a/convert/src/tests.rs +++ b/convert/src/tests.rs @@ -1,7 +1,7 @@ use super::*; #[test] -fn cycle_test_mongo() { +fn convert_to_mongo_timestamp_test_and_back() { let now = std::time::UNIX_EPOCH + std::time::Duration::from_secs( std::time::SystemTime::now() @@ -13,7 +13,7 @@ fn cycle_test_mongo() { assert_eq!(now, mongo::timestamp_to_systime(mongo_now)); } #[test] -fn cycle_test_proto() { +fn convert_to_protobuf_timestamp_test_and_back() { let now = std::time::SystemTime::now(); let proto_now = proto::systime_to_timestamp(now); assert_eq!(now, proto::timestamp_to_systime(proto_now)); From 4bf9bc25b148b6304bdffb1a8829bf9642a9acb9 Mon Sep 17 00:00:00 2001 From: Eugenio Tampieri Date: Sun, 22 Aug 2021 13:52:17 +0200 Subject: [PATCH 5/9] Renamed old tests and added some new ones --- convert/src/tests.rs | 39 +++++++++++++++++++++++++++++++++++++-- 1 file changed, 37 insertions(+), 2 deletions(-) diff --git a/convert/src/tests.rs b/convert/src/tests.rs index c91ea2f..40de999 100644 --- a/convert/src/tests.rs +++ b/convert/src/tests.rs @@ -1,7 +1,7 @@ use super::*; #[test] -fn convert_to_mongo_timestamp_test_and_back() { +fn convert_to_mongo_timestamp_and_back_test() { let now = std::time::UNIX_EPOCH + std::time::Duration::from_secs( std::time::SystemTime::now() @@ -13,8 +13,43 @@ fn convert_to_mongo_timestamp_test_and_back() { assert_eq!(now, mongo::timestamp_to_systime(mongo_now)); } #[test] -fn convert_to_protobuf_timestamp_test_and_back() { +fn convert_to_protobuf_timestamp_and_back_test() { let now = std::time::SystemTime::now(); let proto_now = proto::systime_to_timestamp(now); assert_eq!(now, proto::timestamp_to_systime(proto_now)); } + +#[test] +fn convert_to_protobuf_timestamp_test() { + let epoch = std::time::UNIX_EPOCH; + let proto_epoch = proto::systime_to_timestamp(epoch); + assert_eq!(proto_epoch.seconds, 0); + assert_eq!(proto_epoch.nanos, 0); +} + +#[test] +fn convert_to_mongo_timestamp_test() { + let epoch = std::time::UNIX_EPOCH; + let mongo_epoch = mongo::systime_to_timestamp(epoch); + assert_eq!(mongo_epoch.time, 0); + assert_eq!(mongo_epoch.increment, 0); +} +#[test] +fn convert_from_protobuf_timestamp_test() { + let epoch = std::time::UNIX_EPOCH; + let proto_epoch = prost_types::Timestamp { + seconds: 0, + nanos: 0, + }; + assert_eq!(proto::timestamp_to_systime(proto_epoch), epoch); +} + +#[test] +fn convert_from_mongo_timestamp_test() { + let epoch = std::time::UNIX_EPOCH; + let mongo_epoch = bson::Timestamp { + time: 0, + increment: 0, + }; + assert_eq!(mongo::timestamp_to_systime(mongo_epoch), epoch); +} From eb2f0af374df633c1d0788fa3073d463a80532b0 Mon Sep 17 00:00:00 2001 From: Eugenio Tampieri Date: Sun, 22 Aug 2021 13:53:17 +0200 Subject: [PATCH 6/9] Fixed some comments --- convert/src/mongo.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/convert/src/mongo.rs b/convert/src/mongo.rs index 6f21d3b..6e3c043 100644 --- a/convert/src/mongo.rs +++ b/convert/src/mongo.rs @@ -3,10 +3,10 @@ pub fn timestamp_to_systime(ts: bson::Timestamp) -> std::time::SystemTime { std::time::UNIX_EPOCH + std::time::Duration::from_secs(ts.time.into()) } -/// Convert a SystemTime to a MongoDB timestamp. Note that MongoDB sucks and stores unix timestamps in 32 bits +/// Convert a SystemTime to a MongoDB timestamp. Note that MongoDB stores unix timestamps in 32 bits pub fn systime_to_timestamp(st: std::time::SystemTime) -> bson::Timestamp { let nano_duration = st.duration_since(std::time::UNIX_EPOCH).unwrap().as_nanos(); - let seconds = (nano_duration / 1_000_000_000) as u32; // TODO This is bad, because we will have an overflow in 2106 + let seconds = (nano_duration / 1_000_000_000) as u32; // This will overflow in 2106 bson::Timestamp { time: seconds, increment: 0, From c248ff509e45b0aee77c1817d89e25eec2ad3855 Mon Sep 17 00:00:00 2001 From: Eugenio Tampieri Date: Sun, 22 Aug 2021 14:24:12 +0200 Subject: [PATCH 7/9] Removed magic numbers --- convert/src/lib.rs | 2 ++ convert/src/mongo.rs | 2 +- convert/src/proto.rs | 7 ++++--- 3 files changed, 7 insertions(+), 4 deletions(-) diff --git a/convert/src/lib.rs b/convert/src/lib.rs index 99c3d59..98d3b95 100644 --- a/convert/src/lib.rs +++ b/convert/src/lib.rs @@ -1,3 +1,5 @@ +#![feature(duration_constants)] + //! Convertion utilities between protobuf and mongodb pub mod mongo; pub mod proto; diff --git a/convert/src/mongo.rs b/convert/src/mongo.rs index 6e3c043..bbe72c2 100644 --- a/convert/src/mongo.rs +++ b/convert/src/mongo.rs @@ -6,7 +6,7 @@ pub fn timestamp_to_systime(ts: bson::Timestamp) -> std::time::SystemTime { /// Convert a SystemTime to a MongoDB timestamp. Note that MongoDB stores unix timestamps in 32 bits pub fn systime_to_timestamp(st: std::time::SystemTime) -> bson::Timestamp { let nano_duration = st.duration_since(std::time::UNIX_EPOCH).unwrap().as_nanos(); - let seconds = (nano_duration / 1_000_000_000) as u32; // This will overflow in 2106 + let seconds = (nano_duration / std::time::Duration::SECOND.as_nanos()) as u32; // This will overflow in 2106 bson::Timestamp { time: seconds, increment: 0, diff --git a/convert/src/proto.rs b/convert/src/proto.rs index a9d8cc0..090647b 100644 --- a/convert/src/proto.rs +++ b/convert/src/proto.rs @@ -1,13 +1,14 @@ /// Convert a SystemTime to a protobuf timestamp. Not sure about the correctness of nanoseconds calculation pub fn systime_to_timestamp(t: std::time::SystemTime) -> prost_types::Timestamp { let nano_duration = t.duration_since(std::time::UNIX_EPOCH).unwrap().as_nanos(); - let seconds = (nano_duration / 1_000_000_000) as i64; - let nanos = (nano_duration % 1_000_000_000) as i32; // TODO Check correctness of this + let seconds = (nano_duration / std::time::Duration::SECOND.as_nanos()) as i64; + let nanos = (nano_duration % std::time::Duration::SECOND.as_nanos()) as i32; // TODO Check correctness of this prost_types::Timestamp { seconds, nanos } } /// Convert a protobuf timestamp to a Rust SystemTime pub fn timestamp_to_systime(t: prost_types::Timestamp) -> std::time::SystemTime { - let nano_duration = t.nanos as u64 + t.seconds as u64 * 1_000_000_000; + let nano_duration = + t.nanos as u64 + t.seconds as u64 * std::time::Duration::SECOND.as_nanos() as u64; std::time::UNIX_EPOCH + std::time::Duration::from_nanos(nano_duration) } From 402846fb984eba7272bdb58e9f0ead8847ff986a Mon Sep 17 00:00:00 2001 From: Eugenio Tampieri Date: Sun, 22 Aug 2021 14:24:26 +0200 Subject: [PATCH 8/9] No longer using unix epoch as test value --- convert/src/tests.rs | 39 ++++++++++++++++++++++----------------- 1 file changed, 22 insertions(+), 17 deletions(-) diff --git a/convert/src/tests.rs b/convert/src/tests.rs index 40de999..9693a2a 100644 --- a/convert/src/tests.rs +++ b/convert/src/tests.rs @@ -1,5 +1,10 @@ use super::*; +fn get_test_time() -> std::time::SystemTime { + std::time::UNIX_EPOCH + + std::time::Duration::from_secs(10) + + std::time::Duration::from_nanos(101) +} #[test] fn convert_to_mongo_timestamp_and_back_test() { let now = std::time::UNIX_EPOCH @@ -21,35 +26,35 @@ fn convert_to_protobuf_timestamp_and_back_test() { #[test] fn convert_to_protobuf_timestamp_test() { - let epoch = std::time::UNIX_EPOCH; - let proto_epoch = proto::systime_to_timestamp(epoch); - assert_eq!(proto_epoch.seconds, 0); - assert_eq!(proto_epoch.nanos, 0); + let test_time = get_test_time(); + let proto_test_time = proto::systime_to_timestamp(test_time); + assert_eq!(proto_test_time.seconds, 10); + assert_eq!(proto_test_time.nanos, 101); } #[test] fn convert_to_mongo_timestamp_test() { - let epoch = std::time::UNIX_EPOCH; - let mongo_epoch = mongo::systime_to_timestamp(epoch); - assert_eq!(mongo_epoch.time, 0); - assert_eq!(mongo_epoch.increment, 0); + let test_time = get_test_time(); + let mongo_test_time = mongo::systime_to_timestamp(test_time); + assert_eq!(mongo_test_time.time, 10); + assert_eq!(mongo_test_time.increment, 0); } #[test] fn convert_from_protobuf_timestamp_test() { - let epoch = std::time::UNIX_EPOCH; - let proto_epoch = prost_types::Timestamp { - seconds: 0, - nanos: 0, + let test_time = get_test_time(); + let proto_test_time = prost_types::Timestamp { + seconds: 10, + nanos: 101, }; - assert_eq!(proto::timestamp_to_systime(proto_epoch), epoch); + assert_eq!(proto::timestamp_to_systime(proto_test_time), test_time); } #[test] fn convert_from_mongo_timestamp_test() { - let epoch = std::time::UNIX_EPOCH; - let mongo_epoch = bson::Timestamp { - time: 0, + let test_time = get_test_time() - std::time::Duration::from_nanos(101); + let mongo_test_time = bson::Timestamp { + time: 10, increment: 0, }; - assert_eq!(mongo::timestamp_to_systime(mongo_epoch), epoch); + assert_eq!(mongo::timestamp_to_systime(mongo_test_time), test_time); } From 295cea443f504d5208527530c6f80c3532408f70 Mon Sep 17 00:00:00 2001 From: Eugenio Tampieri Date: Sun, 22 Aug 2021 14:31:30 +0200 Subject: [PATCH 9/9] Removed useless code --- convert/src/lib.rs | 2 +- convert/src/proto.rs | 14 -------------- convert/src/tests.rs | 23 ----------------------- 3 files changed, 1 insertion(+), 38 deletions(-) delete mode 100644 convert/src/proto.rs diff --git a/convert/src/lib.rs b/convert/src/lib.rs index 98d3b95..3cdb825 100644 --- a/convert/src/lib.rs +++ b/convert/src/lib.rs @@ -2,7 +2,7 @@ //! Convertion utilities between protobuf and mongodb pub mod mongo; -pub mod proto; +//pub mod proto; #[cfg(test)] mod tests; diff --git a/convert/src/proto.rs b/convert/src/proto.rs deleted file mode 100644 index 090647b..0000000 --- a/convert/src/proto.rs +++ /dev/null @@ -1,14 +0,0 @@ -/// Convert a SystemTime to a protobuf timestamp. Not sure about the correctness of nanoseconds calculation -pub fn systime_to_timestamp(t: std::time::SystemTime) -> prost_types::Timestamp { - let nano_duration = t.duration_since(std::time::UNIX_EPOCH).unwrap().as_nanos(); - let seconds = (nano_duration / std::time::Duration::SECOND.as_nanos()) as i64; - let nanos = (nano_duration % std::time::Duration::SECOND.as_nanos()) as i32; // TODO Check correctness of this - prost_types::Timestamp { seconds, nanos } -} - -/// Convert a protobuf timestamp to a Rust SystemTime -pub fn timestamp_to_systime(t: prost_types::Timestamp) -> std::time::SystemTime { - let nano_duration = - t.nanos as u64 + t.seconds as u64 * std::time::Duration::SECOND.as_nanos() as u64; - std::time::UNIX_EPOCH + std::time::Duration::from_nanos(nano_duration) -} diff --git a/convert/src/tests.rs b/convert/src/tests.rs index 9693a2a..bab36f5 100644 --- a/convert/src/tests.rs +++ b/convert/src/tests.rs @@ -17,20 +17,6 @@ fn convert_to_mongo_timestamp_and_back_test() { let mongo_now = mongo::systime_to_timestamp(now); assert_eq!(now, mongo::timestamp_to_systime(mongo_now)); } -#[test] -fn convert_to_protobuf_timestamp_and_back_test() { - let now = std::time::SystemTime::now(); - let proto_now = proto::systime_to_timestamp(now); - assert_eq!(now, proto::timestamp_to_systime(proto_now)); -} - -#[test] -fn convert_to_protobuf_timestamp_test() { - let test_time = get_test_time(); - let proto_test_time = proto::systime_to_timestamp(test_time); - assert_eq!(proto_test_time.seconds, 10); - assert_eq!(proto_test_time.nanos, 101); -} #[test] fn convert_to_mongo_timestamp_test() { @@ -39,15 +25,6 @@ fn convert_to_mongo_timestamp_test() { assert_eq!(mongo_test_time.time, 10); assert_eq!(mongo_test_time.increment, 0); } -#[test] -fn convert_from_protobuf_timestamp_test() { - let test_time = get_test_time(); - let proto_test_time = prost_types::Timestamp { - seconds: 10, - nanos: 101, - }; - assert_eq!(proto::timestamp_to_systime(proto_test_time), test_time); -} #[test] fn convert_from_mongo_timestamp_test() {