File tree 2 files changed +28
-11
lines changed
2 files changed +28
-11
lines changed Original file line number Diff line number Diff line change @@ -151,21 +151,14 @@ mod read_policy;
151
151
mod sequencer;
152
152
mod sql;
153
153
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
-
162
154
/// 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 ) ;
165
158
166
159
/// `DEFAULT_LOGICAL_COMPACTION_WINDOW` as an `EpochMillis` timestamp
167
160
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 ) ;
169
162
170
163
/// A dummy availability zone to use when no availability zones are explicitly
171
164
/// specified.
Original file line number Diff line number Diff line change @@ -85,6 +85,30 @@ impl Timestamp {
85
85
}
86
86
}
87
87
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
+
88
112
pub fn to_bytes ( & self ) -> [ u8 ; 8 ] {
89
113
self . internal . to_le_bytes ( )
90
114
}
You can’t perform that action at this time.
0 commit comments