-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbackends.go
449 lines (376 loc) · 11.6 KB
/
backends.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
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
package backends
import (
"context"
"fmt"
"log"
"strconv"
"strings"
"sync"
"github.com/Microkubes/microservice-tools/config"
)
// Filter is a map property => value/pattern to match the DB entries against.
type Filter map[string]interface{}
// NewFilter is a builder method to create new filter.
// All filter methods are chained, so you can convinientry do somethind like this:
// filter := backends.NewFilter().MatchPattern("name", "John%").Match("role", "user")
func NewFilter() Filter {
return Filter{}
}
// Match sets an exact match for a given property.
// For example:
// filter := backends.NewFilter().Match("id", "0001")
// would match the entry with ID equals to "0001".
func (f Filter) Match(property string, value interface{}) Filter {
f[property] = value
return f
}
// MatchPattern sets a pattern match for the given property.
// The match works similar to how 'LIKE' pattern matching works
// in SQL:
// "%a" matches "ba", "tada" but not "ab"
// "a%" matches "ab" but not "ba"
// "%ab%" matches anything that contains "ab"
// "ab" does an exact match to "ab"
func (f Filter) MatchPattern(property, value string) Filter {
f[property] = map[string]string{
"$pattern": value,
}
return f
}
// Set is an alias for Filter.Match - do an exact match on the given property.
func (f Filter) Set(property string, value interface{}) Filter {
f[property] = value
return f
}
// Repository defines the interface for accessing the data
type Repository interface {
GetOne(filter Filter, result interface{}) (interface{}, error)
GetAll(filter Filter, resultsTypeHint interface{}, order string, sorting string, limit int, offset int) (interface{}, error)
Save(object interface{}, filter Filter) (interface{}, error)
DeleteOne(filter Filter) error
DeleteAll(filter Filter) error
}
type Index interface {
GetName() string
GetFields() []string
Unique() bool
}
// RepositoryDefinition defines interface for accessing collection props
type RepositoryDefinition interface {
GetName() string
GetIndexes() []Index
EnableTTL() bool
GetTTL() int
GetTTLAttribute() string
GetHashKey() string
GetRangeKey() string
GetHashKeyType() string
GetRangeKeyType() string
GetReadCapacity() int64
GetWriteCapacity() int64
GetGSI() map[string]interface{}
IsCustomID() bool
}
// Backend defines interface for defining the repository
type Backend interface {
DefineRepository(name string, def RepositoryDefinition) (Repository, error)
GetRepository(name string) (Repository, error)
GetConfig() *config.DBInfo
GetFromContext(key string) interface{}
SetInContext(key string, value interface{})
Shutdown()
}
// BackendManager defines interface for managing the backend
type BackendManager interface {
GetBackend(backendType string) (Backend, error)
SupportBackend(backendType string, builder BackendBuilder, properties map[string]interface{})
GetSupportedBackends() []string
GetRequiredBackendProperties(backendType string) (map[string]interface{}, error)
}
// BackendBuilder builds the backend
type BackendBuilder func(conf *config.DBInfo, manager BackendManager) (Backend, error)
// RepoBuilder builds the repo (collection or table)
type RepoBuilder func(def RepositoryDefinition, backend Backend) (Repository, error)
// RepositoryDefinitionMap is the configuration map
type RepositoryDefinitionMap map[string]interface{}
// BackendCleanup is the collection/table clean up func
type BackendCleanup func()
// DefaultBackendManager represents the backend store
type DefaultBackendManager struct {
backendBuilders map[string]BackendBuilder
backends map[string]Backend
backendProps map[string]interface{}
dbConfig map[string]*config.DBInfo
mutex *sync.Mutex
}
// RepositoriesBackend represents the repository store
type RepositoriesBackend struct {
repositories map[string]Repository
repositoryBuilder RepoBuilder
mutex *sync.Mutex
DBInfo *config.DBInfo
ctx context.Context
cleanupFn BackendCleanup
}
// GetIndexes returns the indexes for colletion or table
func (m RepositoryDefinitionMap) GetIndexes() []Index {
indexes := []Index{}
if idxArr, ok := m["indexes"]; ok {
if idxArrayOfIndex, ok := idxArr.([]Index); ok {
return idxArrayOfIndex
}
log.Fatal("The indexes must be defined as []Index")
}
return indexes
}
// IsCustomID returns if the ID (property "id") has custom handling.
// If customId is false, then the hadling of the ID is left to the
// underlying backend.
func (m RepositoryDefinitionMap) IsCustomID() bool {
if customID, ok := m["customId"]; ok {
return customID.(bool)
}
return false
}
// GetName returns the collection/table name
func (m RepositoryDefinitionMap) GetName() string {
if name, ok := m["name"]; ok {
return name.(string)
}
return ""
}
// EnableTTL set the TTL for collection or table
func (m RepositoryDefinitionMap) EnableTTL() bool {
if ttlEnabled, ok := m["enableTtl"]; ok {
return ttlEnabled.(bool)
}
return false
}
// GetTTL returns the time in seconds for TTL
func (m RepositoryDefinitionMap) GetTTL() int {
if ttl, ok := m["ttl"]; ok {
return ttl.(int)
}
return 0
}
// GetTTLAttribute returns the TTL attribute
func (m RepositoryDefinitionMap) GetTTLAttribute() string {
if ttlField, ok := m["ttlAttribute"]; ok {
return ttlField.(string)
}
return ""
}
// GetHashKey return the hashKey for dynamoDB
func (m RepositoryDefinitionMap) GetHashKey() string {
if hashKey, ok := m["hashKey"]; ok {
return hashKey.(string)
}
return ""
}
// GetRangeKey return the rangeKey for dynamoDB
func (m RepositoryDefinitionMap) GetRangeKey() string {
if rangeKey, ok := m["rangeKey"]; ok {
return rangeKey.(string)
}
return ""
}
// GetReadCapacity return the read capacity for dynamoDB table
func (m RepositoryDefinitionMap) GetReadCapacity() int64 {
if readCapacity, ok := m["readCapacity"]; ok {
return asInt64(readCapacity)
}
return 0
}
// GetWriteCapacity return the write capacity for dynamoDB table
func (m RepositoryDefinitionMap) GetWriteCapacity() int64 {
if writeCapacity, ok := m["writeCapacity"]; ok {
return asInt64(writeCapacity)
}
return 0
}
// GetGSI returns global secondary indexes
func (m RepositoryDefinitionMap) GetGSI() map[string]interface{} {
if gsi, ok := m["GSI"]; ok {
return gsi.(map[string]interface{})
}
return nil
}
// GetHashKeyType return the type of the hash key - AWS DynamoDB specific. Type may be "S", "N", "SS", "SN".
func (m RepositoryDefinitionMap) GetHashKeyType() string {
if hashKeyType, ok := m["hashKeyType"]; ok {
return hashKeyType.(string)
}
return ""
}
// GetRangeKeyType return the type of the range key - AWS DynamoDB specific. Type may be "S", "N", "SS", "SN".
func (m RepositoryDefinitionMap) GetRangeKeyType() string {
if rangeKeyType, ok := m["rangeKeyType"]; ok {
return rangeKeyType.(string)
}
return ""
}
// DefineRepository defines the repository (collection/table)
func (m *RepositoriesBackend) DefineRepository(name string, def RepositoryDefinition) (Repository, error) {
if repository, ok := m.repositories[name]; ok {
return repository, nil
}
m.mutex.Lock()
defer m.mutex.Unlock()
repository, err := m.repositoryBuilder(def, m)
if err != nil {
return nil, err
}
m.repositories[name] = repository
return repository, nil
}
// GetRepository return the repository (collection/table)
func (m *RepositoriesBackend) GetRepository(name string) (Repository, error) {
if repo, ok := m.repositories[name]; ok {
return repo, nil
}
return nil, fmt.Errorf("unknown repo")
}
// GetConfig return the config
func (m *RepositoriesBackend) GetConfig() *config.DBInfo {
return m.DBInfo
}
// GetFromContext returns from config
func (m *RepositoriesBackend) GetFromContext(key string) interface{} {
return m.ctx.Value(key)
}
// SetInContext sets in context
func (m *RepositoriesBackend) SetInContext(key string, value interface{}) {
m.mutex.Lock()
defer m.mutex.Unlock()
m.ctx = context.WithValue(m.ctx, key, value)
}
// Shutdown close the session
func (m *RepositoriesBackend) Shutdown() {
if m.cleanupFn != nil {
m.cleanupFn()
}
}
// GetBackend returns the RepositoryBackend
func (m *DefaultBackendManager) GetBackend(backendType string) (Backend, error) {
if backend, ok := m.backends[backendType]; ok {
return backend, nil
}
m.mutex.Lock()
defer m.mutex.Unlock()
backend, err := m.buildBackend(backendType)
if err != nil {
return nil, err
}
return backend, nil
}
// SupportBackend register the DB builder function and required props for the DB
func (m *DefaultBackendManager) SupportBackend(backendType string, builder BackendBuilder, properties map[string]interface{}) {
m.backendBuilders[backendType] = builder
m.backendProps[backendType] = properties
}
// GetSupportedBackends returns the supported backedns
func (m *DefaultBackendManager) GetSupportedBackends() []string {
supported := []string{}
for backendType, _ := range m.backendBuilders {
supported = append(supported, backendType)
}
return supported
}
// GetRequiredBackendProperties returns the required props for the selected backend
func (m *DefaultBackendManager) GetRequiredBackendProperties(backendType string) (map[string]interface{}, error) {
if props, ok := m.backendProps[backendType]; ok {
return props.(map[string]interface{}), nil
}
return nil, fmt.Errorf("backend not supported")
}
// buildBackend builds new backend
func (m *DefaultBackendManager) buildBackend(backendType string) (Backend, error) {
if backendBuilder, ok := m.backendBuilders[backendType]; ok {
dbInfo, ok := m.dbConfig[backendType]
if !ok || dbInfo == nil {
return nil, fmt.Errorf("backend not configured")
}
backend, err := backendBuilder(dbInfo, m)
if err != nil {
return nil, err
}
m.backends[backendType] = backend
return backend, nil
}
return nil, fmt.Errorf("backend not supported")
}
// NewRepositoriesBackend sets new RepositoriesBackend
func NewRepositoriesBackend(ctx context.Context, dbInfo *config.DBInfo, repoBuilder RepoBuilder, cleanup BackendCleanup) Backend {
return &RepositoriesBackend{
DBInfo: dbInfo,
mutex: &sync.Mutex{},
repositories: map[string]Repository{},
repositoryBuilder: repoBuilder,
ctx: ctx,
cleanupFn: cleanup,
}
}
// NewBackendManager returns new backend manager
func NewBackendManager(dbConfig map[string]*config.DBInfo) BackendManager {
return &DefaultBackendManager{
backendBuilders: map[string]BackendBuilder{},
backendProps: map[string]interface{}{},
backends: map[string]Backend{},
dbConfig: dbConfig,
mutex: &sync.Mutex{},
}
}
// Index interface implementation
type fieldsIndex struct {
fields []string
name string
unique bool
}
func (f *fieldsIndex) GetName() string {
return f.name
}
func (f *fieldsIndex) GetFields() []string {
return f.fields
}
func (f *fieldsIndex) Unique() bool {
return f.unique
}
func NewIndex(name string, unique bool, fields ...string) Index {
if fields == nil {
fields = []string{}
}
return &fieldsIndex{
name: name,
fields: fields,
unique: unique,
}
}
func indexNameFromFields(fields ...string) string {
name := ""
if fields != nil {
name = strings.Join(fields, "_")
}
return name
}
func NewUniqueIndex(fields ...string) Index {
return NewIndex(indexNameFromFields(fields...), true, fields...)
}
func NewNonUniqueIndex(fields ...string) Index {
return NewIndex(indexNameFromFields(fields...), false, fields...)
}
func asInt64(v interface{}) int64 {
if i, ok := v.(int64); ok {
return i
}
if i, ok := v.(int); ok {
return int64(i)
}
if i, ok := v.(string); ok {
i64, err := strconv.ParseInt(i, 10, 64)
if err != nil {
panic(err)
}
return i64
}
panic(fmt.Errorf("%v cannot be transformed to int64", v))
}