-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdataStore.go
More file actions
551 lines (479 loc) · 17 KB
/
Copy pathdataStore.go
File metadata and controls
551 lines (479 loc) · 17 KB
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
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
package fscache
import (
"context"
"encoding/json"
"fmt"
"reflect"
"strings"
"time"
"go.mongodb.org/mongo-driver/mongo"
"gorm.io/gorm"
)
const (
DBTypePostgres DBType = "postgres"
DBTypeMysql DBType = "mysql"
)
type (
DBType string
Namespace struct {
dataStore *DataStore
namespace string
}
Find struct{}
First struct{}
ConnectSQLDB struct {
namespace *Namespace
DB *gorm.DB
}
ConnectMongoDB struct {
namespace *Namespace
DB *mongo.Database
}
)
// Namespace creates or retrieves a namespace within the DataStore.
// If a schema is provided, it will be associated with the namespace.
// If no schema is provided, the namespace will be initialized with a nil schema.
// The function returns a Namespace struct containing the logger, data, indexes, schemas, and mutex from the DataStore.
//
// Parameters:
// - name: The name of the namespace to create or retrieve.
// - schemas: Optional variadic parameter to provide a schema for the namespace.
//
// Returns:
// - Namespace: A struct containing the logger, data, indexes, schemas, and mutex from the DataStore.
func (ds *DataStore) Namespace(name any, schema ...Schema) Namespace {
ds.mu.Lock()
defer ds.mu.Unlock()
t := reflect.TypeOf(name)
if reflect.ValueOf(name).IsZero() && name == nil {
ds.logger.Error().Msg("Namespace cannot be empty.")
panic("Error ::: Namespace cannot be empty.")
}
if t.Kind() != reflect.Struct && t.Kind() != reflect.String {
ds.logger.Error().Msg("Namespace must either be a [string] or a [struct]")
panic("Error ::: Namespace must either be a [string] or a [struct]")
}
var nameSpace string
if t.Kind() == reflect.Struct {
nameSpace = strings.ToLower(t.Name())
} else {
nameSpace = strings.ToLower(name.(string))
}
if len(nameSpace) > 0 && string(nameSpace[len(nameSpace)-1]) != "s" {
nameSpace = fmt.Sprintf("%ss", nameSpace)
}
// If no schema is passed, initialize it as nil or empty
var schemas Schema
if len(schema) > 0 {
schemas = schema[0] // Use the first schema if passed
} else {
schemas = nil // No schema provided
}
ds.schemas[nameSpace] = schemas
ds.indexes[nameSpace] = make(map[string]map[any][]int)
return Namespace{
dataStore: ds,
namespace: nameSpace,
}
}
// toSnakeCase converts a given CamelCase string to snake_case.
// It inserts an underscore before each uppercase letter (except the first one)
// and converts all characters to lowercase.
//
// Parameters:
//
// s - the CamelCase string to be converted.
//
// Returns:
//
// A snake_case representation of the input string.
func toSnakeCase(s string) string {
var result []rune
for i, r := range s {
if i > 0 && r >= 'A' && r <= 'Z' {
result = append(result, '_', r)
} else {
result = append(result, r)
}
}
return strings.ToLower(string(result))
}
// Create adds a new entry to the namespace's data store. It first locks the data store
// to ensure thread safety. If a schema is defined for the namespace, it enforces the schema
// by checking the types of the provided values. If the types do not match the schema, it returns an error.
// After validation, it appends the entry to the data store and updates the indexes for quick lookups.
//
// Parameters:
//
// v - A map containing the key-value pairs to be added to the data store.
//
// Returns:
//
// error - An error if the schema validation fails, otherwise nil.
func (ns *Namespace) Create(v map[string]any) error {
ns.dataStore.mu.Lock()
defer ns.dataStore.mu.Unlock()
normalized := make(map[string]any)
for key, val := range v {
normalized[toSnakeCase(key)] = val
}
// Schema enforcement
if schema, ok := ns.dataStore.schemas[ns.namespace]; ok {
for key, val := range normalized {
if expectedType, exists := schema[key]; exists {
if reflect.TypeOf(val).String() != expectedType {
ns.dataStore.logger.Error().Msgf("Error ::: invalid type for field %s: expected %s, got %s", key, expectedType, reflect.TypeOf(val).String())
return fmt.Errorf("invalid type for field %s: expected %s, got %s", key, expectedType, reflect.TypeOf(val).String())
}
}
}
}
// Add a field of isSynced to each record inserted
normalized["is_synced"] = false
ns.dataStore.data[ns.namespace] = append(ns.dataStore.data[ns.namespace], normalized)
// Update indexes
for key, value := range normalized {
if _, exists := ns.dataStore.indexes[ns.namespace][key]; !exists {
ns.dataStore.indexes[ns.namespace][key] = make(map[any][]int)
}
ns.dataStore.indexes[ns.namespace][key][value] = append(ns.dataStore.indexes[ns.namespace][key][value], len(ns.dataStore.data[ns.namespace])-1)
}
return nil
}
// Query retrieves documents from the namespace's data store that match the provided filters.
// It returns a slice of maps, where each map represents a document, and an error if any occurs.
//
// Parameters:
//
// filters - A map where the key is the field name and the value is the value to filter by.
//
// Returns:
//
// A slice of maps, where each map represents a document that matches the filters.
// An error if any occurs during the query process.
func (ns *Namespace) Query(filters map[string]any) ([]map[string]any, error) {
var result []map[string]any
if len(filters) == 0 {
return ns.dataStore.data[ns.namespace], nil
}
docIndexes := make(map[int]bool)
for key, value := range filters {
if idx, exists := ns.dataStore.indexes[ns.namespace][toSnakeCase(key)]; exists {
if docIdxs, exists := idx[value]; exists {
for _, idx := range docIdxs {
docIndexes[idx] = true
}
}
}
}
for idx := range docIndexes {
result = append(result, ns.dataStore.data[ns.namespace][idx])
}
return result, nil
}
// Find searches for records in the namespace that match the given filters.
// If one result is found, an error is returned suggesting to use First() for one result.
// The results are decoded into the provided variable.
//
// Parameters:
// - filters: A map of filter criteria to apply to the query.
// - v: A variable to store the decoded results.
//
// Returns:
// - error: An error if the query fails, if only one result is found, or if decoding fails.
func (ns *Namespace) Find(filters map[string]any, v any) error {
result, err := ns.Query(filters)
if err != nil {
return err
}
if err := ns.decodeMany(result, &v); err != nil {
return err
}
return nil
}
// First retrieves the first result matching the provided filters and decodes it into the provided variable.
// If more than one result is found, an error is returned suggesting to use Find() for multiple results.
// Only use First() if the expected result is a single object.
// Parameters:
//
// filters - a map containing the filters to apply to the query.
// v - a variable to decode the result into.
//
// Returns:
//
// error - an error if the query fails, more than one result is found, or decoding fails.
func (ns *Namespace) First(filters map[string]any, v any) error {
result, err := ns.Query(filters)
if err != nil {
return err
}
if len(result) > 1 {
return fmt.Errorf("find() expects one result, but got %d. Use Find() instead for multiple data", len(result))
}
if err := ns.decodeOne(result[0], &v); err != nil {
return err
}
return nil
}
// decodeMany takes a slice of maps with string keys and values of any type,
// marshals it into JSON, and then unmarshals it into the provided variable v.
// It returns an error if either the marshaling or unmarshalling process fails.
//
// Params:
// - params: A slice of maps where each map has string keys and values of any type.
// - v: A pointer to a variable where the unmarshaled JSON will be stored.
//
// Returns:
// - error: An error if marshaling or unmarshalling fails, otherwise nil.
func (ns *Namespace) decodeMany(params []map[string]any, v any) error {
jsobByte, err := json.Marshal(¶ms)
if err != nil {
return err
}
if err := json.Unmarshal(jsobByte, &v); err != nil {
return err
}
return nil
}
// decodeOne decodes a map of parameters into a given value.
//
// Parameters:
// - params: A map containing the parameters to be decoded.
// - v: A pointer to the value where the decoded parameters will be stored.
//
// Returns:
// - An error if the encoding or decoding process fails, otherwise nil.
func (ns *Namespace) decodeOne(params map[string]any, v any) error {
jsobByte, err := json.Marshal(¶ms)
if err != nil {
return err
}
if err := json.Unmarshal(jsobByte, &v); err != nil {
return err
}
return nil
}
// Update modifies documents in the data store that match the given filters with the provided new data.
// It acquires a lock on the data store to ensure thread safety, queries for matching documents,
// updates each matching document with the new data, and rebuilds indexes if necessary.
//
// Parameters:
// - filters: A map of key-value pairs used to filter documents that need to be updated.
// - newData: A map of key-value pairs representing the new data to be applied to the matching documents.
//
// Returns:
// - error: An error if the query fails or any other issue occurs during the update process.
func (ns *Namespace) Update(filters map[string]any, newData map[string]any) error {
ns.dataStore.mu.Lock()
defer ns.dataStore.mu.Unlock()
matchingDocs, err := ns.Query(filters)
if err != nil {
return err
}
for _, doc := range matchingDocs {
for key, value := range newData {
doc[toSnakeCase(key)] = value
}
}
// Rebuild indexes if necessary
ns.rebuildIndexes()
return nil
}
// cs.namespace.dataStore.indexes[namespace]["isSynced"][false] = append(cs.namespace.dataStore.indexes[namespace]["isSynced"][true], index)
// Delete removes documents from the namespace's data store that match the given filters.
// It first queries the data store to find matching documents, then removes them from the slice,
// and finally rebuilds the indexes.
//
// Parameters:
//
// filters - a map of field names to values that documents must match to be deleted.
//
// Returns:
//
// error - an error if the query fails, otherwise nil.
func (ns *Namespace) Delete(filters map[string]any) error {
ns.dataStore.mu.Lock()
defer ns.dataStore.mu.Unlock()
// Perform query first to find matching documents
matchingDocs, err := ns.Query(filters)
if err != nil {
return err
}
// Remove matching documents from the slice
for _, doc := range matchingDocs {
for i, storedDoc := range ns.dataStore.data[ns.namespace] {
if reflect.DeepEqual(storedDoc, doc) {
// Delete the document from the slice
ns.dataStore.data[ns.namespace] = append(ns.dataStore.data[ns.namespace][:i], ns.dataStore.data[ns.namespace][i+1:]...)
break
}
}
}
// Rebuild indexes after deletion
ns.rebuildIndexes()
return nil
}
// rebuildIndexes rebuilds the indexes for the namespace.
// It resets the current index and iterates over all documents
// in the namespace to recreate the index based on the document
// keys and values. Each value is mapped to a list of document
// indices where it appears.
func (ns *Namespace) rebuildIndexes() {
// Reset the namespace index
ns.dataStore.indexes[ns.namespace] = make(map[string]map[any][]int)
// Iterate over all documents in the namespace
for i, doc := range ns.dataStore.data[ns.namespace] {
for key, value := range doc {
if _, exists := ns.dataStore.indexes[ns.namespace][key]; !exists {
ns.dataStore.indexes[ns.namespace][key] = make(map[any][]int)
}
ns.dataStore.indexes[ns.namespace][key][value] = append(ns.dataStore.indexes[ns.namespace][key][value], i)
}
}
}
// ListNamespaces returns a list of all namespace names present in the DataStore.
// It acquires a read lock to ensure thread-safe access to the underlying data.
func (ds *DataStore) ListNamespaces() []string {
var namespaces []string
for namespace := range ds.schemas {
namespaces = append(namespaces, namespace)
}
return namespaces
}
// ConnectSQLDB establishes a connection to the SQL database using the provided gorm.DB instance.
// It returns a pointer to a ConnectSQLDB struct that contains the database connection and the namespace.
//
// Parameters:
//
// - db: A pointer to a gorm.DB instance representing the database connection.
//
// Returns:
//
// - A pointer to a ConnectSQLDB struct containing the database connection and namespace.
func (ns *Namespace) ConnectSQLDB(db *gorm.DB) *ConnectSQLDB {
// if db == nil {
// return nil, fmt.Errorf("invalid database connection")
// }
return &ConnectSQLDB{DB: db, namespace: ns}
}
// Sync periodically synchronizes the in-memory data store with the SQL database.
// It runs in a separate goroutine and uses a ticker to trigger the synchronization
// process at the specified interval.
//
// Parameters:
// - interval: The duration between each synchronization attempt.
//
// The synchronization process involves the following steps:
// 1. Lock the data store to ensure thread safety.
// 2. Iterate over each namespace and its associated records.
// 3. For each document, check if it is already synced by looking at the "is_synced" field.
// 4. If the document is not synced, create a copy of the document excluding the "is_synced" field.
// 5. Attempt to sync the document to the SQL database.
// 6. If the sync is successful, mark the document as synced and log the success.
// 7. If an error occurs during the sync, log the error and continue with the next document.
// 8. Unlock the data store after processing all documents.
//
// Note: The synchronization process continues indefinitely until the program terminates.
func (cs *ConnectSQLDB) Sync(interval time.Duration) {
go func() {
ticker := time.NewTicker(interval)
defer ticker.Stop()
for range ticker.C {
cs.namespace.dataStore.mu.Lock()
for namespace, records := range cs.namespace.dataStore.data {
for index, doc := range records {
if isSynced, ok := doc["is_synced"].(bool); ok && isSynced {
continue
}
docCopy := make(map[string]any)
for key, value := range doc {
if key != "is_synced" {
docCopy[key] = value
}
}
if err := cs.DB.Table(namespace).Create(docCopy).Error; err != nil {
cs.namespace.dataStore.logger.Err(err).Msgf(
"Error syncing document at index %d in namespace %s: %v",
index, namespace, err,
)
// Log the error and continue with the next document
continue
}
doc["is_synced"] = true
cs.namespace.dataStore.data[namespace][index] = doc
cs.namespace.dataStore.logger.Info().Msgf(
"Synced document at index %d in namespace %s",
index, namespace,
)
}
}
cs.namespace.dataStore.mu.Unlock()
}
}()
}
// ConnectMongoDB initializes a new ConnectMongoDB instance with the provided MongoDB database
// and namespace.
//
// Parameters:
// - db: A pointer to a mongo.Database instance representing the MongoDB database connection.
// - ns: A pointer to a Namespace instance representing the namespace context.
//
// Returns:
// - A pointer to a ConnectMongoDB instance initialized with the provided database and namespace.
func (ns *Namespace) ConnectMongoDB(db *mongo.Database) *ConnectMongoDB {
return &ConnectMongoDB{DB: db, namespace: ns}
}
// Sync periodically synchronizes the in-memory data store with the MongoDB database.
// It runs in a separate goroutine and uses a ticker to trigger the synchronization
// process at the specified interval.
//
// The synchronization process involves the following steps:
// 1. Lock the data store to ensure thread safety.
// 2. Iterate over each namespace and its associated records in the data store.
// 3. For each document, check if it has already been synced by examining the "is_synced" field.
// 4. If the document is not synced, create a copy of the document excluding the "is_synced" field.
// 5. Insert the copied document into the corresponding MongoDB collection.
// 6. If the insertion is successful, mark the document as synced by setting the "is_synced" field to true.
// 7. Log the result of the synchronization process.
//
// Parameters:
//
// - ctx: The context to control the synchronization process.
// - interval: The duration between each synchronization attempt.
func (cm *ConnectMongoDB) Sync(ctx context.Context, interval time.Duration) {
go func() {
ticker := time.NewTicker(interval)
defer ticker.Stop()
for range ticker.C {
cm.namespace.dataStore.mu.Lock()
for namespace, records := range cm.namespace.dataStore.data {
for index, doc := range records {
if isSynced, ok := doc["is_synced"].(bool); ok && isSynced {
continue
}
docCopy := make(map[string]any)
for key, value := range doc {
if key != "is_synced" {
docCopy[key] = value
}
}
if _, err := cm.DB.Collection(namespace).InsertOne(ctx, docCopy); err != nil {
cm.namespace.dataStore.logger.Err(err).Msgf(
"Error syncing document at index %d in namespace %s: %v",
index, namespace, err,
)
// Log the error and continue with the next document
continue
}
doc["is_synced"] = true
cm.namespace.dataStore.data[namespace][index] = doc
cm.namespace.dataStore.logger.Info().Msgf(
"Synced document at index %d in namespace %s",
index, namespace,
)
}
}
cm.namespace.dataStore.mu.Unlock()
}
}()
}