1414
1515package graph
1616
17- import "github.com/cayleygraph/cayley/quad"
17+ import (
18+ "github.com/cayleygraph/cayley/internal/mapset"
19+ "github.com/cayleygraph/cayley/quad"
20+ )
1821
1922// Transaction stores a bunch of Deltas to apply together in an atomic step on the database.
2023type Transaction struct {
2124 // Deltas stores the deltas in the right order
2225 Deltas []Delta
2326 // deltas stores the deltas in a map to avoid duplications
24- deltas map [ Delta ] struct {}
27+ deltas mapset. Map
2528}
2629
2730// NewTransaction initialize a new transaction.
@@ -31,7 +34,7 @@ func NewTransaction() *Transaction {
3134
3235// NewTransactionN initialize a new transaction with a predefined capacity.
3336func NewTransactionN (n int ) * Transaction {
34- return & Transaction {Deltas : make ([]Delta , 0 , n ), deltas : make ( map [ Delta ] struct {}, n )}
37+ return & Transaction {Deltas : make ([]Delta , 0 , n ), deltas : mapset . NewMapWithComparator ( mapset . GenericComparator )}
3538}
3639
3740// AddQuad adds a new quad to the transaction if it is not already present in it.
@@ -40,8 +43,8 @@ func NewTransactionN(n int) *Transaction {
4043func (t * Transaction ) AddQuad (q quad.Quad ) {
4144 ad , rd := createDeltas (q )
4245
43- if _ , adExists := t .deltas [ ad ]; ! adExists {
44- if _ , rdExists := t .deltas [ rd ]; rdExists {
46+ if ! t .deltas . Contains ( ad ) {
47+ if t .deltas . Contains ( rd ) {
4548 t .deleteDelta (rd )
4649 } else {
4750 t .addDelta (ad )
@@ -55,10 +58,10 @@ func (t *Transaction) AddQuad(q quad.Quad) {
5558func (t * Transaction ) RemoveQuad (q quad.Quad ) {
5659 ad , rd := createDeltas (q )
5760
58- if _ , adExists := t .deltas [ ad ]; adExists {
61+ if t .deltas . Contains ( ad ) {
5962 t .deleteDelta (ad )
6063 } else {
61- if _ , rdExists := t .deltas [ rd ]; ! rdExists {
64+ if ! t .deltas . Contains ( rd ) {
6265 t .addDelta (rd )
6366 }
6467 }
@@ -78,11 +81,11 @@ func createDeltas(q quad.Quad) (ad, rd Delta) {
7881
7982func (t * Transaction ) addDelta (d Delta ) {
8083 t .Deltas = append (t .Deltas , d )
81- t .deltas [ d ] = struct {}{}
84+ t .deltas . Put ( d , struct {}{})
8285}
8386
8487func (t * Transaction ) deleteDelta (d Delta ) {
85- delete ( t .deltas , d )
88+ t .deltas . Remove ( d )
8689
8790 for i , id := range t .Deltas {
8891 if id == d {
0 commit comments