Make Storage methods async

This will better support concurrent requests.
This commit is contained in:
Dustin J. Mitchell
2025-07-11 17:28:05 -04:00
parent 4de5c9a345
commit 7559364017
13 changed files with 597 additions and 412 deletions

View File

@ -44,32 +44,35 @@ pub struct Version {
///
/// Changes in a transaction that is dropped without calling `commit` must not appear in any other
/// transaction.
#[async_trait::async_trait(?Send)]
pub trait StorageTxn {
/// Get information about the client for this transaction
fn get_client(&mut self) -> anyhow::Result<Option<Client>>;
async fn get_client(&mut self) -> anyhow::Result<Option<Client>>;
/// Create the client for this transaction, with the given latest_version_id. The client must
/// not already exist.
fn new_client(&mut self, latest_version_id: Uuid) -> anyhow::Result<()>;
async fn new_client(&mut self, latest_version_id: Uuid) -> anyhow::Result<()>;
/// Set the client's most recent snapshot.
fn set_snapshot(&mut self, snapshot: Snapshot, data: Vec<u8>) -> anyhow::Result<()>;
async fn set_snapshot(&mut self, snapshot: Snapshot, data: Vec<u8>) -> anyhow::Result<()>;
/// Get the data for the most recent snapshot. The version_id
/// is used to verify that the snapshot is for the correct version.
fn get_snapshot_data(&mut self, version_id: Uuid) -> anyhow::Result<Option<Vec<u8>>>;
async fn get_snapshot_data(&mut self, version_id: Uuid) -> anyhow::Result<Option<Vec<u8>>>;
/// Get a version, indexed by parent version id
fn get_version_by_parent(&mut self, parent_version_id: Uuid)
-> anyhow::Result<Option<Version>>;
async fn get_version_by_parent(
&mut self,
parent_version_id: Uuid,
) -> anyhow::Result<Option<Version>>;
/// Get a version, indexed by its own version id
fn get_version(&mut self, version_id: Uuid) -> anyhow::Result<Option<Version>>;
async fn get_version(&mut self, version_id: Uuid) -> anyhow::Result<Option<Version>>;
/// Add a version (that must not already exist), and
/// - update latest_version_id
/// - increment snapshot.versions_since
fn add_version(
async fn add_version(
&mut self,
version_id: Uuid,
parent_version_id: Uuid,
@ -78,12 +81,13 @@ pub trait StorageTxn {
/// Commit any changes made in the transaction. It is an error to call this more than
/// once. It is safe to skip this call for read-only operations.
fn commit(&mut self) -> anyhow::Result<()>;
async fn commit(&mut self) -> anyhow::Result<()>;
}
/// A trait for objects able to act as storage. Most of the interesting behavior is in the
/// [`crate::storage::StorageTxn`] trait.
#[async_trait::async_trait]
pub trait Storage: Send + Sync {
/// Begin a transaction for the given client ID.
fn txn(&self, client_id: Uuid) -> anyhow::Result<Box<dyn StorageTxn + '_>>;
async fn txn(&self, client_id: Uuid) -> anyhow::Result<Box<dyn StorageTxn + '_>>;
}