acid-state-0.7.9: Add ACID guarantees to any serializable Haskell data structure.

Portabilitynon-portable (uses GHC extensions)
Maintainerlemmih@gmail.com
Safe HaskellNone

Data.Acid

Description

AcidState container using a transaction log on disk.

To see how it all fits together, have a look at these example http://mirror.seize.it/acid-state/examples/.

Synopsis

Documentation

data AcidState st Source

State container offering full ACID (Atomicity, Consistency, Isolation and Durability) guarantees.

Atomicity
State changes are all-or-nothing. This is what you'd expect of any state variable in Haskell and AcidState doesn't change that.
Consistency
No event or set of events will break your data invariants.
Isolation
Transactions cannot interfere with each other even when issued in parallel.
Durability
Successful transaction are guaranteed to survive unexpected system shutdowns (both those caused by hardware and software).

openLocalStateSource

Arguments

:: (Typeable st, IsAcidic st) 
=> st

Initial state value. This value is only used if no checkpoint is found.

-> IO (AcidState st) 

Create an AcidState given an initial value.

This will create or resume a log found in the "state/[typeOf state]/" directory.

openLocalStateFromSource

Arguments

:: IsAcidic st 
=> FilePath

Location of the checkpoint and transaction files.

-> st

Initial state value. This value is only used if no checkpoint is found.

-> IO (AcidState st) 

Create an AcidState given a log directory and an initial value.

This will create or resume a log found in directory. Running two AcidState's from the same directory is an error but will not result in dataloss.

closeAcidState :: AcidState st -> IO ()Source

Close an AcidState and associated resources. Any subsequent usage of the AcidState will throw an exception.

createCheckpoint :: AcidState st -> IO ()Source

Take a snapshot of the state and save it to disk. Creating checkpoints makes it faster to resume AcidStates and you're free to create them as often or seldom as fits your needs. Transactions can run concurrently with this call.

This call will not return until the operation has succeeded.

update :: UpdateEvent event => AcidState (EventState event) -> event -> IO (EventResult event)Source

Issue an Update event and wait for its result. Once this call returns, you are guaranteed that the changes to the state are durable. Events may be issued in parallel.

It's a run-time error to issue events that aren't supported by the AcidState.

query :: QueryEvent event => AcidState (EventState event) -> event -> IO (EventResult event)Source

Issue a Query event and wait for its result. Events may be issued in parallel.

type EventResult ev = MethodResult evSource

Events return the same thing as Methods. The exact type of EventResult depends on the event.

class Method ev => UpdateEvent ev Source

All UpdateEvents are also Methods.

class Method ev => QueryEvent ev Source

All QueryEvents are also Methods.

data Update st a Source

Context monad for Update events.

Instances

Monad (Update st) => MonadState st (Update st) 
Monad (Update st) 
Functor (Update st) 
Functor (Update st) => Applicative (Update st) 

data Query st a Source

Context monad for Query events.

Instances

Monad (Query st) => MonadReader st (Query st) 
Monad (Query st) 
Functor (Query st) 
Functor (Query st) => Applicative (Query st) 

class SafeCopy st => IsAcidic st Source

makeAcidic :: Name -> [Name] -> Q [Dec]Source

Create the control structures required for acid states using Template Haskell.

This code:

myUpdate :: Argument -> Update State Result
myUpdate arg = ...

myQuery :: Argument -> Query State Result
myQuery arg = ...

$(makeAcidic ''State ['myUpdate, 'myQuery])

will make State an instance of IsAcidic and provide the following events:

data MyUpdate = MyUpdate Argument
data MyQuery  = MyQuery Argument

runQuery :: Query st a -> Update st aSource

Run a query in the Update Monad.