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




AcidState container using a transaction log on disk. The term 'Event' is loosely used for transactions with ACID guarantees. 'Method' is loosely used for state operations without ACID guarantees (see Data.State.Acid.Core).



data AcidState st Source

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

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

data Event st whereSource

We distinguish between events that modify the state and those that do not.

UpdateEvents are executed in a MonadState context and have to be serialized to disk before they are considered durable.

QueryEvents are executed in a MonadReader context and obviously do not have to be serialized to disk.


UpdateEvent :: UpdateEvent ev => (ev -> Update st (EventResult ev)) -> Event st 
QueryEvent :: QueryEvent ev => (ev -> Query st (EventResult ev)) -> Event st 

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.


MonadState st (Update st) 
Monad (Update st) 

data Query st a Source

Context monad for Query events.


MonadReader st (Query st) 
Monad (Query st) 



:: (Typeable st, Binary st) 
=> [Event st]

List of events capable of updating or querying the state.

-> st

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

-> IO (AcidState st) 

Create an AcidState given a list of events (aka. transactions) and an initial value.

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

closeAcidState :: AcidState st -> IO ()Source

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

createCheckpoint :: Binary st => 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 st -> 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 st -> event -> IO (EventResult event)Source

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