-
Notifications
You must be signed in to change notification settings - Fork 50
/
kvstore.go
90 lines (76 loc) · 2.77 KB
/
kvstore.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
package kvstore
import (
"context"
"time"
"github.com/justtrackio/gosoline/pkg/cfg"
"github.com/justtrackio/gosoline/pkg/encoding/json"
"github.com/justtrackio/gosoline/pkg/log"
"github.com/pkg/errors"
"github.com/spf13/cast"
)
type Settings struct {
cfg.AppId
DdbSettings DdbSettings
Name string
Ttl time.Duration
BatchSize int
MetricsEnabled bool
InMemorySettings
}
type InMemorySettings struct {
MaxSize int64
Buckets uint32
ItemsToPrune uint32
DeleteBuffer uint32
PromoteBuffer uint32
GetsPerPromote int32
}
//go:generate mockery --name KvStore
type KvStore[T any] interface {
// Check if a key exists in the store.
Contains(ctx context.Context, key any) (bool, error)
// Retrieve a value from the store by the given key. If the value does
// not exist, false is returned and value is not modified.
// value should be a pointer to the model you want to retrieve.
Get(ctx context.Context, key any, value *T) (bool, error)
// Retrieve a set of values from the store. Each value is written to the
// map in values at its key. Values should be something which can be converted to map[interface{}]T.
// Returns a list of missing keys in the store.
GetBatch(ctx context.Context, keys any, values any) ([]any, error)
// Write a value to the store
Put(ctx context.Context, key any, value T) error
// Write a batch of values to the store. Values should be something which
// can be converted to map[interface{}]T.
PutBatch(ctx context.Context, values any) error
// Remove the value with the given key from the store
Delete(ctx context.Context, key any) error
// Remove all values with the given keys from the store
DeleteBatch(ctx context.Context, keys any) error
}
//go:generate mockery --name SizedStore
type SizedStore[T any] interface {
KvStore[T]
// return an estimate about how many elements are currently in the store
// returns nil if no estimate could be returned
EstimateSize() *int64
}
type Factory[T any] func(elementFactory ElementFactory[T], settings *Settings) (KvStore[T], error)
type ElementFactory[T any] func(ctx context.Context, config cfg.Config, logger log.Logger, settings *Settings) (KvStore[T], error)
func buildFactory[T any](ctx context.Context, config cfg.Config, logger log.Logger) Factory[T] {
return func(elementFactory ElementFactory[T], settings *Settings) (KvStore[T], error) {
return elementFactory(ctx, config, logger, settings)
}
}
func CastKeyToString(key interface{}) (string, error) {
str, err := cast.ToStringE(key)
if err == nil {
return str, nil
}
return "", errors.Wrapf(err, "unknown type [%T] for kvstore key", key)
}
func Marshal(v interface{}) ([]byte, error) {
return json.Marshal(v)
}
func Unmarshal(data []byte, v interface{}) error {
return json.Unmarshal(data, v)
}