2018-09-05 17:10:35 +01:00
|
|
|
// Copyright (C) 2018 Storj Labs, Inc.
|
|
|
|
// See LICENSE for copying information.
|
|
|
|
|
|
|
|
package storage
|
|
|
|
|
|
|
|
// ListKeys returns keys starting from first and upto limit
|
2018-09-07 10:00:00 +01:00
|
|
|
// limit is capped to LookupLimit
|
2018-09-07 15:20:15 +01:00
|
|
|
func ListKeys(store KeyValueStore, first Key, limit int) (Keys, error) {
|
2018-09-07 10:00:00 +01:00
|
|
|
if limit <= 0 || limit > LookupLimit {
|
|
|
|
limit = LookupLimit
|
2018-09-05 17:10:35 +01:00
|
|
|
}
|
|
|
|
|
2018-09-07 10:00:00 +01:00
|
|
|
keys := make(Keys, 0, limit)
|
2018-09-05 17:10:35 +01:00
|
|
|
err := store.Iterate(IterateOptions{
|
|
|
|
First: first,
|
|
|
|
Recurse: true,
|
|
|
|
}, func(it Iterator) error {
|
|
|
|
var item ListItem
|
|
|
|
for ; limit > 0 && it.Next(&item); limit-- {
|
|
|
|
if item.Key == nil {
|
|
|
|
panic("nil key")
|
|
|
|
}
|
|
|
|
keys = append(keys, CloneKey(item.Key))
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
|
|
|
|
return keys, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// ReverseListKeys returns keys starting from first and upto limit in reverse order
|
2018-09-07 10:00:00 +01:00
|
|
|
// limit is capped to LookupLimit
|
2018-09-07 15:20:15 +01:00
|
|
|
func ReverseListKeys(store KeyValueStore, first Key, limit int) (Keys, error) {
|
2018-09-07 10:00:00 +01:00
|
|
|
if limit <= 0 || limit > LookupLimit {
|
|
|
|
limit = LookupLimit
|
2018-09-05 17:10:35 +01:00
|
|
|
}
|
|
|
|
|
2018-09-07 10:00:00 +01:00
|
|
|
keys := make(Keys, 0, limit)
|
2018-09-05 17:10:35 +01:00
|
|
|
err := store.Iterate(IterateOptions{
|
|
|
|
First: first,
|
|
|
|
Recurse: true,
|
|
|
|
Reverse: true,
|
|
|
|
}, func(it Iterator) error {
|
|
|
|
var item ListItem
|
|
|
|
for ; limit > 0 && it.Next(&item); limit-- {
|
|
|
|
if item.Key == nil {
|
|
|
|
panic("nil key")
|
|
|
|
}
|
|
|
|
keys = append(keys, CloneKey(item.Key))
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
|
|
|
|
return keys, err
|
|
|
|
}
|