store/memory: fix List

Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org>
This commit is contained in:
Василий Толстов 2021-07-15 22:53:12 +03:00
parent f54658830d
commit 96aa0b6906

View File

@ -70,19 +70,19 @@ func (m *memoryStore) delete(prefix, key string) {
func (m *memoryStore) list(prefix string, limit, offset uint) []string { func (m *memoryStore) list(prefix string, limit, offset uint) []string {
allItems := m.store.Items() allItems := m.store.Items()
allKeys := make([]string, len(allItems)) allKeys := make([]string, 0, len(allItems))
i := 0
for k := range allItems { for k := range allItems {
if !strings.HasPrefix(k, prefix) { if !strings.HasPrefix(k, prefix) {
continue continue
} }
allKeys[i] = strings.TrimPrefix(k, prefix) k = strings.TrimPrefix(k, prefix)
if allKeys[i][0] == '/' { if k[0] == '/' {
allKeys[i] = allKeys[i][1:] k = k[1:]
} }
i++ allKeys = append(allKeys, k)
} }
if limit != 0 || offset != 0 { if limit != 0 || offset != 0 {
sort.Slice(allKeys, func(i, j int) bool { return allKeys[i] < allKeys[j] }) sort.Slice(allKeys, func(i, j int) bool { return allKeys[i] < allKeys[j] })
sort.Slice(allKeys, func(i, j int) bool { return allKeys[i] < allKeys[j] }) sort.Slice(allKeys, func(i, j int) bool { return allKeys[i] < allKeys[j] })
@ -99,7 +99,6 @@ func (m *memoryStore) list(prefix string, limit, offset uint) []string {
} }
return allKeys[offset:end] return allKeys[offset:end]
} }
return allKeys return allKeys
} }