Fix Redis Iterators (#318)

Redis SCAN may return same item multiple times, add a basic check to avoid duplicate entries.
This commit is contained in:
Egon Elbre 2018-09-06 23:50:22 +03:00 committed by GitHub
parent f58bb3e41d
commit 8a4151f397
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -151,12 +151,12 @@ func (client *Client) Iterate(opts storage.IterateOptions, fn func(it storage.It
func (client *Client) allPrefixedItems(prefix, first, last storage.Key) (storage.Items, error) { func (client *Client) allPrefixedItems(prefix, first, last storage.Key) (storage.Items, error) {
var all storage.Items var all storage.Items
seen := map[string]struct{}{}
match := string(escapeMatch([]byte(prefix))) + "*" match := string(escapeMatch([]byte(prefix))) + "*"
it := client.db.Scan(0, match, 0).Iterator() it := client.db.Scan(0, match, 0).Iterator()
for it.Next() { for it.Next() {
key := it.Val() key := it.Val()
if first != nil && storage.Key(key).Less(first) { if first != nil && storage.Key(key).Less(first) {
continue continue
} }
@ -164,6 +164,11 @@ func (client *Client) allPrefixedItems(prefix, first, last storage.Key) (storage
continue continue
} }
if _, ok := seen[key]; ok {
continue
}
seen[key] = struct{}{}
value, err := client.db.Get(key).Bytes() value, err := client.db.Get(key).Bytes()
if err != nil { if err != nil {
return nil, err return nil, err