module Data.Acid.Abstract
( AcidState(..)
, scheduleUpdate
, groupUpdates
, update
, update'
, query
, query'
, mkAnyState
, downcast
) where
import Data.Acid.Common
import Data.Acid.Core
import Control.Concurrent ( MVar, takeMVar )
import Data.ByteString.Lazy ( ByteString )
import Control.Monad ( void )
import Control.Monad.Trans ( MonadIO(liftIO) )
#if __GLASGOW_HASKELL__ >= 707
import Data.Typeable ( Typeable, gcast, typeOf )
#else
import Data.Typeable ( Typeable1, gcast1, typeOf1 )
#endif
data AnyState st where
#if __GLASGOW_HASKELL__ >= 707
AnyState :: Typeable sub_st => sub_st st -> AnyState st
#else
AnyState :: Typeable1 sub_st => sub_st st -> AnyState st
#endif
data AcidState st
= AcidState {
_scheduleUpdate :: forall event. (UpdateEvent event, EventState event ~ st) => event -> IO (MVar (EventResult event))
, scheduleColdUpdate :: Tagged ByteString -> IO (MVar ByteString)
, _query :: forall event. (QueryEvent event, EventState event ~ st) => event -> IO (EventResult event)
, queryCold :: Tagged ByteString -> IO ByteString
,
createCheckpoint :: IO ()
, createArchive :: IO ()
,
closeAcidState :: IO ()
, acidSubState :: AnyState st
}
scheduleUpdate :: UpdateEvent event => AcidState (EventState event) -> event -> IO (MVar (EventResult event))
scheduleUpdate = _scheduleUpdate
groupUpdates :: UpdateEvent event => AcidState (EventState event) -> [event] -> IO ()
groupUpdates acidState events
= go events
where
go [] = return ()
go [x] = void $ update acidState x
go (x:xs) = scheduleUpdate acidState x >> go xs
update :: UpdateEvent event => AcidState (EventState event) -> event -> IO (EventResult event)
update acidState event = takeMVar =<< scheduleUpdate acidState event
update' :: (UpdateEvent event, MonadIO m) => AcidState (EventState event) -> event -> m (EventResult event)
update' acidState event = liftIO (update acidState event)
query :: QueryEvent event => AcidState (EventState event) -> event -> IO (EventResult event)
query = _query
query' :: (QueryEvent event, MonadIO m) => AcidState (EventState event) -> event -> m (EventResult event)
query' acidState event = liftIO (query acidState event)
#if __GLASGOW_HASKELL__ >= 707
mkAnyState :: Typeable sub_st => sub_st st -> AnyState st
#else
mkAnyState :: Typeable1 sub_st => sub_st st -> AnyState st
#endif
mkAnyState = AnyState
#if __GLASGOW_HASKELL__ >= 707
downcast :: (Typeable sub, Typeable st) => AcidState st -> sub st
downcast AcidState{acidSubState = AnyState sub}
= r
where
r = case gcast (Just sub) of
Just (Just x) -> x
_ ->
error $
"Data.Acid: Invalid subtype cast: " ++ show (typeOf sub) ++ " -> " ++ show (typeOf r)
#else
downcast :: Typeable1 sub => AcidState st -> sub st
downcast AcidState{acidSubState = AnyState sub}
= r
where
r = case gcast1 (Just sub) of
Just (Just x) -> x
_ ->
error $
"Data.Acid: Invalid subtype cast: " ++ show (typeOf1 sub) ++ " -> " ++ show (typeOf1 r)
#endif