Skip to content

Commit

Permalink
Merge pull request #2104 from Ace-Tang/master
Browse files Browse the repository at this point in the history
bugfix: fix meta store return all item when empty prefix
  • Loading branch information
allencloud authored Aug 17, 2018
2 parents 12b4ec3 + 1cdcd14 commit 94aae1f
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 0 deletions.
4 changes: 4 additions & 0 deletions pkg/meta/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,10 @@ func (s *Store) GetWithPrefix(prefix string) ([]Object, error) {
func (s *Store) KeysWithPrefix(prefix string) ([]string, error) {
var keys []string

if len(prefix) == 0 {
return keys, nil
}

fn := func(prefix patricia.Prefix, item patricia.Item) error {
keys = append(keys, string(prefix))
return nil
Expand Down
60 changes: 60 additions & 0 deletions pkg/meta/store_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -200,3 +200,63 @@ func TestBoltdbRemove(t *testing.T) {
t.Fatal(err)
}
}

type Demo4 struct {
A int
B string
}

func (d *Demo4) Key() string {
return d.B
}

var boltdb4 *Store

var boltdbCfg4 = Config{
Driver: "boltdb",
BaseDir: "/tmp/bolt4.db",
Buckets: []Bucket{
{"boltdb", reflect.TypeOf(Demo4{})},
},
}

func TestKeysWithPrefix(t *testing.T) {
var err error
boltdb4, err = NewStore(boltdbCfg4)
if err != nil {
t.Fatal(err)
}

// put 1
if err := boltdb4.Put(&Demo4{
A: 1,
B: "prefixkey",
}); err != nil {
t.Fatal(err)
}
// put 2
if err := boltdb4.Put(&Demo4{
A: 2,
B: "prefixkey2",
}); err != nil {
t.Fatal(err)
}

// find item with prefix
obj, err := boltdb4.KeysWithPrefix("prefixkey")
if err != nil {
t.Fatal(err)
}
if len(obj) != 2 {
t.Fatal("should get 2 item")
}

// find item with prefix empty should return null item
obj, err = boltdb4.KeysWithPrefix("")
if err != nil {
t.Fatal(err)
}
if len(obj) != 0 {
t.Fatal("should get empty item")
}
}

0 comments on commit 94aae1f

Please sign in to comment.