{-# LANGUAGE DeriveDataTypeable, BangPatterns, CPP #-}
module Data.Acid.Local
( openLocalState
, openLocalStateFrom
, openLocalStateWithSerialiser
, prepareLocalState
, prepareLocalStateFrom
, prepareLocalStateWithSerialiser
, defaultStateDirectory
, scheduleLocalUpdate'
, scheduleLocalColdUpdate'
, createCheckpointAndClose
, LocalState(..)
, Checkpoint(..)
, SerialisationLayer(..)
, defaultSerialisationLayer
, mkEventsLogKey
, mkCheckpointsLogKey
) where
import Data.Acid.Archive
import Data.Acid.Log as Log
import Data.Acid.Core
import Data.Acid.Common
import Data.Acid.Abstract
import Control.Concurrent ( newEmptyMVar, putMVar, takeMVar, MVar )
import Control.Exception ( onException, evaluate, Exception, throwIO )
import Control.Monad.State ( runState )
import Control.Monad ( join )
#if !MIN_VERSION_base(4,8,0)
import Control.Applicative ( (<$>), (<*>) )
#endif
import Data.ByteString.Lazy ( ByteString )
import qualified Data.ByteString.Lazy as Lazy ( length )
import Data.Serialize ( runPutLazy, runGetLazy )
import Data.SafeCopy ( SafeCopy(..), safeGet, safePut
, primitive, contain )
import Data.Typeable ( Typeable, typeOf )
import Data.IORef
import System.FilePath ( (</>), takeDirectory )
import System.FileLock
import System.Directory ( createDirectoryIfMissing )
data LocalState st
= LocalState { forall st. LocalState st -> Core st
localCore :: Core st
, forall st. LocalState st -> IORef st
localCopy :: IORef st
, forall st. LocalState st -> FileLog (Tagged ByteString)
localEvents :: FileLog (Tagged ByteString)
, forall st. LocalState st -> FileLog (Checkpoint st)
localCheckpoints :: FileLog (Checkpoint st)
, forall st. LocalState st -> FileLock
localLock :: FileLock
} deriving (Typeable)
newtype StateIsLocked = StateIsLocked FilePath deriving (Int -> StateIsLocked -> ShowS
[StateIsLocked] -> ShowS
StateIsLocked -> String
(Int -> StateIsLocked -> ShowS)
-> (StateIsLocked -> String)
-> ([StateIsLocked] -> ShowS)
-> Show StateIsLocked
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> StateIsLocked -> ShowS
showsPrec :: Int -> StateIsLocked -> ShowS
$cshow :: StateIsLocked -> String
show :: StateIsLocked -> String
$cshowList :: [StateIsLocked] -> ShowS
showList :: [StateIsLocked] -> ShowS
Show, Typeable)
instance Exception StateIsLocked
scheduleLocalUpdate :: UpdateEvent event => LocalState (EventState event) -> event -> IO (MVar (EventResult event))
scheduleLocalUpdate :: forall event.
UpdateEvent event =>
LocalState (EventState event)
-> event -> IO (MVar (EventResult event))
scheduleLocalUpdate LocalState (EventState event)
acidState event
event
= do MVar (EventResult event)
mvar <- IO (MVar (EventResult event))
forall a. IO (MVar a)
newEmptyMVar
let encoded :: ByteString
encoded = MethodSerialiser event -> event -> ByteString
forall method. MethodSerialiser method -> method -> ByteString
encodeMethod MethodSerialiser event
ms event
event
Int64 -> IO Int64
forall a. a -> IO a
evaluate (ByteString -> Int64
Lazy.length ByteString
encoded)
Core (EventState event)
-> (EventState event -> IO (EventState event)) -> IO ()
forall st. Core st -> (st -> IO st) -> IO ()
modifyCoreState_ (LocalState (EventState event) -> Core (EventState event)
forall st. LocalState st -> Core st
localCore LocalState (EventState event)
acidState) ((EventState event -> IO (EventState event)) -> IO ())
-> (EventState event -> IO (EventState event)) -> IO ()
forall a b. (a -> b) -> a -> b
$ \EventState event
st ->
do let !(EventResult event
result, !EventState event
st') = State (EventState event) (EventResult event)
-> EventState event -> (EventResult event, EventState event)
forall s a. State s a -> s -> (a, s)
runState State (EventState event) (EventResult event)
hotMethod EventState event
st
FileLog (Tagged ByteString) -> Tagged ByteString -> IO () -> IO ()
forall object. FileLog object -> object -> IO () -> IO ()
pushEntry (LocalState (EventState event) -> FileLog (Tagged ByteString)
forall st. LocalState st -> FileLog (Tagged ByteString)
localEvents LocalState (EventState event)
acidState) (event -> ByteString
forall ev. Method ev => ev -> ByteString
methodTag event
event, ByteString
encoded) (IO () -> IO ()) -> IO () -> IO ()
forall a b. (a -> b) -> a -> b
$ do IORef (EventState event) -> EventState event -> IO ()
forall a. IORef a -> a -> IO ()
writeIORef (LocalState (EventState event) -> IORef (EventState event)
forall st. LocalState st -> IORef st
localCopy LocalState (EventState event)
acidState) EventState event
st'
MVar (EventResult event) -> EventResult event -> IO ()
forall a. MVar a -> a -> IO ()
putMVar MVar (EventResult event)
mvar EventResult event
result
EventState event -> IO (EventState event)
forall a. a -> IO a
forall (m :: * -> *) a. Monad m => a -> m a
return EventState event
st'
MVar (EventResult event) -> IO (MVar (EventResult event))
forall a. a -> IO a
forall (m :: * -> *) a. Monad m => a -> m a
return MVar (EventResult event)
mvar
where (State (EventState event) (EventResult event)
hotMethod, MethodSerialiser event
ms) = MethodMap (EventState event)
-> event
-> (State (EventState event) (EventResult event),
MethodSerialiser event)
forall method.
Method method =>
MethodMap (MethodState method)
-> method
-> (State (MethodState method) (MethodResult method),
MethodSerialiser method)
lookupHotMethodAndSerialiser (Core (EventState event) -> MethodMap (EventState event)
forall st. Core st -> MethodMap st
coreMethods (LocalState (EventState event) -> Core (EventState event)
forall st. LocalState st -> Core st
localCore LocalState (EventState event)
acidState)) event
event
scheduleLocalUpdate' :: UpdateEvent event => LocalState (EventState event) -> event -> MVar (EventResult event) -> IO (IO ())
scheduleLocalUpdate' :: forall event.
UpdateEvent event =>
LocalState (EventState event)
-> event -> MVar (EventResult event) -> IO (IO ())
scheduleLocalUpdate' LocalState (EventState event)
acidState event
event MVar (EventResult event)
mvar
= do
let encoded :: ByteString
encoded = MethodSerialiser event -> event -> ByteString
forall method. MethodSerialiser method -> method -> ByteString
encodeMethod MethodSerialiser event
ms event
event
Int64 -> IO Int64
forall a. a -> IO a
evaluate (ByteString -> Int64
Lazy.length ByteString
encoded)
IO ()
act <- Core (EventState event)
-> (EventState event -> IO (EventState event, IO ())) -> IO (IO ())
forall st a. Core st -> (st -> IO (st, a)) -> IO a
modifyCoreState (LocalState (EventState event) -> Core (EventState event)
forall st. LocalState st -> Core st
localCore LocalState (EventState event)
acidState) ((EventState event -> IO (EventState event, IO ())) -> IO (IO ()))
-> (EventState event -> IO (EventState event, IO ())) -> IO (IO ())
forall a b. (a -> b) -> a -> b
$ \EventState event
st ->
do let !(EventResult event
result, !EventState event
st') = State (EventState event) (EventResult event)
-> EventState event -> (EventResult event, EventState event)
forall s a. State s a -> s -> (a, s)
runState State (EventState event) (EventResult event)
hotMethod EventState event
st
FileLog (Tagged ByteString) -> Tagged ByteString -> IO () -> IO ()
forall object. FileLog object -> object -> IO () -> IO ()
pushEntry (LocalState (EventState event) -> FileLog (Tagged ByteString)
forall st. LocalState st -> FileLog (Tagged ByteString)
localEvents LocalState (EventState event)
acidState) (event -> ByteString
forall ev. Method ev => ev -> ByteString
methodTag event
event, ByteString
encoded) (IO () -> IO ()) -> IO () -> IO ()
forall a b. (a -> b) -> a -> b
$ () -> IO ()
forall a. a -> IO a
forall (m :: * -> *) a. Monad m => a -> m a
return ()
let action :: IO ()
action = do IORef (EventState event) -> EventState event -> IO ()
forall a. IORef a -> a -> IO ()
writeIORef (LocalState (EventState event) -> IORef (EventState event)
forall st. LocalState st -> IORef st
localCopy LocalState (EventState event)
acidState) EventState event
st'
MVar (EventResult event) -> EventResult event -> IO ()
forall a. MVar a -> a -> IO ()
putMVar MVar (EventResult event)
mvar EventResult event
result
(EventState event, IO ()) -> IO (EventState event, IO ())
forall a. a -> IO a
forall (m :: * -> *) a. Monad m => a -> m a
return (EventState event
st', IO ()
action)
IO () -> IO (IO ())
forall a. a -> IO a
forall (m :: * -> *) a. Monad m => a -> m a
return IO ()
act
where (State (EventState event) (EventResult event)
hotMethod, MethodSerialiser event
ms) = MethodMap (EventState event)
-> event
-> (State (EventState event) (EventResult event),
MethodSerialiser event)
forall method.
Method method =>
MethodMap (MethodState method)
-> method
-> (State (MethodState method) (MethodResult method),
MethodSerialiser method)
lookupHotMethodAndSerialiser (Core (EventState event) -> MethodMap (EventState event)
forall st. Core st -> MethodMap st
coreMethods (LocalState (EventState event) -> Core (EventState event)
forall st. LocalState st -> Core st
localCore LocalState (EventState event)
acidState)) event
event
scheduleLocalColdUpdate :: LocalState st -> Tagged ByteString -> IO (MVar ByteString)
scheduleLocalColdUpdate :: forall st.
LocalState st -> Tagged ByteString -> IO (MVar ByteString)
scheduleLocalColdUpdate LocalState st
acidState Tagged ByteString
event
= do MVar ByteString
mvar <- IO (MVar ByteString)
forall a. IO (MVar a)
newEmptyMVar
Core st -> (st -> IO st) -> IO ()
forall st. Core st -> (st -> IO st) -> IO ()
modifyCoreState_ (LocalState st -> Core st
forall st. LocalState st -> Core st
localCore LocalState st
acidState) ((st -> IO st) -> IO ()) -> (st -> IO st) -> IO ()
forall a b. (a -> b) -> a -> b
$ \st
st ->
do let !(ByteString
result, !st
st') = State st ByteString -> st -> (ByteString, st)
forall s a. State s a -> s -> (a, s)
runState State st ByteString
coldMethod st
st
FileLog (Tagged ByteString) -> Tagged ByteString -> IO () -> IO ()
forall object. FileLog object -> object -> IO () -> IO ()
pushEntry (LocalState st -> FileLog (Tagged ByteString)
forall st. LocalState st -> FileLog (Tagged ByteString)
localEvents LocalState st
acidState) Tagged ByteString
event (IO () -> IO ()) -> IO () -> IO ()
forall a b. (a -> b) -> a -> b
$ do IORef st -> st -> IO ()
forall a. IORef a -> a -> IO ()
writeIORef (LocalState st -> IORef st
forall st. LocalState st -> IORef st
localCopy LocalState st
acidState) st
st'
MVar ByteString -> ByteString -> IO ()
forall a. MVar a -> a -> IO ()
putMVar MVar ByteString
mvar ByteString
result
st -> IO st
forall a. a -> IO a
forall (m :: * -> *) a. Monad m => a -> m a
return st
st'
MVar ByteString -> IO (MVar ByteString)
forall a. a -> IO a
forall (m :: * -> *) a. Monad m => a -> m a
return MVar ByteString
mvar
where coldMethod :: State st ByteString
coldMethod = Core st -> Tagged ByteString -> State st ByteString
forall st. Core st -> Tagged ByteString -> State st ByteString
lookupColdMethod (LocalState st -> Core st
forall st. LocalState st -> Core st
localCore LocalState st
acidState) Tagged ByteString
event
scheduleLocalColdUpdate' :: LocalState st -> Tagged ByteString -> MVar ByteString -> IO (IO ())
scheduleLocalColdUpdate' :: forall st.
LocalState st -> Tagged ByteString -> MVar ByteString -> IO (IO ())
scheduleLocalColdUpdate' LocalState st
acidState Tagged ByteString
event MVar ByteString
mvar
= do IO ()
act <- Core st -> (st -> IO (st, IO ())) -> IO (IO ())
forall st a. Core st -> (st -> IO (st, a)) -> IO a
modifyCoreState (LocalState st -> Core st
forall st. LocalState st -> Core st
localCore LocalState st
acidState) ((st -> IO (st, IO ())) -> IO (IO ()))
-> (st -> IO (st, IO ())) -> IO (IO ())
forall a b. (a -> b) -> a -> b
$ \st
st ->
do let !(ByteString
result, !st
st') = State st ByteString -> st -> (ByteString, st)
forall s a. State s a -> s -> (a, s)
runState State st ByteString
coldMethod st
st
FileLog (Tagged ByteString) -> Tagged ByteString -> IO () -> IO ()
forall object. FileLog object -> object -> IO () -> IO ()
pushEntry (LocalState st -> FileLog (Tagged ByteString)
forall st. LocalState st -> FileLog (Tagged ByteString)
localEvents LocalState st
acidState) Tagged ByteString
event (IO () -> IO ()) -> IO () -> IO ()
forall a b. (a -> b) -> a -> b
$ () -> IO ()
forall a. a -> IO a
forall (m :: * -> *) a. Monad m => a -> m a
return ()
let action :: IO ()
action = do IORef st -> st -> IO ()
forall a. IORef a -> a -> IO ()
writeIORef (LocalState st -> IORef st
forall st. LocalState st -> IORef st
localCopy LocalState st
acidState) st
st'
MVar ByteString -> ByteString -> IO ()
forall a. MVar a -> a -> IO ()
putMVar MVar ByteString
mvar ByteString
result
(st, IO ()) -> IO (st, IO ())
forall a. a -> IO a
forall (m :: * -> *) a. Monad m => a -> m a
return (st
st', IO ()
action)
IO () -> IO (IO ())
forall a. a -> IO a
forall (m :: * -> *) a. Monad m => a -> m a
return IO ()
act
where coldMethod :: State st ByteString
coldMethod = Core st -> Tagged ByteString -> State st ByteString
forall st. Core st -> Tagged ByteString -> State st ByteString
lookupColdMethod (LocalState st -> Core st
forall st. LocalState st -> Core st
localCore LocalState st
acidState) Tagged ByteString
event
localQuery :: QueryEvent event => LocalState (EventState event) -> event -> IO (EventResult event)
localQuery :: forall event.
QueryEvent event =>
LocalState (EventState event) -> event -> IO (EventResult event)
localQuery LocalState (EventState event)
acidState event
event
= do EventState event
st <- IORef (EventState event) -> IO (EventState event)
forall a. IORef a -> IO a
readIORef (LocalState (EventState event) -> IORef (EventState event)
forall st. LocalState st -> IORef st
localCopy LocalState (EventState event)
acidState)
let (EventResult event
result, EventState event
_st) = State (EventState event) (EventResult event)
-> EventState event -> (EventResult event, EventState event)
forall s a. State s a -> s -> (a, s)
runState State (EventState event) (EventResult event)
hotMethod EventState event
st
EventResult event -> IO (EventResult event)
forall a. a -> IO a
forall (m :: * -> *) a. Monad m => a -> m a
return EventResult event
result
where hotMethod :: State (EventState event) (EventResult event)
hotMethod = MethodMap (EventState event)
-> event -> State (EventState event) (EventResult event)
forall method.
Method method =>
MethodMap (MethodState method)
-> method -> State (MethodState method) (MethodResult method)
lookupHotMethod (Core (EventState event) -> MethodMap (EventState event)
forall st. Core st -> MethodMap st
coreMethods (LocalState (EventState event) -> Core (EventState event)
forall st. LocalState st -> Core st
localCore LocalState (EventState event)
acidState)) event
event
localQueryCold :: LocalState st -> Tagged ByteString -> IO ByteString
localQueryCold :: forall st. LocalState st -> Tagged ByteString -> IO ByteString
localQueryCold LocalState st
acidState Tagged ByteString
event
= do st
st <- IORef st -> IO st
forall a. IORef a -> IO a
readIORef (LocalState st -> IORef st
forall st. LocalState st -> IORef st
localCopy LocalState st
acidState)
let (ByteString
result, st
_st) = State st ByteString -> st -> (ByteString, st)
forall s a. State s a -> s -> (a, s)
runState State st ByteString
coldMethod st
st
ByteString -> IO ByteString
forall a. a -> IO a
forall (m :: * -> *) a. Monad m => a -> m a
return ByteString
result
where coldMethod :: State st ByteString
coldMethod = Core st -> Tagged ByteString -> State st ByteString
forall st. Core st -> Tagged ByteString -> State st ByteString
lookupColdMethod (LocalState st -> Core st
forall st. LocalState st -> Core st
localCore LocalState st
acidState) Tagged ByteString
event
createLocalCheckpoint :: IsAcidic st => LocalState st -> IO ()
createLocalCheckpoint :: forall st. IsAcidic st => LocalState st -> IO ()
createLocalCheckpoint LocalState st
acidState
= do FileLog (Tagged ByteString) -> IO Int
forall object. FileLog object -> IO Int
cutFileLog (LocalState st -> FileLog (Tagged ByteString)
forall st. LocalState st -> FileLog (Tagged ByteString)
localEvents LocalState st
acidState)
MVar ()
mvar <- IO (MVar ())
forall a. IO (MVar a)
newEmptyMVar
Core st -> (st -> IO ()) -> IO ()
forall st a. Core st -> (st -> IO a) -> IO a
withCoreState (LocalState st -> Core st
forall st. LocalState st -> Core st
localCore LocalState st
acidState) ((st -> IO ()) -> IO ()) -> (st -> IO ()) -> IO ()
forall a b. (a -> b) -> a -> b
$ \st
st ->
do Int
eventId <- FileLog (Tagged ByteString) -> IO Int
forall object. FileLog object -> IO Int
askCurrentEntryId (LocalState st -> FileLog (Tagged ByteString)
forall st. LocalState st -> FileLog (Tagged ByteString)
localEvents LocalState st
acidState)
FileLog (Tagged ByteString) -> IO () -> IO ()
forall object. FileLog object -> IO () -> IO ()
pushAction (LocalState st -> FileLog (Tagged ByteString)
forall st. LocalState st -> FileLog (Tagged ByteString)
localEvents LocalState st
acidState) (IO () -> IO ()) -> IO () -> IO ()
forall a b. (a -> b) -> a -> b
$
FileLog (Checkpoint st) -> Checkpoint st -> IO () -> IO ()
forall object. FileLog object -> object -> IO () -> IO ()
pushEntry (LocalState st -> FileLog (Checkpoint st)
forall st. LocalState st -> FileLog (Checkpoint st)
localCheckpoints LocalState st
acidState) (Int -> st -> Checkpoint st
forall s. Int -> s -> Checkpoint s
Checkpoint Int
eventId st
st) (MVar () -> () -> IO ()
forall a. MVar a -> a -> IO ()
putMVar MVar ()
mvar ())
MVar () -> IO ()
forall a. MVar a -> IO a
takeMVar MVar ()
mvar
createCheckpointAndClose :: (IsAcidic st, Typeable st) => AcidState st -> IO ()
createCheckpointAndClose :: forall st. (IsAcidic st, Typeable st) => AcidState st -> IO ()
createCheckpointAndClose AcidState st
abstract_state
= do MVar ()
mvar <- IO (MVar ())
forall a. IO (MVar a)
newEmptyMVar
Core st -> (st -> IO ()) -> IO ()
forall st. Core st -> (st -> IO ()) -> IO ()
closeCore' (LocalState st -> Core st
forall st. LocalState st -> Core st
localCore LocalState st
acidState) ((st -> IO ()) -> IO ()) -> (st -> IO ()) -> IO ()
forall a b. (a -> b) -> a -> b
$ \st
st ->
do Int
eventId <- FileLog (Tagged ByteString) -> IO Int
forall object. FileLog object -> IO Int
askCurrentEntryId (LocalState st -> FileLog (Tagged ByteString)
forall st. LocalState st -> FileLog (Tagged ByteString)
localEvents LocalState st
acidState)
FileLog (Tagged ByteString) -> IO () -> IO ()
forall object. FileLog object -> IO () -> IO ()
pushAction (LocalState st -> FileLog (Tagged ByteString)
forall st. LocalState st -> FileLog (Tagged ByteString)
localEvents LocalState st
acidState) (IO () -> IO ()) -> IO () -> IO ()
forall a b. (a -> b) -> a -> b
$
FileLog (Checkpoint st) -> Checkpoint st -> IO () -> IO ()
forall object. FileLog object -> object -> IO () -> IO ()
pushEntry (LocalState st -> FileLog (Checkpoint st)
forall st. LocalState st -> FileLog (Checkpoint st)
localCheckpoints LocalState st
acidState) (Int -> st -> Checkpoint st
forall s. Int -> s -> Checkpoint s
Checkpoint Int
eventId st
st) (MVar () -> () -> IO ()
forall a. MVar a -> a -> IO ()
putMVar MVar ()
mvar ())
MVar () -> IO ()
forall a. MVar a -> IO a
takeMVar MVar ()
mvar
FileLog (Tagged ByteString) -> IO ()
forall object. FileLog object -> IO ()
closeFileLog (LocalState st -> FileLog (Tagged ByteString)
forall st. LocalState st -> FileLog (Tagged ByteString)
localEvents LocalState st
acidState)
FileLog (Checkpoint st) -> IO ()
forall object. FileLog object -> IO ()
closeFileLog (LocalState st -> FileLog (Checkpoint st)
forall st. LocalState st -> FileLog (Checkpoint st)
localCheckpoints LocalState st
acidState)
FileLock -> IO ()
unlockFile (LocalState st -> FileLock
forall st. LocalState st -> FileLock
localLock LocalState st
acidState)
where acidState :: LocalState st
acidState = AcidState st -> LocalState st
forall (sub :: * -> *) st.
(Typeable sub, Typeable st) =>
AcidState st -> sub st
downcast AcidState st
abstract_state
data Checkpoint s = Checkpoint EntryId s
instance SafeCopy s => SafeCopy (Checkpoint s) where
kind :: Kind (Checkpoint s)
kind = Kind (Checkpoint s)
forall a. Kind a
primitive
putCopy :: Checkpoint s -> Contained Put
putCopy (Checkpoint Int
eventEntryId s
content)
= Put -> Contained Put
forall a. a -> Contained a
contain (Put -> Contained Put) -> Put -> Contained Put
forall a b. (a -> b) -> a -> b
$
do Int -> Put
forall a. SafeCopy a => a -> Put
safePut Int
eventEntryId
ByteString -> Put
forall a. SafeCopy a => a -> Put
safePut (Put -> ByteString
runPutLazy (s -> Put
forall a. SafeCopy a => a -> Put
safePut s
content))
getCopy :: Contained (Get (Checkpoint s))
getCopy = Get (Checkpoint s) -> Contained (Get (Checkpoint s))
forall a. a -> Contained a
contain (Get (Checkpoint s) -> Contained (Get (Checkpoint s)))
-> Get (Checkpoint s) -> Contained (Get (Checkpoint s))
forall a b. (a -> b) -> a -> b
$ Int -> s -> Checkpoint s
forall s. Int -> s -> Checkpoint s
Checkpoint (Int -> s -> Checkpoint s) -> Get Int -> Get (s -> Checkpoint s)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Get Int
forall a. SafeCopy a => Get a
safeGet Get (s -> Checkpoint s) -> Get s -> Get (Checkpoint s)
forall a b. Get (a -> b) -> Get a -> Get b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> (ByteString -> s
forall {a}. SafeCopy a => ByteString -> a
fromNested (ByteString -> s) -> Get ByteString -> Get s
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Get ByteString
forall a. SafeCopy a => Get a
safeGet)
where
fromNested :: ByteString -> a
fromNested ByteString
b = case Get a -> ByteString -> Either String a
forall a. Get a -> ByteString -> Either String a
runGetLazy Get a
forall a. SafeCopy a => Get a
safeGet ByteString
b of
Left String
msg -> String -> a
forall {a}. String -> a
checkpointRestoreError String
msg
Right a
v -> a
v
errorTypeName :: Proxy (Checkpoint s) -> String
errorTypeName Proxy (Checkpoint s)
s = String
"Checkpoint " String -> ShowS
forall a. [a] -> [a] -> [a]
++ Proxy (Checkpoint s) -> String
forall a. SafeCopy a => Proxy a -> String
errorTypeName Proxy (Checkpoint s)
s
openLocalState :: (Typeable st, IsAcidic st, SafeCopy st)
=> st
-> IO (AcidState st)
openLocalState :: forall st.
(Typeable st, IsAcidic st, SafeCopy st) =>
st -> IO (AcidState st)
openLocalState st
initialState =
String -> st -> IO (AcidState st)
forall st.
(IsAcidic st, SafeCopy st) =>
String -> st -> IO (AcidState st)
openLocalStateFrom (st -> String
forall st. Typeable st => st -> String
defaultStateDirectory st
initialState) st
initialState
prepareLocalState :: (Typeable st, IsAcidic st, SafeCopy st)
=> st
-> IO (IO (AcidState st))
prepareLocalState :: forall st.
(Typeable st, IsAcidic st, SafeCopy st) =>
st -> IO (IO (AcidState st))
prepareLocalState st
initialState =
String -> st -> IO (IO (AcidState st))
forall st.
(IsAcidic st, SafeCopy st) =>
String -> st -> IO (IO (AcidState st))
prepareLocalStateFrom (st -> String
forall st. Typeable st => st -> String
defaultStateDirectory st
initialState) st
initialState
defaultStateDirectory :: Typeable st => st -> FilePath
defaultStateDirectory :: forall st. Typeable st => st -> String
defaultStateDirectory st
initialState = String
"state" String -> ShowS
</> TypeRep -> String
forall a. Show a => a -> String
show (st -> TypeRep
forall a. Typeable a => a -> TypeRep
typeOf st
initialState)
openLocalStateFrom :: (IsAcidic st, SafeCopy st)
=> FilePath
-> st
-> IO (AcidState st)
openLocalStateFrom :: forall st.
(IsAcidic st, SafeCopy st) =>
String -> st -> IO (AcidState st)
openLocalStateFrom String
directory st
initialState =
String -> st -> SerialisationLayer st -> IO (AcidState st)
forall st.
IsAcidic st =>
String -> st -> SerialisationLayer st -> IO (AcidState st)
openLocalStateWithSerialiser String
directory st
initialState SerialisationLayer st
forall st. SafeCopy st => SerialisationLayer st
defaultSerialisationLayer
openLocalStateWithSerialiser :: (IsAcidic st)
=> FilePath
-> st
-> SerialisationLayer st
-> IO (AcidState st)
openLocalStateWithSerialiser :: forall st.
IsAcidic st =>
String -> st -> SerialisationLayer st -> IO (AcidState st)
openLocalStateWithSerialiser String
directory st
initialState SerialisationLayer st
serialisationLayer =
IO (IO (AcidState st)) -> IO (AcidState st)
forall (m :: * -> *) a. Monad m => m (m a) -> m a
join (IO (IO (AcidState st)) -> IO (AcidState st))
-> IO (IO (AcidState st)) -> IO (AcidState st)
forall a b. (a -> b) -> a -> b
$ String
-> st -> Bool -> SerialisationLayer st -> IO (IO (AcidState st))
forall st.
IsAcidic st =>
String
-> st -> Bool -> SerialisationLayer st -> IO (IO (AcidState st))
resumeLocalStateFrom String
directory st
initialState Bool
False SerialisationLayer st
serialisationLayer
prepareLocalStateFrom :: (IsAcidic st, SafeCopy st)
=> FilePath
-> st
-> IO (IO (AcidState st))
prepareLocalStateFrom :: forall st.
(IsAcidic st, SafeCopy st) =>
String -> st -> IO (IO (AcidState st))
prepareLocalStateFrom String
directory st
initialState =
String -> st -> SerialisationLayer st -> IO (IO (AcidState st))
forall st.
IsAcidic st =>
String -> st -> SerialisationLayer st -> IO (IO (AcidState st))
prepareLocalStateWithSerialiser String
directory st
initialState SerialisationLayer st
forall st. SafeCopy st => SerialisationLayer st
defaultSerialisationLayer
prepareLocalStateWithSerialiser :: (IsAcidic st)
=> FilePath
-> st
-> SerialisationLayer st
-> IO (IO (AcidState st))
prepareLocalStateWithSerialiser :: forall st.
IsAcidic st =>
String -> st -> SerialisationLayer st -> IO (IO (AcidState st))
prepareLocalStateWithSerialiser String
directory st
initialState SerialisationLayer st
serialisationLayer =
String
-> st -> Bool -> SerialisationLayer st -> IO (IO (AcidState st))
forall st.
IsAcidic st =>
String
-> st -> Bool -> SerialisationLayer st -> IO (IO (AcidState st))
resumeLocalStateFrom String
directory st
initialState Bool
True SerialisationLayer st
serialisationLayer
data SerialisationLayer st =
SerialisationLayer
{ forall st. SerialisationLayer st -> Serialiser (Checkpoint st)
checkpointSerialiser :: Serialiser (Checkpoint st)
, forall st. SerialisationLayer st -> Serialiser (Tagged ByteString)
eventSerialiser :: Serialiser (Tagged ByteString)
, forall st. SerialisationLayer st -> Archiver
archiver :: Archiver
}
defaultSerialisationLayer :: SafeCopy st => SerialisationLayer st
defaultSerialisationLayer :: forall st. SafeCopy st => SerialisationLayer st
defaultSerialisationLayer = Serialiser (Checkpoint st)
-> Serialiser (Tagged ByteString)
-> Archiver
-> SerialisationLayer st
forall st.
Serialiser (Checkpoint st)
-> Serialiser (Tagged ByteString)
-> Archiver
-> SerialisationLayer st
SerialisationLayer Serialiser (Checkpoint st)
forall a. SafeCopy a => Serialiser a
safeCopySerialiser Serialiser (Tagged ByteString)
forall a. SafeCopy a => Serialiser a
safeCopySerialiser Archiver
defaultArchiver
mkEventsLogKey :: FilePath -> SerialisationLayer object -> LogKey (Tagged ByteString)
mkEventsLogKey :: forall object.
String -> SerialisationLayer object -> LogKey (Tagged ByteString)
mkEventsLogKey String
directory SerialisationLayer object
serialisationLayer =
LogKey { logDirectory :: String
logDirectory = String
directory
, logPrefix :: String
logPrefix = String
"events"
, logSerialiser :: Serialiser (Tagged ByteString)
logSerialiser = SerialisationLayer object -> Serialiser (Tagged ByteString)
forall st. SerialisationLayer st -> Serialiser (Tagged ByteString)
eventSerialiser SerialisationLayer object
serialisationLayer
, logArchiver :: Archiver
logArchiver = SerialisationLayer object -> Archiver
forall st. SerialisationLayer st -> Archiver
archiver SerialisationLayer object
serialisationLayer }
mkCheckpointsLogKey :: FilePath -> SerialisationLayer object -> LogKey (Checkpoint object)
mkCheckpointsLogKey :: forall object.
String -> SerialisationLayer object -> LogKey (Checkpoint object)
mkCheckpointsLogKey String
directory SerialisationLayer object
serialisationLayer =
LogKey { logDirectory :: String
logDirectory = String
directory
, logPrefix :: String
logPrefix = String
"checkpoints"
, logSerialiser :: Serialiser (Checkpoint object)
logSerialiser = SerialisationLayer object -> Serialiser (Checkpoint object)
forall st. SerialisationLayer st -> Serialiser (Checkpoint st)
checkpointSerialiser SerialisationLayer object
serialisationLayer
, logArchiver :: Archiver
logArchiver = SerialisationLayer object -> Archiver
forall st. SerialisationLayer st -> Archiver
archiver SerialisationLayer object
serialisationLayer }
resumeLocalStateFrom :: (IsAcidic st)
=> FilePath
-> st
-> Bool
-> SerialisationLayer st
-> IO (IO (AcidState st))
resumeLocalStateFrom :: forall st.
IsAcidic st =>
String
-> st -> Bool -> SerialisationLayer st -> IO (IO (AcidState st))
resumeLocalStateFrom String
directory st
initialState Bool
delayLocking SerialisationLayer st
serialisationLayer =
case Bool
delayLocking of
Bool
True -> do
(Int
n, st
st) <- IO (Int, st)
loadCheckpoint
IO (AcidState st) -> IO (IO (AcidState st))
forall a. a -> IO a
forall (m :: * -> *) a. Monad m => a -> m a
return (IO (AcidState st) -> IO (IO (AcidState st)))
-> IO (AcidState st) -> IO (IO (AcidState st))
forall a b. (a -> b) -> a -> b
$ do
FileLock
lock <- String -> IO FileLock
maybeLockFile String
lockFile
FileLock -> Int -> st -> IO (AcidState st)
replayEvents FileLock
lock Int
n st
st
Bool
False -> do
FileLock
lock <- String -> IO FileLock
maybeLockFile String
lockFile
(Int
n, st
st) <- IO (Int, st)
loadCheckpoint IO (Int, st) -> IO () -> IO (Int, st)
forall a b. IO a -> IO b -> IO a
`onException` FileLock -> IO ()
unlockFile FileLock
lock
IO (AcidState st) -> IO (IO (AcidState st))
forall a. a -> IO a
forall (m :: * -> *) a. Monad m => a -> m a
return (IO (AcidState st) -> IO (IO (AcidState st)))
-> IO (AcidState st) -> IO (IO (AcidState st))
forall a b. (a -> b) -> a -> b
$ do
FileLock -> Int -> st -> IO (AcidState st)
replayEvents FileLock
lock Int
n st
st
where
lockFile :: String
lockFile = String
directory String -> ShowS
</> String
"open.lock"
eventsLogKey :: LogKey (Tagged ByteString)
eventsLogKey = String -> SerialisationLayer st -> LogKey (Tagged ByteString)
forall object.
String -> SerialisationLayer object -> LogKey (Tagged ByteString)
mkEventsLogKey String
directory SerialisationLayer st
serialisationLayer
checkpointsLogKey :: LogKey (Checkpoint st)
checkpointsLogKey = String -> SerialisationLayer st -> LogKey (Checkpoint st)
forall object.
String -> SerialisationLayer object -> LogKey (Checkpoint object)
mkCheckpointsLogKey String
directory SerialisationLayer st
serialisationLayer
loadCheckpoint :: IO (Int, st)
loadCheckpoint = do
Maybe (Checkpoint st)
mbLastCheckpoint <- LogKey (Checkpoint st) -> IO (Maybe (Checkpoint st))
forall object. LogKey object -> IO (Maybe object)
Log.newestEntry LogKey (Checkpoint st)
checkpointsLogKey
case Maybe (Checkpoint st)
mbLastCheckpoint of
Maybe (Checkpoint st)
Nothing ->
(Int, st) -> IO (Int, st)
forall a. a -> IO a
forall (m :: * -> *) a. Monad m => a -> m a
return (Int
0, st
initialState)
Just (Checkpoint Int
eventCutOff !st
val) ->
(Int, st) -> IO (Int, st)
forall a. a -> IO a
forall (m :: * -> *) a. Monad m => a -> m a
return (Int
eventCutOff, st
val)
replayEvents :: FileLock -> Int -> st -> IO (AcidState st)
replayEvents FileLock
lock Int
n st
st = do
Core st
core <- [MethodContainer st] -> st -> IO (Core st)
forall st. [MethodContainer st] -> st -> IO (Core st)
mkCore ([Event st] -> [MethodContainer st]
forall st. [Event st] -> [MethodContainer st]
eventsToMethods [Event st]
forall st. IsAcidic st => [Event st]
acidEvents) st
st
FileLog (Tagged ByteString)
eventsLog <- LogKey (Tagged ByteString) -> IO (FileLog (Tagged ByteString))
forall object. LogKey object -> IO (FileLog object)
openFileLog LogKey (Tagged ByteString)
eventsLogKey
[Tagged ByteString]
events <- FileLog (Tagged ByteString) -> Int -> IO [Tagged ByteString]
forall object. FileLog object -> Int -> IO [object]
readEntriesFrom FileLog (Tagged ByteString)
eventsLog Int
n
(Tagged ByteString -> IO ByteString)
-> [Tagged ByteString] -> IO ()
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
(a -> m b) -> t a -> m ()
mapM_ (Core st -> Tagged ByteString -> IO ByteString
forall st. Core st -> Tagged ByteString -> IO ByteString
runColdMethod Core st
core) [Tagged ByteString]
events
FileLog (Tagged ByteString) -> Int -> IO ()
forall object. FileLog object -> Int -> IO ()
ensureLeastEntryId FileLog (Tagged ByteString)
eventsLog Int
n
FileLog (Checkpoint st)
checkpointsLog <- LogKey (Checkpoint st) -> IO (FileLog (Checkpoint st))
forall object. LogKey object -> IO (FileLog object)
openFileLog LogKey (Checkpoint st)
checkpointsLogKey
IORef st
stateCopy <- st -> IO (IORef st)
forall a. a -> IO (IORef a)
newIORef st
forall a. HasCallStack => a
undefined
Core st -> (st -> IO ()) -> IO ()
forall st a. Core st -> (st -> IO a) -> IO a
withCoreState Core st
core (IORef st -> st -> IO ()
forall a. IORef a -> a -> IO ()
writeIORef IORef st
stateCopy)
AcidState st -> IO (AcidState st)
forall a. a -> IO a
forall (m :: * -> *) a. Monad m => a -> m a
return (AcidState st -> IO (AcidState st))
-> AcidState st -> IO (AcidState st)
forall a b. (a -> b) -> a -> b
$ LocalState st -> AcidState st
forall st. IsAcidic st => LocalState st -> AcidState st
toAcidState LocalState { localCore :: Core st
localCore = Core st
core
, localCopy :: IORef st
localCopy = IORef st
stateCopy
, localEvents :: FileLog (Tagged ByteString)
localEvents = FileLog (Tagged ByteString)
eventsLog
, localCheckpoints :: FileLog (Checkpoint st)
localCheckpoints = FileLog (Checkpoint st)
checkpointsLog
, localLock :: FileLock
localLock = FileLock
lock
}
maybeLockFile :: String -> IO FileLock
maybeLockFile String
path = do
Bool -> String -> IO ()
createDirectoryIfMissing Bool
True (ShowS
takeDirectory String
path)
IO FileLock
-> (FileLock -> IO FileLock) -> Maybe FileLock -> IO FileLock
forall b a. b -> (a -> b) -> Maybe a -> b
maybe (StateIsLocked -> IO FileLock
forall e a. Exception e => e -> IO a
throwIO (String -> StateIsLocked
StateIsLocked String
path))
FileLock -> IO FileLock
forall a. a -> IO a
forall (m :: * -> *) a. Monad m => a -> m a
return (Maybe FileLock -> IO FileLock)
-> IO (Maybe FileLock) -> IO FileLock
forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< String -> SharedExclusive -> IO (Maybe FileLock)
tryLockFile String
path SharedExclusive
Exclusive
checkpointRestoreError :: String -> a
checkpointRestoreError String
msg
= String -> a
forall a. HasCallStack => String -> a
error (String -> a) -> String -> a
forall a b. (a -> b) -> a -> b
$ String
"Could not parse saved checkpoint due to the following error: " String -> ShowS
forall a. [a] -> [a] -> [a]
++ String
msg
closeLocalState :: LocalState st -> IO ()
closeLocalState :: forall st. LocalState st -> IO ()
closeLocalState LocalState st
acidState
= do Core st -> IO ()
forall st. Core st -> IO ()
closeCore (LocalState st -> Core st
forall st. LocalState st -> Core st
localCore LocalState st
acidState)
FileLog (Tagged ByteString) -> IO ()
forall object. FileLog object -> IO ()
closeFileLog (LocalState st -> FileLog (Tagged ByteString)
forall st. LocalState st -> FileLog (Tagged ByteString)
localEvents LocalState st
acidState)
FileLog (Checkpoint st) -> IO ()
forall object. FileLog object -> IO ()
closeFileLog (LocalState st -> FileLog (Checkpoint st)
forall st. LocalState st -> FileLog (Checkpoint st)
localCheckpoints LocalState st
acidState)
FileLock -> IO ()
unlockFile (LocalState st -> FileLock
forall st. LocalState st -> FileLock
localLock LocalState st
acidState)
createLocalArchive :: LocalState st -> IO ()
createLocalArchive :: forall st. LocalState st -> IO ()
createLocalArchive LocalState st
state
= do
Int
currentCheckpointId <- FileLog (Checkpoint st) -> IO Int
forall object. FileLog object -> IO Int
cutFileLog (LocalState st -> FileLog (Checkpoint st)
forall st. LocalState st -> FileLog (Checkpoint st)
localCheckpoints LocalState st
state)
let durableCheckpointId :: Int
durableCheckpointId = Int
currentCheckpointIdInt -> Int -> Int
forall a. Num a => a -> a -> a
-Int
1
[Checkpoint st]
checkpoints <- FileLog (Checkpoint st) -> Int -> IO [Checkpoint st]
forall object. FileLog object -> Int -> IO [object]
readEntriesFrom (LocalState st -> FileLog (Checkpoint st)
forall st. LocalState st -> FileLog (Checkpoint st)
localCheckpoints LocalState st
state) Int
durableCheckpointId
case [Checkpoint st]
checkpoints of
[] -> () -> IO ()
forall a. a -> IO a
forall (m :: * -> *) a. Monad m => a -> m a
return ()
(Checkpoint Int
entryId st
_content : [Checkpoint st]
_)
-> do
FileLog (Tagged ByteString) -> Int -> IO ()
forall object. FileLog object -> Int -> IO ()
archiveFileLog (LocalState st -> FileLog (Tagged ByteString)
forall st. LocalState st -> FileLog (Tagged ByteString)
localEvents LocalState st
state) Int
entryId
FileLog (Checkpoint st) -> Int -> IO ()
forall object. FileLog object -> Int -> IO ()
archiveFileLog (LocalState st -> FileLog (Checkpoint st)
forall st. LocalState st -> FileLog (Checkpoint st)
localCheckpoints LocalState st
state) Int
durableCheckpointId
toAcidState :: IsAcidic st => LocalState st -> AcidState st
toAcidState :: forall st. IsAcidic st => LocalState st -> AcidState st
toAcidState LocalState st
local
= AcidState { _scheduleUpdate :: forall event.
(UpdateEvent event, EventState event ~ st) =>
event -> IO (MVar (EventResult event))
_scheduleUpdate = LocalState (EventState event)
-> event -> IO (MVar (MethodResult event))
forall event.
UpdateEvent event =>
LocalState (EventState event)
-> event -> IO (MVar (EventResult event))
scheduleLocalUpdate LocalState st
LocalState (EventState event)
local
, scheduleColdUpdate :: Tagged ByteString -> IO (MVar ByteString)
scheduleColdUpdate = LocalState st -> Tagged ByteString -> IO (MVar ByteString)
forall st.
LocalState st -> Tagged ByteString -> IO (MVar ByteString)
scheduleLocalColdUpdate LocalState st
local
, _query :: forall event.
(QueryEvent event, EventState event ~ st) =>
event -> IO (EventResult event)
_query = LocalState (EventState event) -> event -> IO (MethodResult event)
forall event.
QueryEvent event =>
LocalState (EventState event) -> event -> IO (EventResult event)
localQuery LocalState st
LocalState (EventState event)
local
, queryCold :: Tagged ByteString -> IO ByteString
queryCold = LocalState st -> Tagged ByteString -> IO ByteString
forall st. LocalState st -> Tagged ByteString -> IO ByteString
localQueryCold LocalState st
local
, createCheckpoint :: IO ()
createCheckpoint = LocalState st -> IO ()
forall st. IsAcidic st => LocalState st -> IO ()
createLocalCheckpoint LocalState st
local
, createArchive :: IO ()
createArchive = LocalState st -> IO ()
forall st. LocalState st -> IO ()
createLocalArchive LocalState st
local
, closeAcidState :: IO ()
closeAcidState = LocalState st -> IO ()
forall st. LocalState st -> IO ()
closeLocalState LocalState st
local
, acidSubState :: AnyState st
acidSubState = LocalState st -> AnyState st
forall (sub_st :: * -> *) st.
Typeable sub_st =>
sub_st st -> AnyState st
mkAnyState LocalState st
local
}