-- Hoogle documentation, generated by Haddock -- See Hoogle, http://www.haskell.org/hoogle/ -- | STM transactions involving persistent storage -- -- While Haskell's STM monad allows you to execute code transactionally, -- it does not allow you to persist the state on disk. This package -- implements a persistent storage bridge that runs inside the already -- existing STM monad. @package persistent-stm @version 0.1.0.0 -- | A scheme for adding persistence to Haskell's STM transactions. A -- DBRef a is like a TVar (Maybe -- a), except that it exists (or not) in persistent storage as well -- as in memory. -- -- The choice of persistent storage is up to the user, and is specified -- with a Persistence. There is a default implementation called -- filePersistence that uses files on disk. Note that -- filePersistence doesn't guarantee transactional atomicity in -- the presence of sudden termination of the process, such as in a power -- outage or system crash. Therefore, for serious use, it's recommended -- that you use a different Persistence implementation based on a -- storage layer with stronger transactional guarantees. -- -- For this scheme to work at all, this process must be the only entity -- to access the persistent storage. You may not even use a -- single-writer, multiple-reader architecture, because consistency -- guarantees for reads, as well, depend on all writes happening in the -- current process. module PersistentSTM data DB -- | Opens a DB using the given Persistence. The caller -- should guarantee that closeDB is called when the DB is -- no longer needed. openDB :: Persistence -> IO DB -- | Closes a DB. When this call returns, all data will be written -- to persistent storage, and the program can exit without possibly -- losing data. closeDB :: DB -> IO () -- | Runs an action with a DB open. The DB will be closed -- when the action is finished. The DB value should not be used -- after the action has returned. withDB :: Persistence -> (DB -> IO a) -> IO a -- | Check that there are at most the given number of queued writes to the -- database, and retries the transaction if so. Adding this to the -- beginning of your transactions can help prevent writes from falling -- too far behind the live data. Prioritizing writes this way can also -- reduce memory usage, because unreachable DBRefs no longer need -- to be retained once they are written to disk. waitForMaxBacklog :: DB -> Int -> STM () -- | Atomically performs an STM transaction just like atomically, -- but also waits for any changes it might have observed in the DB -- to be written to persistent storage before returning. This guarantees -- that a transaction whose results were observed will not be rolled back -- if the program crashes. synchronously :: DB -> STM a -> IO a data DBRef a -- | A type class for things that can be stored in a DBRef. This is similar -- to a serialization class like Binary, but reads have access to -- the DB and the STM monad, which is important because it allows -- for one DBRef to be stored inside the value of another. (In -- this case, decode will call getDBRef.) class Typeable a => DBStorable a decode :: DBStorable a => DB -> ByteString -> STM a decode :: (DBStorable a, Binary a) => DB -> ByteString -> STM a encode :: DBStorable a => a -> ByteString encode :: (DBStorable a, Binary a) => a -> ByteString -- | Retrieves a DBRef from a DB for the given key. Throws an -- exception if the DBRef requested has a different type from a -- previous time the key was used in this process, or if a serialized -- value in persistent storage cannot be parsed. getDBRef :: forall a. DBStorable a => DB -> String -> STM (DBRef a) -- | Gets the value stored in a DBRef. The value is Just -- x if x was last value stored in the database using this -- key, or Nothing if there is no value stored in the database. readDBRef :: DBRef a -> STM (Maybe a) -- | Updates the value stored in a DBRef. The update will be -- persisted to storage soon, but not synchronously. writeDBRef :: DBStorable a => DBRef a -> a -> STM () -- | Deletes the value stored in a DBRef. The delete will be -- persisted to storage soon, but not synchronously. deleteDBRef :: DBStorable a => DBRef a -> STM () -- | A strategy for persisting values from DBRef to some persistent -- storage. The filePersistence implementation is provided as a -- quick way to get started, but note the weaknesses in its -- documentation. -- -- A Persistence can read one value at a time, but should be able -- to atomically write/delete an entire set of keys at once, preferably -- atomically. data Persistence Persistence :: (String -> IO (Maybe ByteString)) -> (Map String (Maybe ByteString) -> IO ()) -> IO () -> Persistence -- | Read a single value from persistent storage. Return the serialized -- representation if it exists, and Nothing otherwise. [persistentRead] :: Persistence -> String -> IO (Maybe ByteString) -- | Write (for Just values) or delete (for Nothing values) -- an entire set of values to persistent storage. The values should -- ideally be written atomically, and if they are not then the -- implementation will be vulnerable to inconsistent data and corruption -- if the process is suddenly terminated. [persistentWrite] :: Persistence -> Map String (Maybe ByteString) -> IO () -- | Perform any cleanup that is needed after the DB is closed. This -- can include releasing locks, for example. [persistentFinish] :: Persistence -> IO () -- | A simple Persistence that stores data in a directory in the -- local filesystem. This is an easy way to get started. However, note -- that because writes are not atomic, your data can be corrupted during -- a crash or power outage. For this reason, it's recommended that you -- use a different Persistence for most applications. filePersistence :: FilePath -> IO Persistence instance GHC.Classes.Eq (PersistentSTM.DBRef a) instance GHC.Classes.Ord (PersistentSTM.DBRef a) instance GHC.Show.Show (PersistentSTM.DBRef a) instance PersistentSTM.DBStorable a => PersistentSTM.DBStorable (PersistentSTM.DBRef a) instance PersistentSTM.DBStorable () instance PersistentSTM.DBStorable GHC.Types.Bool instance PersistentSTM.DBStorable GHC.Types.Char instance PersistentSTM.DBStorable GHC.Types.Double instance PersistentSTM.DBStorable GHC.Types.Float instance PersistentSTM.DBStorable GHC.Types.Int instance PersistentSTM.DBStorable GHC.Int.Int8 instance PersistentSTM.DBStorable GHC.Int.Int16 instance PersistentSTM.DBStorable GHC.Int.Int32 instance PersistentSTM.DBStorable GHC.Int.Int64 instance PersistentSTM.DBStorable GHC.Integer.Type.Integer instance PersistentSTM.DBStorable GHC.Natural.Natural instance PersistentSTM.DBStorable GHC.Types.Ordering instance PersistentSTM.DBStorable GHC.Types.Word instance PersistentSTM.DBStorable GHC.Word.Word8 instance PersistentSTM.DBStorable GHC.Word.Word16 instance PersistentSTM.DBStorable GHC.Word.Word32 instance PersistentSTM.DBStorable GHC.Word.Word64 instance PersistentSTM.DBStorable Data.ByteString.Internal.ByteString instance PersistentSTM.DBStorable Data.ByteString.Lazy.Internal.ByteString instance PersistentSTM.DBStorable Data.ByteString.Short.Internal.ShortByteString instance PersistentSTM.DBStorable a => PersistentSTM.DBStorable [a]