forked from schmittlauch/Hash2Pub
make local lookup return Sets instead of lists
This commit is contained in:
parent
90daa1ba9a
commit
66be1cc2b6
|
@ -6,7 +6,9 @@ module Hash2Pub.DHTProtocol
|
||||||
)
|
)
|
||||||
where
|
where
|
||||||
|
|
||||||
import Data.Maybe (catMaybes)
|
import Data.Maybe (maybe)
|
||||||
|
import qualified Data.Set as Set
|
||||||
|
|
||||||
import Hash2Pub.FediChord
|
import Hash2Pub.FediChord
|
||||||
( NodeID
|
( NodeID
|
||||||
, NodeState (..)
|
, NodeState (..)
|
||||||
|
@ -21,7 +23,9 @@ import Hash2Pub.FediChord
|
||||||
, localCompare
|
, 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
|
-- whole cache entry is returned for making
|
||||||
-- the entry time stamp available to the
|
-- the entry time stamp available to the
|
||||||
-- protocol serialiser
|
-- 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
|
| (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
|
-- 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
|
-- 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
|
where
|
||||||
ownID = nid ownState
|
ownID = nid ownState
|
||||||
|
|
||||||
closestSuccessor :: Maybe CacheEntry
|
closestSuccessor :: Set.Set CacheEntry
|
||||||
closestSuccessor = cacheLookupSucc targetID nCache
|
closestSuccessor = maybe Set.empty Set.singleton $ cacheLookupSucc targetID nCache
|
||||||
|
|
||||||
closestPredecessors :: [Maybe CacheEntry]
|
closestPredecessors :: Set.Set CacheEntry
|
||||||
closestPredecessors = closestPredecessor (lBestNodes-1) $ nid ownState
|
closestPredecessors = closestPredecessor (lBestNodes-1) $ nid ownState
|
||||||
closestPredecessor :: (Integral n) => n -> NodeID -> [Maybe CacheEntry]
|
closestPredecessor :: (Integral n, Show n) => n -> NodeID -> Set.Set CacheEntry
|
||||||
closestPredecessor 0 _ = []
|
closestPredecessor 0 _ = Set.empty
|
||||||
closestPredecessor remainingLookups lastID =
|
closestPredecessor remainingLookups lastID
|
||||||
|
| remainingLookups < 0 = Set.empty
|
||||||
|
| otherwise =
|
||||||
let result = cacheLookupPred lastID nCache
|
let result = cacheLookupPred lastID nCache
|
||||||
in
|
in
|
||||||
case result of
|
case result of
|
||||||
Nothing -> []
|
Nothing -> Set.empty
|
||||||
Just nPred -> result:closestPredecessor (remainingLookups-1) (nid . cacheGetNodeStateUnvalidated $ nPred)
|
Just nPred -> Set.insert nPred $ closestPredecessor (remainingLookups-1) (nid . cacheGetNodeStateUnvalidated $ nPred)
|
||||||
|
|
|
@ -145,6 +145,14 @@ data CacheEntry =
|
||||||
| ProxyEntry (NodeID, ProxyDirection) (Maybe CacheEntry)
|
| ProxyEntry (NodeID, ProxyDirection) (Maybe CacheEntry)
|
||||||
deriving (Show, Eq)
|
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)
|
data ProxyDirection = Backwards | Forwards deriving (Show, Eq)
|
||||||
|
|
||||||
instance Enum ProxyDirection where
|
instance Enum ProxyDirection where
|
||||||
|
|
Loading…
Reference in a new issue