Skip to content

Commit

Permalink
fix(txn): remove redundant keys/preds while merging txn context (#142)
Browse files Browse the repository at this point in the history
TxnContext.Keys is used as a map. This PR removes the redundant keys while merging the transaction context.
  • Loading branch information
NamanJain8 authored Mar 4, 2021
1 parent c7f0a90 commit 0c1d8e0
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 10 deletions.
15 changes: 7 additions & 8 deletions acl_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,13 @@ import (
)

var (
grootpassword = "password"
username = "alice"
userpassword = "alicepassword"
readpred = "predicate_to_read"
writepred = "predicate_to_write"
modifypred = "predicate_to_modify"
unusedgroup = "unused"
devgroup = "dev"
username = "alice"
userpassword = "alicepassword"
readpred = "predicate_to_read"
writepred = "predicate_to_write"
modifypred = "predicate_to_modify"
unusedgroup = "unused"
devgroup = "dev"

dgraphAddress = "127.0.0.1:9180"
adminUrl = "http://127.0.0.1:8180/admin"
Expand Down
24 changes: 22 additions & 2 deletions txn.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,9 @@ var (
type Txn struct {
context *api.TxnContext

keys map[string]struct{}
preds map[string]struct{}

finished bool
mutated bool
readOnly bool
Expand All @@ -63,6 +66,8 @@ func (d *Dgraph) NewTxn() *Txn {
dg: d,
dc: d.anyClient(),
context: &api.TxnContext{},
keys: make(map[string]struct{}),
preds: make(map[string]struct{}),
}
}

Expand Down Expand Up @@ -266,8 +271,13 @@ func (txn *Txn) mergeContext(src *api.TxnContext) error {
if txn.context.StartTs != src.StartTs {
return errors.New("StartTs mismatch")
}
txn.context.Keys = append(txn.context.Keys, src.Keys...)
txn.context.Preds = append(txn.context.Preds, src.Preds...)

for _, key := range src.Keys {
txn.keys[key] = struct{}{}
}
for _, pred := range src.Keys {
txn.preds[pred] = struct{}{}
}
return nil
}

Expand All @@ -280,6 +290,16 @@ func (txn *Txn) commitOrAbort(ctx context.Context) error {
return nil
}

txn.context.Keys = make([]string, 0, len(txn.keys))
for key := range txn.keys {
txn.context.Keys = append(txn.context.Keys, key)
}

txn.context.Preds = make([]string, 0, len(txn.preds))
for pred := range txn.preds {
txn.context.Preds = append(txn.context.Preds, pred)
}

ctx = txn.dg.getContext(ctx)
_, err := txn.dc.CommitOrAbort(ctx, txn.context)

Expand Down

0 comments on commit 0c1d8e0

Please sign in to comment.