pub struct CachedDbItem<T, TLegacy = T> { /* private fields */ }Expand description
A cached DB item with concurrency support.
§Versioned items
CachedDbItem supports an opt-in “version-suffix” mode used by items
whose on-disk value layout changed across a hardfork (currently
DbVirtualStateStore across Toccata). A version-aware item is
constructed with CachedDbItem::new_with_version_suffix.
The store keeps two precomputed physical keys:
live_key— where the current-format row is read and written.legacy_key— where a legacy-format row, if any, lives.Nonefor non-versioned items.
On reads, the live key is probed first; on miss, the legacy key is
probed and decoded through a caller-provided TLegacy: Into<T> shadow
type. A successful legacy fallback eagerly migrates: the converted
bytes are written under live_key and legacy_key is deleted, so the
next read short-circuits on the live path and no dangling pre-fork
bytes linger on disk. remove clears both layouts.
Dispatch is on physical-key presence, not on bincode decode outcome, so the version-aware path is robust to silent decode “successes” against mismatched bytes.
The TLegacy generic defaults to T, so existing callers that spell
only the single type parameter continue to compile unchanged: T: Into<T> is supplied by the blanket impl<U> From<U> for U.
Implementations§
Source§impl<T, TLegacy> CachedDbItem<T, TLegacy>
impl<T, TLegacy> CachedDbItem<T, TLegacy>
pub fn new(db: Arc<DB>, key: Vec<u8>) -> Self
Sourcepub fn new_with_version_suffix(
db: Arc<DB>,
key_prefix: Vec<u8>,
current_suffix: u8,
legacy_suffix: Option<u8>,
) -> Self
pub fn new_with_version_suffix( db: Arc<DB>, key_prefix: Vec<u8>, current_suffix: u8, legacy_suffix: Option<u8>, ) -> Self
Constructs a version-aware item.
Writes go to [key_prefix || current_suffix]. Reads probe that
key first and, on miss, fall back to the legacy row decoded through
TLegacy and converted via TLegacy: Into<T>. The legacy row’s
physical key is:
[key_prefix]whenlegacy_suffix = None(pre-versioning, unversioned on-disk layout — the Toccata case);[key_prefix || s]whenlegacy_suffix = Some(s)(a previously versioned layout — placeholder for future migrations).
A successful legacy fallback triggers an eager migration: the
converted T is written under the live key and the legacy row is
deleted. remove clears both layouts.
pub fn read(&self) -> Result<T, StoreError>
pub fn write( &mut self, writer: impl DbWriter, item: &T, ) -> Result<(), StoreError>
pub fn remove(&mut self, writer: impl DbWriter) -> Result<(), StoreError>
pub fn update<F>( &mut self, writer: impl DbWriter, op: F, ) -> Result<T, StoreError>
Trait Implementations§
Auto Trait Implementations§
impl<T, TLegacy = T> !RefUnwindSafe for CachedDbItem<T, TLegacy>
impl<T, TLegacy = T> !UnwindSafe for CachedDbItem<T, TLegacy>
impl<T, TLegacy> Freeze for CachedDbItem<T, TLegacy>
impl<T, TLegacy> Send for CachedDbItem<T, TLegacy>
impl<T, TLegacy> Sync for CachedDbItem<T, TLegacy>
impl<T, TLegacy> Unpin for CachedDbItem<T, TLegacy>
impl<T, TLegacy> UnsafeUnpin for CachedDbItem<T, TLegacy>
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more