tbox-0.1.0: Transactional variables and data structures with IO hooks

Portabilitynon-portable (requires STM)
Stabilityexperimental
MaintainerPeter Robinson <thaldyron@gmail.com>

Control.Concurrent.TFile

Contents

Description

A transactional variable that writes its content to a file when updated. Due to the atomicity guarantees of the AdvSTM monad, an update to a TFile is only committed when the file operation succeeds.

This module should be imported qualified.

Synopsis

Data type

data TFile k a Source

A transactional variable that writes its content to a file on each update. The file is created in directory "./_TFile/".

  • The onCommit hook of the AdvSTM monad guarantee that the updated memory content of the TFile is only visible to other threads iff the file has been written successfully.
  • If the TFile is "dirty", the content is (re)read from the file on the next read.

Instances

Typeable2 TFile 
(Binary a, Show k) => TBox TFile k a 

new :: TBox t k a => k -> a -> AdvSTM (t k a)Source

Takes a key and an initial value

newIO :: TBox t k a => k -> a -> IO (t k a)Source

Takes a key and an initial value. Has a default implementation.

newFromFileIO :: (Read k, TBox TFile k a) => FilePath -> IO (TFile k a, k)Source

Tries to construct a TFile from a given filepath. Reads the content of the file into memory.

newEmpty :: TBox t k a => k -> AdvSTM (t k a)Source

Takes a key and returns an empty t

newEmptyIO :: TBox t k a => k -> IO (t k a)Source

Takes a key and returns an empty t. Has a default implementation.

newEmptyFromFileIO :: (Read k, TBox TFile k a) => FilePath -> IO (TFile k a, k)Source

Tries to construct a TFile from a given filepath. Note that the content of the file is read into memory only on demand, i.e., when executing TBox.read. Throws AssertionFailed if the filename could not be parsed.

Operations

read :: TBox t k a => t k a -> AdvSTM (Maybe a)Source

If the TBox is dirty, this retries the transaction and rereads the content using readIO in a separate thread. Otherwise it simply returns the result of readSTM.

Note: Depending on the instance implementation, careless use of setDirty and read in the same transaction might lead to nonterminating retry loops.

write :: TBox t k a => t k a -> a -> AdvSTM ()Source

Writes the new content.

clear :: TBox t k a => t k a -> AdvSTM ()Source

Deletes the content.

isEmpty :: TBox t k a => t k a -> AdvSTM BoolSource

Returns True iff the TBox is empty.

Utilities

basedir :: FilePathSource

Currently set to "./_TFile" TODO: provide interface for updating base directory within a transaction(?)