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...)
}
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.
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.
@ -85,19 +91,26 @@ func (n *Node) Closest(id ID) Peer {
return n.self
}
// Get obtains the value for a key owned by the node.
func (n *Node) Get(k ID) (v string, found bool) {
// GetLocal obtains the value for a key if it is local to and owned by the node.
func (n *Node) GetLocal(k ID) (v string, found bool) {
n.mu.Lock()
defer n.mu.Unlock()
v, found = n.data[k]
if n.localLocked(k) {
v, found = n.data[k]
}
return v, found
}
// Set sets the value for a key.
func (n *Node) Set(k ID, v string) {
// SetLocal sets the value for a key.
// Returns false if the key is not owned by the node.
func (n *Node) SetLocal(k ID, v string) bool {
n.mu.Lock()
defer n.mu.Unlock()
n.data[k] = v
if n.localLocked(k) {
n.data[k] = v
return true
}
return false
}
// Peer is the ID and address of a node.