Browse Source

consul: Support listkeys without seperator

pull/98/head
Armon Dadgar 11 years ago committed by Jack Pearkes
parent
commit
a325630d7a
  1. 6
      consul/kvs_endpoint.go
  2. 10
      consul/state_store.go
  3. 15
      consul/state_store_test.go

6
consul/kvs_endpoint.go

@ -118,12 +118,6 @@ func (k *KVS) List(args *structs.KeyRequest, reply *structs.IndexedDirEntries) e
// ListKeys is used to list all keys with a given prefix to a seperator // ListKeys is used to list all keys with a given prefix to a seperator
func (k *KVS) ListKeys(args *structs.KeyListRequest, reply *structs.IndexedKeyList) error { func (k *KVS) ListKeys(args *structs.KeyListRequest, reply *structs.IndexedKeyList) error {
// Ensure we have a seperator
if args.Seperator == "" {
return fmt.Errorf("Missing seperator")
}
// Forward if necessary
if done, err := k.srv.forward("KVS.ListKeys", args, args, reply); done { if done, err := k.srv.forward("KVS.ListKeys", args, args, reply); done {
return err return err
} }

10
consul/state_store.go

@ -923,19 +923,25 @@ func (s *StateStore) KVSListKeys(prefix, seperator string) (uint64, []string, er
var keys []string var keys []string
go func() { go func() {
prefixLen := len(prefix) prefixLen := len(prefix)
sepLen := len(seperator)
last := "" last := ""
for raw := range stream { for raw := range stream {
ent := raw.(*structs.DirEntry) ent := raw.(*structs.DirEntry)
after := ent.Key[prefixLen:] after := ent.Key[prefixLen:]
// If there is no seperator, always accumulate
if sepLen == 0 {
keys = append(keys, ent.Key)
continue
}
// Check for the seperator // Check for the seperator
if idx := strings.Index(after, seperator); idx >= 0 { if idx := strings.Index(after, seperator); idx >= 0 {
toSep := ent.Key[:prefixLen+idx+1] toSep := ent.Key[:prefixLen+idx+sepLen]
if last != toSep { if last != toSep {
keys = append(keys, toSep) keys = append(keys, toSep)
last = toSep last = toSep
} }
} else { } else {
keys = append(keys, ent.Key) keys = append(keys, ent.Key)
} }

15
consul/state_store_test.go

@ -1499,6 +1499,21 @@ func TestKVS_ListKeys(t *testing.T) {
if keys[0] != "/web/sub/c" { if keys[0] != "/web/sub/c" {
t.Fatalf("bad: %v", keys) t.Fatalf("bad: %v", keys)
} }
// Should list all
idx, keys, err = store.KVSListKeys("/web/", "")
if err != nil {
t.Fatalf("err: %v", err)
}
if idx != 1002 {
t.Fatalf("bad: %v", idx)
}
if len(keys) != 3 {
t.Fatalf("bad: %v", keys)
}
if keys[2] != "/web/sub/c" {
t.Fatalf("bad: %v", keys)
}
} }
func TestKVSDeleteTree(t *testing.T) { func TestKVSDeleteTree(t *testing.T) {

Loading…
Cancel
Save