don't handle keys that aren't owned

This commit is contained in:
Branden J Brown 2025-03-13 20:54:31 -04:00
parent f4a8b104ab
commit ef03e13a87

View File

@ -47,9 +47,15 @@ func (n *Node) Neighbors(s []Peer) (Peer, []Peer) {
return n.pred, append(s, n.succ...) return n.pred, append(s, n.succ...)
} }
func (n *Node) localLocked(id ID) bool {
return contains(n.self.id, n.succ[0].id, id)
}
// IsLocal reports whether this node owns the given key. // IsLocal reports whether this node owns the given key.
func (n *Node) IsLocal(id ID) bool { func (n *Node) IsLocal(id ID) bool {
return contains(n.self.id, n.Successor().id, id) n.mu.Lock()
defer n.mu.Unlock()
return n.localLocked(id)
} }
// Closest finds the locally known peer which is the closest predecessor of key. // Closest finds the locally known peer which is the closest predecessor of key.
@ -85,19 +91,26 @@ func (n *Node) Closest(id ID) Peer {
return n.self return n.self
} }
// Get obtains the value for a key owned by the node. // GetLocal obtains the value for a key if it is local to and owned by the node.
func (n *Node) Get(k ID) (v string, found bool) { func (n *Node) GetLocal(k ID) (v string, found bool) {
n.mu.Lock() n.mu.Lock()
defer n.mu.Unlock() defer n.mu.Unlock()
if n.localLocked(k) {
v, found = n.data[k] v, found = n.data[k]
}
return v, found return v, found
} }
// Set sets the value for a key. // SetLocal sets the value for a key.
func (n *Node) Set(k ID, v string) { // Returns false if the key is not owned by the node.
func (n *Node) SetLocal(k ID, v string) bool {
n.mu.Lock() n.mu.Lock()
defer n.mu.Unlock() defer n.mu.Unlock()
if n.localLocked(k) {
n.data[k] = v n.data[k] = v
return true
}
return false
} }
// Peer is the ID and address of a node. // Peer is the ID and address of a node.