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