Skip to content

Commit

Permalink
Make current_epoch in EpochStorage trait return error
Browse files Browse the repository at this point in the history
  • Loading branch information
nicholas-mainardi committed Jan 3, 2025
1 parent 14d374b commit ba06b9d
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 18 deletions.
6 changes: 4 additions & 2 deletions ryhope/src/storage/memory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,8 +94,10 @@ impl<T> EpochStorage<T> for VersionedStorage<T>
where
T: Debug + Send + Sync + Clone + Serialize + for<'b> Deserialize<'b>,
{
async fn current_epoch(&self) -> UserEpoch {
self.epoch_mapper.to_user_epoch(self.inner_epoch()).await as UserEpoch
async fn current_epoch(&self) -> Result<UserEpoch> {
self.epoch_mapper.try_to_user_epoch(self.inner_epoch())
.await
.ok_or(CurrenEpochUndefined(self.inner_epoch()).into())
}

async fn fetch_at(&self, epoch: UserEpoch) -> T {
Expand Down
16 changes: 7 additions & 9 deletions ryhope/src/storage/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -334,13 +334,13 @@ pub trait EpochStorage<T: Debug + Send + Sync + Clone + Serialize + for<'a> Dese
where
Self: Send + Sync,
{
/// Return the current epoch of the storage
fn current_epoch(&self) -> impl Future<Output = UserEpoch> + Send;
/// Return the current epoch of the storage. It returns an error
/// if the current epoch is undefined, which might happen when the epochs
/// are handled by another storage.
fn current_epoch(&self) -> impl Future<Output = Result<UserEpoch>> + Send;

/// Return the value stored at the current epoch.
fn fetch(&self) -> impl Future<Output = T> + Send {
async { self.fetch_at(self.current_epoch().await).await }
}
/// Return the value stored at the current epoch.
fn fetch(&self) -> impl Future<Output = T> + Send;

/// Return the value stored at the given epoch.
fn fetch_at(&self, epoch: UserEpoch) -> impl Future<Output = T> + Send;
Expand All @@ -357,9 +357,7 @@ where
}

/// Roll back this storage one epoch in the past.
fn rollback(&mut self) -> impl Future<Output = Result<()>> {
async move { self.rollback_to(self.current_epoch().await - 1).await }
}
fn rollback(&mut self) -> impl Future<Output = Result<()>>;

/// Roll back this storage to the given epoch
fn rollback_to(&mut self, epoch: UserEpoch) -> impl Future<Output = Result<()>>;
Expand Down
2 changes: 1 addition & 1 deletion ryhope/src/storage/pgsql/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -980,7 +980,7 @@ where
assert_eq!(self.epoch, self.tree_store.read().await.current_epoch());
assert_eq!(
self.epoch_mapper
.to_incremental_epoch(self.state.current_epoch().await,)
.to_incremental_epoch(self.state.current_epoch().await?)
.await,
self.epoch
);
Expand Down
9 changes: 5 additions & 4 deletions ryhope/src/storage/pgsql/storages.rs
Original file line number Diff line number Diff line change
Expand Up @@ -696,7 +696,7 @@ pub struct CachedDbStore<V: Debug + Clone + Send + Sync + Serialize + for<'a> De
/// True if the wrapped state has been modified
dirty: bool,
/// The current epoch
epoch: UserEpoch,
epoch: IncrementalEpoch,
/// The table in which the data must be persisted
table: String,
// epoch mapper
Expand Down Expand Up @@ -730,7 +730,7 @@ impl<T: Debug + Clone + Send + Sync + Serialize + for<'a> Deserialize<'a>> Cache
t: T,
mapper: RoSharedEpochMapper<EpochMapperStorage>,
) -> Result<Self> {
let initial_epoch = INITIAL_INCREMENTAL_EPOCH as UserEpoch;
let initial_epoch = INITIAL_INCREMENTAL_EPOCH;
{
let connection = db.get().await.unwrap();
connection
Expand Down Expand Up @@ -956,8 +956,9 @@ where
let _ = self.cache.write().await.insert(t);
}

async fn current_epoch(&self) -> UserEpoch {
self.epoch_mapper.to_user_epoch(self.epoch).await as UserEpoch
async fn current_epoch(&self) -> Result<UserEpoch> {
self.epoch_mapper.try_to_user_epoch(self.epoch).await
.ok_or(CurrenEpochUndefined(self.epoch).into())
}

async fn rollback_to(&mut self, new_epoch: UserEpoch) -> Result<()> {
Expand Down
8 changes: 6 additions & 2 deletions ryhope/src/storage/view.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,8 @@ impl<
where
T: Send,
{
async fn current_epoch(&self) -> UserEpoch {
self.1
async fn current_epoch(&self) -> Result<UserEpoch> {
Ok(self.1)
}

async fn fetch_at(&self, epoch: UserEpoch) -> T {
Expand All @@ -72,6 +72,10 @@ where
async fn rollback_to(&mut self, _epoch: UserEpoch) -> Result<()> {
unimplemented!("storage views are read only")
}

async fn rollback(&mut self) -> Result<()> {
unimplemented!("storage views are read only")
}
}

/// An epoch-locked, read-only, view over an [`EpochKvStorage`].
Expand Down

0 comments on commit ba06b9d

Please sign in to comment.