From 66be1cc2b60f54374f6e227a8f3275af4b44c267 Mon Sep 17 00:00:00 2001 From: Trolli Schmittlauch Date: Fri, 17 Apr 2020 12:17:40 +0200 Subject: [PATCH] make local lookup return Sets instead of lists --- Hash2Pub/src/Hash2Pub/DHTProtocol.hs | 29 +++++++++++++++++----------- Hash2Pub/src/Hash2Pub/FediChord.hs | 8 ++++++++ 2 files changed, 26 insertions(+), 11 deletions(-) diff --git a/Hash2Pub/src/Hash2Pub/DHTProtocol.hs b/Hash2Pub/src/Hash2Pub/DHTProtocol.hs index 90e6daa..46b78ad 100644 --- a/Hash2Pub/src/Hash2Pub/DHTProtocol.hs +++ b/Hash2Pub/src/Hash2Pub/DHTProtocol.hs @@ -6,7 +6,9 @@ module Hash2Pub.DHTProtocol ) where -import Data.Maybe (catMaybes) +import Data.Maybe (maybe) +import qualified Data.Set as Set + import Hash2Pub.FediChord ( NodeID , NodeState (..) @@ -21,7 +23,9 @@ import Hash2Pub.FediChord , localCompare ) -data QueryResponse = FORWARD [CacheEntry] -- ^return closest nodes from local cache. +import Debug.Trace (trace) + +data QueryResponse = FORWARD (Set.Set CacheEntry) -- ^return closest nodes from local cache. -- whole cache entry is returned for making -- the entry time stamp available to the -- protocol serialiser @@ -37,20 +41,23 @@ incomingQuery ownState nCache lBestNodes targetID | (targetID `localCompare` ownID) `elem` [LT, EQ] && (targetID `localCompare` (head . predecessors) ownState) == GT = FOUND ownState -- my interpretation: the "l best next hops" are the l-1 closest preceding nodes and -- the closest succeeding node (like with the p initiated parallel queries - | otherwise = FORWARD . catMaybes $ closestSuccessor : closestPredecessors + | otherwise = trace ("--- Query for " ++ show targetID ++ " wanting " ++ show lBestNodes ++ " results---") $ + FORWARD $ closestSuccessor `Set.union` closestPredecessors where ownID = nid ownState - closestSuccessor :: Maybe CacheEntry - closestSuccessor = cacheLookupSucc targetID nCache + closestSuccessor :: Set.Set CacheEntry + closestSuccessor = maybe Set.empty Set.singleton $ cacheLookupSucc targetID nCache - closestPredecessors :: [Maybe CacheEntry] + closestPredecessors :: Set.Set CacheEntry closestPredecessors = closestPredecessor (lBestNodes-1) $ nid ownState - closestPredecessor :: (Integral n) => n -> NodeID -> [Maybe CacheEntry] - closestPredecessor 0 _ = [] - closestPredecessor remainingLookups lastID = + closestPredecessor :: (Integral n, Show n) => n -> NodeID -> Set.Set CacheEntry + closestPredecessor 0 _ = Set.empty + closestPredecessor remainingLookups lastID + | remainingLookups < 0 = Set.empty + | otherwise = let result = cacheLookupPred lastID nCache in case result of - Nothing -> [] - Just nPred -> result:closestPredecessor (remainingLookups-1) (nid . cacheGetNodeStateUnvalidated $ nPred) + Nothing -> Set.empty + Just nPred -> Set.insert nPred $ closestPredecessor (remainingLookups-1) (nid . cacheGetNodeStateUnvalidated $ nPred) diff --git a/Hash2Pub/src/Hash2Pub/FediChord.hs b/Hash2Pub/src/Hash2Pub/FediChord.hs index d4902e9..91f26ac 100644 --- a/Hash2Pub/src/Hash2Pub/FediChord.hs +++ b/Hash2Pub/src/Hash2Pub/FediChord.hs @@ -145,6 +145,14 @@ data CacheEntry = | ProxyEntry (NodeID, ProxyDirection) (Maybe CacheEntry) deriving (Show, Eq) +-- | as a compromise, only NodeEntry components are ordered by their NodeID +-- while ProxyEntry components should never be tried to be ordered. +instance Ord CacheEntry where + a `compare` b = compare (extractID a) (extractID b) + where + extractID (NodeEntry _ eState _) = nid eState + extractID (ProxyEntry _ _) = error "proxy entries should never appear outside of the NodeCache" + data ProxyDirection = Backwards | Forwards deriving (Show, Eq) instance Enum ProxyDirection where