diff --git a/scan.go b/scan.go index f14559b..f2596e6 100644 --- a/scan.go +++ b/scan.go @@ -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() } diff --git a/scan_test.go b/scan_test.go index 1052641..c08e77e 100644 --- a/scan_test.go +++ b/scan_test.go @@ -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)