implement data storage cli commands

This commit is contained in:
Branden J Brown 2025-03-15 20:30:05 -04:00
parent bfc9fe5d56
commit 94580d73be
2 changed files with 39 additions and 3 deletions

36
main.go
View File

@ -2,7 +2,6 @@ package main
import (
"context"
"errors"
"fmt"
"log/slog"
"net"
@ -223,8 +222,39 @@ func cliLookup(ctx context.Context, cmd *cli.Command) error {
return nil
}
func cliPut(ctx context.Context, cmd *cli.Command) error { return errors.New("not implemented") }
func cliGet(ctx context.Context, cmd *cli.Command) error { return errors.New("not implemented") }
func cliPut(ctx context.Context, cmd *cli.Command) error {
n, err := netip.ParseAddrPort(cmd.String("n"))
if err != nil {
return err
}
k := chord.Key(cmd.String("k"))
v := cmd.String("v")
cl := &httpnode.Client{HTTP: http.Client{Timeout: 5 * time.Second}}
p, err := cl.FindSuccessor(ctx, chord.Address(n), k)
if err != nil {
return err
}
return cl.Set(ctx, p, k, v)
}
func cliGet(ctx context.Context, cmd *cli.Command) error {
n, err := netip.ParseAddrPort(cmd.String("n"))
if err != nil {
return err
}
k := chord.Key(cmd.String("k"))
cl := &httpnode.Client{HTTP: http.Client{Timeout: 5 * time.Second}}
p, err := cl.FindSuccessor(ctx, chord.Address(n), k)
if err != nil {
return err
}
v, err := cl.Get(ctx, p, k)
if err != nil {
return err
}
fmt.Println(v)
return nil
}
func main() {
ctx, stop := signal.NotifyContext(context.Background(), os.Interrupt)

View File

@ -26,6 +26,12 @@ sleep 5
./chord-node lookup -k key2 -n 127.0.0.1:3000
./chord-node lookup -k key3 -n 127.0.0.1:3000
# Test data storage.
./chord-node put -k key1 -v value1 -n 127.0.0.1:3000
./chord-node get -k key1 -n 127.0.0.1:3000
./chord-node put -k key1 -v value2 -n 127.0.0.1:3001
./chord-node get -k key1 -n 127.0.0.1:3002
# Test leaving.
./chord-node leave -n 127.0.0.1:3000