Skip to main content

CachedDbItem

Struct CachedDbItem 

Source
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. None for 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>

Source

pub fn new(db: Arc<DB>, key: Vec<u8>) -> Self

Source

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] when legacy_suffix = None (pre-versioning, unversioned on-disk layout — the Toccata case);
  • [key_prefix || s] when legacy_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.

Source

pub fn read(&self) -> Result<T, StoreError>

Source

pub fn write( &mut self, writer: impl DbWriter, item: &T, ) -> Result<(), StoreError>
where T: Clone + Serialize,

Source

pub fn remove(&mut self, writer: impl DbWriter) -> Result<(), StoreError>

Source

pub fn update<F>( &mut self, writer: impl DbWriter, op: F, ) -> Result<T, StoreError>
where T: Clone + Serialize + DeserializeOwned, TLegacy: DeserializeOwned + Into<T>, F: Fn(T) -> T,

Trait Implementations§

Source§

impl<T, TLegacy> Clone for CachedDbItem<T, TLegacy>

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more

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>
where Arc<RwLock<RawRwLock, Option<T>>>: Freeze, PhantomData<fn() -> TLegacy>: Freeze,

§

impl<T, TLegacy> Send for CachedDbItem<T, TLegacy>
where Arc<RwLock<RawRwLock, Option<T>>>: Send, PhantomData<fn() -> TLegacy>: Send,

§

impl<T, TLegacy> Sync for CachedDbItem<T, TLegacy>
where Arc<RwLock<RawRwLock, Option<T>>>: Sync, PhantomData<fn() -> TLegacy>: Sync,

§

impl<T, TLegacy> Unpin for CachedDbItem<T, TLegacy>
where Arc<RwLock<RawRwLock, Option<T>>>: Unpin, PhantomData<fn() -> TLegacy>: Unpin,

§

impl<T, TLegacy> UnsafeUnpin for CachedDbItem<T, TLegacy>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ

Converts 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 more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
where F: FnOnce(&Self) -> bool,

Converts 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
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V