check cache slice invariant for predecessor slices as well

closes #30 \0/
This commit is contained in:
Trolli Schmittlauch 2020-06-27 16:23:16 +02:00
parent 2c3ef44064
commit f7ed0ee8d8

View file

@ -253,9 +253,10 @@ checkCacheSliceInvariants :: LocalNodeState
-> NodeCache
-> [NodeID] -- ^ list of middle IDs of slices not
-- ^ fulfilling the invariant
checkCacheSliceInvariants ns = checkSuccessorSlice jEntries (getNid ns) startBound lastSucc
checkCacheSliceInvariants ns = checkPredecessorSlice jEntries (getNid ns) startBound lastPred <> checkSuccessorSlice jEntries (getNid ns) startBound lastSucc
where
jEntries = jEntriesPerSlice ns
lastPred = getNid <$> lastMay (predecessors ns)
lastSucc = getNid <$> lastMay (successors ns)
-- start slice boundary: 1/2 key space
startBound = getNid ns + 2^(idBits - 1)
@ -279,6 +280,24 @@ checkCacheSliceInvariants ns = checkSuccessorSlice jEntries (getNid ns) startBou
-- if not enough entries, add the middle of the slice to list
else middleID : checkSuccessorSlice j ownID (lowerBound - 1) (Just lastSuccID) cache
checkPredecessorSlice :: Integral i => i -> NodeID -> NodeID -> Maybe NodeID -> NodeCache -> [NodeID]
checkPredecessorSlice _ _ _ Nothing _ = []
checkPredecessorSlice j ownID lowerBound (Just lastPredID) cache
| (lowerBound `localCompare` lastPredID) == GT = []
| otherwise =
let
diff = getNodeID $ ownID - lowerBound
upperBound = ownID - fromInteger (diff `div` 2)
middleID = lowerBound + fromInteger (diff `div` 4)
lookupResult = Set.map (getNid . remoteNode) $ closestCachePredecessors jEntries upperBound cache
in
-- check whether j entries are in the slice
if length lookupResult == jEntries
&& all (\r -> (r `localCompare` lowerBound) == GT) lookupResult
&& all (\r -> (r `localCompare` upperBound) == LT) lookupResult
then checkPredecessorSlice j ownID (upperBound + 1) (Just lastPredID) cache
-- if not enough entries, add the middle of the slice to list
else middleID : checkPredecessorSlice j ownID (upperBound + 1) (Just lastPredID) cache
-- | Periodically send @StabiliseRequest' s to the closest neighbour nodes, until