Skip to content

Commit 3e24834

Browse files
authored
Merge pull request #31 from rmg/empty-result-on-empty-bucket
Give empty results on empty bucket queries
2 parents 66d5d23 + acc7aa3 commit 3e24834

File tree

3 files changed

+23
-1
lines changed

3 files changed

+23
-1
lines changed

.travis.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,4 @@ install:
77

88
script:
99
- go test -v -covermode=count -coverprofile=coverage.out
10-
- $HOME/gopath/bin/goveralls -coverprofile=coverage.out -service=travis-ci -repotoken $COVERALLS_TOKEN
10+
- $HOME/gopath/bin/goveralls -coverprofile=coverage.out -service=travis-ci

find_test.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -633,6 +633,21 @@ func TestFindOnInvalidIndex(t *testing.T) {
633633
})
634634
}
635635

636+
func TestFindOnEmptyBucketWithIndex(t *testing.T) {
637+
testWrap(t, func(store *bolthold.Store, t *testing.T) {
638+
// DO NOT INSERT DATA
639+
var result []ItemTest
640+
641+
err := store.Find(&result, bolthold.Where("Category").Eq("animal").Index("Category"))
642+
if err != nil {
643+
t.Fatalf("Find query against a valid index name but an empty data bucket return an error!")
644+
}
645+
if len(result) > 0 {
646+
t.Fatalf("Find query against an empty bucket returned results!")
647+
}
648+
})
649+
}
650+
636651
func TestQueryStringPrint(t *testing.T) {
637652
q := bolthold.Where("FirstField").Eq("first value").And("SecondField").Gt("Second Value").And("ThirdField").
638653
Lt("Third Value").And("FourthField").Ge("FourthValue").And("FifthField").Le("FifthValue").And("SixthField").

query.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -510,6 +510,13 @@ type record struct {
510510

511511
func runQuery(tx *bolt.Tx, dataType interface{}, query *Query, retrievedKeys keyList, skip int, action func(r *record) error) error {
512512
storer := newStorer(dataType)
513+
514+
bkt := tx.Bucket([]byte(storer.Type()))
515+
if bkt == nil || bkt.Stats().KeyN == 0 {
516+
// if the bucket doesn't exist or is empty then our job is really easy!
517+
return nil
518+
}
519+
513520
if query.index != "" && tx.Bucket(indexBucketName(storer.Type(), query.index)) == nil {
514521
return fmt.Errorf("The index %s does not exist", query.index)
515522
}

0 commit comments

Comments
 (0)