module Network.Legion.PartitionState (
PartitionPropState,
PartitionPowerState,
ask,
mergeEither,
actions,
new,
initProp,
participating,
getPowerState,
delta,
heartbeat,
participate,
projParticipants,
projected,
infimum,
) where
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 s = PartitionPowerState {
unPowerState :: PropPowerState PartitionKey s Peer i
} deriving (Show, Binary)
newtype PartitionPropState i s = PartitionPropState {
unPropState :: PropState PartitionKey s Peer i
} deriving (Eq, Show)
ask :: (ApplyDelta i s) => PartitionPropState i s -> s
ask = P.ask . unPropState
mergeEither :: (Show i, Show s, ApplyDelta i s)
=> Peer
-> PartitionPowerState i s
-> PartitionPropState i s
-> Either String (PartitionPropState i s)
mergeEither peer ps prop =
PartitionPropState <$>
P.mergeEither peer (unPowerState ps) (unPropState prop)
actions
:: PartitionPropState i s
-> (Set Peer, PartitionPowerState i s, PartitionPropState i 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 s
new key self = PartitionPropState . P.new key self
initProp :: (ApplyDelta i s)
=> Peer
-> PartitionPowerState i s
-> PartitionPropState i s
initProp self = PartitionPropState . P.initProp self . unPowerState
participating :: PartitionPropState i s -> Bool
participating = P.participating . unPropState
getPowerState :: PartitionPropState i s -> PartitionPowerState i s
getPowerState = PartitionPowerState . P.getPowerState . unPropState
delta :: (ApplyDelta i s)
=> i
-> PartitionPropState i s
-> PartitionPropState i s
delta d = PartitionPropState . P.delta d . unPropState
heartbeat :: UTCTime -> PartitionPropState i s -> PartitionPropState i s
heartbeat now = PartitionPropState . P.heartbeat now . unPropState
participate :: (ApplyDelta i s)
=> Peer
-> PartitionPropState i s
-> PartitionPropState i s
participate peer = PartitionPropState . P.participate peer . unPropState
projParticipants :: PartitionPropState i s -> Set Peer
projParticipants = P.projParticipants . unPropState
projected :: (ApplyDelta i s) => PartitionPowerState i s -> s
projected = P.projected . unPowerState
infimum :: PartitionPowerState i s -> s
infimum = P.infimum . unPowerState