consul: Support listkeys without seperator

pull/98/head
Armon Dadgar 2014-04-28 16:42:32 -07:00 committed by Jack Pearkes
parent 18723c96be
commit a325630d7a
3 changed files with 23 additions and 8 deletions

View File

@ -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
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 {
return err
}

View File

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

View File

@ -1499,6 +1499,21 @@ func TestKVS_ListKeys(t *testing.T) {
if keys[0] != "/web/sub/c" {
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) {