/
store.go
403 lines (346 loc) · 10.4 KB
/
store.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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package store
import (
"context"
"encoding/json"
"fmt"
"os"
"reflect"
"sort"
"sync"
"time"
"github.com/chain5j/chain5j-pkg/util/convutil"
"github.com/chain5j/logger"
"github.com/xwc1125/apisix-go/internal/apisix/core/entity"
"github.com/xwc1125/apisix-go/internal/apisix/core/params"
storage2 "github.com/xwc1125/apisix-go/internal/apisix/core/storage"
"github.com/xwc1125/apisix-go/internal/apisix/utils/runtime"
)
type Pagination struct {
PageSize int `json:"page_size" form:"page_size" auto_read:"page_size"`
PageNumber int `json:"page" form:"page" auto_read:"page"`
}
type Interface interface {
Type() HubKey
Get(ctx context.Context, key string) (interface{}, error) // 通过key查询
List(ctx context.Context, input ListInput) (*ListOutput, error) // 查询list
Create(ctx context.Context, obj interface{}) (interface{}, error) // 创建
Update(ctx context.Context, obj interface{}, createIfNotExist bool) (interface{}, error) // 更新
BatchDelete(ctx context.Context, keys []string) error // 批量删除
}
type GenericStore struct {
Stg storage2.Interface
cache sync.Map
opt GenericStoreOption
cancel context.CancelFunc
}
type WatchEvent interface {
WatchEventPut(key string, objPtr interface{})
WatchEventDelete(key string)
}
type GenericStoreOption struct {
BasePath string
ObjType reflect.Type
KeyFunc func(obj interface{}) string
StockCheck func(obj interface{}, stockObj interface{}) error
Validator Validator
HubKey HubKey
WatchEvent WatchEvent
}
func NewGenericStore(opt GenericStoreOption) (*GenericStore, error) {
if opt.BasePath == "" {
logger.Error("base path empty")
return nil, fmt.Errorf("base path can not be empty")
}
if opt.ObjType == nil {
logger.Error("object type is nil")
return nil, fmt.Errorf("object type can not be nil")
}
if opt.KeyFunc == nil {
logger.Error("key func is nil")
return nil, fmt.Errorf("key func can not be nil")
}
if opt.ObjType.Kind() == reflect.Ptr {
opt.ObjType = opt.ObjType.Elem()
}
if opt.ObjType.Kind() != reflect.Struct {
logger.Error("obj type is invalid")
return nil, fmt.Errorf("obj type is invalid")
}
s := &GenericStore{
opt: opt,
}
s.Stg = storage2.GenEtcdStorage()
return s, nil
}
func (s *GenericStore) Init() error {
lc, lcancel := context.WithTimeout(context.TODO(), 5*time.Second)
defer lcancel()
ret, err := s.Stg.List(lc, s.opt.BasePath)
if err != nil {
return err
}
for i := range ret {
key := ret[i].Key[len(s.opt.BasePath)+1:]
objPtr, err := s.StringToObjPtr(ret[i].Value, key)
if err != nil {
_, _ = fmt.Fprintln(os.Stderr, "Error occurred while initializing logical store: ", s.opt.BasePath)
return err
}
s.cache.Store(s.opt.KeyFunc(objPtr), objPtr)
}
c, cancel := context.WithCancel(context.TODO())
ch := s.Stg.Watch(c, s.opt.BasePath)
go func() {
defer runtime.HandlePanic()
for event := range ch {
if event.Canceled {
logger.Warn("watch failed", "err", event.Error)
}
for i := range event.Events {
key := event.Events[i].Key[len(s.opt.BasePath)+1:]
switch event.Events[i].Type {
case storage2.EventTypePut:
objPtr, err := s.StringToObjPtr(event.Events[i].Value, key)
if err != nil {
logger.Warn("value convert to obj failed", "basePath", s.opt.BasePath, "key", key, "err", err)
continue
}
logger.Debug("watch event put", "basePath", s.opt.BasePath, "key", key)
s.cache.Store(key, objPtr)
if s.opt.WatchEvent != nil {
s.opt.WatchEvent.WatchEventPut(key, objPtr)
}
case storage2.EventTypeDelete:
logger.Debug("watch event delete", "basePath", s.opt.BasePath, "key", key)
s.cache.Delete(key)
if s.opt.WatchEvent != nil {
s.opt.WatchEvent.WatchEventDelete(key)
}
}
}
}
}()
s.cancel = cancel
return nil
}
func (s *GenericStore) Type() HubKey {
return s.opt.HubKey
}
func (s *GenericStore) Get(_ context.Context, key string) (interface{}, error) {
ret, ok := s.cache.Load(key)
if !ok {
logger.Warn("data not found by key", "key", key)
return nil, params.ErrNotFound
}
return ret, nil
}
type ListInput struct {
Predicate func(obj interface{}) bool
Format func(obj interface{}) interface{}
PageSize int
// start from 1
PageNumber int
Less func(i, j interface{}) bool
}
type ListOutput struct {
Rows []interface{} `json:"rows"`
TotalSize int `json:"total_size"`
}
// NewListOutput returns JSON marshalling safe struct pointer for empty slice
func NewListOutput() *ListOutput {
return &ListOutput{Rows: make([]interface{}, 0)}
}
var defLessFunc = func(i, j interface{}) bool {
iBase := i.(entity.GetBaseInfo).GetBaseInfo()
jBase := j.(entity.GetBaseInfo).GetBaseInfo()
if iBase.CreateTime != jBase.CreateTime {
return iBase.CreateTime < jBase.CreateTime
}
if iBase.UpdateTime != jBase.UpdateTime {
return iBase.UpdateTime < jBase.UpdateTime
}
iID := convutil.ToString(iBase.ID)
jID := convutil.ToString(jBase.ID)
return iID < jID
}
func (s *GenericStore) List(_ context.Context, input ListInput) (*ListOutput, error) {
var ret []interface{}
// 从缓存中不断读取过滤
s.cache.Range(func(key, value interface{}) bool {
if input.Predicate != nil && !input.Predicate(value) {
return true
}
if input.Format != nil {
value = input.Format(value)
}
ret = append(ret, value)
return true
})
// should return an empty array not a null for client
if ret == nil {
ret = []interface{}{}
}
output := &ListOutput{
Rows: ret,
TotalSize: len(ret),
}
if input.Less == nil {
input.Less = defLessFunc
}
sort.Slice(output.Rows, func(i, j int) bool {
return input.Less(output.Rows[i], output.Rows[j])
})
if input.PageSize > 0 && input.PageNumber > 0 {
skipCount := (input.PageNumber - 1) * input.PageSize
if skipCount > output.TotalSize {
output.Rows = []interface{}{}
return output, nil
}
endIdx := skipCount + input.PageSize
if endIdx >= output.TotalSize {
output.Rows = ret[skipCount:]
return output, nil
}
output.Rows = ret[skipCount:endIdx]
}
return output, nil
}
func (s *GenericStore) Range(_ context.Context, f func(key string, obj interface{}) bool) {
s.cache.Range(func(key, value interface{}) bool {
return f(key.(string), value)
})
}
func (s *GenericStore) ingestValidate(obj interface{}) (err error) {
if s.opt.Validator != nil {
if err := s.opt.Validator.Validate(obj); err != nil {
logger.Error("data validate failed", "err", err, "obj", obj)
return err
}
}
if s.opt.StockCheck != nil {
s.cache.Range(func(key, value interface{}) bool {
if err = s.opt.StockCheck(obj, value); err != nil {
return false
}
return true
})
}
return err
}
func (s *GenericStore) CreateCheck(obj interface{}) ([]byte, error) {
if setter, ok := obj.(entity.GetBaseInfo); ok {
info := setter.GetBaseInfo()
info.Creating()
}
if err := s.ingestValidate(obj); err != nil {
return nil, err
}
key := s.opt.KeyFunc(obj)
if key == "" {
return nil, fmt.Errorf("key is required")
}
_, ok := s.cache.Load(key)
if ok {
logger.Warn("key is conflicted", "key", key)
return nil, fmt.Errorf("key: %s is conflicted", key)
}
bytes, err := json.Marshal(obj)
if err != nil {
logger.Error("json marshal failed", "err", err)
return nil, fmt.Errorf("json marshal failed: %s", err)
}
return bytes, nil
}
func (s *GenericStore) Create(ctx context.Context, obj interface{}) (interface{}, error) {
if setter, ok := obj.(entity.GetBaseInfo); ok {
info := setter.GetBaseInfo()
info.Creating()
}
bytes, err := s.CreateCheck(obj)
if err != nil {
return nil, err
}
if err := s.Stg.Create(ctx, s.GetObjStorageKey(obj), string(bytes)); err != nil {
return nil, err
}
return obj, nil
}
func (s *GenericStore) Update(ctx context.Context, obj interface{}, createIfNotExist bool) (interface{}, error) {
if err := s.ingestValidate(obj); err != nil {
return nil, err
}
key := s.opt.KeyFunc(obj)
if key == "" {
return nil, fmt.Errorf("key is required")
}
storedObj, ok := s.cache.Load(key)
if !ok {
if createIfNotExist {
return s.Create(ctx, obj)
}
logger.Warn("key is not found", "err", key)
return nil, fmt.Errorf("key: %s is not found", key)
}
if setter, ok := obj.(entity.GetBaseInfo); ok {
storedGetter := storedObj.(entity.GetBaseInfo)
storedInfo := storedGetter.GetBaseInfo()
info := setter.GetBaseInfo()
info.Updating(storedInfo)
}
bs, err := json.Marshal(obj)
if err != nil {
logger.Error("json marshal failed", "err", err)
return nil, fmt.Errorf("json marshal failed: %s", err)
}
if err := s.Stg.Update(ctx, s.GetObjStorageKey(obj), string(bs)); err != nil {
return nil, err
}
return obj, nil
}
func (s *GenericStore) BatchDelete(ctx context.Context, keys []string) error {
var storageKeys []string
for i := range keys {
storageKeys = append(storageKeys, s.GetStorageKey(keys[i]))
}
return s.Stg.BatchDelete(ctx, storageKeys)
}
func (s *GenericStore) Close() error {
s.cancel()
return nil
}
func (s *GenericStore) StringToObjPtr(str, key string) (interface{}, error) {
objPtr := reflect.New(s.opt.ObjType)
ret := objPtr.Interface()
err := json.Unmarshal([]byte(str), ret)
if err != nil {
logger.Error("json unmarshal failed", "err", err)
return nil, fmt.Errorf("json unmarshal failed\n\tRelated Key:\t\t%s\n\tError Description:\t%s", key, err)
}
if setter, ok := ret.(entity.GetBaseInfo); ok {
info := setter.GetBaseInfo()
info.KeyCompat(key)
}
return ret, nil
}
func (s *GenericStore) GetObjStorageKey(obj interface{}) string {
return s.GetStorageKey(s.opt.KeyFunc(obj))
}
func (s *GenericStore) GetStorageKey(key string) string {
return fmt.Sprintf("%s/%s", s.opt.BasePath, key)
}