File
Blob: cmd/wal/main.go
| 1 | package main |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "flag" |
| 6 | "fmt" |
| 7 | "os" |
| 8 | "strings" |
| 9 | "time" |
| 10 | |
| 11 | "go.miragespace.co/specter/kv/aof" |
| 12 | chordSpec "go.miragespace.co/specter/spec/chord" |
| 13 | |
| 14 | "github.com/jedib0t/go-pretty/v6/table" |
| 15 | "go.uber.org/zap" |
| 16 | ) |
| 17 | |
| 18 | var ( |
| 19 | dataDir = flag.String("data", "data", "data dir") |
| 20 | op = flag.String("op", "", "operation to perform") |
| 21 | key = flag.String("key", "example", "key to fetch") |
| 22 | ) |
| 23 | |
| 24 | func main() { |
| 25 | flag.Parse() |
| 26 | |
| 27 | cfg := zap.NewDevelopmentConfig() |
| 28 | cfg.OutputPaths = []string{"stderr"} |
| 29 | |
| 30 | logger, err := cfg.Build() |
| 31 | if err != nil { |
| 32 | panic(err) |
| 33 | } |
| 34 | |
| 35 | kvProvider, err := aof.New(aof.Config{ |
| 36 | Logger: logger.With(zap.String("component", "kv")), |
| 37 | HasnFn: chordSpec.Hash, |
| 38 | DataDir: *dataDir, |
| 39 | FlushInterval: time.Second, |
| 40 | }) |
| 41 | if err != nil { |
| 42 | panic(err) |
| 43 | } |
| 44 | go kvProvider.Start() |
| 45 | |
| 46 | defer kvProvider.Stop() |
| 47 | |
| 48 | switch *op { |
| 49 | case "list": |
| 50 | keys, err := kvProvider.RangeKeys(context.Background(), 0, 0) |
| 51 | if err != nil { |
| 52 | panic(err) |
| 53 | } |
| 54 | for _, k := range keys { |
| 55 | fmt.Printf("%s\n", k) |
| 56 | } |
| 57 | case "simple-get": |
| 58 | val, err := kvProvider.Get(context.Background(), []byte(*key)) |
| 59 | if err != nil { |
| 60 | panic(err) |
| 61 | } |
| 62 | fmt.Printf("%s", val) |
| 63 | case "prefix-list": |
| 64 | vals, err := kvProvider.PrefixList(context.Background(), []byte(*key)) |
| 65 | if err != nil { |
| 66 | panic(err) |
| 67 | } |
| 68 | for _, val := range vals { |
| 69 | fmt.Printf("%s\n", val) |
| 70 | } |
| 71 | case "simple-delete-prefix": |
| 72 | keys, err := kvProvider.RangeKeys(context.Background(), 0, 0) |
| 73 | if err != nil { |
| 74 | panic(err) |
| 75 | } |
| 76 | for _, k := range keys { |
| 77 | if strings.HasPrefix(string(k), *key) { |
| 78 | fmt.Printf("deleting simple key %s\n", k) |
| 79 | kvProvider.Delete(context.Background(), k) |
| 80 | } |
| 81 | } |
| 82 | case "prefix-remove": |
| 83 | vals, err := kvProvider.PrefixList(context.Background(), []byte(*key)) |
| 84 | if err != nil { |
| 85 | panic(err) |
| 86 | } |
| 87 | for _, val := range vals { |
| 88 | fmt.Printf("deleting prefix %s with child %s\n", *key, val) |
| 89 | kvProvider.PrefixRemove(context.Background(), []byte(*key), val) |
| 90 | } |
| 91 | case "delete-all": |
| 92 | keys := [][]byte{[]byte(*key)} |
| 93 | fmt.Printf("removing everything under key %s\n", *key) |
| 94 | err := kvProvider.RemoveKeys(context.Background(), keys) |
| 95 | if err != nil { |
| 96 | panic(err) |
| 97 | } |
| 98 | default: |
| 99 | fmt.Printf("available ops:\n") |
| 100 | opsTable := table.NewWriter() |
| 101 | opsTable.SetOutputMirror(os.Stdout) |
| 102 | |
| 103 | opsTable.AppendHeader(table.Row{"Op", "Description"}) |
| 104 | opsTable.AppendRow(table.Row{"list", "list all keys from the kv storage"}) |
| 105 | opsTable.AppendRow(table.Row{"simple-get", "corresponds to KV Get"}) |
| 106 | opsTable.AppendRow(table.Row{"prefix-list", "corresponds to KV PrefixList"}) |
| 107 | opsTable.AppendRow(table.Row{"prefix-remove", "remove all children under prefix"}) |
| 108 | opsTable.AppendRow(table.Row{"simple-delete-prefix", "operates KV Delete on keys matching this prefix"}) |
| 109 | opsTable.AppendRow(table.Row{"remove-all", "remove everything under this key, including Simple, Prefix, and Lease"}) |
| 110 | |
| 111 | opsTable.SetStyle(table.StyleDefault) |
| 112 | opsTable.Style().Options.SeparateRows = true |
| 113 | opsTable.Render() |
| 114 | } |
| 115 | } |