83df0ee1b0
1. Added KeyValueStore.Iterate for implementing the different List, ListV2 etc. implementations. This allows for more efficient use of memory depending on the situation. 2. Implemented an inmemory teststore for running tests. This should allow to replace MockKeyValueStore in most places. 3. Rewrote tests 4. Pulled out logger from bolt implementation so it can be used for all other storage implementations. 5. Fixed multiple things in bolt and redis implementations.
60 lines
1.3 KiB
Go
60 lines
1.3 KiB
Go
// Copyright (C) 2018 Storj Labs, Inc.
|
|
// See LICENSE for copying information.
|
|
|
|
package storage
|
|
|
|
// ListKeys returns keys starting from first and upto limit
|
|
func ListKeys(store KeyValueStore, first Key, limit Limit) (Keys, error) {
|
|
const unlimited = Limit(1 << 31)
|
|
|
|
keys := make(Keys, 0, limit)
|
|
if limit == 0 {
|
|
// TODO: this shouldn't be probably the case
|
|
limit = unlimited
|
|
}
|
|
|
|
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
|
|
func ReverseListKeys(store KeyValueStore, first Key, limit Limit) (Keys, error) {
|
|
const unlimited = Limit(1 << 31)
|
|
|
|
keys := make(Keys, 0, limit)
|
|
if limit == 0 {
|
|
// TODO: this shouldn't be probably the case
|
|
limit = unlimited
|
|
}
|
|
|
|
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
|
|
}
|