Fix panic when using ScanPrefix with no data. Fixes #257

This commit is contained in:
Asdine El Hrychy
2019-10-14 19:11:23 +02:00
parent 8e84f96d56
commit c370e07ad6
2 changed files with 13 additions and 3 deletions

12
scan.go
View File

@@ -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
@@ -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()
}

View File

@@ -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)