Skip to content

Commit

Permalink
Fix panic when using ScanPrefix with no data. Fixes #257
Browse files Browse the repository at this point in the history
  • Loading branch information
asdine committed Oct 14, 2019
1 parent 8e84f96 commit c370e07
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 3 deletions.
12 changes: 9 additions & 3 deletions scan.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,16 @@ func (n *node) PrefixScan(prefix string) []Node {
}

func (n *node) prefixScan(tx *bolt.Tx, prefix string) []Node {

var (
prefixBytes = []byte(prefix)
nodes []Node
c = n.cursor(tx)
)

if c == nil {
return nil
}

for k, v := c.Seek(prefixBytes); k != nil && bytes.HasPrefix(k, prefixBytes); k, v = c.Next() {
if v != nil {
continue
Expand Down Expand Up @@ -86,11 +89,14 @@ func (n *node) rangeScan(tx *bolt.Tx, min, max string) []Node {
}

func (n *node) cursor(tx *bolt.Tx) *bolt.Cursor {

var c *bolt.Cursor

if len(n.rootBucket) > 0 {
c = n.GetBucket(tx).Cursor()
b := n.GetBucket(tx)
if b == nil {
return nil
}
c = b.Cursor()
} else {
c = tx.Cursor()
}
Expand Down
4 changes: 4 additions & 0 deletions scan_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ func TestPrefixScan(t *testing.T) {

node := db.From("node")

// run prefix scan on empty data
list := node.PrefixScan("foo")
require.Empty(t, list)

doTestPrefixScan(t, node)
doTestPrefixScan(t, db)

Expand Down

0 comments on commit c370e07

Please sign in to comment.