Skip to content

Commit

Permalink
Add optional index metrics.
Browse files Browse the repository at this point in the history
  • Loading branch information
akrennmair committed Oct 30, 2023
1 parent 63a683d commit 929c22f
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 7 deletions.
2 changes: 1 addition & 1 deletion cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ type LRUCache struct {
metrics *CacheMetrics
}

// WithCacheMetrics is an option for LRUCache to set a CacheMetrics option.
// WithCacheMetrics is an option for LRUCache to set a CacheMetrics object.
func WithCacheMetrics(metrics *CacheMetrics) LRUCacheOption {
return func(c *LRUCache) {
c.metrics = metrics
Expand Down
21 changes: 20 additions & 1 deletion index.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ func OpenIndexFromBoltDatabase(db *bbolt.DB, opts ...IndexOption) (*Index, error
}

idx.cache = &nullCache{}
idx.metrics = &IndexMetrics{}

for _, opt := range opts {
if err := opt(idx); err != nil {
Expand All @@ -77,7 +78,8 @@ type Index struct {

values colGetter

cache Cache
cache Cache
metrics *IndexMetrics
}

type colGetter interface {
Expand Down Expand Up @@ -158,6 +160,23 @@ func WithPreloadedData() IndexOption {
}
}

// WithIndexMetrics is an option for OpenIndex and OpenIndexFromBoltDatabase to set
// an IndexMetrics object.
func WithIndexMetrics(metrics *IndexMetrics) IndexOption {
return func(c *Index) error {
c.metrics = metrics
return nil
}
}

type IndexMetrics struct {
ExecuteDuration HistogramMetric
}

type HistogramMetric interface {
Observe(float64)
}

func newPreloadedColGetter(db *bbolt.DB) (colGetter, error) {
cg := &preloadedColGetter{
values: map[uint64]*roaring.Bitmap{},
Expand Down
11 changes: 9 additions & 2 deletions query.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"math/bits"
"sort"
"strings"
"time"

"github.com/RoaringBitmap/roaring"
)
Expand All @@ -24,8 +25,14 @@ type Query struct {
groupByFields []groupBy
}

// Execute runs the query on the provided index and returns the query result.
func (q *Query) Execute(idx *Index) (*Result, error) {
// Execute runs the provided query on the index and returns the query result.
func (idx *Index) Execute(q *Query) (*Result, error) {
if idx.metrics.ExecuteDuration != nil {
defer func(t0 time.Time) {
idx.metrics.ExecuteDuration.Observe(time.Since(t0).Seconds())
}(time.Now())
}

idx.mtx.RLock()
defer idx.mtx.RUnlock()

Expand Down
6 changes: 3 additions & 3 deletions query_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ func TestQuery(t *testing.T) {
for _, tt := range testData {
t.Run(tt.name, func(t *testing.T) {
t.Logf("query = %s", tt.query.Expr.String())
result, err := tt.query.Execute(idx)
result, err := idx.Execute(tt.query)
require.NoError(t, err)
require.NotNil(t, result)
require.Equal(t, tt.expectedResult, result.Count)
Expand Down Expand Up @@ -333,7 +333,7 @@ func TestQueryGroupBy(t *testing.T) {

for _, tt := range testData {
t.Run(tt.name, func(t *testing.T) {
result, err := tt.query.Execute(idx)
result, err := idx.Execute(tt.query)
require.NoError(t, err)
require.NotNil(t, result)
require.Equal(t, tt.expectedResult, result)
Expand Down Expand Up @@ -431,7 +431,7 @@ func runBenchmarkQuery(b *testing.B, idx *Index) {
GroupBy: []string{"is_cool"},
}

_, err := q.Execute(idx)
_, err := idx.Execute(q)
require.NoError(b, err)
}
}

0 comments on commit 929c22f

Please sign in to comment.