Skip to content

Commit

Permalink
fix(pkger): lazy init the pkger store
Browse files Browse the repository at this point in the history
closes: #18383
  • Loading branch information
jsteenb2 committed Jun 5, 2020
1 parent 394198a commit 831fad6
Showing 1 changed file with 20 additions and 2 deletions.
22 changes: 20 additions & 2 deletions pkger/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"bytes"
"context"
"encoding/json"
"sync"
"time"

"github.com/influxdata/influxdb/v2"
Expand Down Expand Up @@ -42,6 +43,8 @@ type (
type StoreKV struct {
kvStore kv.Store
indexBase *kv.IndexStore

once sync.Once
}

var _ Store = (*StoreKV)(nil)
Expand Down Expand Up @@ -88,7 +91,7 @@ func (s *StoreKV) ListStacks(ctx context.Context, orgID influxdb.ID, f ListFilte
}

var stacks []Stack
err = s.kvStore.View(ctx, func(tx kv.Tx) error {
err = s.view(ctx, func(tx kv.Tx) error {
return s.indexBase.Find(ctx, tx, kv.FindOpts{
CaptureFn: func(key []byte, decodedVal interface{}) error {
stack, err := convertStackEntToStack(decodedVal.(*entStack))
Expand Down Expand Up @@ -165,7 +168,7 @@ func (s *StoreKV) listStacksByID(ctx context.Context, orgID influxdb.ID, stackID
// ReadStackByID reads a stack by the provided ID.
func (s *StoreKV) ReadStackByID(ctx context.Context, id influxdb.ID) (Stack, error) {
var stack Stack
err := s.kvStore.View(ctx, func(tx kv.Tx) error {
err := s.view(ctx, func(tx kv.Tx) error {
decodedEnt, err := s.indexBase.FindEnt(ctx, tx, kv.Entity{PK: kv.EncID(id)})
if err != nil {
return err
Expand Down Expand Up @@ -256,6 +259,21 @@ func (s *StoreKV) indexStoreBase(resource string) *kv.StoreBase {
return kv.NewStoreBase(resource, indexBucket, kv.EncUniqKey, kv.EncIDKey, kv.DecIndexID, decValToEntFn)
}

func (s *StoreKV) view(ctx context.Context, fn func(tx kv.Tx) error) error {
if err := s.lazyInit(ctx); err != nil {
return err
}
return s.kvStore.View(ctx, fn)
}

func (s *StoreKV) lazyInit(ctx context.Context) error {
var err error
s.once.Do(func() {
err = s.Init(ctx)
})
return err
}

func convertStackToEnt(stack Stack) (kv.Entity, error) {
idBytes, err := stack.ID.Encode()
if err != nil {
Expand Down

0 comments on commit 831fad6

Please sign in to comment.