Skip to content

Commit a8646f4

Browse files
committed
feat(lib): add stats.Cache to the lib instance as stats
added `stats` field of type `stats.Stats` to the `InstanceOptions` and `Instance` struct. `newStats` func is called in `NewInstance`. We create a new Cache based on the `cfg.Stats.Type` given in config (for now only option is "fs"), with a MaxSize of `cfg.Stats.MaxSize` (by default 25MiB). The cache is saved based on the `repoPath`, or `cfg.Stats.Path` if it's given.
1 parent ba85eac commit a8646f4

File tree

2 files changed

+41
-0
lines changed

2 files changed

+41
-0
lines changed

lib/lib.go

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ import (
3434
"github.com/qri-io/qri/repo"
3535
fsrepo "github.com/qri-io/qri/repo/fs"
3636
"github.com/qri-io/qri/repo/profile"
37+
"github.com/qri-io/qri/stats"
3738
"github.com/qri-io/qri/update"
3839
"github.com/qri-io/qri/update/cron"
3940
)
@@ -104,6 +105,7 @@ type InstanceOptions struct {
104105
store cafs.Filestore
105106
qfs qfs.Filesystem
106107
regclient *regclient.Client
108+
stats *stats.Stats
107109

108110
// use OptRemoteOptions to set this
109111
remoteOptsFunc func(*remote.Options)
@@ -222,6 +224,14 @@ func OptRegistryClient(cli *regclient.Client) Option {
222224
}
223225
}
224226

227+
// OptStatsCache overrides the configured stats cache
228+
func OptStatsCache(stats *stats.Stats) Option {
229+
return func(o *InstanceOptions) error {
230+
o.stats = stats
231+
return nil
232+
}
233+
}
234+
225235
// NewInstance creates a new Qri Instance, if no Option funcs are provided,
226236
// New uses a default set of Option funcs. Any Option functions passed to this
227237
// function must check whether their fields are nil or not.
@@ -331,6 +341,12 @@ func NewInstance(ctx context.Context, repoPath string, opts ...Option) (qri *Ins
331341
}
332342
}
333343

344+
if o.stats != nil {
345+
inst.stats = o.stats
346+
} else if inst.stats == nil {
347+
inst.stats = newStats(inst.repoPath, cfg)
348+
}
349+
334350
if inst.repo != nil {
335351
inst.fsi = fsi.NewFSI(inst.repo)
336352
}
@@ -476,6 +492,27 @@ func newRepo(path string, cfg *config.Config, store cafs.Filestore, fs qfs.Files
476492
}
477493
}
478494

495+
func newStats(repoPath string, cfg *config.Config) *stats.Stats {
496+
// The stats cache default location is repoPath/stats
497+
// can be overridden in the config: cfg.Stats.Path
498+
path := filepath.Join(repoPath, "stats")
499+
if cfg.Stats.Path != "" {
500+
path = cfg.Stats.Path
501+
}
502+
switch cfg.Stats.Type {
503+
case "fs":
504+
return stats.New(stats.NewOSCache(path, cfg.Stats.MaxSize))
505+
// TODO (ramfox): return a mem and/or postgres version of the stats.Stats
506+
// once those are implemented
507+
// case "mem":
508+
// return stats.New(stats.NewMemCache(path, cfg.Stats.MaxSize))
509+
// case "postgres":
510+
// return stats.New(stats.NewSqlCache(path, cfg.Stats.MaxSize))
511+
default:
512+
return stats.New(nil)
513+
}
514+
}
515+
479516
func newFilesystem(cfg *config.Config, store cafs.Filestore) (qfs.Filesystem, error) {
480517
mux := map[string]qfs.Filesystem{
481518
"local": localfs.NewFS(),
@@ -575,6 +612,7 @@ type Instance struct {
575612
remote *remote.Remote
576613
remoteClient *remote.Client
577614
registry *regclient.Client
615+
stats *stats.Stats
578616

579617
rpc *rpc.Client
580618
}

lib/lib_test.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,9 @@ func TestNewDefaultInstance(t *testing.T) {
113113
if err != nil {
114114
t.Fatal(err)
115115
}
116+
if _, err = os.Stat(filepath.Join(tempDir, "stats")); os.IsNotExist(err) {
117+
t.Errorf("NewInstance error: stats cache never created")
118+
}
116119
}
117120

118121
func CompareInstances(a, b *Instance) error {

0 commit comments

Comments
 (0)