feat: memtable stats (#1591)

* feat: memtable stats

* chore: add tests for timestamp subtraction

* feat: add `Value:as_timestamp` method
This commit is contained in:
Lei, HUANG
2023-05-17 11:07:07 +08:00
committed by GitHub
parent ca75a7b744
commit e70d49b9cf
12 changed files with 177 additions and 39 deletions

View File

@@ -48,7 +48,7 @@ impl Timestamp {
/// The result time unit remains unchanged even if `duration` has a different unit with `self`.
/// For example, a timestamp with value 1 and time unit second, subtracted by 1 millisecond
/// and the result is still 1 second.
pub fn sub(&self, duration: Duration) -> error::Result<Self> {
pub fn sub_duration(&self, duration: Duration) -> error::Result<Self> {
let duration: i64 = match self.unit {
TimeUnit::Second => {
i64::try_from(duration.as_secs()).context(TimestampOverflowSnafu)?
@@ -79,6 +79,13 @@ impl Timestamp {
})
}
/// Subtracts current timestamp with another timestamp, yielding a duration.
pub fn sub(&self, rhs: Self) -> Option<chrono::Duration> {
let lhs = self.to_chrono_datetime()?;
let rhs = rhs.to_chrono_datetime()?;
Some(lhs - rhs)
}
pub fn new(value: i64, unit: TimeUnit) -> Self {
Self { unit, value }
}
@@ -863,19 +870,19 @@ mod tests {
#[test]
fn test_timestamp_sub() {
let res = Timestamp::new(1, TimeUnit::Second)
.sub(Duration::from_secs(1))
.sub_duration(Duration::from_secs(1))
.unwrap();
assert_eq!(0, res.value);
assert_eq!(TimeUnit::Second, res.unit);
let res = Timestamp::new(0, TimeUnit::Second)
.sub(Duration::from_secs(1))
.sub_duration(Duration::from_secs(1))
.unwrap();
assert_eq!(-1, res.value);
assert_eq!(TimeUnit::Second, res.unit);
let res = Timestamp::new(1, TimeUnit::Second)
.sub(Duration::from_millis(1))
.sub_duration(Duration::from_millis(1))
.unwrap();
assert_eq!(1, res.value);
assert_eq!(TimeUnit::Second, res.unit);
@@ -914,4 +921,17 @@ mod tests {
Timestamp::new(1, TimeUnit::Second).to_local_string()
);
}
#[test]
fn test_subtract_timestamp() {
assert_eq!(
Some(chrono::Duration::milliseconds(42)),
Timestamp::new_millisecond(100).sub(Timestamp::new_millisecond(58))
);
assert_eq!(
Some(chrono::Duration::milliseconds(-42)),
Timestamp::new_millisecond(58).sub(Timestamp::new_millisecond(100))
);
}
}