Skip to content

Commit 0d391e6

Browse files
committed
use map instead of list for keys/preds
1 parent 5a28ee1 commit 0d391e6

File tree

9 files changed

+52
-26
lines changed

9 files changed

+52
-26
lines changed

dgraph/cmd/alpha/http.go

+5-8
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ import (
2626
"io/ioutil"
2727
"mime"
2828
"net/http"
29-
"sort"
3029
"strconv"
3130
"strings"
3231
"time"
@@ -422,12 +421,10 @@ func mutationHandler(w http.ResponseWriter, r *http.Request) {
422421
Txn: resp.Txn,
423422
Latency: resp.Latency,
424423
}
425-
sort.Strings(e.Txn.Keys)
426-
sort.Strings(e.Txn.Preds)
427424

428425
// Don't send keys array which is part of txn context if its commit immediately.
429426
if req.CommitNow {
430-
e.Txn.Keys = e.Txn.Keys[:0]
427+
e.Txn.Keys = nil
431428
}
432429

433430
response := map[string]interface{}{}
@@ -533,10 +530,10 @@ func handleCommit(startTs uint64, reqText []byte) (map[string]interface{}, error
533530
}
534531

535532
if useList {
536-
tc.Keys = reqList
533+
tc.Keys = x.ListToMap(reqList)
537534
} else {
538-
tc.Keys = reqMap["keys"]
539-
tc.Preds = reqMap["preds"]
535+
tc.Keys = x.ListToMap(reqMap["keys"])
536+
tc.Preds = x.ListToMap(reqMap["preds"])
540537
}
541538

542539
cts, err := worker.CommitOverNetwork(context.Background(), tc)
@@ -550,7 +547,7 @@ func handleCommit(startTs uint64, reqText []byte) (map[string]interface{}, error
550547
e := query.Extensions{
551548
Txn: resp.Txn,
552549
}
553-
e.Txn.Keys = e.Txn.Keys[:0]
550+
e.Txn.Keys = make(map[string]bool)
554551
response := map[string]interface{}{}
555552
response["extensions"] = e
556553
mp := map[string]interface{}{}

dgraph/cmd/alpha/http_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -232,8 +232,8 @@ func mutationWithTs(m, t string, isJson bool, commitNow bool, ts uint64) (
232232
return mr, err
233233
}
234234

235-
mr.keys = r.Extensions.Txn.Keys
236-
mr.preds = r.Extensions.Txn.Preds
235+
mr.keys = x.MapToList(r.Extensions.Txn.Keys)
236+
mr.preds = x.MapToList(r.Extensions.Txn.Preds)
237237
mr.startTs = r.Extensions.Txn.StartTs
238238
sort.Strings(mr.preds)
239239

dgraph/cmd/zero/oracle.go

+3-8
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ func (o *Oracle) hasConflict(src *api.TxnContext) bool {
8383
if src.StartTs < o.startTxnTs {
8484
return true
8585
}
86-
for _, k := range src.Keys {
86+
for k := range src.Keys {
8787
ki, err := strconv.ParseUint(k, 36, 64)
8888
if err != nil {
8989
glog.Errorf("Got error while parsing conflict key %q: %v\n", k, err)
@@ -139,7 +139,7 @@ func (o *Oracle) commit(src *api.TxnContext) error {
139139
// We store src.Keys as string to ensure compatibility with all the various language clients we
140140
// have. But, really they are just uint64s encoded as strings. We use base 36 during creation of
141141
// these keys in FillContext in posting/mvcc.go.
142-
for _, k := range src.Keys {
142+
for k := range src.Keys {
143143
ki, err := strconv.ParseUint(k, 36, 64)
144144
if err != nil {
145145
glog.Errorf("Got error while parsing conflict key %q: %v\n", k, err)
@@ -367,12 +367,7 @@ func (s *Server) commit(ctx context.Context, src *api.TxnContext) error {
367367

368368
checkPreds := func() error {
369369
// Check if any of these tablets is being moved. If so, abort the transaction.
370-
preds := make(map[string]struct{})
371-
372-
for _, k := range src.Preds {
373-
preds[k] = struct{}{}
374-
}
375-
for pkey := range preds {
370+
for pkey := range src.Preds {
376371
splits := strings.Split(pkey, "-")
377372
if len(splits) < 2 {
378373
return errors.Errorf("Unable to find group id in %s", pkey)

edgraph/server.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -595,7 +595,7 @@ func (s *Server) doMutate(ctx context.Context, qc *queryContext, resp *api.Respo
595595
if resp.Txn == nil {
596596
return errors.Wrapf(err, "Txn Context is nil")
597597
}
598-
resp.Txn.Keys = resp.Txn.Keys[:0]
598+
resp.Txn.Keys = make(map[string]bool)
599599
resp.Txn.CommitTs = qc.req.StartTs
600600
return err
601601
}
@@ -649,7 +649,7 @@ func (s *Server) doMutate(ctx context.Context, qc *queryContext, resp *api.Respo
649649
}
650650

651651
// CommitNow was true, no need to send keys.
652-
resp.Txn.Keys = resp.Txn.Keys[:0]
652+
resp.Txn.Keys = make(map[string]bool)
653653
resp.Txn.CommitTs = cts
654654
calculateMutationMetrics()
655655
return nil

go.mod

+1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ go 1.12
55
// replace github.com/dgraph-io/badger/v2 => /home/mrjn/go/src/github.com/dgraph-io/badger
66
// replace github.com/dgraph-io/ristretto => /home/mrjn/go/src/github.com/dgraph-io/ristretto
77
// replace github.com/dgraph-io/roaring => /home/mrjn/go/src/github.com/dgraph-io/roaring
8+
replace github.com/dgraph-io/dgo/v200 => /home/algod/go/src/github.com/dgraph-io/dgo
89

910
require (
1011
contrib.go.opencensus.io/exporter/jaeger v0.1.0

posting/lists.go

+4-2
Original file line numberDiff line numberDiff line change
@@ -241,7 +241,9 @@ func (lc *LocalCache) fillPreds(ctx *api.TxnContext, gid uint32) {
241241
// Also send the group id that the predicate was being served by. This is useful when
242242
// checking if Zero should allow a commit during a predicate move.
243243
predKey := fmt.Sprintf("%d-%s", gid, pk.Attr)
244-
ctx.Preds = append(ctx.Preds, predKey)
244+
if ctx.Preds == nil {
245+
ctx.Preds = make(map[string]bool)
246+
}
247+
ctx.Preds[predKey] = true
245248
}
246-
ctx.Preds = x.Unique(ctx.Preds)
247249
}

posting/mvcc.go

+4-2
Original file line numberDiff line numberDiff line change
@@ -212,9 +212,11 @@ func (txn *Txn) FillContext(ctx *api.TxnContext, gid uint32) {
212212
// should be done by sending a list of mutating predicates to Zero,
213213
// along with the keys to be used for conflict detection.
214214
fps := strconv.FormatUint(key, 36)
215-
ctx.Keys = append(ctx.Keys, fps)
215+
if ctx.Keys == nil {
216+
ctx.Keys = make(map[string]bool)
217+
}
218+
ctx.Keys[fps] = true
216219
}
217-
ctx.Keys = x.Unique(ctx.Keys)
218220

219221
txn.Unlock()
220222
txn.Update()

worker/mutation.go

+12-2
Original file line numberDiff line numberDiff line change
@@ -688,8 +688,18 @@ func MutateOverNetwork(ctx context.Context, m *pb.Mutations) (*api.TxnContext, e
688688
e = res.err
689689
}
690690
if res.ctx != nil {
691-
tctx.Keys = append(tctx.Keys, res.ctx.Keys...)
692-
tctx.Preds = append(tctx.Preds, res.ctx.Preds...)
691+
if tctx.Keys == nil {
692+
tctx.Keys = make(map[string]bool)
693+
}
694+
for key := range res.ctx.Keys {
695+
tctx.Keys[key] = true
696+
}
697+
if tctx.Preds == nil {
698+
tctx.Preds = make(map[string]bool)
699+
}
700+
for pred := range res.ctx.Preds {
701+
tctx.Preds[pred] = true
702+
}
693703
}
694704
}
695705
close(resCh)

x/x.go

+19
Original file line numberDiff line numberDiff line change
@@ -632,6 +632,25 @@ func Unique(a []string) []string {
632632
return a[:idx]
633633
}
634634

635+
func MapToList(mp map[string]bool) []string {
636+
if mp == nil {
637+
return nil
638+
}
639+
list := make([]string, 0, len(mp))
640+
for k := range mp {
641+
list = append(list, k)
642+
}
643+
return list
644+
}
645+
646+
func ListToMap(list []string) map[string]bool {
647+
mp := make(map[string]bool)
648+
for _, k := range list {
649+
mp[k] = true
650+
}
651+
return mp
652+
}
653+
635654
// ReadLine reads a single line from a buffered reader. The line is read into the
636655
// passed in buffer to minimize allocations. This is the preferred
637656
// method for loading long lines which could be longer than the buffer

0 commit comments

Comments
 (0)