Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add count_at_time function to SqLiteKeyPackageStorage #208

Merged
merged 1 commit into from
Nov 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion mls-rs-provider-sqlite/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "mls-rs-provider-sqlite"
version = "0.13.2"
version = "0.13.3"
edition = "2021"
description = "SQLite based state storage for mls-rs"
homepage = "https://github.com/awslabs/mls-rs"
Expand Down
37 changes: 37 additions & 0 deletions mls-rs-provider-sqlite/src/key_package.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,10 +75,13 @@ impl SqLiteKeyPackageStorage {
.map_err(|e| SqLiteDataStorageError::SqlEngineError(e.into()))
}

/// Delete key packages that are expired based on the current system clock time.
pub fn delete_expired(&self) -> Result<(), SqLiteDataStorageError> {
self.delete_expired_by_time(MlsTime::now().seconds_since_epoch())
}

/// Delete key packages that are expired based on an application provided time in seconds since
/// unix epoch.
pub fn delete_expired_by_time(&self, time: u64) -> Result<(), SqLiteDataStorageError> {
let connection = self.connection.lock().unwrap();

Expand All @@ -91,6 +94,7 @@ impl SqLiteKeyPackageStorage {
.map_err(|e| SqLiteDataStorageError::SqlEngineError(e.into()))
}

/// Total number of key packages held in storage.
pub fn count(&self) -> Result<usize, SqLiteDataStorageError> {
let connection = self.connection.lock().unwrap();

Expand All @@ -100,6 +104,21 @@ impl SqLiteKeyPackageStorage {
})
.map_err(|e| SqLiteDataStorageError::SqlEngineError(e.into()))
}

/// Total number of key packages that will still remain in storage at a specific application provided
/// time in seconds since unix epoch. This assumes that the application would also be calling
/// [SqLiteKeyPackageStorage::delete_expired] at a reasonable cadence to be accurate.
pub fn count_at_time(&self, time: u64) -> Result<usize, SqLiteDataStorageError> {
let connection = self.connection.lock().unwrap();

connection
.query_row(
"SELECT count(*) FROM key_package where expiration >= ?",
params![time],
|row| row.get(0),
)
.map_err(|e| SqLiteDataStorageError::SqlEngineError(e.into()))
}
}

#[cfg_attr(not(mls_build_async), maybe_async::must_be_sync)]
Expand Down Expand Up @@ -240,4 +259,22 @@ mod tests {

assert_eq!(storage.count().unwrap(), 10);
}

#[test]
fn key_count_at_time() {
let mut storage = test_storage();

let mut kp_1 = test_key_package();
kp_1.1.expiration = 1;
storage.insert(&kp_1.0, kp_1.1).unwrap();

let mut kp_2 = test_key_package();
kp_2.1.expiration = 2;
storage.insert(&kp_2.0, kp_2.1).unwrap();

assert_eq!(storage.count_at_time(3).unwrap(), 0);
assert_eq!(storage.count_at_time(2).unwrap(), 1);
assert_eq!(storage.count_at_time(1).unwrap(), 2);
assert_eq!(storage.count_at_time(0).unwrap(), 2);
}
}
Loading