From 70f4e149d4eab969d4633fb622115061c714a5ef Mon Sep 17 00:00:00 2001 From: Branden J Brown Date: Thu, 13 Mar 2025 20:58:14 -0400 Subject: [PATCH] add Set operation --- chord/client.go | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/chord/client.go b/chord/client.go index 621f5db..4280b9e 100644 --- a/chord/client.go +++ b/chord/client.go @@ -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) {