Skip to content

Commit

Permalink
switch to NanoLIB kv storage
Browse files Browse the repository at this point in the history
  • Loading branch information
jessepeterson committed Jun 13, 2024
1 parent 3552d7b commit 1455cac
Show file tree
Hide file tree
Showing 15 changed files with 52 additions and 203 deletions.
11 changes: 6 additions & 5 deletions engine/storage/diskv/diskv.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@ import (
"path/filepath"

"github.com/micromdm/nanocmd/engine/storage/kv"
"github.com/micromdm/nanocmd/utils/kv/kvdiskv"
"github.com/micromdm/nanocmd/utils/uuid"

"github.com/micromdm/nanolib/storage/kv/kvdiskv"
"github.com/peterbourgon/diskv/v3"
)

Expand All @@ -18,23 +19,23 @@ type Diskv struct {
func New(path string) *Diskv {
flatTransform := func(s string) []string { return []string{} }
return &Diskv{KV: kv.New(
kvdiskv.NewBucket(diskv.New(diskv.Options{
kvdiskv.New(diskv.New(diskv.Options{
BasePath: filepath.Join(path, "engine", "step"),
Transform: flatTransform,
CacheSizeMax: 1024 * 1024,
})),
kvdiskv.NewBucket(diskv.New(diskv.Options{
kvdiskv.New(diskv.New(diskv.Options{
BasePath: filepath.Join(path, "engine", "idcmd"),
Transform: flatTransform,
CacheSizeMax: 1024 * 1024,
})),
kvdiskv.NewBucket(diskv.New(diskv.Options{
kvdiskv.New(diskv.New(diskv.Options{
BasePath: filepath.Join(path, "engine", "eventsubs"),
Transform: flatTransform,
CacheSizeMax: 1024 * 1024,
})),
uuid.NewUUID(),
kvdiskv.NewBucket(diskv.New(diskv.Options{
kvdiskv.New(diskv.New(diskv.Options{
BasePath: filepath.Join(path, "engine", "wfstatus"),
Transform: flatTransform,
CacheSizeMax: 1024 * 1024,
Expand Down
11 changes: 6 additions & 5 deletions engine/storage/inmem/inmem.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@ package inmem

import (
"github.com/micromdm/nanocmd/engine/storage/kv"
"github.com/micromdm/nanocmd/utils/kv/kvmap"
"github.com/micromdm/nanocmd/utils/uuid"

"github.com/micromdm/nanolib/storage/kv/kvmap"
)

// InMem is an in-memory engine storage backend.
Expand All @@ -14,10 +15,10 @@ type InMem struct {

func New() *InMem {
return &InMem{KV: kv.New(
kvmap.NewBucket(),
kvmap.NewBucket(),
kvmap.NewBucket(),
kvmap.New(),
kvmap.New(),
kvmap.New(),
uuid.NewUUID(),
kvmap.NewBucket(),
kvmap.New(),
)}
}
7 changes: 4 additions & 3 deletions engine/storage/kv/event.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@ import (
"strings"

"github.com/micromdm/nanocmd/engine/storage"
"github.com/micromdm/nanocmd/utils/kv"
"github.com/micromdm/nanocmd/workflow"

"github.com/micromdm/nanolib/storage/kv"
)

const (
Expand Down Expand Up @@ -100,12 +101,12 @@ func (s *KV) RetrieveEventSubscriptions(ctx context.Context, names []string) (ma
return ret, nil
}

func kvFindEventSubNamesByEvent(ctx context.Context, b kv.TraversingBucket, f workflow.EventFlag) ([]string, error) {
func kvFindEventSubNamesByEvent(ctx context.Context, b kv.KeysPrefixTraversingBucket, f workflow.EventFlag) ([]string, error) {
var names []string

// this.. is not very efficient. perhaps it would be better to
// make a specific bucket/index for this.
for k := range b.Keys(nil) {
for k := range b.Keys(ctx, nil) {
if !strings.HasSuffix(k, keySfxEventFlag) {
continue
}
Expand Down
15 changes: 8 additions & 7 deletions engine/storage/kv/kv.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,22 +10,23 @@ import (
"time"

"github.com/micromdm/nanocmd/engine/storage"
"github.com/micromdm/nanocmd/utils/kv"
"github.com/micromdm/nanocmd/utils/uuid"

"github.com/micromdm/nanolib/storage/kv"
)

// KV is a workflow engine storage backend using a key-value interface.
type KV struct {
mu sync.RWMutex
stepStore kv.TraversingBucket
idCmdStore kv.TraversingBucket
eventStore kv.TraversingBucket
stepStore kv.KeysPrefixTraversingBucket
idCmdStore kv.KeysPrefixTraversingBucket
eventStore kv.KeysPrefixTraversingBucket
ider uuid.IDer
statusStore kv.TraversingBucket
statusStore kv.KeysPrefixTraversingBucket
}

// New creates a new key-value workflow engine storage backend.
func New(stepStore kv.TraversingBucket, idCmdStore kv.TraversingBucket, eventStore kv.TraversingBucket, ider uuid.IDer, statusStore kv.TraversingBucket) *KV {
func New(stepStore kv.KeysPrefixTraversingBucket, idCmdStore kv.KeysPrefixTraversingBucket, eventStore kv.KeysPrefixTraversingBucket, ider uuid.IDer, statusStore kv.KeysPrefixTraversingBucket) *KV {
return &KV{
stepStore: stepStore,
idCmdStore: idCmdStore,
Expand Down Expand Up @@ -324,7 +325,7 @@ func (s *KV) RecordWorkflowStarted(ctx context.Context, ids []string, workflowNa
// ClearWorkflowStatus removes all workflow start times for id.
func (s *KV) ClearWorkflowStatus(ctx context.Context, id string) error {
var toDelete []string
for k := range s.statusStore.Keys(nil) {
for k := range s.statusStore.Keys(ctx, nil) {
// very inefficient! this could be a large table
if strings.HasPrefix(k, id+".") {
toDelete = append(toDelete, k)
Expand Down
7 changes: 4 additions & 3 deletions engine/storage/kv/prim.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ import (
"time"

"github.com/micromdm/nanocmd/engine/storage"
"github.com/micromdm/nanocmd/utils/kv"

"github.com/micromdm/nanolib/storage/kv"
)

const (
Expand Down Expand Up @@ -257,13 +258,13 @@ func kvDeleteStepNotUntil(ctx context.Context, b kv.Bucket, stepID string) error
}

// kvFindWorkflowStepsWithIDs finds specific workflow steps (step IDs) for specific enrollment IDs.
func kvFindWorkflowStepsWithIDs(ctx context.Context, b kv.TraversingBucket, name string, ids []string) ([]string, error) {
func kvFindWorkflowStepsWithIDs(ctx context.Context, b kv.KeysPrefixTraversingBucket, name string, ids []string) ([]string, error) {
var stepIDs []string

// this.. is not very efficient. perhaps it would be better to
// make a specific bucket/index for this.
start:
for k := range b.Keys(nil) {
for k := range b.Keys(ctx, nil) {
if !strings.HasSuffix(k, keySfxStepMeta) {
continue
}
Expand Down
15 changes: 8 additions & 7 deletions engine/storage/kv/worker_prim.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,18 @@ import (
"time"

"github.com/micromdm/nanocmd/engine/storage"
"github.com/micromdm/nanocmd/utils/kv"

"github.com/micromdm/nanolib/storage/kv"
)

func kvFindNotUntilStepsWithIDs(ctx context.Context, b kv.TraversingBucket) ([]string, error) {
func kvFindNotUntilStepsWithIDs(ctx context.Context, b kv.KeysPrefixTraversingBucket) ([]string, error) {
var stepIDs []string

now := time.Now()

// this.. is not very efficient. perhaps it would be better to
// make a specific bucket/index for this.
for k := range b.Keys(nil) {
for k := range b.Keys(ctx, nil) {
if !strings.HasSuffix(k, keySfxStepNotUntil) {
continue
}
Expand Down Expand Up @@ -104,14 +105,14 @@ func kvGetIDCmdRaw(ctx context.Context, b kv.Bucket, id, cmdUUID string) (*stora
}, nil
}

func kvFindTimedOutStepIDs(ctx context.Context, b kv.TraversingBucket) ([]string, error) {
func kvFindTimedOutStepIDs(ctx context.Context, b kv.KeysPrefixTraversingBucket) ([]string, error) {
var stepIDs []string

now := time.Now()

// this.. is not very efficient. perhaps it would be better to
// make a specific bucket/index for this.
for k := range b.Keys(nil) {
for k := range b.Keys(ctx, nil) {
if !strings.HasSuffix(k, keySfxStepTimeout) {
continue
}
Expand Down Expand Up @@ -167,14 +168,14 @@ func kvGetIDCmdStepResult(ctx context.Context, b kv.Bucket, id, cmdUUID string,
return result, err
}

func kvFindCommandsToRePush(ctx context.Context, b kv.TraversingBucket, ifBefore time.Time, setTo time.Time) ([]string, error) {
func kvFindCommandsToRePush(ctx context.Context, b kv.KeysPrefixTraversingBucket, ifBefore time.Time, setTo time.Time) ([]string, error) {
var ids []string

resetLastPushes := make(map[string][]byte)

// this.. is not very efficient. perhaps it would be better to
// make a specific bucket/index for this.
for k := range b.Keys(nil) {
for k := range b.Keys(ctx, nil) {
if !strings.HasSuffix(k, keySfxCmdLastPush) {
continue
}
Expand Down
5 changes: 3 additions & 2 deletions subsystem/cmdplan/storage/diskv/diskv.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ import (
"path/filepath"

"github.com/micromdm/nanocmd/subsystem/cmdplan/storage/kv"
"github.com/micromdm/nanocmd/utils/kv/kvdiskv"

"github.com/micromdm/nanolib/storage/kv/kvdiskv"
"github.com/peterbourgon/diskv/v3"
)

Expand All @@ -18,7 +19,7 @@ type Diskv struct {
func New(path string) *Diskv {
flatTransform := func(s string) []string { return []string{} }
return &Diskv{
KV: kv.New(kvdiskv.NewBucket(diskv.New(diskv.Options{
KV: kv.New(kvdiskv.New(diskv.New(diskv.Options{
BasePath: filepath.Join(path, "cmdplan"),
Transform: flatTransform,
CacheSizeMax: 1024 * 1024,
Expand Down
5 changes: 3 additions & 2 deletions subsystem/cmdplan/storage/inmem/inmem.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ package inmem

import (
"github.com/micromdm/nanocmd/subsystem/cmdplan/storage/kv"
"github.com/micromdm/nanocmd/utils/kv/kvmap"

"github.com/micromdm/nanolib/storage/kv/kvmap"
)

// InMem is a command plan storage backend backed by an in-memory key-valye store.
Expand All @@ -12,5 +13,5 @@ type InMem struct {
}

func New() *InMem {
return &InMem{KV: kv.New(kvmap.NewBucket())}
return &InMem{KV: kv.New(kvmap.New())}
}
3 changes: 2 additions & 1 deletion subsystem/cmdplan/storage/kv/kv.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ import (
"sync"

"github.com/micromdm/nanocmd/subsystem/cmdplan/storage"
"github.com/micromdm/nanocmd/utils/kv"

"github.com/micromdm/nanolib/storage/kv"
)

// KV is a cmdplan storage backend using JSON with key-value storage.
Expand Down
5 changes: 3 additions & 2 deletions subsystem/filevault/storage/diskv/diskv.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ import (

"github.com/micromdm/nanocmd/subsystem/filevault/storage"
"github.com/micromdm/nanocmd/subsystem/filevault/storage/kv"
"github.com/micromdm/nanocmd/utils/kv/kvdiskv"

"github.com/micromdm/nanolib/storage/kv/kvdiskv"
"github.com/peterbourgon/diskv/v3"
)

Expand All @@ -20,7 +21,7 @@ func New(path string, p storage.PRKStorage) (*Diskv, error) {
flatTransform := func(s string) []string { return []string{} }
kvStore, err := kv.New(
context.Background(),
kvdiskv.NewBucket(diskv.New(diskv.Options{
kvdiskv.New(diskv.New(diskv.Options{
BasePath: filepath.Join(path, "fvkey"),
Transform: flatTransform,
CacheSizeMax: 1024 * 1024,
Expand Down
5 changes: 3 additions & 2 deletions subsystem/filevault/storage/inmem/inmem.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ import (

"github.com/micromdm/nanocmd/subsystem/filevault/storage"
"github.com/micromdm/nanocmd/subsystem/filevault/storage/kv"
"github.com/micromdm/nanocmd/utils/kv/kvmap"

"github.com/micromdm/nanolib/storage/kv/kvmap"
)

// InMem implements an in-memory FileVault storage backend.
Expand All @@ -15,6 +16,6 @@ type InMem struct {
}

func New(p storage.PRKStorage) (*InMem, error) {
kvStore, err := kv.New(context.Background(), kvmap.NewBucket(), p)
kvStore, err := kv.New(context.Background(), kvmap.New(), p)
return &InMem{KV: kvStore}, err
}
3 changes: 2 additions & 1 deletion subsystem/filevault/storage/kv/kv.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ import (

"github.com/micromdm/nanocmd/subsystem/filevault/storage"
"github.com/micromdm/nanocmd/utils/cryptoutil"
"github.com/micromdm/nanocmd/utils/kv"

"github.com/micromdm/nanolib/storage/kv"
"github.com/smallstep/pkcs7"
)

Expand Down
56 changes: 0 additions & 56 deletions utils/kv/kv.go

This file was deleted.

37 changes: 0 additions & 37 deletions utils/kv/kvdiskv/kvdiskv.go

This file was deleted.

Loading

0 comments on commit 1455cac

Please sign in to comment.