-- Hoogle documentation, generated by Haddock -- See Hoogle, http://www.haskell.org/hoogle/ -- | Event-based distributed state. -- -- Unplug your machine and restart and have your app recover to exactly -- where it left off. Happstack-State spares you the need to deal with -- all the marshalling, consistency, and configuration headache that you -- would have if you used an external DBMS for this purpose. Its -- component model makes it easy to compose big applications from smaller -- reliable parts. Use event subscription to trigger IO actions and -- support comet-style or irc-bot applications. @package happstack-state @version 0.2.1 module Happstack.State.ComponentSystem -- | Equivalent of [] for type level lists. Used for Components that have -- no dependencies data End End :: End -- | Type level Cons for enumerating type dependencies of a Component data (:+:) h t (:+:) :: h -> t -> :+: h t class (Serialize ev, Serialize res) => UpdateEvent ev res | ev -> res class (Serialize ev, Serialize res) => QueryEvent ev res | ev -> res data Method st Update :: (ev -> Update st res) -> Method st Query :: (ev -> Query st res) -> Method st -- | Displays the type of a Method methodType :: Method t -> String -- | Class for enumerating the set of defined methods by the type of the -- state. Instances should not be defined directly, but using mkMethods class Methods a methods :: (Methods a) => Proxy a -> [Method a] data MethodMap MethodMap :: Map String (Method st) -> MethodMap -- | State type -> method map type ComponentTree = Map String MethodMap -- | State type -> all versions type ComponentVersions = Map String [ByteString] -- | In order to be used as a part of Happstack's MACID state, a data type -- needs to be an instance of Component. The minimal definition is an -- initialValue and the type corresponding to the set of Dependencies. -- Note that the SubHandlers condition will be automatically met if the -- Dependencies is built from End and ':+:' with other instances -- of Component and Methods class (SubHandlers (Dependencies a), Serialize a) => Component a where { type family Dependencies a; { onLoad _ = return () } } initialValue :: (Component a) => a onLoad :: (Component a) => Proxy a -> IO () class SubHandlers a subHandlers :: (SubHandlers a) => a -> Collect () data Collection Collection :: ComponentTree -> ComponentVersions -> [IO ()] -> Collection addItem :: (MonadState Collection m) => String -> MethodMap -> [ByteString] -> IO () -> m () type Collect = State Collection collectHandlers :: (Methods a, Component a) => Proxy a -> (ComponentTree, ComponentVersions, [IO ()]) collectHandlers' :: (Methods a, Component a) => Proxy a -> Collect () dup :: String -> b instance [overlap ok] (Methods a, Component a, SubHandlers b) => SubHandlers (a :+: b) instance [overlap ok] SubHandlers End instance [overlap ok] Show MethodMap instance [overlap ok] Show (Method st) module Happstack.State.ComponentTH -- | This function will derive, for the provided type name and the provided -- list of methods, an instance of Methods for the given type and -- necessary instance of UpdateEvent and QueryEvent to make the provided -- list of functions into Methods. This TH splice needs to be executed -- for any MACID state type in order to have the necessary code for event -- handling generated. Ex: $(mkMethods ''Foo -- ['fooUpdate,fooQuery]) mkMethods :: Name -> [Name] -> Q [Dec] module Happstack.State.Saver data ReaderStream a ReaderStream :: IO () -> IO ([a], Int) -> IO [a] -> ReaderStream a readerClose :: ReaderStream a -> IO () readerGet :: ReaderStream a -> IO ([a], Int) readerGetUncut :: ReaderStream a -> IO [a] data WriterStream a WriterStream :: IO () -> (a -> IO () -> IO ()) -> (a -> IO ()) -> IO Int -> WriterStream a writerClose :: WriterStream a -> IO () writerAdd :: WriterStream a -> a -> IO () -> IO () writerAtomicReplace :: WriterStream a -> a -> IO () writerCut :: WriterStream a -> IO Int data Saver -- | A saver that discards all output NullSaver :: Saver -- | A saver that operates on files. The parameter is the prefix for the -- files. Creates the prefix directory. FileSaver :: String -> Saver -- | Enable queueing. Queue :: Saver -> Saver Memory :: (MVar Store) -> Saver createReader :: (Serialize a) => Saver -> String -> Int -> IO (ReaderStream a) createWriter :: (Serialize a) => Saver -> String -> Int -> IO (WriterStream a) module Happstack.State.Transaction type ExceptionT = SomeException logMT :: Priority -> String -> IO () getTime :: (Integral epochTime) => AnyEv epochTime getEventClockTime :: AnyEv ClockTime getEventId :: (Integral txId) => AnyEv txId data GetCheckpointState GetCheckpointState :: GetCheckpointState newtype SetCheckpointState SetCheckpointState :: ByteString -> SetCheckpointState type TypeString = String data EventHandler UpdateHandler :: (TxContext -> ev -> IO ()) -> (ev -> IO res) -> (Object -> ev) -> EventHandler QueryHandler :: (ev -> IO res) -> (Object -> ev) -> EventHandler type EventMap = Map TypeString EventHandler data EmitInternal EmitInternal :: EventMap -> EmitInternal emitRef :: IORef EmitInternal emitFunc :: (Serialize ev, Typeable res) => EventMap -> TypeString -> ev -> IO res emitEvent' :: (Serialize ev, Typeable res) => TypeString -> ev -> IO res emitEvent :: (Serialize ev, Typeable res) => ev -> IO res setNewEventMap :: EventMap -> IO () data EventItem EventItem :: TxContext -> Dynamic -> EventItem eventContext :: EventItem -> TxContext eventData :: EventItem -> Dynamic eventStreamRef :: MVar (Chan EventItem) initEventStream :: IO () pushEventItem :: (Serialize ev) => TxContext -> ev -> IO () getEventStream :: IO (IO EventItem) createEventMap :: (Methods st, Component st) => MVar TxControl -> Proxy st -> IO EventMap extraEvents :: (Serialize st) => TxRun st -> EventMap allStateTypes :: (Methods a, Component a) => Proxy a -> [TypeString] componentVersions :: (Methods a, Component a) => Proxy a -> Map String [ByteString] componentIO :: (Methods a, Component a) => Proxy a -> [IO ()] createNewTxRun :: IO (TxRun st) setNewStateType :: String -> String getStateType :: String -> String setNewState :: TypeString -> ByteString -> IO () getState :: TypeString -> IO ByteString data SetNewState st SetNewState :: ByteString -> SetNewState st data GetState st GetState :: GetState st -- | Schedule an update and wait for it to complete. When this function -- returns, you're guaranteed the update will be persistent. update :: (MonadIO m, UpdateEvent ev res) => ev -> m res -- | Emit a state query and wait for the result. query :: (MonadIO m, QueryEvent ev res) => ev -> m res quickQuery' :: (Serialize st) => TxRun st -> HR st -> IO () type Runner ev res = IO (IO ev, res -> IO ()) type EH i o = i -> IO o data Event Event :: ev -> Event data IHR st IHR :: TxContext -> ev -> (RunHandler st ev) -> IHR st data HR st HR :: ev -> (RunHandler st ev) -> HR st type RunHandler st ev = TxContext -> st -> IO (Maybe st, IO ()) data Res a Ok :: a -> Res a Error :: ExceptionT -> Res a type EventQueue st = TChan (HR st) type ProcessQueue st = TChan (IHR st) data TxRun st TxRun :: !ProcessQueue st -> !TVar TxContext -> TxRun st txProcessQueue :: TxRun st -> !ProcessQueue st txLastTxContext :: TxRun st -> !TVar TxContext type EvLoaders' st = Map String (ProcessQueue st -> ByteString -> IO (TxId, ByteString)) type EvLoaders = Map String (ByteString -> IO (TxId, ByteString)) setEvLoadersQueue :: ProcessQueue st -> EvLoaders' st -> EvLoaders runObjectEvent :: Object -> IO Object runObjectEventFunc :: Object -> EventMap -> IO Object runColdEvent :: TxContext -> Object -> IO () runColdEventFunc :: TxContext -> Object -> EventMap -> IO () lookupEventHandler :: TypeString -> EventMap -> IO EventHandler eventTString :: (Serialize ev) => ev -> TypeString handleEvent :: (st -> Env -> Ev m res -> STM intermediate) -> (st -> intermediate -> IO (Maybe st, res)) -> (Either ExceptionT res -> IO ()) -> Ev m res -> RunHandler st ev handleQuery :: (Either ExceptionT res -> IO ()) -> Query st res -> RunHandler st ev handleUpdate :: (Either ExceptionT res -> IO ()) -> Update st res -> RunHandler st ev checkDiff :: a -> a -> IO (Maybe a) getEpochMilli :: IO EpochMilli newTxContext :: IO TxContext addTxId :: TxRun st -> TxContext -> STM TxContext globalRandomGen :: MVar StdGen data TxConfig TxConfig :: Seconds -> OperationMode -> Int -> Int -> Int -> TxConfig -- | Perform checkpoint at least every N seconds. txcCheckpointSeconds :: TxConfig -> Seconds txcOperationMode :: TxConfig -> OperationMode -- | Number of active nodes in the cluster (not counting this node). txcClusterSize :: TxConfig -> Int txcClusterPort :: TxConfig -> Int -- | Commits per second. Only applies to cluster mode. txcCommitFrequency :: TxConfig -> Int data TxControl TxControl :: Saver -> MVar (WriterStream EventLogEntry) -> [String] -> Map String [ByteString] -> [(ThreadId, MVar ())] -> TxControl -- | Saver given by the user. ctlSaver :: TxControl -> Saver ctlEventSaver :: TxControl -> MVar (WriterStream EventLogEntry) -- | Types of each component used. ctlAllComponents :: TxControl -> [String] -- | Map listing all versions of a component ctlComponentVersions :: TxControl -> Map String [ByteString] ctlChildren :: TxControl -> [(ThreadId, MVar ())] data EventLogEntry EventLogEntry :: TxContext -> Object -> EventLogEntry data OperationMode SingleMode :: OperationMode ClusterMode :: String -> OperationMode nullTxConfig :: TxConfig runTxLoop :: MVar (WriterStream EventLogEntry) -> ProcessQueue st -> st -> IO () instance [overlap ok] Typeable EventLogEntry instance [overlap ok] Typeable1 GetState instance [overlap ok] Typeable1 SetNewState instance [overlap ok] Show EventLogEntry instance [overlap ok] Serialize EventLogEntry instance [overlap ok] Version EventLogEntry instance [overlap ok] (Typeable st) => QueryEvent (GetState st) ByteString instance [overlap ok] (Typeable st) => UpdateEvent (SetNewState st) () instance [overlap ok] (Typeable st) => Serialize (GetState st) instance [overlap ok] Version (GetState st) instance [overlap ok] (Typeable st) => Serialize (SetNewState st) instance [overlap ok] Version (SetNewState st) instance [overlap ok] UpdateEvent SetCheckpointState () instance [overlap ok] QueryEvent GetCheckpointState ByteString instance [overlap ok] Serialize StdGen instance [overlap ok] Version StdGen instance [overlap ok] Serialize SetCheckpointState instance [overlap ok] Typeable SetCheckpointState instance [overlap ok] Version SetCheckpointState instance [overlap ok] Serialize GetCheckpointState instance [overlap ok] Typeable GetCheckpointState instance [overlap ok] Version GetCheckpointState instance [overlap ok] Serialize TxContext instance [overlap ok] Version TxContext module Happstack.State.Control -- | Starts the MACID system without multimaster support. Uses the default -- Saver. startSystemState :: (Methods a, Component a) => Proxy a -> IO (MVar TxControl) -- | Starts the MACID system with multimaster support. Uses the default -- Saver. startSystemStateMultimaster :: (Methods a, Component a) => Proxy a -> IO (MVar TxControl) -- | Returns the default Saver. stdSaver :: IO Saver -- | Wait for a signal. On unix, a signal is sigINT or sigTERM. On windows, -- the signal is entering e. waitForTermination :: IO () instance [overlap ok] Show Flag instance [overlap ok] Read Target instance [overlap ok] Show Target instance [overlap ok] Eq Target instance [overlap ok] Ord Target instance [overlap ok] LogHandler NullLogger module Happstack.State -- | Monad for ACID event handlers. data Ev m t -- | ACID computations that work with any state and event types. type AnyEv a = forall t. (Monad (t STM), MonadTrans t) => Ev (t STM) a data TxControl -- | Emit a state query and wait for the result. query :: (MonadIO m, QueryEvent ev res) => ev -> m res -- | Schedule an update and wait for it to complete. When this function -- returns, you're guaranteed the update will be persistent. update :: (MonadIO m, UpdateEvent ev res) => ev -> m res type Update state = Ev (StateT state STM) type Query state = Ev (ReaderT state STM) type TxId = Int64 type EpochMilli = Int64 data TxConfig TxConfig :: Seconds -> OperationMode -> Int -> Int -> Int -> TxConfig -- | Perform checkpoint at least every N seconds. txcCheckpointSeconds :: TxConfig -> Seconds txcOperationMode :: TxConfig -> OperationMode -- | Number of active nodes in the cluster (not counting this node). txcClusterSize :: TxConfig -> Int txcClusterPort :: TxConfig -> Int -- | Commits per second. Only applies to cluster mode. txcCommitFrequency :: TxConfig -> Int nullTxConfig :: TxConfig data Saver -- | A saver that discards all output NullSaver :: Saver -- | A saver that operates on files. The parameter is the prefix for the -- files. Creates the prefix directory. FileSaver :: String -> Saver -- | Enable queueing. Queue :: Saver -> Saver Memory :: (MVar Store) -> Saver -- | Use a proxy to force the type of an update action. setUpdateType :: Proxy t -> Update t () proxyUpdate :: Ev (StateT t STM) b -> Proxy t -> Ev (StateT t STM) b -- | Use a proxy to force the type of a query action. setQueryType :: Proxy t -> Query t () proxyQuery :: Ev (ReaderT t STM) b -> Proxy t -> Ev (ReaderT t STM) b -- | Currying version of setUpdateType. asUpdate :: Update t a -> Proxy t -> Update t a -- | Currying version of setQueryType. asQuery :: Query t a -> Proxy t -> Query t a -- | Specialized version of ask askState :: Query st st -- | Specialized version of get getState :: Update st st -- | Specialized version of put. putState :: st -> Update st () -- | Lift an STM action into Ev. liftSTM :: STM a -> AnyEv a class CatchEv m catchEv :: (CatchEv m) => Ev m a -> (SomeException -> a) -> Ev m a -- | Select a part of the environment. sel :: (Env -> b) -> AnyEv b -- | Run a computation with local state. Changes to state will be visible -- to outside. localState :: (outer -> inner) -> (inner -> outer -> outer) -> Ev (StateT inner STM) a -> Ev (StateT outer STM) a -- | Run a computation with local state. localStateReader :: (outer -> inner) -> Ev (ReaderT inner STM) a -> Ev (ReaderT outer STM) a -- | Execute a Query action in the Update monad. runQuery :: Query st a -> Update st a getEventId :: (Integral txId) => AnyEv txId getTime :: (Integral epochTime) => AnyEv epochTime getEventClockTime :: AnyEv ClockTime getEventStream :: IO (IO EventItem) data EventItem EventItem :: TxContext -> Dynamic -> EventItem eventContext :: EventItem -> TxContext eventData :: EventItem -> Dynamic -- | Get a random number. getRandom :: (Random a) => AnyEv a -- | Get a random number inside the range. getRandomR :: (Random a) => (a, a) -> AnyEv a -- | Infer updating functions for a record a_foo :: component -> -- record -> record and withFoo = localState foo a_foo. inferRecordUpdaters :: Name -> Q [Dec] -- | Run the MACID system without multimaster support and with the given -- Saver. runTxSystem :: (Methods st, Component st) => Saver -> Proxy st -> IO (MVar TxControl) -- | Run the MACID system with multimaster support turned on if the first -- argument is True. runTxSystem' :: (Methods st, Component st) => Bool -> Saver -> Proxy st -> IO (MVar TxControl) -- | Shuts down a transaction system shutdownSystem :: MVar TxControl -> IO () -- | Saves and ends the TxControl closeTxControl :: MVar TxControl -> IO () -- | Creates a checkpoint using the provided TxControl. This checkpoint may -- be used as a safe state with which to start the system, e.g. one may -- delete all the serialized events that took place after the last -- checkpoint and start the application fresh from the checkpoint. -- Calling this function manually is the only way to create checkpoints. createCheckpoint :: MVar TxControl -> IO () unsafeIOToEv :: IO a -> AnyEv a