mirror of
https://github.com/asdine/storm.git
synced 2025-09-26 19:01:14 +08:00
Fix panic when using ScanPrefix with no data. Fixes #257
This commit is contained in:
12
scan.go
12
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()
|
||||
}
|
||||
|
@@ -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)
|
||||
|
||||
|
Reference in New Issue
Block a user