-
Notifications
You must be signed in to change notification settings - Fork 49
/
trans.go
1159 lines (861 loc) · 26.3 KB
/
trans.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
/*
* EliasDB
*
* Copyright 2016 Matthias Ladkau. All rights reserved.
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/
package graph
import (
"fmt"
"strings"
"sync"
"github.com/krotik/common/errorutil"
"github.com/krotik/eliasdb/graph/data"
"github.com/krotik/eliasdb/graph/util"
)
/*
Trans is a transaction object which should be used to group node and edge operations.
*/
type Trans interface {
/*
ID returns a unique transaction ID.
*/
ID() string
/*
String returns a string representation of this transatction.
*/
String() string
/*
Counts returns the transaction size in terms of objects. Returned values
are nodes to store, edges to store, nodes to remove and edges to remove.
*/
Counts() (int, int, int, int)
/*
IsEmpty returns if this transaction is empty.
*/
IsEmpty() bool
/*
Commit writes the transaction to the graph database. An automatic rollback is done if
any non-fatal error occurs. Failed transactions cannot be committed again.
Serious write errors which may corrupt the database will cause a panic.
*/
Commit() error
/*
StoreNode stores a single node in a partition of the graph. This function will
overwrites any existing node.
*/
StoreNode(part string, node data.Node) error
/*
UpdateNode updates a single node in a partition of the graph. This function will
only update the given values of the node.
*/
UpdateNode(part string, node data.Node) error
/*
RemoveNode removes a single node from a partition of the graph.
*/
RemoveNode(part string, nkey string, nkind string) error
/*
StoreEdge stores a single edge in a partition of the graph. This function will
overwrites any existing edge.
*/
StoreEdge(part string, edge data.Edge) error
/*
RemoveEdge removes a single edge from a partition of the graph.
*/
RemoveEdge(part string, ekey string, ekind string) error
}
/*
NewGraphTrans creates a new graph transaction. This object is not thread safe
and should only be used for non-concurrent use cases; use NewConcurrentGraphTrans
for concurrent use cases.
*/
func NewGraphTrans(gm *Manager) Trans {
return newInternalGraphTrans(gm)
}
/*
NewConcurrentGraphTrans creates a new thread-safe graph transaction.
*/
func NewConcurrentGraphTrans(gm *Manager) Trans {
return &concurrentTrans{NewGraphTrans(gm), &sync.RWMutex{}}
}
/*
NewRollingTrans wraps an existing transaction into a rolling transaction.
Rolling transactions can be used for VERY large datasets and will commit
themselves after n operations. Rolling transactions are always thread-safe.
*/
func NewRollingTrans(t Trans, n int, gm *Manager, newTrans func(*Manager) Trans) Trans {
idCounterLock.Lock()
defer idCounterLock.Unlock()
idCounter++
// Smallest commit threshold is 1
if n < 1 {
n = 1
}
return &rollingTrans{
id: fmt.Sprint(idCounter),
gm: gm,
currentTrans: t,
newTransFunc: newTrans,
transErrors: errorutil.NewCompositeError(),
opThreshold: n,
opCount: 0,
inFlightCount: 0,
wg: &sync.WaitGroup{},
countNodeIns: 0,
countNodeRem: 0,
countEdgeIns: 0,
countEdgeRem: 0,
transLock: &sync.RWMutex{},
}
}
/*
newInternalGraphTrans is used for internal transactions. The returned object
contains extra fields which are only for internal use.
*/
func newInternalGraphTrans(gm *Manager) *baseTrans {
idCounterLock.Lock()
defer idCounterLock.Unlock()
idCounter++
return &baseTrans{fmt.Sprint(idCounter), gm, false, make(map[string]data.Node), make(map[string]data.Node),
make(map[string]data.Edge), make(map[string]data.Edge)}
}
/*
idCounter is a simple counter for ids
*/
var idCounter uint64
var idCounterLock = &sync.Mutex{}
/*
baseTrans is the main data structure for a graph transaction
*/
type baseTrans struct {
id string // Unique transaction ID - not used by EliasDB
gm *Manager // Graph manager which created this transaction
subtrans bool // Flag if the transaction is a subtransaction
storeNodes map[string]data.Node // Nodes which should be stored
removeNodes map[string]data.Node // Nodes which should be removed
storeEdges map[string]data.Edge // Edges which should be stored
removeEdges map[string]data.Edge // Edges which should be removed
}
/*
ID returns a unique transaction ID.
*/
func (gt *baseTrans) ID() string {
return gt.id
}
/*
IsEmpty returns if this transaction is empty.
*/
func (gt *baseTrans) IsEmpty() bool {
sn, se, rn, re := gt.Counts()
return sn == 0 && se == 0 && rn == 0 && re == 0
}
/*
Counts returns the transaction size in terms of objects. Returned values
are nodes to store, edges to store, nodes to remove and edges to remove.
*/
func (gt *baseTrans) Counts() (int, int, int, int) {
return len(gt.storeNodes), len(gt.storeEdges), len(gt.removeNodes), len(gt.removeEdges)
}
/*
String returns a string representation of this transatction.
*/
func (gt *baseTrans) String() string {
sn, se, rn, re := gt.Counts()
return fmt.Sprintf("Transaction %v - Nodes: I:%v R:%v - Edges: I:%v R:%v",
gt.id, sn, rn, se, re)
}
/*
Commit writes the transaction to the graph database. An automatic rollback is done if
any non-fatal error occurs. Failed transactions cannot be committed again.
Serious write errors which may corrupt the database will cause a panic.
*/
func (gt *baseTrans) Commit() error {
// Take writer lock if we are not in a subtransaction
if !gt.subtrans {
gt.gm.mutex.Lock()
defer gt.gm.mutex.Unlock()
}
// Return if there is nothing to do
if gt.IsEmpty() {
return nil
}
doRollback := func(nodePartsAndKinds map[string]string,
edgePartsAndKinds map[string]string) {
// Rollback main database
gt.gm.gs.RollbackMain()
// Rollback node storages
for kkey := range nodePartsAndKinds {
partAndKind := strings.Split(kkey, "#")
gt.gm.rollbackNodeIndex(partAndKind[0], partAndKind[1])
gt.gm.rollbackNodeStorage(partAndKind[0], partAndKind[1])
}
gt.storeNodes = make(map[string]data.Node)
gt.removeNodes = make(map[string]data.Node)
// Rollback edge storages
if edgePartsAndKinds != nil {
for kkey := range edgePartsAndKinds {
partAndKind := strings.Split(kkey, "#")
gt.gm.rollbackEdgeIndex(partAndKind[0], partAndKind[1])
gt.gm.rollbackEdgeStorage(partAndKind[0], partAndKind[1])
}
}
gt.storeEdges = make(map[string]data.Edge)
gt.removeEdges = make(map[string]data.Edge)
}
// Write nodes and edges until everything has been written
nodePartsAndKinds := make(map[string]string)
edgePartsAndKinds := make(map[string]string)
for !gt.IsEmpty() {
// Write the nodes first
if err := gt.commitNodes(nodePartsAndKinds, edgePartsAndKinds); err != nil {
doRollback(nodePartsAndKinds, nil)
return err
}
// After the nodes write the edges
if err := gt.commitEdges(nodePartsAndKinds, edgePartsAndKinds); err != nil {
doRollback(nodePartsAndKinds, edgePartsAndKinds)
return err
}
}
// Flush changes - panic instead of error reporting since the database
// may be inconsistent
panicIfError := func(err error) {
if err != nil {
panic("Fatal GraphError:" + err.Error())
}
}
panicIfError(gt.gm.gs.FlushMain())
for kkey := range nodePartsAndKinds {
partAndKind := strings.Split(kkey, "#")
panicIfError(gt.gm.flushNodeIndex(partAndKind[0], partAndKind[1]))
panicIfError(gt.gm.flushNodeStorage(partAndKind[0], partAndKind[1]))
}
for kkey := range edgePartsAndKinds {
partAndKind := strings.Split(kkey, "#")
panicIfError(gt.gm.flushEdgeIndex(partAndKind[0], partAndKind[1]))
panicIfError(gt.gm.flushEdgeStorage(partAndKind[0], partAndKind[1]))
}
return nil
}
/*
commitNodes tries to commit all transaction nodes.
*/
func (gt *baseTrans) commitNodes(nodePartsAndKinds map[string]string, edgePartsAndKinds map[string]string) error {
// First insert nodes
for tkey, node := range gt.storeNodes {
// Get partition and kind
partAndKind := strings.Split(tkey, "#")
nodePartsAndKinds[partAndKind[0]+"#"+partAndKind[1]] = ""
part := partAndKind[0]
// Get the HTrees which stores the node index and node
iht, err := gt.gm.getNodeIndexHTree(part, node.Kind(), true)
if err != nil {
return err
}
attht, valht, err := gt.gm.getNodeStorageHTree(part, node.Kind(), true)
if err != nil {
return err
}
// Write the node to the datastore
oldnode, err := gt.gm.writeNode(node, false, attht, valht, nodeAttributeFilter)
if err != nil {
return err
}
// Increase node count if the node was inserted and write the changes
// to the index.
if oldnode == nil {
currentCount := gt.gm.NodeCount(node.Kind())
gt.gm.writeNodeCount(node.Kind(), currentCount+1, false)
if iht != nil {
err := util.NewIndexManager(iht).Index(node.Key(), node.IndexMap())
if err != nil {
// The node was written at this point and the model is
// consistent only the index is missing entries
return err
}
}
} else if iht != nil {
err := util.NewIndexManager(iht).Reindex(node.Key(), node.IndexMap(),
oldnode.IndexMap())
if err != nil {
// The node was written at this point and the model is
// consistent only the index is missing entries
return err
}
}
// Execute rules
var event int
if oldnode == nil {
event = EventNodeCreated
} else {
event = EventNodeUpdated
}
if err := gt.gm.gr.graphEvent(gt, event, part, node, oldnode); err != nil {
return err
}
delete(gt.storeNodes, tkey)
}
// Then remove nodes
for tkey, node := range gt.removeNodes {
// Get partition and kind
partAndKind := strings.Split(tkey, "#")
nodePartsAndKinds[partAndKind[0]+"#"+partAndKind[1]] = ""
part := partAndKind[0]
// Get the HTree which stores the node index and node kind
iht, err := gt.gm.getNodeIndexHTree(part, node.Kind(), false)
if err != nil {
return err
}
attTree, valTree, err := gt.gm.getNodeStorageHTree(part, node.Kind(), false)
if err != nil {
return err
}
if attTree == nil || valTree == nil {
// Kind does not exist - continue
delete(gt.removeNodes, tkey)
continue
}
// Delete the node from the datastore
oldnode, err := gt.gm.deleteNode(node.Key(), node.Kind(), attTree, valTree)
if err != nil {
return err
}
// Update the index
if oldnode != nil {
if iht != nil {
err := util.NewIndexManager(iht).Deindex(node.Key(), oldnode.IndexMap())
if err != nil {
return err
}
}
// Decrease the node count
currentCount := gt.gm.NodeCount(node.Kind())
gt.gm.writeNodeCount(node.Kind(), currentCount-1, false)
// Execute rules
if err := gt.gm.gr.graphEvent(gt, EventNodeDeleted, part, oldnode); err != nil {
return err
}
}
delete(gt.removeNodes, tkey)
}
return nil
}
/*
commitEdges tries to commit all transaction edges.
*/
func (gt *baseTrans) commitEdges(nodePartsAndKinds map[string]string, edgePartsAndKinds map[string]string) error {
// First insert edges
for tkey, edge := range gt.storeEdges {
// Get partition and kind
partAndKind := strings.Split(tkey, "#")
edgePartsAndKinds[partAndKind[0]+"#"+partAndKind[1]] = ""
nodePartsAndKinds[partAndKind[0]+"#"+edge.End1Kind()] = ""
nodePartsAndKinds[partAndKind[0]+"#"+edge.End2Kind()] = ""
part := partAndKind[0]
// Get the HTrees which stores the edges and the edge index
iht, err := gt.gm.getEdgeIndexHTree(part, edge.Kind(), true)
if err != nil {
return err
}
edgeht, err := gt.gm.getEdgeStorageHTree(part, edge.Kind(), true)
if err != nil {
return err
}
// Get the HTrees which stores the edge endpoints and make sure the endpoints
// do exist
end1nodeht, end1ht, err := gt.gm.getNodeStorageHTree(part, edge.End1Kind(), false)
if err != nil {
return err
} else if end1ht == nil {
return &util.GraphError{
Type: util.ErrInvalidData,
Detail: fmt.Sprintf("Can't store edge to non-existing node kind: %v", edge.End1Kind()),
}
} else if end1, err := end1nodeht.Get([]byte(PrefixNSAttrs + edge.End1Key())); err != nil || end1 == nil {
return &util.GraphError{
Type: util.ErrInvalidData,
Detail: fmt.Sprintf("Can't find edge endpoint: %s (%s)", edge.End1Key(), edge.End1Kind()),
}
}
end2nodeht, end2ht, err := gt.gm.getNodeStorageHTree(part, edge.End2Kind(), false)
if err != nil {
return err
} else if end2ht == nil {
return &util.GraphError{
Type: util.ErrInvalidData,
Detail: "Can't store edge to non-existing node kind: " + edge.End2Kind()}
} else if end2, err := end2nodeht.Get([]byte(PrefixNSAttrs + edge.End2Key())); err != nil || end2 == nil {
return &util.GraphError{
Type: util.ErrInvalidData,
Detail: fmt.Sprintf("Can't find edge endpoint: %s (%s)", edge.End2Key(), edge.End2Kind()),
}
}
// Write edge to the datastore
oldedge, err := gt.gm.writeEdge(edge, edgeht, end1ht, end2ht)
if err != nil {
return err
}
// Increase edge count if the edge was inserted and write the changes
// to the index.
if oldedge == nil {
// Increase edge count
currentCount := gt.gm.EdgeCount(edge.Kind())
gt.gm.writeEdgeCount(edge.Kind(), currentCount+1, false)
// Write edge data to the index
if iht != nil {
if err := util.NewIndexManager(iht).Index(edge.Key(), edge.IndexMap()); err != nil {
// The edge was written at this point and the model is
// consistent only the index is missing entries
return err
}
}
} else if iht != nil {
err := util.NewIndexManager(iht).Reindex(edge.Key(), edge.IndexMap(),
oldedge.IndexMap())
if err != nil {
// The edge was written at this point and the model is
// consistent only the index is missing entries
return err
}
}
// Execute rules
var event int
if oldedge == nil {
event = EventEdgeCreated
} else {
event = EventEdgeUpdated
}
if err := gt.gm.gr.graphEvent(gt, event, part, edge, oldedge); err != nil {
return err
}
delete(gt.storeEdges, tkey)
}
// Then remove edges
for tkey, edge := range gt.removeEdges {
// Get partition and kind
partAndKind := strings.Split(tkey, "#")
edgePartsAndKinds[partAndKind[0]+"#"+partAndKind[1]] = ""
nodePartsAndKinds[partAndKind[0]+"#"+edge.End1Kind()] = ""
nodePartsAndKinds[partAndKind[0]+"#"+edge.End2Kind()] = ""
part := partAndKind[0]
// Get the HTrees which stores the edges and the edge index
iht, err := gt.gm.getEdgeIndexHTree(part, edge.Kind(), true)
if err != nil {
return err
}
edgeht, err := gt.gm.getEdgeStorageHTree(part, edge.Kind(), true)
if err != nil {
return err
}
// Delete the node from the datastore
node, err := gt.gm.deleteNode(edge.Key(), edge.Kind(), edgeht, edgeht)
oldedge := data.NewGraphEdgeFromNode(node)
if err != nil {
return err
}
if node != nil {
// Get the HTrees which stores the edge endpoints
_, end1ht, err := gt.gm.getNodeStorageHTree(part, oldedge.End1Kind(), false)
if err != nil {
return err
}
_, end2ht, err := gt.gm.getNodeStorageHTree(part, oldedge.End2Kind(), false)
if err != nil {
return err
}
// Delete edge info from node storage
if err := gt.gm.deleteEdge(oldedge, end1ht, end2ht); err != nil {
return err
}
if iht != nil {
err := util.NewIndexManager(iht).Deindex(edge.Key(), oldedge.IndexMap())
if err != nil {
return err
}
}
// Decrease edge count
currentCount := gt.gm.EdgeCount(oldedge.Kind())
gt.gm.writeEdgeCount(oldedge.Kind(), currentCount-1, false)
// Execute rules
if err := gt.gm.gr.graphEvent(gt, EventEdgeDeleted, part, oldedge); err != nil {
return err
}
}
delete(gt.removeEdges, tkey)
}
return nil
}
/*
StoreNode stores a single node in a partition of the graph. This function will
overwrites any existing node.
*/
func (gt *baseTrans) StoreNode(part string, node data.Node) error {
if err := gt.gm.checkPartitionName(part); err != nil {
return err
} else if err := gt.gm.checkNode(node); err != nil {
return err
}
key := gt.createKey(part, node.Key(), node.Kind())
if _, ok := gt.removeNodes[key]; ok {
delete(gt.removeNodes, key)
}
gt.storeNodes[key] = node
return nil
}
/*
UpdateNode updates a single node in a partition of the graph. This function will
only update the given values of the node.
*/
func (gt *baseTrans) UpdateNode(part string, node data.Node) error {
if err := gt.gm.checkPartitionName(part); err != nil {
return err
} else if err := gt.gm.checkNode(node); err != nil {
return err
}
key := gt.createKey(part, node.Key(), node.Kind())
if _, ok := gt.removeNodes[key]; ok {
delete(gt.removeNodes, key)
} else if storeNode, ok := gt.storeNodes[key]; ok {
node = data.NodeMerge(storeNode, node)
} else {
// Check the actual database if the node exists
storeNode, err := gt.gm.FetchNode(part, node.Key(), node.Kind())
if err != nil {
return err
} else if storeNode != nil {
node = data.NodeMerge(storeNode, node)
}
}
gt.storeNodes[key] = node
return nil
}
/*
RemoveNode removes a single node from a partition of the graph.
*/
func (gt *baseTrans) RemoveNode(part string, nkey string, nkind string) error {
if err := gt.gm.checkPartitionName(part); err != nil {
return err
}
key := gt.createKey(part, nkey, nkind)
if _, ok := gt.storeNodes[key]; ok {
delete(gt.storeNodes, key)
}
node := data.NewGraphNode()
node.SetAttr(data.NodeKey, nkey)
node.SetAttr(data.NodeKind, nkind)
gt.removeNodes[key] = node
return nil
}
/*
StoreEdge stores a single edge in a partition of the graph. This function will
overwrites any existing edge.
*/
func (gt *baseTrans) StoreEdge(part string, edge data.Edge) error {
if err := gt.gm.checkPartitionName(part); err != nil {
return err
} else if err := gt.gm.checkEdge(edge); err != nil {
return err
}
key := gt.createKey(part, edge.Key(), edge.Kind())
if _, ok := gt.removeEdges[key]; ok {
delete(gt.removeEdges, key)
}
gt.storeEdges[key] = edge
return nil
}
/*
RemoveEdge removes a single edge from a partition of the graph.
*/
func (gt *baseTrans) RemoveEdge(part string, ekey string, ekind string) error {
if err := gt.gm.checkPartitionName(part); err != nil {
return err
}
key := gt.createKey(part, ekey, ekind)
if _, ok := gt.storeEdges[key]; ok {
delete(gt.storeEdges, key)
}
edge := data.NewGraphEdge()
edge.SetAttr(data.NodeKey, ekey)
edge.SetAttr(data.NodeKind, ekind)
gt.removeEdges[key] = edge
return nil
}
/*
Create a key for the transaction storage.
*/
func (gt *baseTrans) createKey(part string, key string, kind string) string {
return part + "#" + kind + "#" + key
}
/*
concurrentTrans is a lock-wrapper around baseTrans which allows concurrent use.
*/
type concurrentTrans struct {
Trans
transLock *sync.RWMutex
}
/*
ID returns a unique transaction ID.
*/
func (gt *concurrentTrans) ID() string {
gt.transLock.RLock()
defer gt.transLock.RUnlock()
return gt.Trans.ID()
}
/*
String returns a string representation of this transatction.
*/
func (gt *concurrentTrans) String() string {
gt.transLock.RLock()
defer gt.transLock.RUnlock()
return gt.Trans.String()
}
/*
Counts returns the transaction size in terms of objects. Returned values
are nodes to store, edges to store, nodes to remove and edges to remove.
*/
func (gt *concurrentTrans) Counts() (int, int, int, int) {
gt.transLock.RLock()
defer gt.transLock.RUnlock()
return gt.Trans.Counts()
}
/*
IsEmpty returns if this transaction is empty.
*/
func (gt *concurrentTrans) IsEmpty() bool {
gt.transLock.RLock()
defer gt.transLock.RUnlock()
return gt.Trans.IsEmpty()
}
/*
Commit writes the transaction to the graph database. An automatic rollback is done if
any non-fatal error occurs. Failed transactions cannot be committed again.
Serious write errors which may corrupt the database will cause a panic.
*/
func (gt *concurrentTrans) Commit() error {
gt.transLock.Lock()
defer gt.transLock.Unlock()
return gt.Trans.Commit()
}
/*
StoreNode stores a single node in a partition of the graph. This function will
overwrites any existing node.
*/
func (gt *concurrentTrans) StoreNode(part string, node data.Node) error {
gt.transLock.Lock()
defer gt.transLock.Unlock()
return gt.Trans.StoreNode(part, node)
}
/*
UpdateNode updates a single node in a partition of the graph. This function will
only update the given values of the node.
*/
func (gt *concurrentTrans) UpdateNode(part string, node data.Node) error {
gt.transLock.Lock()
defer gt.transLock.Unlock()
return gt.Trans.UpdateNode(part, node)
}
/*
RemoveNode removes a single node from a partition of the graph.
*/
func (gt *concurrentTrans) RemoveNode(part string, nkey string, nkind string) error {
gt.transLock.Lock()
defer gt.transLock.Unlock()
return gt.Trans.RemoveNode(part, nkey, nkind)
}
/*
StoreEdge stores a single edge in a partition of the graph. This function will
overwrites any existing edge.
*/
func (gt *concurrentTrans) StoreEdge(part string, edge data.Edge) error {
gt.transLock.Lock()
defer gt.transLock.Unlock()
return gt.Trans.StoreEdge(part, edge)
}
/*
RemoveEdge removes a single edge from a partition of the graph.
*/
func (gt *concurrentTrans) RemoveEdge(part string, ekey string, ekind string) error {
gt.transLock.Lock()
defer gt.transLock.Unlock()
return gt.Trans.RemoveEdge(part, ekey, ekind)
}
/*
rollingTrans is a rolling transaction which will commit itself after
n operations.
*/
type rollingTrans struct {
id string // ID of this transaction
gm *Manager // Graph manager which created this transaction
currentTrans Trans // Current transaction which is build up
newTransFunc func(*Manager) Trans // Function to create a new transaction
transErrors *errorutil.CompositeError // Collected transaction errors
opThreshold int // Operation threshold
opCount int // Operation count
inFlightCount int // Previous transactions which are still committing
wg *sync.WaitGroup // WaitGroup which releases after all in-flight transactions
countNodeIns int // Count for inserted nodes
countNodeRem int // Count for removed nodes
countEdgeIns int // Count for inserted edges
countEdgeRem int // Count for removed edges
transLock *sync.RWMutex // Lock for this transaction
}
/*
ID returns a unique transaction ID.
*/
func (gt *rollingTrans) ID() string {
gt.transLock.RLock()
defer gt.transLock.RUnlock()
return gt.id
}
/*
IsEmpty returns if this transaction is empty.
*/
func (gt *rollingTrans) IsEmpty() bool {
sn, se, rn, re := gt.Counts()
return sn == 0 && se == 0 && rn == 0 && re == 0
}
/*
Counts returns the transaction size in terms of objects. Returned values
are nodes to store, edges to store, nodes to remove and edges to remove.
*/
func (gt *rollingTrans) Counts() (int, int, int, int) {
gt.transLock.RLock()
defer gt.transLock.RUnlock()
// Count current trans
ns, es, nr, er := gt.currentTrans.Counts()
return ns + gt.countNodeIns, es + gt.countEdgeIns,
nr + gt.countNodeRem, er + gt.countEdgeRem
}
/*
String returns a string representation of this transatction.
*/
func (gt *rollingTrans) String() string {
gt.transLock.RLock()
defer gt.transLock.RUnlock()
ns, es, nr, er := gt.currentTrans.Counts()
return fmt.Sprintf("Rolling transaction %v - Nodes: I:%v R:%v - "+
"Edges: I:%v R:%v - Threshold: %v - In-flight: %v",
gt.id, ns+gt.countNodeIns, nr+gt.countNodeRem, es+gt.countEdgeIns,
er+gt.countEdgeRem, gt.opThreshold, gt.inFlightCount)
}
/*
Commit writes the remaining operations of this rolling transaction to
the graph database.
*/
func (gt *rollingTrans) Commit() error {
// Commit current transaction
gt.transLock.Lock()