deleting RingMap entries, list conversion

This commit is contained in:
Trolli Schmittlauch 2020-06-17 02:21:27 +02:00
parent 6142ee61d7
commit 2269357ed0

View file

@ -23,6 +23,9 @@ module Hash2Pub.FediChordTypes (
, addRMapEntryWith
, takeRMapPredecessors
, takeRMapSuccessors
, deleteRMapEntry
, rMapFromList
, rMapToList
, cacheGetNodeStateUnvalidated
, initCache
, cacheEntries
@ -288,14 +291,15 @@ instance Enum ProxyDirection where
fromEnum Backwards = - 1
fromEnum Forwards = 1
-- | helper function for getting the a from a RingEntry a
extractRingEntry :: HasKeyID a => RingEntry a -> Maybe a
extractRingEntry (KeyEntry entry) = Just entry
extractRingEntry (ProxyEntry _ (Just (KeyEntry entry))) = Just entry
extractRingEntry _ = Nothing
--- useful function for getting entries for a full cache transfer
cacheEntries :: NodeCache -> [CacheEntry]
cacheEntries = mapMaybe extractNodeEntries . Map.elems . getRingMap
where
extractNodeEntries :: RingEntry CacheEntry -> Maybe CacheEntry
extractNodeEntries (ProxyEntry _ (Just (KeyEntry entry))) = Just entry
extractNodeEntries (KeyEntry entry) = Just entry
extractNodeEntries _ = Nothing
cacheEntries = mapMaybe extractRingEntry . Map.elems . getRingMap
-- | An empty 'RingMap' needs to be initialised with 2 proxy entries,
-- linking the modular name space together by connecting @minBound@ and @maxBound@
@ -312,10 +316,7 @@ rMapLookup :: HasKeyID a
=> NodeID -- ^lookup key
-> RingMap a -- ^lookup cache
-> Maybe a
rMapLookup key rmap = case Map.lookup key $ getRingMap rmap of
Just (ProxyEntry _ (Just (KeyEntry result))) -> Just result
Just (KeyEntry res) -> Just res
_ -> Nothing
rMapLookup key rmap = extractRingEntry =<< Map.lookup key (getRingMap rmap)
cacheLookup :: NodeID -- ^lookup key
-> NodeCache -- ^lookup cache
@ -425,6 +426,21 @@ setRMapEntries :: (Foldable t, HasKeyID a)
-> RingMap a
setRMapEntries entries = addRMapEntries entries emptyRMap
deleteRMapEntry :: (HasKeyID a)
=> NodeID
-> RingMap a
-> RingMap a
deleteRMapEntry nid = RingMap . Map.update modifier nid . getRingMap
where
modifier (ProxyEntry idPointer _) = Just (ProxyEntry idPointer Nothing)
modifier KeyEntry {} = Nothing
rMapToList :: (HasKeyID a) => RingMap a -> [a]
rMapToList = mapMaybe extractRingEntry . Map.elems . getRingMap
rMapFromList :: (HasKeyID a) => [a] -> RingMap a
rMapFromList = setRMapEntries
-- | takes up to i entries from a 'RingMap' by calling a getter function on a
-- *startAt* value and after that on the previously returned value.
-- Stops once i entries have been taken or an entry has been encountered twice