Skip to content

Commit 30588db

Browse files
committed
branch start, add const fn from_duration for Timestamp
1 parent efbc8df commit 30588db

File tree

2 files changed

+28
-11
lines changed

2 files changed

+28
-11
lines changed

src/adapter/src/coord.rs

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -151,21 +151,14 @@ mod read_policy;
151151
mod sequencer;
152152
mod sql;
153153

154-
// TODO: We can have only two consts here, instead of three,
155-
// once there exists a `const` way to convert between a `Timestamp`
156-
// and a `Duration`
157-
158-
/// `DEFAULT_LOGICAL_COMPACTION_WINDOW`, in milliseconds.
159-
/// The default is set to a second to track the default timestamp frequency for sources.
160-
const DEFAULT_LOGICAL_COMPACTION_WINDOW_MILLIS: u64 = 1000;
161-
162154
/// The default logical compaction window for new objects
163-
pub const DEFAULT_LOGICAL_COMPACTION_WINDOW: Duration =
164-
Duration::from_millis(DEFAULT_LOGICAL_COMPACTION_WINDOW_MILLIS);
155+
///
156+
/// The default is set to a second to track the default timestamp frequency for sources.
157+
pub const DEFAULT_LOGICAL_COMPACTION_WINDOW: Duration = Duration::from_secs(1);
165158

166159
/// `DEFAULT_LOGICAL_COMPACTION_WINDOW` as an `EpochMillis` timestamp
167160
pub const DEFAULT_LOGICAL_COMPACTION_WINDOW_TS: mz_repr::Timestamp =
168-
Timestamp::new(DEFAULT_LOGICAL_COMPACTION_WINDOW_MILLIS);
161+
Timestamp::from_duration(DEFAULT_LOGICAL_COMPACTION_WINDOW);
169162

170163
/// A dummy availability zone to use when no availability zones are explicitly
171164
/// specified.

src/repr/src/timestamp.rs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,30 @@ impl Timestamp {
8585
}
8686
}
8787

88+
/// Creates a [`Timestamp`] from a [`Duration`]
89+
///
90+
/// # Panics
91+
/// * If the provided [`Duration`], when represented in milliseconds, overflows a `u64`
92+
///
93+
/// ```should_panic
94+
/// use mz_repr::Timestamp;
95+
/// use std::time::Duration;
96+
///
97+
/// let d = Duration::from_secs(u64::MAX);
98+
/// let t = Timestamp::from_duration(d);
99+
/// ```
100+
pub const fn from_duration(duration: Duration) -> Self {
101+
let millis = duration.as_millis();
102+
assert!(
103+
millis <= u64::MAX as u128,
104+
"overflow when creating Timestamp from Duration!"
105+
);
106+
107+
Self {
108+
internal: millis as u64,
109+
}
110+
}
111+
88112
pub fn to_bytes(&self) -> [u8; 8] {
89113
self.internal.to_le_bytes()
90114
}

0 commit comments

Comments
 (0)