Replace uses of SystemTime with new UInt-based timestamp types

This commit is contained in:
Jonas Platte
2021-05-13 01:13:24 +02:00
parent c34d570fff
commit 5710d2740c
45 changed files with 217 additions and 270 deletions

View File

@@ -18,6 +18,7 @@ unstable-pre-spec = ["ruma-events/unstable-pre-spec"]
itertools = "0.10.0"
js_int = "0.2.0"
maplit = "1.0.2"
ruma-common = { version = "0.5.0", path = "../ruma-common" }
ruma-events = { version = "=0.22.0-alpha.3", path = "../ruma-events" }
ruma-identifiers = { version = "0.19.0", path = "../ruma-identifiers" }
ruma-serde = { version = "0.3.1", path = "../ruma-serde" }

View File

@@ -6,15 +6,15 @@
// `cargo bench --bench <name of the bench> -- --save-baseline <name>`.
use std::{
collections::{BTreeMap, BTreeSet},
convert::TryFrom,
convert::{TryFrom, TryInto},
sync::Arc,
time::{Duration, UNIX_EPOCH},
};
use criterion::{criterion_group, criterion_main, Criterion};
use event::StateEvent;
use js_int::uint;
use maplit::btreemap;
use ruma_common::MilliSecondsSinceUnixEpoch;
use ruma_events::{
pdu::{EventHash, Pdu, RoomV3Pdu},
room::{
@@ -40,7 +40,7 @@ fn lexico_topo_sort(c: &mut Criterion) {
};
b.iter(|| {
let _ = StateResolution::lexicographical_topological_sort(&graph, |id| {
(0, UNIX_EPOCH, id.clone())
(0, MilliSecondsSinceUnixEpoch(uint!(0)), id.clone())
});
})
});
@@ -376,7 +376,7 @@ where
rest: Pdu::RoomV3Pdu(RoomV3Pdu {
room_id: room_id(),
sender,
origin_server_ts: UNIX_EPOCH + Duration::from_secs(ts),
origin_server_ts: MilliSecondsSinceUnixEpoch(ts.try_into().unwrap()),
state_key,
kind: ev_type,
content,
@@ -522,9 +522,10 @@ fn BAN_STATE_SET() -> BTreeMap<EventId, Arc<StateEvent>> {
}
pub mod event {
use std::{collections::BTreeMap, time::SystemTime};
use std::collections::BTreeMap;
use js_int::UInt;
use ruma_common::MilliSecondsSinceUnixEpoch;
use ruma_events::{
pdu::{EventHash, Pdu},
room::member::MembershipState,
@@ -559,7 +560,7 @@ pub mod event {
self.content()
}
fn origin_server_ts(&self) -> SystemTime {
fn origin_server_ts(&self) -> MilliSecondsSinceUnixEpoch {
*self.origin_server_ts()
}
@@ -656,7 +657,7 @@ pub mod event {
}
}
pub fn origin_server_ts(&self) -> &SystemTime {
pub fn origin_server_ts(&self) -> &MilliSecondsSinceUnixEpoch {
match &self.rest {
Pdu::RoomV1Pdu(ev) => &ev.origin_server_ts,
Pdu::RoomV3Pdu(ev) => &ev.origin_server_ts,

View File

@@ -2,10 +2,10 @@ use std::{
cmp::Reverse,
collections::{BTreeMap, BTreeSet, BinaryHeap},
sync::Arc,
time::SystemTime,
};
use maplit::btreeset;
use ruma_common::MilliSecondsSinceUnixEpoch;
use ruma_events::{
room::{
member::{MemberEventContent, MembershipState},
@@ -288,7 +288,7 @@ impl StateResolution {
key_fn: F,
) -> Vec<EventId>
where
F: Fn(&EventId) -> (i64, SystemTime, EventId),
F: Fn(&EventId) -> (i64, MilliSecondsSinceUnixEpoch, EventId),
{
info!("starting lexicographical topological sort");
// NOTE: an event that has no incoming edges happened most recently,

View File

@@ -1,6 +1,7 @@
use std::{collections::BTreeMap, time::SystemTime};
use std::collections::BTreeMap;
use js_int::UInt;
use ruma_common::MilliSecondsSinceUnixEpoch;
use ruma_events::{pdu::EventHash, EventType};
use ruma_identifiers::{EventId, RoomId, ServerName, ServerSigningKeyId, UserId};
use serde_json::value::Value as JsonValue;
@@ -17,7 +18,7 @@ pub trait Event {
fn sender(&self) -> &UserId;
/// The time of creation on the originating server.
fn origin_server_ts(&self) -> SystemTime;
fn origin_server_ts(&self) -> MilliSecondsSinceUnixEpoch;
/// The kind of event.
fn kind(&self) -> EventType;

View File

@@ -1,6 +1,8 @@
use std::{sync::Arc, time::UNIX_EPOCH};
use std::sync::Arc;
use js_int::uint;
use maplit::btreemap;
use ruma_common::MilliSecondsSinceUnixEpoch;
use ruma_events::{room::join_rules::JoinRule, EventType};
use ruma_identifiers::{EventId, RoomVersionId};
use ruma_state_res::{EventMap, StateMap, StateResolution};
@@ -276,8 +278,9 @@ fn test_lexicographical_sort() {
event_id("p") => vec![event_id("o")],
};
let res =
StateResolution::lexicographical_topological_sort(&graph, |id| (0, UNIX_EPOCH, id.clone()));
let res = StateResolution::lexicographical_topological_sort(&graph, |id| {
(0, MilliSecondsSinceUnixEpoch(uint!(0)), id.clone())
});
assert_eq!(
vec!["o", "l", "n", "m", "p"],

View File

@@ -2,13 +2,13 @@
use std::{
collections::{BTreeMap, BTreeSet},
convert::TryFrom,
convert::{TryFrom, TryInto},
sync::{Arc, Once},
time::{Duration, UNIX_EPOCH},
};
use js_int::uint;
use maplit::btreemap;
use ruma_common::MilliSecondsSinceUnixEpoch;
use ruma_events::{
pdu::{EventHash, Pdu, RoomV3Pdu},
room::{
@@ -77,9 +77,9 @@ pub fn do_check(
// resolve the current state and add it to the state_at_event map then continue
// on in "time"
for node in
StateResolution::lexicographical_topological_sort(&graph, |id| (0, UNIX_EPOCH, id.clone()))
{
for node in StateResolution::lexicographical_topological_sort(&graph, |id| {
(0, MilliSecondsSinceUnixEpoch(uint!(0)), id.clone())
}) {
let fake_event = fake_event_map.get(&node).unwrap();
let event_id = fake_event.event_id().clone();
@@ -340,7 +340,7 @@ pub fn to_init_pdu_event(
rest: Pdu::RoomV3Pdu(RoomV3Pdu {
room_id: room_id(),
sender,
origin_server_ts: UNIX_EPOCH + Duration::from_secs(ts),
origin_server_ts: MilliSecondsSinceUnixEpoch(ts.try_into().unwrap()),
state_key,
kind: ev_type,
content,
@@ -385,7 +385,7 @@ where
rest: Pdu::RoomV3Pdu(RoomV3Pdu {
room_id: room_id(),
sender,
origin_server_ts: UNIX_EPOCH + Duration::from_secs(ts),
origin_server_ts: MilliSecondsSinceUnixEpoch(ts.try_into().unwrap()),
state_key,
kind: ev_type,
content,
@@ -497,10 +497,11 @@ pub fn INITIAL_EDGES() -> Vec<EventId> {
}
pub mod event {
use std::{collections::BTreeMap, time::SystemTime};
use std::collections::BTreeMap;
use js_int::UInt;
use ruma_events::{
exports::ruma_common::MilliSecondsSinceUnixEpoch,
pdu::{EventHash, Pdu},
room::member::{MemberEventContent, MembershipState},
EventType,
@@ -532,7 +533,7 @@ pub mod event {
fn content(&self) -> serde_json::Value {
self.content()
}
fn origin_server_ts(&self) -> SystemTime {
fn origin_server_ts(&self) -> MilliSecondsSinceUnixEpoch {
*self.origin_server_ts()
}
@@ -623,7 +624,7 @@ pub mod event {
Pdu::RoomV3Pdu(ev) => serde_json::from_value(ev.content.clone()),
}
}
pub fn origin_server_ts(&self) -> &SystemTime {
pub fn origin_server_ts(&self) -> &MilliSecondsSinceUnixEpoch {
match &self.rest {
Pdu::RoomV1Pdu(ev) => &ev.origin_server_ts,
Pdu::RoomV3Pdu(ev) => &ev.origin_server_ts,