Skip to content

Commit

Permalink
Prevent panic when index matcher returns nil tree. Fixes #37
Browse files Browse the repository at this point in the history
  • Loading branch information
asdine committed Sep 20, 2019
1 parent afbb55a commit e9e4ff1
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 0 deletions.
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,5 @@ require (
go.etcd.io/bbolt v1.3.3 // indirect
golang.org/x/tools v0.0.0-20190524210228-3d17549cdc6b
)

go 1.13
5 changes: 5 additions & 0 deletions query/matcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -452,6 +452,11 @@ type indexResultTable struct {
func (i *indexResultTable) Iterate(fn func([]byte, record.Record) error) error {
var err error

// if no results,
if i.tree == nil {
return nil
}

i.tree.Ascend(func(it btree.Item) bool {
var rec record.Record
recordID := []byte(it.(Item))
Expand Down
27 changes: 27 additions & 0 deletions query/query_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,33 @@ func TestSelect(t *testing.T) {
require.NoError(t, err)
})

t.Run("WithEmptyIndex", func(t *testing.T) {
db, err := genji.New(memory.NewEngine())
require.NoError(t, err)

var n int
err = db.Update(func(tx *genji.Tx) error {
tb, err := tx.CreateTable("test")
if err != nil {
return err
}

_, err = tb.CreateIndex("a", index.Options{})
if err != nil {
return err
}

res := Select().From(tb).Where(And(StringField("a").Eq("foo"))).Limit(1).Run(tx)
if res.Err() != nil {
return res.Err()
}

n, err = res.Count()
return err
})
require.NoError(t, err)
require.Equal(t, 0, n)
})
}

func TestDelete(t *testing.T) {
Expand Down

0 comments on commit e9e4ff1

Please sign in to comment.