delta-store-1.0.0.0: Facilities for storing a Haskell value, using delta types.
Copyright© 2022-2023 IOHK 2023-2025 Cardano Foundation
LicenseApache-2.0
Safe HaskellSafe-Inferred
LanguageHaskell2010

Data.DBVar

Description

DBVar represents a mutable variable that stores one Haskell value in volatile memory (RAM), but mirrors the value to persistent storage, for example to a database on disk.

  • Store represents the storage facility to which the variable is mirrored.
  • Read-access is from volatile memory and highly concurrent.
  • Updates are incremental and use delta types, see Data.Delta.
Synopsis

Documentation

data DBVar m delta Source #

A DBVar m delta is a mutable reference to a Haskell value of type a. The type delta is a delta type for this value type a, that is we have a ~ Base delta.

The Haskell value is cached in memory, in weak head normal form (WHNF). However, whenever the value is updated, a copy of the value will be written to persistent storage like a file or database on disk; the specific storage facility is represented by a Store. For efficient updates, the delta type delta is used in the update.

Concurrency: DBVar fully supports concurrent reads and updates.

  • Updates are atomic and will block other updates.
  • Reads will not be blocked during an update (except for a small moment where the new value atomically replaces the old one).

readDBVar :: (Delta da, a ~ Base da) => DBVar m da -> m a Source #

Read the current value of the DBVar.

updateDBVar :: (Delta da, Monad m) => DBVar m da -> da -> m () Source #

Update the value of the DBVar using a delta type.

The new value will be evaluated to weak head normal form.

modifyDBVar :: (Delta da, Monad m, a ~ Base da) => DBVar m da -> (a -> (da, b)) -> m b Source #

Modify the value in a DBVar.

The new value will be evaluated to weak head normal form (WHNF).

modifyDBMaybe :: (Delta da, Monad m, a ~ Base da) => DBVar m da -> forall b. (a -> (Maybe da, b)) -> m b Source #

Maybe modify the value in a DBVar

If updated, the new value will be evaluated to weak head normal form (WHNF).

initDBVar Source #

Arguments

:: (MonadSTM m, MonadThrow m, MonadEvaluate m, MonadMask m, Delta da, a ~ Base da) 
=> UpdateStore m da

Store for mirroring.

-> a

Initial value.

-> m (DBVar m da) 

Initialize a new DBVar that mirrors to a given Store.

loadDBVar Source #

Arguments

:: (MonadSTM m, MonadThrow m, MonadEvaluate m, MonadMask m, Delta da) 
=> UpdateStore m da

Store for mirroring and for reading the initial value.

-> m (DBVar m da) 

Create a DBVar that mirrors to a given Store, and also loads its initial value from there. Throws an exception if the value cannot be loaded.