make local lookup return Sets instead of lists

This commit is contained in:
Trolli Schmittlauch 2020-04-17 12:17:40 +02:00
parent 90daa1ba9a
commit 66be1cc2b6
2 changed files with 26 additions and 11 deletions

View file

@ -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)

View file

@ -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