-
Notifications
You must be signed in to change notification settings - Fork 0
/
kv.go
191 lines (168 loc) · 3.65 KB
/
kv.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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
// Copyright 2021 The June Authors. All rights reserved.
// Use of this source code is governed by Apache License
// that can be found in the LICENSE file.
package storekv
import (
"bytes"
"context"
"fmt"
"io"
"strings"
"sync"
"sync/atomic"
"github.com/reusee/june/key"
"github.com/reusee/june/store"
"github.com/reusee/june/sys"
"github.com/reusee/pr2"
"github.com/reusee/sb"
)
type StringKey string
func (s StringKey) Error() string {
return "key: " + string(s)
}
type KV interface {
StoreID() string // IndexID is only used in initialization, should be stable and unique
Name() string // Name is for human readable
KeyPut(key string, r io.Reader) error // implementation should not retain reader underlying bytes after return
KeyGet(key string, fn func(io.Reader) error) error
KeyExists(key string) (bool, error)
KeyIter(prefix string, fn func(key string) error) error
KeyDelete(key ...string) error
CostInfo() CostInfo
}
type CostInfo struct {
Put int
Get int
Exists int
Iter int
Delete int
}
type Store struct {
wg *pr2.WaitGroup
name string
kv KV
codec Codec
cache store.Cache
newHashState key.NewHashState
_id store.ID
prefix string
setupIDOnce sync.Once
readDisabled bool
writeDisabled bool
parallel int
offloads []WithOffload
costInfo CostInfo
}
var _ store.Store = new(Store)
type New func(
ctx context.Context,
kv KV,
prefix string,
options ...NewOption,
) (*Store, error)
type NewOption interface {
IsNewOption()
}
var serial int64
func (Def) New(
newHashState key.NewHashState,
parallel sys.Parallel,
) (
newStore New,
) {
newStore = func(
ctx context.Context,
kv KV,
prefix string,
options ...NewOption,
) (*Store, error) {
codec := Codec(DefaultCodec)
var cache Cache
writeDisabled := false
readDisabled := false
var offloads []WithOffload
for _, option := range options {
switch option := option.(type) {
case withCodec:
codec = option.Codec
case withCache:
cache = option.Cache
case WithoutWrite:
writeDisabled = true
case WithoutRead:
readDisabled = true
case WithOffload:
offloads = append(offloads, option)
default:
panic(fmt.Errorf("not handled option: %T", option))
}
}
store := &Store{
wg: pr2.NewWaitGroup(ctx),
name: fmt.Sprintf("kv%d(%s, %s)",
atomic.AddInt64(&serial, 1),
kv.Name(),
prefix,
),
kv: kv,
newHashState: newHashState,
prefix: prefix,
codec: codec,
cache: cache,
writeDisabled: writeDisabled,
readDisabled: readDisabled,
parallel: int(parallel),
offloads: offloads,
costInfo: kv.CostInfo(),
}
return store, nil
}
return
}
func (s *Store) Name() string {
return s.name
}
func (s *Store) getID() (id store.ID, err error) {
defer he(&err)
s.setupIDOnce.Do(func() {
idPath := strings.Join([]string{
s.codec.ID(),
s.prefix,
"__id__",
}, "")
setupID := func() (err error) {
defer he(&err)
id := StoreID(fmt.Sprintf("kv(%s)", s.kv.StoreID()))
buf := new(bytes.Buffer)
if err := sb.Copy(
sb.Marshal(id),
sb.Encode(buf),
); err != nil {
return err
}
err = s.kv.KeyPut(idPath, buf)
ce(err)
s._id = id
return nil
}
if err := s.kv.KeyGet(idPath, func(r io.Reader) (err error) {
defer he(&err)
var id StoreID
if err := sb.Copy(
sb.Decode(r),
sb.Unmarshal(&id),
); err != nil {
return err
}
s._id = id
return nil
}); is(err, ErrKeyNotFound) {
err := setupID()
ce(err)
} else {
ce(err)
}
})
id = s._id
return
}