forked from open-horizon/edge-sync-service
-
Notifications
You must be signed in to change notification settings - Fork 0
/
inMemoryStorage.go
1167 lines (979 loc) · 38.8 KB
/
inMemoryStorage.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
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
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package storage
import (
"bytes"
"encoding/binary"
"fmt"
"io"
"io/ioutil"
"os"
"strings"
"time"
"github.com/open-horizon/edge-sync-service/common"
"github.com/open-horizon/edge-sync-service/core/dataURI"
"github.com/open-horizon/edge-utilities/logger"
"github.com/open-horizon/edge-utilities/logger/log"
)
// InMemoryStorage is an in-memory store
type InMemoryStorage struct {
lockChannel chan int
objects map[string]inMemoryObject
notifications map[string]common.Notification
webhooks map[string][]string
timebase int64
}
type inMemoryObject struct {
meta common.MetaData
data []byte
tmpData []byte
status string
remainingConsumers int
remainingReceivers int
consumedTimestamp time.Time
removedDestinationPolicyServices []common.ServiceID
}
// Init initializes the InMemory store
func (store *InMemoryStorage) Init() common.SyncServiceError {
store.lockChannel = make(chan int, 1)
store.lockChannel <- 1
store.objects = make(map[string]inMemoryObject)
store.notifications = make(map[string]common.Notification)
store.webhooks = make(map[string][]string)
currentTime := time.Now().UnixNano()
store.timebase = currentTime
currentTimeInSeconds := currentTime / 1e9
persistedTimeBase := currentTimeInSeconds
dir := common.Configuration.PersistenceRootPath + "/sync/local/"
err := os.MkdirAll(dir, 0750)
if err == nil {
path := dir + "persisted-data"
persisted := store.readPersistedTimebase(path)
if persisted != 0 && currentTimeInSeconds <= persisted {
persistedTimeBase++
store.timebase = persistedTimeBase * 1e9
}
if err := store.writePersistedTimebase(path, persistedTimeBase); err != nil {
if log.IsLogging(logger.ERROR) {
log.Fatal("Failed to persist timebase. Error: %s\n", err.Error())
}
}
}
common.HealthStatus.ReconnectedToDatabase()
return nil
}
// Stop stops the InMemory store
func (store *InMemoryStorage) Stop() {
// The InMemory store doesn't have to do anything at shutdown time.
// This empty implementation insures that it implements the storage interface
}
// PerformMaintenance performs store's maintenance
func (store *InMemoryStorage) PerformMaintenance() {
}
// Cleanup erase the on disk Bolt database only for ESS and test
func (store *InMemoryStorage) Cleanup(isTest bool) common.SyncServiceError {
return nil
}
// StoreObject stores an object
func (store *InMemoryStorage) StoreObject(metaData common.MetaData, data []byte, status string) ([]common.StoreDestinationStatus, common.SyncServiceError) {
store.lock()
defer store.unLock()
id := getObjectCollectionID(metaData)
// If the object was receieved from a service (status NotReadyToSend/ReadyToSend), i.e. this node is the origin of the object,
// set instance id. If the object was received from the other side, this node is the receiver of the object:
// keep the instance id of the meta data.
if status == common.NotReadyToSend || status == common.ReadyToSend {
newID := store.getInstanceID()
metaData.InstanceID = newID
if data != nil && !metaData.NoData && !metaData.MetaOnly {
metaData.DataID = newID
}
}
if metaData.MetaOnly {
if object, ok := store.objects[id]; ok {
if object.status == common.ConsumedByDest {
// On ESS we remove the data of consumed objects, therefore we can't accept "meta only" updates
return nil, &Error{"Can't update only the meta data of consumed object"}
}
metaData.DataID = object.meta.DataID // Keep the previous data id
object.meta = metaData
object.status = status
object.remainingConsumers = metaData.ExpectedConsumers
object.remainingReceivers = metaData.ExpectedConsumers
if metaData.NoData {
object.data = nil
}
store.objects[id] = object
return nil, nil
}
// If not found, insert the object
}
if metaData.NoData {
data = nil
}
store.objects[id] = inMemoryObject{meta: metaData, data: data, status: status,
remainingConsumers: metaData.ExpectedConsumers, remainingReceivers: metaData.ExpectedConsumers}
return nil, nil
}
// StoreObjectData stores an object's data
// Return true if the object was found and updated
// Return false and no error, if the object doesn't exist
func (store *InMemoryStorage) StoreObjectData(orgID string, objectType string, objectID string, dataReader io.Reader) (bool, common.SyncServiceError) {
var data []byte
var err error
if data, err = ioutil.ReadAll(dataReader); err != nil {
return false, err
}
store.lock()
defer store.unLock()
id := createObjectCollectionID(orgID, objectType, objectID)
if object, ok := store.objects[id]; ok {
if object.status == common.NotReadyToSend {
object.status = common.ReadyToSend
}
if object.status == common.NotReadyToSend || object.status == common.ReadyToSend {
newID := store.getInstanceID()
object.meta.InstanceID = newID
object.meta.DataID = newID
}
object.data = data
object.meta.ObjectSize = int64(len(object.data))
store.objects[id] = object
return true, nil
}
return false, nil
}
func (store *InMemoryStorage) StoreObjectTempData(orgID string, objectType string, objectID string, dataReader io.Reader) (bool, common.SyncServiceError) {
var data []byte
var err error
if data, err = ioutil.ReadAll(dataReader); err != nil {
return false, err
}
store.lock()
defer store.unLock()
id := createObjectCollectionID(orgID, objectType, objectID)
if object, ok := store.objects[id]; ok {
object.tmpData = data
store.objects[id] = object
return true, nil
}
return false, nil
}
func (store *InMemoryStorage) RemoveObjectTempData(orgID string, objectType string, objectID string) common.SyncServiceError {
store.lock()
defer store.unLock()
id := createObjectCollectionID(orgID, objectType, objectID)
if object, ok := store.objects[id]; ok {
object.tmpData = nil
store.objects[id] = object
return nil
}
return notFound
}
func (store *InMemoryStorage) RetrieveTempObjectData(orgID string, objectType string, objectID string) (io.Reader, common.SyncServiceError) {
store.lock()
defer store.unLock()
id := createObjectCollectionID(orgID, objectType, objectID)
if object, ok := store.objects[id]; ok {
if object.tmpData != nil && len(object.tmpData) > 0 {
return bytes.NewReader(object.tmpData), nil
}
return nil, nil
}
return nil, nil
}
// AppendObjectData appends a chunk of data to the object's data
func (store *InMemoryStorage) AppendObjectData(orgID string, objectType string, objectID string, dataReader io.Reader, dataLength uint32,
offset int64, total int64, isFirstChunk bool, isLastChunk bool) common.SyncServiceError {
store.lock()
defer store.unLock()
id := createObjectCollectionID(orgID, objectType, objectID)
object, ok := store.objects[id]
if ok {
var data []byte
if dataLength == 0 {
dt, err := ioutil.ReadAll(dataReader)
if err != nil {
return &Error{"Failed to read object data. Error: " + err.Error()}
}
data = dt
dataLength = uint32(len(data))
}
if total < offset+int64(dataLength) {
total = offset + int64(dataLength)
}
if isFirstChunk {
object.data = make([]byte, total)
} else {
object.data = ensureArrayCapacity(object.data, total)
}
if data != nil {
copy(object.data[offset:], data)
} else {
count, err := dataReader.Read(object.data[offset:])
if err != nil {
return &Error{"Failed to read object data. Error: " + err.Error()}
}
if count != int(dataLength) {
return &Error{fmt.Sprintf("Read %d bytes for the object data, instead of %d", count, dataLength)}
}
}
store.objects[id] = object
return nil
}
return notFound
}
// UpdateObjectStatus updates an object's status
func (store *InMemoryStorage) UpdateObjectStatus(orgID string, objectType string, objectID string, status string) common.SyncServiceError {
store.lock()
defer store.unLock()
id := createObjectCollectionID(orgID, objectType, objectID)
if object, ok := store.objects[id]; ok {
object.status = status
if status == common.ConsumedByDest {
object.consumedTimestamp = time.Now()
}
store.objects[id] = object
return nil
}
return &NotFound{"Object not found"}
}
// UpdateObjectSourceDataURI updates object's source data URI
func (store *InMemoryStorage) UpdateObjectSourceDataURI(orgID string, objectType string, objectID string, sourceDataURI string) common.SyncServiceError {
store.lock()
defer store.unLock()
id := createObjectCollectionID(orgID, objectType, objectID)
if object, ok := store.objects[id]; ok {
object.meta.SourceDataURI = sourceDataURI
store.objects[id] = object
return nil
}
return notFound
}
// RetrieveObjectStatus finds the object and returns its status
func (store *InMemoryStorage) RetrieveObjectStatus(orgID string, objectType string, objectID string) (string, common.SyncServiceError) {
store.lock()
defer store.unLock()
id := createObjectCollectionID(orgID, objectType, objectID)
if object, ok := store.objects[id]; ok {
return object.status, nil
}
return "", nil
}
// RetrieveObjectRemainingConsumers finds the object and returns the number of remaining consumers
// that haven't consumed the object yet
func (store *InMemoryStorage) RetrieveObjectRemainingConsumers(orgID string, objectType string, objectID string) (int, common.SyncServiceError) {
store.lock()
defer store.unLock()
id := createObjectCollectionID(orgID, objectType, objectID)
if object, ok := store.objects[id]; ok {
return object.remainingConsumers, nil
}
return 0, notFound
}
// ResetObjectRemainingConsumers sets the remaining consumers count to the original ExpectedConsumers value
func (store *InMemoryStorage) ResetObjectRemainingConsumers(orgID string, objectType string, objectID string) common.SyncServiceError {
store.lock()
defer store.unLock()
id := createObjectCollectionID(orgID, objectType, objectID)
if object, ok := store.objects[id]; ok {
object.remainingConsumers = object.meta.ExpectedConsumers
store.objects[id] = object
return nil
}
return notFound
}
// DecrementAndReturnRemainingConsumers decrements the number of remaining consumers of the object
func (store *InMemoryStorage) DecrementAndReturnRemainingConsumers(orgID string, objectType string, objectID string) (int,
common.SyncServiceError) {
store.lock()
defer store.unLock()
id := createObjectCollectionID(orgID, objectType, objectID)
if object, ok := store.objects[id]; ok {
object.remainingConsumers--
store.objects[id] = object
return object.remainingConsumers, nil
}
return 0, notFound
}
// DecrementAndReturnRemainingReceivers decrements the number of remaining receivers of the object
func (store *InMemoryStorage) DecrementAndReturnRemainingReceivers(orgID string, objectType string, objectID string) (int,
common.SyncServiceError) {
store.lock()
defer store.unLock()
id := createObjectCollectionID(orgID, objectType, objectID)
if object, ok := store.objects[id]; ok {
object.remainingReceivers--
store.objects[id] = object
return object.remainingReceivers, nil
}
return 0, notFound
}
// RetrieveUpdatedObjects returns the list of all the edge updated objects that are not marked as consumed or received
// If received is true, return objects marked as received
func (store *InMemoryStorage) RetrieveUpdatedObjects(orgID string, objectType string, received bool) ([]common.MetaData, common.SyncServiceError) {
store.lock()
defer store.unLock()
result := make([]common.MetaData, 0)
for _, obj := range store.objects {
if objectType == obj.meta.ObjectType &&
(obj.status == common.CompletelyReceived || obj.status == common.ObjDeleted ||
(obj.status == common.ObjReceived && received)) {
result = append(result, obj.meta)
}
}
return result, nil
}
// RetrieveObjectsWithDestinationPolicy returns the list of all the objects that have a Destination Policy
// If received is true, return objects marked as policy received
func (store *InMemoryStorage) RetrieveObjectsWithDestinationPolicy(orgID string, received bool) ([]common.ObjectDestinationPolicy, common.SyncServiceError) {
return nil, nil
}
// RetrieveObjectsWithDestinationPolicyByService returns the list of all the object Policies for a particular service
func (store *InMemoryStorage) RetrieveObjectsWithDestinationPolicyByService(orgID, serviceOrgID, serviceName string) ([]common.ObjectDestinationPolicy, common.SyncServiceError) {
return nil, nil
}
// RetrieveObjectsWithDestinationPolicyUpdatedSince returns the list of all the objects that have a Destination Policy updated since the specified time
func (store *InMemoryStorage) RetrieveObjectsWithDestinationPolicyUpdatedSince(orgID string, since int64) ([]common.ObjectDestinationPolicy, common.SyncServiceError) {
return nil, nil
}
// RetrieveObjectsWithFilters returns the list of all othe objects that meet the given conditions
func (store *InMemoryStorage) RetrieveObjectsWithFilters(orgID string, destinationPolicy *bool, dpServiceOrgID string, dpServiceName string, dpPropertyName string, since int64, objectType string, objectID string, destinationType string, destinationID string, noData *bool, expirationTimeBefore string, deleted *bool) ([]common.MetaData, common.SyncServiceError) {
return nil, nil
}
// RetrieveAllObjects returns the list of all the objects of the specified type
func (store *InMemoryStorage) RetrieveAllObjects(orgID string, objectType string) ([]common.ObjectDestinationPolicy, common.SyncServiceError) {
store.lock()
defer store.unLock()
result := make([]common.ObjectDestinationPolicy, 0)
for _, obj := range store.objects {
if objectType == obj.meta.ObjectType {
object := common.ObjectDestinationPolicy{
OrgID: orgID, ObjectType: objectType, ObjectID: obj.meta.ObjectID,
DestinationPolicy: obj.meta.DestinationPolicy}
result = append(result, object)
}
}
return result, nil
}
// RetrieveObjects returns the list of all the objects that need to be sent to the destination
func (store *InMemoryStorage) RetrieveObjects(orgID string, destType string, destID string, resend int) ([]common.MetaData, common.SyncServiceError) {
store.lock()
defer store.unLock()
result := make([]common.MetaData, 0)
for _, obj := range store.objects {
if !obj.meta.Inactive && obj.status == common.ReadyToSend &&
(obj.meta.DestType == "" || obj.meta.DestType == destType || destType == "") &&
(obj.meta.DestID == "" || obj.meta.DestID == destID || destID == "") {
result = append(result, obj.meta)
}
}
return result, nil
}
// RetrieveConsumedObjects returns all the consumed objects originated from this node
func (store *InMemoryStorage) RetrieveConsumedObjects() ([]common.ConsumedObject, common.SyncServiceError) {
store.lock()
defer store.unLock()
result := make([]common.ConsumedObject, 0)
for _, obj := range store.objects {
if obj.status == common.ConsumedByDest {
result = append(result, common.ConsumedObject{MetaData: obj.meta, Timestamp: obj.consumedTimestamp})
}
}
return result, nil
}
// RetrieveObject returns the object meta data with the specified parameters
func (store *InMemoryStorage) RetrieveObject(orgID string, objectType string, objectID string) (*common.MetaData, common.SyncServiceError) {
store.lock()
defer store.unLock()
id := createObjectCollectionID(orgID, objectType, objectID)
if object, ok := store.objects[id]; ok {
return &object.meta, nil
}
return nil, nil
}
// RetrieveObjectAndStatus returns the object meta data and status with the specified parameters
func (store *InMemoryStorage) RetrieveObjectAndStatus(orgID string, objectType string, objectID string) (*common.MetaData, string, common.SyncServiceError) {
store.lock()
defer store.unLock()
id := createObjectCollectionID(orgID, objectType, objectID)
if object, ok := store.objects[id]; ok {
return &object.meta, object.status, nil
}
return nil, "", nil
}
// RetrieveObjectData returns the object data with the specified parameters
func (store *InMemoryStorage) RetrieveObjectData(orgID string, objectType string, objectID string) (io.Reader, common.SyncServiceError) {
store.lock()
defer store.unLock()
id := createObjectCollectionID(orgID, objectType, objectID)
if object, ok := store.objects[id]; ok {
if object.data != nil && len(object.data) > 0 {
return bytes.NewReader(object.data), nil
}
return nil, nil
}
return nil, nil
}
// CloseDataReader closes the data reader if necessary
func (store *InMemoryStorage) CloseDataReader(dataReader io.Reader) common.SyncServiceError {
switch v := dataReader.(type) {
case *os.File:
return v.Close()
}
return nil
}
// ReadObjectData returns the object data with the specified parameters
func (store *InMemoryStorage) ReadObjectData(orgID string, objectType string, objectID string, size int, offset int64) ([]byte, bool, int, common.SyncServiceError) {
store.lock()
defer store.unLock()
id := createObjectCollectionID(orgID, objectType, objectID)
if object, ok := store.objects[id]; ok {
lod := int64(len(object.data))
if lod <= offset {
return make([]byte, 0), true, 0, nil
}
s := int64(size)
eof := false
if s >= lod-offset {
s = lod - offset
eof = true
}
b := make([]byte, s)
copy(b, object.data[offset:])
return b, eof, int(s), nil
}
return nil, true, 0, &common.NotFound{}
}
// MarkObjectDeleted marks the object as deleted
func (store *InMemoryStorage) MarkObjectDeleted(orgID string, objectType string, objectID string) common.SyncServiceError {
store.lock()
defer store.unLock()
id := createObjectCollectionID(orgID, objectType, objectID)
if object, ok := store.objects[id]; ok {
object.meta.Deleted = true
object.status = common.ObjDeleted
store.objects[id] = object
return nil
}
return notFound
}
// MarkDestinationPolicyReceived marks an object's destination policy as having been received
func (store *InMemoryStorage) MarkDestinationPolicyReceived(orgID string, objectType string, objectID string) common.SyncServiceError {
return nil
}
// ActivateObject marks object as active
func (store *InMemoryStorage) ActivateObject(orgID string, objectType string, objectID string) common.SyncServiceError {
store.lock()
defer store.unLock()
id := createObjectCollectionID(orgID, objectType, objectID)
if object, ok := store.objects[id]; ok {
object.meta.Inactive = false
store.objects[id] = object
return nil
}
return notFound
}
// GetObjectsToActivate returns inactive objects that are ready to be activated
func (store *InMemoryStorage) GetObjectsToActivate() ([]common.MetaData, common.SyncServiceError) {
store.lock()
defer store.unLock()
currentTime := time.Now().UTC().Format(time.RFC3339)
result := make([]common.MetaData, 0)
for _, obj := range store.objects {
if (obj.status == common.NotReadyToSend || obj.status == common.ReadyToSend) &&
obj.meta.Inactive && obj.meta.ActivationTime != "" && obj.meta.ActivationTime <= currentTime {
result = append(result, obj.meta)
}
}
return result, nil
}
// DeleteStoredObject deletes the object
func (store *InMemoryStorage) DeleteStoredObject(orgID string, objectType string, objectID string) common.SyncServiceError {
store.lock()
defer store.unLock()
id := createObjectCollectionID(orgID, objectType, objectID)
delete(store.objects, id)
return nil
}
// DeleteStoredData deletes the object's data
func (store *InMemoryStorage) DeleteStoredData(orgID string, objectType string, objectID string) common.SyncServiceError {
store.lock()
defer store.unLock()
id := createObjectCollectionID(orgID, objectType, objectID)
if object, ok := store.objects[id]; ok {
object.data = nil
store.objects[id] = object
return nil
}
return notFound
}
// CleanObjects removes the objects received from the other side.
// For persistant storage only partially recieved objects are removed.
func (store *InMemoryStorage) CleanObjects() common.SyncServiceError {
store.lock()
defer store.unLock()
for _, obj := range store.objects {
if obj.status == common.PartiallyReceived || obj.status == common.CompletelyReceived {
id := createObjectCollectionID(obj.meta.DestOrgID, obj.meta.ObjectType, obj.meta.ObjectID)
delete(store.objects, id)
}
}
return nil
}
// GetObjectDestinations gets destinations that the object has to be sent to
func (store *InMemoryStorage) GetObjectDestinations(metaData common.MetaData) ([]common.Destination, common.SyncServiceError) {
return []common.Destination{common.Destination{DestOrgID: metaData.DestOrgID, DestType: common.Configuration.DestinationType,
DestID: common.Configuration.DestinationID, Communication: common.Configuration.CommunicationProtocol}}, nil
}
// UpdateObjectDeliveryStatus changes the object's delivery status for the destination
// Returns true if the status is Deleted and all the destinations are in status Deleted
func (store *InMemoryStorage) UpdateObjectDeliveryStatus(status string, message string, orgID string, objectType string, objectID string,
destType string, destID string) (bool, common.SyncServiceError) {
return true, nil
}
// UpdateObjectDelivering marks the object as being delivered to all its destinations
func (store *InMemoryStorage) UpdateObjectDelivering(orgID string, objectType string, objectID string) common.SyncServiceError {
return nil
}
// GetObjectDestinationsList gets destinations that the object has to be sent to and their status
func (store *InMemoryStorage) GetObjectDestinationsList(orgID string, objectType string,
objectID string) ([]common.StoreDestinationStatus, common.SyncServiceError) {
return nil, nil
}
// UpdateObjectDestinations updates object's destinations
// Returns the meta data, object's status, an array of deleted destinations, and an array of added destinations
func (store *InMemoryStorage) UpdateObjectDestinations(orgID string, objectType string, objectID string, destinationsList []string) (*common.MetaData, string,
[]common.StoreDestinationStatus, []common.StoreDestinationStatus, common.SyncServiceError) {
return nil, "", nil, nil, nil
}
// AddObjectdestinations adds the destinations to object's destination list
// Returns the metadata, object's status, an array of added destinations after removing the overlapped destinations
func (store *InMemoryStorage) AddObjectDestinations(orgID string, objectType string, objectID string, destinationsList []string) (*common.MetaData, string,
[]common.StoreDestinationStatus, common.SyncServiceError) {
return nil, "", nil, nil
}
// DeleteObjectdestinations deletes the destinations from object's destination list
// Returns the metadata, objects' status, an array of destinations that removed from the current destination list
func (store *InMemoryStorage) DeleteObjectDestinations(orgID string, objectType string, objectID string, destinationsList []string) (*common.MetaData, string,
[]common.StoreDestinationStatus, common.SyncServiceError) {
return nil, "", nil, nil
}
// GetNumberOfStoredObjects returns the number of objects received from the application that are
// currently stored in this node's storage
func (store *InMemoryStorage) GetNumberOfStoredObjects() (uint32, common.SyncServiceError) {
var count uint32
for _, object := range store.objects {
if object.status == common.ReadyToSend || object.status == common.NotReadyToSend {
count++
}
}
return count, nil
}
// AddWebhook stores a webhook for an object type
func (store *InMemoryStorage) AddWebhook(orgID string, objectType string, url string) common.SyncServiceError {
store.lock()
defer store.unLock()
var hooks []string
if h := store.webhooks[objectType]; h != nil {
hooks = h
} else {
hooks = make([]string, 0)
}
// Don't add the webhook if it already is in the list
for _, hook := range hooks {
if url == hook {
return nil
}
}
hooks = append(hooks, url)
store.webhooks[objectType] = hooks
return nil
}
// DeleteWebhook deletes a webhook for an object type
func (store *InMemoryStorage) DeleteWebhook(orgID string, objectType string, url string) common.SyncServiceError {
store.lock()
defer store.unLock()
if hooks := store.webhooks[objectType]; hooks != nil {
for i, hook := range hooks {
if strings.EqualFold(hook, url) {
hooks[i] = hooks[len(hooks)-1]
store.webhooks[objectType] = hooks[:len(hooks)-1]
return nil
}
}
}
return nil
}
// RetrieveWebhooks gets the webhooks for the object type
func (store *InMemoryStorage) RetrieveWebhooks(orgID string, objectType string) ([]string, common.SyncServiceError) {
store.lock()
defer store.unLock()
if hooks := store.webhooks[objectType]; hooks != nil {
if len(hooks) == 0 {
return nil, &NotFound{"No webhooks"}
}
return hooks, nil
}
return nil, &NotFound{"No webhooks"}
}
// RetrieveDestinations returns all the destinations with the provided orgID and destType
func (store *InMemoryStorage) RetrieveDestinations(orgID string, destType string) ([]common.Destination, common.SyncServiceError) {
return nil, nil
}
// DestinationExists returns true if the destination exists, and false otherwise
func (store *InMemoryStorage) DestinationExists(orgID string, destType string, destID string) (bool, common.SyncServiceError) {
return true, nil
}
// StoreDestination stores a destination
func (store *InMemoryStorage) StoreDestination(destination common.Destination) common.SyncServiceError {
return nil
}
// DeleteDestination deletes a destination
func (store *InMemoryStorage) DeleteDestination(orgID string, destType string, destID string) common.SyncServiceError {
return nil
}
// UpdateDestinationLastPingTime updates the last ping time for the destination
func (store *InMemoryStorage) UpdateDestinationLastPingTime(destination common.Destination) common.SyncServiceError {
return nil
}
// RemoveInactiveDestinations removes destinations that haven't sent ping since the provided timestamp
func (store *InMemoryStorage) RemoveInactiveDestinations(lastTimestamp time.Time) {}
// GetNumberOfDestinations returns the number of currently registered ESS nodes (for CSS)
func (store *InMemoryStorage) GetNumberOfDestinations() (uint32, common.SyncServiceError) {
return 0, nil
}
// RetrieveDestination retrieves a destination
func (store *InMemoryStorage) RetrieveDestination(orgID string, destType string, destID string) (*common.Destination, common.SyncServiceError) {
return &common.Destination{DestOrgID: orgID, DestType: destType, DestID: destID,
Communication: common.Configuration.CommunicationProtocol}, nil
}
// RetrieveDestinationProtocol retrieves communication protocol for the destination
func (store *InMemoryStorage) RetrieveDestinationProtocol(orgID string, destType string, destID string) (string, common.SyncServiceError) {
return common.Configuration.CommunicationProtocol, nil
}
// GetObjectsForDestination retrieves objects that are in use on a given node
func (store *InMemoryStorage) GetObjectsForDestination(orgID string, destType string, destID string) ([]common.ObjectStatus, common.SyncServiceError) {
return nil, nil
}
// RetrieveAllObjectsAndUpdateDestinationListForDestination retrieves objects that are in use on a given node and returns the list of metadata
func (store *InMemoryStorage) RetrieveAllObjectsAndUpdateDestinationListForDestination(orgID string, destType string, destID string) ([]common.MetaData, common.SyncServiceError) {
return nil, nil
}
// RetrieveObjectAndRemovedDestinationPolicyServices returns the object metadata and removedDestinationPolicyServices with the specified param, only for ESS
func (store *InMemoryStorage) RetrieveObjectAndRemovedDestinationPolicyServices(orgID string, objectType string, objectID string) (*common.MetaData, []common.ServiceID, common.SyncServiceError) {
store.lock()
defer store.unLock()
id := createObjectCollectionID(orgID, objectType, objectID)
if object, ok := store.objects[id]; ok {
return &object.meta, object.removedDestinationPolicyServices, nil
}
emptyList := []common.ServiceID{}
return nil, emptyList, nil
}
// UpdateRemovedDestinationPolicyServices update the removedDestinationPolicyServices, only for ESS
func (store *InMemoryStorage) UpdateRemovedDestinationPolicyServices(orgID string, objectType string, objectID string, destinationPolicyServices []common.ServiceID) common.SyncServiceError {
store.lock()
defer store.unLock()
id := createObjectCollectionID(orgID, objectType, objectID)
if object, ok := store.objects[id]; ok {
object.removedDestinationPolicyServices = destinationPolicyServices
store.objects[id] = object
return nil
}
return notFound
}
// UpdateNotificationRecord updates/adds a notification record to the object
func (store *InMemoryStorage) UpdateNotificationRecord(notification common.Notification) common.SyncServiceError {
store.lock()
defer store.unLock()
notification.ResendTime = time.Now().Unix() + int64(common.Configuration.ResendInterval*6)
id := getNotificationCollectionID(¬ification)
store.notifications[id] = notification
return nil
}
// UpdateNotificationResendTime sets the resend time of the notification to common.Configuration.ResendInterval*6
func (store *InMemoryStorage) UpdateNotificationResendTime(notification common.Notification) common.SyncServiceError {
store.lock()
defer store.unLock()
id := getNotificationCollectionID(¬ification)
if notification, ok := store.notifications[id]; ok {
resendTime := time.Now().Unix() + int64(common.Configuration.ResendInterval*6)
notification.ResendTime = resendTime
store.notifications[id] = notification
return nil
}
return notFound
}
// RetrieveNotificationRecord retrieves notification
func (store *InMemoryStorage) RetrieveNotificationRecord(orgID string, objectType string, objectID string, destType string,
destID string) (*common.Notification, common.SyncServiceError) {
store.lock()
defer store.unLock()
id := createNotificationCollectionID(orgID, objectType, objectID, destType, destID)
if notification, ok := store.notifications[id]; ok {
return ¬ification, nil
}
return nil, nil
}
// DeleteNotificationRecords deletes notification records to an object
func (store *InMemoryStorage) DeleteNotificationRecords(orgID string, objectType string, objectID string,
destType string, destID string) common.SyncServiceError {
store.lock()
defer store.unLock()
if objectType != "" && objectID != "" {
if destType != "" && destID != "" {
id := createNotificationCollectionID(orgID, objectType, objectID, destType, destID)
delete(store.notifications, id)
} else {
for id, notification := range store.notifications {
if notification.ObjectType == objectType && notification.ObjectID == objectID {
delete(store.notifications, id)
}
}
}
} else {
for id, notification := range store.notifications {
if (notification.DestType == destType || destType == "") && (notification.DestID == destID || destID == "") {
delete(store.notifications, id)
}
}
}
return nil
}
// RetrieveNotifications returns the list of all the notifications that need to be resent to the destination
func (store *InMemoryStorage) RetrieveNotifications(orgID string, destType string, destID string, retrieveReceived bool) ([]common.Notification,
common.SyncServiceError) {
store.lock()
defer store.unLock()
result := make([]common.Notification, 0)
if destID != "" && destType != "" {
for _, notification := range store.notifications {
if notification.DestType == destType && notification.DestID == destID && resendNotification(notification, false) {
result = append(result, notification)
}
}
} else {
currentTime := time.Now().Unix()
for _, notification := range store.notifications {
if resendNotification(notification, false) {
if notification.ResendTime <= currentTime || notification.Status == common.Getdata {
result = append(result, notification)
}
}
}
}
return result, nil
}
// RetrievePendingNotifications returns the list of pending notifications that are waiting to be sent to the destination
func (store *InMemoryStorage) RetrievePendingNotifications(orgID string, destType string, destID string) ([]common.Notification, common.SyncServiceError) {
return nil, nil
}
// InsertInitialLeader inserts the initial leader entry
func (store *InMemoryStorage) InsertInitialLeader(leaderID string) (bool, common.SyncServiceError) {
return true, nil
}
// LeaderPeriodicUpdate does the periodic update of the leader entry by the leader
func (store *InMemoryStorage) LeaderPeriodicUpdate(leaderID string) (bool, common.SyncServiceError) {
return false, nil
}
// RetrieveLeader retrieves the Heartbeat timeout and Last heartbeat time stamp from the leader document
func (store *InMemoryStorage) RetrieveLeader() (string, int32, time.Time, int64, common.SyncServiceError) {
return "", 0, time.Now(), 0, nil
}
// UpdateLeader updates the leader entry for a leadership takeover
func (store *InMemoryStorage) UpdateLeader(leaderID string, version int64) (bool, common.SyncServiceError) {
return false, nil
}
// ResignLeadership causes this sync service to give up the Leadership
func (store *InMemoryStorage) ResignLeadership(leaderID string) common.SyncServiceError {
return nil
}
// RetrieveTimeOnServer retrieves the current time on the database server
func (store *InMemoryStorage) RetrieveTimeOnServer() (time.Time, error) {
return time.Now(), nil
}
// StoreOrgToMessagingGroup inserts organization to messaging groups table
func (store *InMemoryStorage) StoreOrgToMessagingGroup(orgID string, messagingGroup string) common.SyncServiceError {
return nil
}
// DeleteOrgToMessagingGroup deletes organization from messaging groups table
func (store *InMemoryStorage) DeleteOrgToMessagingGroup(orgID string) common.SyncServiceError {
return nil
}
// RetrieveMessagingGroup retrieves messaging group for organization
func (store *InMemoryStorage) RetrieveMessagingGroup(orgID string) (string, common.SyncServiceError) {
return "", nil
}
// RetrieveUpdatedMessagingGroups retrieves messaging groups that were updated after the specified time
func (store *InMemoryStorage) RetrieveUpdatedMessagingGroups(time time.Time) ([]common.MessagingGroup, common.SyncServiceError) {
return nil, nil
}
// DeleteOrganization cleans up the storage from all the records associated with the organization
func (store *InMemoryStorage) DeleteOrganization(orgID string) common.SyncServiceError {
return nil
}
// IsConnected returns false if the storage cannont be reached, and true otherwise
func (store *InMemoryStorage) IsConnected() bool {
return true
}
// StoreOrganization stores organization information
// Returns the stored record timestamp for multiple CSS updates
func (store *InMemoryStorage) StoreOrganization(org common.Organization) (time.Time, common.SyncServiceError) {
return time.Now(), nil
}
// RetrieveOrganizationInfo retrieves organization information
func (store *InMemoryStorage) RetrieveOrganizationInfo(orgID string) (*common.StoredOrganization, common.SyncServiceError) {
return nil, nil
}
// DeleteOrganizationInfo deletes organization information
func (store *InMemoryStorage) DeleteOrganizationInfo(orgID string) common.SyncServiceError {
return nil
}
// RetrieveOrganizations retrieves stored organizations' info
func (store *InMemoryStorage) RetrieveOrganizations() ([]common.StoredOrganization, common.SyncServiceError) {
return nil, nil
}
// RetrieveUpdatedOrganizations retrieves organizations that were updated after the specified time
func (store *InMemoryStorage) RetrieveUpdatedOrganizations(time time.Time) ([]common.StoredOrganization, common.SyncServiceError) {
return nil, nil
}
// AddUsersToACL adds users to an ACL
func (store *InMemoryStorage) AddUsersToACL(aclType string, orgID string, key string, usernames []common.ACLentry) common.SyncServiceError {