{-# LANGUAGE GeneralizedNewtypeDeriving #-}
{-# LANGUAGE MultiParamTypeClasses #-}
module Network.Legion.PartitionState (
PartitionPropState,
PartitionPowerState,
ask,
mergeEither,
actions,
new,
initProp,
participating,
getPowerState,
delta,
heartbeat,
participate,
projParticipants,
projected,
infimum,
idle,
) where
import Data.Aeson (ToJSON)
import Data.Binary (Binary)
import Data.Default.Class (Default)
import Data.Set (Set)
import Data.Time.Clock (UTCTime)
import Network.Legion.Distribution (Peer)
import Network.Legion.PartitionKey (PartitionKey)
import Network.Legion.PowerState (ApplyDelta)
import Network.Legion.Propagation (PropState, PropPowerState)
import qualified Network.Legion.Propagation as P
newtype PartitionPowerState i o s = PartitionPowerState {
unPowerState :: PropPowerState PartitionKey s Peer i o
} deriving (Show, Binary)
newtype PartitionPropState i o s = PartitionPropState {
unPropState :: PropState PartitionKey s Peer i o
} deriving (Eq, Show, ToJSON)
ask :: (ApplyDelta i o s) => PartitionPropState i o s -> s
ask = P.ask . unPropState
mergeEither :: (Show i, Show s, ApplyDelta i o s)
=> Peer
-> PartitionPowerState i o s
-> PartitionPropState i o s
-> Either String (PartitionPropState i o s)
mergeEither peer ps prop =
PartitionPropState <$>
P.mergeEither peer (unPowerState ps) (unPropState prop)
actions
:: PartitionPropState i o s
-> (Set Peer, PartitionPowerState i o s, PartitionPropState i o s)
actions prop =
let (peers, ps, newProp) = P.actions (unPropState prop)
in (peers, PartitionPowerState ps, PartitionPropState newProp)
new :: (Default s)
=> PartitionKey
-> Peer
-> Set Peer
-> PartitionPropState i o s
new key self = PartitionPropState . P.new key self
initProp :: (ApplyDelta i o s)
=> Peer
-> PartitionPowerState i o s
-> PartitionPropState i o s
initProp self = PartitionPropState . P.initProp self . unPowerState
participating :: PartitionPropState i o s -> Bool
participating = P.participating . unPropState
getPowerState :: PartitionPropState i o s -> PartitionPowerState i o s
getPowerState = PartitionPowerState . P.getPowerState . unPropState
delta :: (ApplyDelta i o s)
=> i
-> PartitionPropState i o s
-> PartitionPropState i o s
delta d = PartitionPropState . P.delta d . unPropState
heartbeat :: UTCTime -> PartitionPropState i o s -> PartitionPropState i o s
heartbeat now = PartitionPropState . P.heartbeat now . unPropState
participate :: (ApplyDelta i o s)
=> Peer
-> PartitionPropState i o s
-> PartitionPropState i o s
participate peer = PartitionPropState . P.participate peer . unPropState
projParticipants :: PartitionPropState i o s -> Set Peer
projParticipants = P.projParticipants . unPropState
projected :: (ApplyDelta i o s) => PartitionPowerState i o s -> s
projected = P.projected . unPowerState
infimum :: PartitionPowerState i o s -> s
infimum = P.infimum . unPowerState
idle :: PartitionPropState i o s -> Bool
idle = P.idle . unPropState