From 6c71fc6646a92f33dbb8aa371e928eaa8434ea63 Mon Sep 17 00:00:00 2001 From: Christian Schwarz Date: Mon, 5 Jun 2023 15:12:50 +0200 Subject: [PATCH] struct type for in-memory layer put failure --- .../src/inmem_shared_historic_imm.rs | 51 +++++++++++++++---- 1 file changed, 41 insertions(+), 10 deletions(-) diff --git a/libs/layer_map/src/inmem_shared_historic_imm.rs b/libs/layer_map/src/inmem_shared_historic_imm.rs index d7699a248c..5b411bf7b1 100644 --- a/libs/layer_map/src/inmem_shared_historic_imm.rs +++ b/libs/layer_map/src/inmem_shared_historic_imm.rs @@ -16,12 +16,29 @@ pub trait Types { type HistoricStuff: HistoricStuff; } -pub enum InMemoryLayerPutError { +#[derive(thiserror::Error)] +pub struct InMemoryLayerPutError { + delta: DeltaRecord, + kind: InMemoryLayerPutErrorKind, +} + +#[derive(Debug)] +pub enum InMemoryLayerPutErrorKind { Frozen, LayerFull, AlreadyHaveRecordForKeyAndLsn, } +impl std::fmt::Debug for InMemoryLayerPutError { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + f.debug_struct("InMemoryLayerPutError") + // would require DeltaRecord to impl Debug + // .field("delta", &self.delta) + .field("kind", &self.kind) + .finish() + } +} + pub trait InMemoryLayer: std::fmt::Debug + Default + Clone { type Types: Types; fn put( @@ -29,7 +46,7 @@ pub trait InMemoryLayer: std::fmt::Debug + Default + Clone { key: ::Key, lsn: ::Lsn, delta: ::DeltaRecord, - ) -> Result<(), (::DeltaRecord, InMemoryLayerPutError)>; + ) -> Result<(), InMemoryLayerPutError<::DeltaRecord>>; fn get( &self, key: ::Key, @@ -155,16 +172,25 @@ impl ReadWriter { Ok(()) => { self.shared.advance(lsn, None); } - Err((_delta, InMemoryLayerPutError::Frozen)) => { + Err(InMemoryLayerPutError { + delta: _, + kind: InMemoryLayerPutErrorKind::Frozen, + }) => { unreachable!("this method is &mut self, so, Rust guarantees that we are the only ones who can put() into the inmem layer, and if we freeze it as part of put, we make sure we don't try to put() again") } - Err((delta, InMemoryLayerPutError::AlreadyHaveRecordForKeyAndLsn)) => { + Err(InMemoryLayerPutError { + delta, + kind: InMemoryLayerPutErrorKind::AlreadyHaveRecordForKeyAndLsn, + }) => { return Err(PutError { delta, kind: PutErrorKind::AlreadyHaveInMemoryRecordForKeyAndLsn, }); } - Err((_delta, InMemoryLayerPutError::LayerFull)) => { + Err(InMemoryLayerPutError { + delta: _delta, + kind: InMemoryLayerPutErrorKind::LayerFull, + }) => { inmem.freeze(); let inmem_clone = inmem.clone(); drop(inmem); @@ -241,6 +267,8 @@ mod tests { use crate::tests_common::UsizeCounter; + use super::InMemoryLayerPutError; + /// The ZST for which we impl the `super::Types` type collection trait. struct TestTypes; @@ -317,17 +345,20 @@ mod tests { key: usize, lsn: usize, delta: &'static str, - ) -> Result<(), (&'static str, super::InMemoryLayerPutError)> { + ) -> Result<(), super::InMemoryLayerPutError<&'static str>> { if self.frozen { - return Err((delta, super::InMemoryLayerPutError::Frozen)); + return Err(InMemoryLayerPutError { + delta, + kind: super::InMemoryLayerPutErrorKind::Frozen, + }); } let by_key = self.by_key.entry(key).or_default(); match by_key.entry(lsn) { Entry::Occupied(_record) => { - return Err(( + return Err(InMemoryLayerPutError { delta, - super::InMemoryLayerPutError::AlreadyHaveRecordForKeyAndLsn, - )); + kind: super::InMemoryLayerPutErrorKind::AlreadyHaveRecordForKeyAndLsn, + }); } Entry::Vacant(vacant) => vacant.insert(delta), };