add Set operation

This commit is contained in:
Branden J Brown 2025-03-13 20:58:14 -04:00
parent ef03e13a87
commit 70f4e149d4

View File

@ -13,13 +13,15 @@ type Client interface {
// If the ID is not associated with a key, the result must be the empty
// string with a nil error.
Find(ctx context.Context, s Peer, id ID) (Peer, string, error)
// Set asks s to save a value for an ID.
Set(ctx context.Context, s Peer, id ID, v string) error
// Notify tells s we believe n to be its predecessor.
Notify(ctx context.Context, n *Node, s Peer) error
// Neighbors requests a peer's beliefs about its own neighbors.
Neighbors(ctx context.Context, p Peer) (pred Peer, succ []Peer, err error)
}
// TODO(branden): FindSuccessor should be plural; if we have multiple keys to
// TODO(branden): Find should be plural; if we have multiple keys to
// search, we shouldn't have to do the whole query for all of them, especially
// considering we can sort by increasing distance from the origin and then do
// the query in linear time.
@ -33,6 +35,18 @@ func Find(ctx context.Context, cl Client, n *Node, id ID) (Peer, string, error)
return p, s, err
}
// TODO(branden): Set should be plural for the same reasons. It should also
// return an error if the key isn't local to the peer.
// Set saves a value in the Chord network.
func Set(ctx context.Context, cl Client, n *Node, key ID, val string) error {
p, _, err := Find(ctx, cl, n, key)
if err != nil {
return fmt.Errorf("couldn't find peer to save key: %w", err)
}
return cl.Set(ctx, p, key, val)
}
// Join creates a new node joining an existing Chord network by communicating
// with any peer already in the network.
func Join(ctx context.Context, cl Client, addr netip.AddrPort, np Peer) (*Node, error) {