From 91fc9ee3dea448ecbe3b33e312bb0dacea4951cf Mon Sep 17 00:00:00 2001 From: Quentin Machu Date: Tue, 6 Oct 2015 15:21:33 -0400 Subject: [PATCH] Improve Transaction: deduplicate quads, allow adding/removing a quad in the same tx --- graph/transaction.go | 12 ++++++------ graph/transaction_test.go | 2 +- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/graph/transaction.go b/graph/transaction.go index 81daeab..a37d9df 100644 --- a/graph/transaction.go +++ b/graph/transaction.go @@ -16,19 +16,19 @@ package graph import "github.com/google/cayley/quad" -// Transaction stores a bunch of Deltas to apply atomatically on the database +// Transaction stores a bunch of Deltas to apply atomatically on the database. type Transaction struct { Deltas map[Delta]struct{} } -// NewTransaction initialize a new transaction +// NewTransaction initialize a new transaction. func NewTransaction() *Transaction { return &Transaction{Deltas: make(map[Delta]struct{}, 100)} } -// AddQuad adds a new quad to the transaction if it is not already present in it +// AddQuad adds a new quad to the transaction if it is not already present in it. // If there is a 'remove' delta for that quad, it will remove that delta from -// the transaction instead of actually addind the quad +// the transaction instead of actually addind the quad. func (t *Transaction) AddQuad(q quad.Quad) { ad := Delta{ Quad: q, @@ -48,9 +48,9 @@ func (t *Transaction) AddQuad(q quad.Quad) { } } -// RemoveQuad adds a quad to remove to the transaction +// RemoveQuad adds a quad to remove to the transaction. // The quad will be removed from the database if it is not present in the -// transaction, otherwise it simply remove it from the transaction +// transaction, otherwise it simply remove it from the transaction. func (t *Transaction) RemoveQuad(q quad.Quad) { ad := Delta{ Quad: q, diff --git a/graph/transaction_test.go b/graph/transaction_test.go index e3c18d4..d566539 100644 --- a/graph/transaction_test.go +++ b/graph/transaction_test.go @@ -29,8 +29,8 @@ func TestTransaction(t *testing.T) { // remove, add -> nothing tx = NewTransaction() - tx.AddQuad(quad.Quad{Subject: "E", Predicate: "follows", Object: "G", Label: ""}) tx.RemoveQuad(quad.Quad{Subject: "E", Predicate: "follows", Object: "G", Label: ""}) + tx.AddQuad(quad.Quad{Subject: "E", Predicate: "follows", Object: "G", Label: ""}) if len(tx.Deltas) != 0 { t.Errorf("Expected [add, remove]->[], have %d delta(s)", len(tx.Deltas)) }