{-# LANGUAGE RankNTypes, TypeFamilies, GADTs, CPP #-}
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 {
AcidState st
-> forall event.
(UpdateEvent event, EventState event ~ st) =>
event -> IO (MVar (EventResult event))
_scheduleUpdate :: forall event. (UpdateEvent event, EventState event ~ st) => event -> IO (MVar (EventResult event))
, AcidState st -> Tagged ByteString -> IO (MVar ByteString)
scheduleColdUpdate :: Tagged ByteString -> IO (MVar ByteString)
, AcidState st
-> forall event.
(QueryEvent event, EventState event ~ st) =>
event -> IO (EventResult event)
_query :: forall event. (QueryEvent event, EventState event ~ st) => event -> IO (EventResult event)
, AcidState st -> Tagged ByteString -> IO ByteString
queryCold :: Tagged ByteString -> IO ByteString
,
AcidState st -> IO ()
createCheckpoint :: IO ()
, AcidState st -> IO ()
createArchive :: IO ()
,
AcidState st -> IO ()
closeAcidState :: IO ()
, AcidState st -> AnyState st
acidSubState :: AnyState st
}
scheduleUpdate :: UpdateEvent event => AcidState (EventState event) -> event -> IO (MVar (EventResult event))
scheduleUpdate :: AcidState (EventState event)
-> event -> IO (MVar (EventResult event))
scheduleUpdate AcidState (EventState event)
acid = AcidState (EventState event)
-> forall event.
(UpdateEvent event, EventState event ~ EventState event) =>
event -> IO (MVar (EventResult event))
forall st.
AcidState st
-> forall event.
(UpdateEvent event, EventState event ~ st) =>
event -> IO (MVar (EventResult event))
_scheduleUpdate AcidState (EventState event)
acid
groupUpdates :: UpdateEvent event => AcidState (EventState event) -> [event] -> IO ()
groupUpdates :: AcidState (EventState event) -> [event] -> IO ()
groupUpdates AcidState (EventState event)
acidState [event]
events
= [event] -> IO ()
go [event]
events
where
go :: [event] -> IO ()
go [] = () -> IO ()
forall (m :: * -> *) a. Monad m => a -> m a
return ()
go [event
x] = IO (MethodResult event) -> IO ()
forall (f :: * -> *) a. Functor f => f a -> f ()
void (IO (MethodResult event) -> IO ())
-> IO (MethodResult event) -> IO ()
forall a b. (a -> b) -> a -> b
$ AcidState (EventState event) -> event -> IO (MethodResult event)
forall event.
UpdateEvent event =>
AcidState (EventState event) -> event -> IO (EventResult event)
update AcidState (EventState event)
acidState event
x
go (event
x:[event]
xs) = AcidState (EventState event)
-> event -> IO (MVar (MethodResult event))
forall event.
UpdateEvent event =>
AcidState (EventState event)
-> event -> IO (MVar (EventResult event))
scheduleUpdate AcidState (EventState event)
acidState event
x IO (MVar (MethodResult event)) -> IO () -> IO ()
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> [event] -> IO ()
go [event]
xs
update :: UpdateEvent event => AcidState (EventState event) -> event -> IO (EventResult event)
update :: AcidState (EventState event) -> event -> IO (EventResult event)
update AcidState (EventState event)
acidState event
event = MVar (EventResult event) -> IO (EventResult event)
forall a. MVar a -> IO a
takeMVar (MVar (EventResult event) -> IO (EventResult event))
-> IO (MVar (EventResult event)) -> IO (EventResult event)
forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< AcidState (EventState event)
-> event -> IO (MVar (EventResult event))
forall event.
UpdateEvent event =>
AcidState (EventState event)
-> event -> IO (MVar (EventResult event))
scheduleUpdate AcidState (EventState event)
acidState event
event
update' :: (UpdateEvent event, MonadIO m) => AcidState (EventState event) -> event -> m (EventResult event)
update' :: AcidState (EventState event) -> event -> m (EventResult event)
update' AcidState (EventState event)
acidState event
event = IO (EventResult event) -> m (EventResult event)
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (AcidState (EventState event) -> event -> IO (EventResult event)
forall event.
UpdateEvent event =>
AcidState (EventState event) -> event -> IO (EventResult event)
update AcidState (EventState event)
acidState event
event)
query :: QueryEvent event => AcidState (EventState event) -> event -> IO (EventResult event)
query :: AcidState (EventState event) -> event -> IO (EventResult event)
query AcidState (EventState event)
acid = AcidState (EventState event)
-> forall event.
(QueryEvent event, EventState event ~ EventState event) =>
event -> IO (EventResult event)
forall st.
AcidState st
-> forall event.
(QueryEvent event, EventState event ~ st) =>
event -> IO (EventResult event)
_query AcidState (EventState event)
acid
query' :: (QueryEvent event, MonadIO m) => AcidState (EventState event) -> event -> m (EventResult event)
query' :: AcidState (EventState event) -> event -> m (EventResult event)
query' AcidState (EventState event)
acidState event
event = IO (EventResult event) -> m (EventResult event)
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (AcidState (EventState event) -> event -> IO (EventResult event)
forall event.
QueryEvent event =>
AcidState (EventState event) -> event -> IO (EventResult event)
query AcidState (EventState event)
acidState event
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 :: sub_st st -> AnyState st
mkAnyState = sub_st st -> AnyState st
forall (sub_st :: * -> *) st.
Typeable sub_st =>
sub_st st -> AnyState st
AnyState
#if __GLASGOW_HASKELL__ >= 707
downcast :: (Typeable sub, Typeable st) => AcidState st -> sub st
downcast :: AcidState st -> sub st
downcast AcidState{acidSubState :: forall st. AcidState st -> AnyState st
acidSubState = AnyState sub_st st
sub}
= sub st
r
where
r :: sub st
r = case Maybe (sub_st st) -> Maybe (Maybe (sub st))
forall k (a :: k) (b :: k) (c :: k -> *).
(Typeable a, Typeable b) =>
c a -> Maybe (c b)
gcast (sub_st st -> Maybe (sub_st st)
forall a. a -> Maybe a
Just sub_st st
sub) of
Just (Just sub st
x) -> sub st
x
Maybe (Maybe (sub st))
_ ->
[Char] -> sub st
forall a. HasCallStack => [Char] -> a
error ([Char] -> sub st) -> [Char] -> sub st
forall a b. (a -> b) -> a -> b
$
[Char]
"Data.Acid.Abstract: Invalid subtype cast: " [Char] -> [Char] -> [Char]
forall a. [a] -> [a] -> [a]
++ TypeRep -> [Char]
forall a. Show a => a -> [Char]
show (sub_st st -> TypeRep
forall a. Typeable a => a -> TypeRep
typeOf sub_st st
sub) [Char] -> [Char] -> [Char]
forall a. [a] -> [a] -> [a]
++ [Char]
" -> " [Char] -> [Char] -> [Char]
forall a. [a] -> [a] -> [a]
++ TypeRep -> [Char]
forall a. Show a => a -> [Char]
show (sub st -> TypeRep
forall a. Typeable a => a -> TypeRep
typeOf sub st
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.Abstract: Invalid subtype cast: " ++ show (typeOf1 sub) ++ " -> " ++ show (typeOf1 r)
#endif