Skip to content

Commit

Permalink
chore(kv): ensure kv store implementation return seek missing prefix …
Browse files Browse the repository at this point in the history
…error in tests
  • Loading branch information
GeorgeMac committed Jan 15, 2020
1 parent 4fa8158 commit 0384cc2
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 1 deletion.
7 changes: 6 additions & 1 deletion bolt/kv.go
Original file line number Diff line number Diff line change
Expand Up @@ -201,13 +201,18 @@ func (b *Bucket) ForwardCursor(seek []byte, opts ...kv.CursorOption) (kv.Forward
var (
cursor = b.bucket.Cursor()
key, value = cursor.Seek(seek)
config = kv.NewCursorConfig(opts...)
)

if config.Prefix != nil && !bytes.HasPrefix(seek, config.Prefix) {
return nil, fmt.Errorf("seek bytes %q not prefixed with %q: %w", string(seek), string(config.Prefix), kv.ErrSeekMissingPrefix)
}

return &Cursor{
cursor: cursor,
key: key,
value: value,
config: kv.NewCursorConfig(opts...),
config: config,
}, nil
}

Expand Down
24 changes: 24 additions & 0 deletions testing/kv.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package testing
import (
"bytes"
"context"
"errors"
"fmt"
"testing"
"time"
Expand Down Expand Up @@ -736,6 +737,24 @@ func KVForwardCursor(
},
exp: []string{"aaa/00", "aaa/01", "aaa/02", "aaa/03"},
},
{
name: "prefix - does not prefix seek",
fields: KVStoreFields{
Bucket: []byte("bucket"),
Pairs: pairs(
"aa/00", "aa/01",
"aaa/00", "aaa/01", "aaa/02", "aaa/03",
"bbb/00", "bbb/01", "bbb/02"),
},
args: args{
seek: "aaa/00",
until: "bbb/02",
opts: []kv.CursorOption{
kv.WithCursorPrefix([]byte("aab")),
},
},
expErr: kv.ErrSeekMissingPrefix,
},
{
name: "prefix hint",
fields: KVStoreFields{
Expand Down Expand Up @@ -924,6 +943,11 @@ func KVForwardCursor(

cur, err := b.ForwardCursor([]byte(tt.args.seek), tt.args.opts...)
if err != nil {
if tt.expErr != nil && errors.Is(err, tt.expErr) {
// successfully returned expected error
return nil
}

t.Errorf("unexpected error: %v", err)
return err
}
Expand Down

0 comments on commit 0384cc2

Please sign in to comment.