From af2eaaeb5e49184f30bc33d054e008cc138a4b21 Mon Sep 17 00:00:00 2001 From: Ryan Graham Date: Mon, 30 Apr 2018 16:29:01 -0700 Subject: [PATCH 1/3] test: add failing test for #30 When running Find() against an empty store then it shouldn't give an error when the requested index doesn't exist because the index won't exist until at least 1 record has been added to the store. --- find_test.go | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/find_test.go b/find_test.go index 108d290..b465487 100644 --- a/find_test.go +++ b/find_test.go @@ -633,6 +633,21 @@ func TestFindOnInvalidIndex(t *testing.T) { }) } +func TestFindOnEmptyBucketWithIndex(t *testing.T) { + testWrap(t, func(store *bolthold.Store, t *testing.T) { + // DO NOT INSERT DATA + var result []ItemTest + + err := store.Find(&result, bolthold.Where("Category").Eq("animal").Index("Category")) + if err != nil { + t.Fatalf("Find query against a valid index name but an empty data bucket return an error!") + } + if len(result) > 0 { + t.Fatalf("Find query against an empty bucket returned results!") + } + }) +} + func TestQueryStringPrint(t *testing.T) { q := bolthold.Where("FirstField").Eq("first value").And("SecondField").Gt("Second Value").And("ThirdField"). Lt("Third Value").And("FourthField").Ge("FourthValue").And("FifthField").Le("FifthValue").And("SixthField"). From 2ed8df0da6652410a7fb8d06e6f916bf90c71d98 Mon Sep 17 00:00:00 2001 From: Ryan Graham Date: Mon, 30 Apr 2018 16:33:23 -0700 Subject: [PATCH 2/3] improve query handling on empty buckets If the bucket for the storer doesn't exist or is empty then we can bypass all of the usual work and just immediately return. In addition to being faster, this also allows us to return an empty result set instead of an error when attempting to use an index before it has been created. Fixes #30 --- query.go | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/query.go b/query.go index 2f4dd41..15fbb3c 100644 --- a/query.go +++ b/query.go @@ -510,6 +510,13 @@ type record struct { func runQuery(tx *bolt.Tx, dataType interface{}, query *Query, retrievedKeys keyList, skip int, action func(r *record) error) error { storer := newStorer(dataType) + + bkt := tx.Bucket([]byte(storer.Type())) + if bkt == nil || bkt.Stats().KeyN == 0 { + // if the bucket doesn't exist or is empty then our job is really easy! + return nil + } + if query.index != "" && tx.Bucket(indexBucketName(storer.Type(), query.index)) == nil { return fmt.Errorf("The index %s does not exist", query.index) } From acc7aa397bfa0c05dd8c8c93e8625f3b6d0b2ed7 Mon Sep 17 00:00:00 2001 From: Ryan Graham Date: Mon, 30 Apr 2018 17:06:47 -0700 Subject: [PATCH 3/3] ci: remove redundant $COVERALLS_TOKEN reference According to the goveralls docs, the `$COVERALLS_TOKEN` env var is automatically used as the default value for `-repotoken`. Removing the explicit reference should allow pull requests to build successfully by not having a dangling CLI argument from `$COVERALLS_TOKEN` not being exported. --- .travis.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.travis.yml b/.travis.yml index be5221f..4c36a28 100644 --- a/.travis.yml +++ b/.travis.yml @@ -7,4 +7,4 @@ install: script: - go test -v -covermode=count -coverprofile=coverage.out - - $HOME/gopath/bin/goveralls -coverprofile=coverage.out -service=travis-ci -repotoken $COVERALLS_TOKEN + - $HOME/gopath/bin/goveralls -coverprofile=coverage.out -service=travis-ci