Skip to content

Commit

Permalink
use map instead of list for keys/preds
Browse files Browse the repository at this point in the history
  • Loading branch information
NamanJain8 committed Mar 2, 2021
1 parent 5a28ee1 commit 0d391e6
Show file tree
Hide file tree
Showing 9 changed files with 52 additions and 26 deletions.
13 changes: 5 additions & 8 deletions dgraph/cmd/alpha/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ import (
"io/ioutil"
"mime"
"net/http"
"sort"
"strconv"
"strings"
"time"
Expand Down Expand Up @@ -422,12 +421,10 @@ func mutationHandler(w http.ResponseWriter, r *http.Request) {
Txn: resp.Txn,
Latency: resp.Latency,
}
sort.Strings(e.Txn.Keys)
sort.Strings(e.Txn.Preds)

// Don't send keys array which is part of txn context if its commit immediately.
if req.CommitNow {
e.Txn.Keys = e.Txn.Keys[:0]
e.Txn.Keys = nil
}

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

if useList {
tc.Keys = reqList
tc.Keys = x.ListToMap(reqList)
} else {
tc.Keys = reqMap["keys"]
tc.Preds = reqMap["preds"]
tc.Keys = x.ListToMap(reqMap["keys"])
tc.Preds = x.ListToMap(reqMap["preds"])
}

cts, err := worker.CommitOverNetwork(context.Background(), tc)
Expand All @@ -550,7 +547,7 @@ func handleCommit(startTs uint64, reqText []byte) (map[string]interface{}, error
e := query.Extensions{
Txn: resp.Txn,
}
e.Txn.Keys = e.Txn.Keys[:0]
e.Txn.Keys = make(map[string]bool)
response := map[string]interface{}{}
response["extensions"] = e
mp := map[string]interface{}{}
Expand Down
4 changes: 2 additions & 2 deletions dgraph/cmd/alpha/http_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -232,8 +232,8 @@ func mutationWithTs(m, t string, isJson bool, commitNow bool, ts uint64) (
return mr, err
}

mr.keys = r.Extensions.Txn.Keys
mr.preds = r.Extensions.Txn.Preds
mr.keys = x.MapToList(r.Extensions.Txn.Keys)
mr.preds = x.MapToList(r.Extensions.Txn.Preds)
mr.startTs = r.Extensions.Txn.StartTs
sort.Strings(mr.preds)

Expand Down
11 changes: 3 additions & 8 deletions dgraph/cmd/zero/oracle.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ func (o *Oracle) hasConflict(src *api.TxnContext) bool {
if src.StartTs < o.startTxnTs {
return true
}
for _, k := range src.Keys {
for k := range src.Keys {
ki, err := strconv.ParseUint(k, 36, 64)
if err != nil {
glog.Errorf("Got error while parsing conflict key %q: %v\n", k, err)
Expand Down Expand Up @@ -139,7 +139,7 @@ func (o *Oracle) commit(src *api.TxnContext) error {
// We store src.Keys as string to ensure compatibility with all the various language clients we
// have. But, really they are just uint64s encoded as strings. We use base 36 during creation of
// these keys in FillContext in posting/mvcc.go.
for _, k := range src.Keys {
for k := range src.Keys {
ki, err := strconv.ParseUint(k, 36, 64)
if err != nil {
glog.Errorf("Got error while parsing conflict key %q: %v\n", k, err)
Expand Down Expand Up @@ -367,12 +367,7 @@ func (s *Server) commit(ctx context.Context, src *api.TxnContext) error {

checkPreds := func() error {
// Check if any of these tablets is being moved. If so, abort the transaction.
preds := make(map[string]struct{})

for _, k := range src.Preds {
preds[k] = struct{}{}
}
for pkey := range preds {
for pkey := range src.Preds {
splits := strings.Split(pkey, "-")
if len(splits) < 2 {
return errors.Errorf("Unable to find group id in %s", pkey)
Expand Down
4 changes: 2 additions & 2 deletions edgraph/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -595,7 +595,7 @@ func (s *Server) doMutate(ctx context.Context, qc *queryContext, resp *api.Respo
if resp.Txn == nil {
return errors.Wrapf(err, "Txn Context is nil")
}
resp.Txn.Keys = resp.Txn.Keys[:0]
resp.Txn.Keys = make(map[string]bool)
resp.Txn.CommitTs = qc.req.StartTs
return err
}
Expand Down Expand Up @@ -649,7 +649,7 @@ func (s *Server) doMutate(ctx context.Context, qc *queryContext, resp *api.Respo
}

// CommitNow was true, no need to send keys.
resp.Txn.Keys = resp.Txn.Keys[:0]
resp.Txn.Keys = make(map[string]bool)
resp.Txn.CommitTs = cts
calculateMutationMetrics()
return nil
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ go 1.12
// replace github.com/dgraph-io/badger/v2 => /home/mrjn/go/src/github.com/dgraph-io/badger
// replace github.com/dgraph-io/ristretto => /home/mrjn/go/src/github.com/dgraph-io/ristretto
// replace github.com/dgraph-io/roaring => /home/mrjn/go/src/github.com/dgraph-io/roaring
replace github.com/dgraph-io/dgo/v200 => /home/algod/go/src/github.com/dgraph-io/dgo

require (
contrib.go.opencensus.io/exporter/jaeger v0.1.0
Expand Down
6 changes: 4 additions & 2 deletions posting/lists.go
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,9 @@ func (lc *LocalCache) fillPreds(ctx *api.TxnContext, gid uint32) {
// Also send the group id that the predicate was being served by. This is useful when
// checking if Zero should allow a commit during a predicate move.
predKey := fmt.Sprintf("%d-%s", gid, pk.Attr)
ctx.Preds = append(ctx.Preds, predKey)
if ctx.Preds == nil {
ctx.Preds = make(map[string]bool)
}
ctx.Preds[predKey] = true
}
ctx.Preds = x.Unique(ctx.Preds)
}
6 changes: 4 additions & 2 deletions posting/mvcc.go
Original file line number Diff line number Diff line change
Expand Up @@ -212,9 +212,11 @@ func (txn *Txn) FillContext(ctx *api.TxnContext, gid uint32) {
// should be done by sending a list of mutating predicates to Zero,
// along with the keys to be used for conflict detection.
fps := strconv.FormatUint(key, 36)
ctx.Keys = append(ctx.Keys, fps)
if ctx.Keys == nil {
ctx.Keys = make(map[string]bool)
}
ctx.Keys[fps] = true
}
ctx.Keys = x.Unique(ctx.Keys)

txn.Unlock()
txn.Update()
Expand Down
14 changes: 12 additions & 2 deletions worker/mutation.go
Original file line number Diff line number Diff line change
Expand Up @@ -688,8 +688,18 @@ func MutateOverNetwork(ctx context.Context, m *pb.Mutations) (*api.TxnContext, e
e = res.err
}
if res.ctx != nil {
tctx.Keys = append(tctx.Keys, res.ctx.Keys...)
tctx.Preds = append(tctx.Preds, res.ctx.Preds...)
if tctx.Keys == nil {
tctx.Keys = make(map[string]bool)
}
for key := range res.ctx.Keys {
tctx.Keys[key] = true
}
if tctx.Preds == nil {
tctx.Preds = make(map[string]bool)
}
for pred := range res.ctx.Preds {
tctx.Preds[pred] = true
}
}
}
close(resCh)
Expand Down
19 changes: 19 additions & 0 deletions x/x.go
Original file line number Diff line number Diff line change
Expand Up @@ -632,6 +632,25 @@ func Unique(a []string) []string {
return a[:idx]
}

func MapToList(mp map[string]bool) []string {
if mp == nil {
return nil
}
list := make([]string, 0, len(mp))
for k := range mp {
list = append(list, k)
}
return list
}

func ListToMap(list []string) map[string]bool {
mp := make(map[string]bool)
for _, k := range list {
mp[k] = true
}
return mp
}

// ReadLine reads a single line from a buffered reader. The line is read into the
// passed in buffer to minimize allocations. This is the preferred
// method for loading long lines which could be longer than the buffer
Expand Down

0 comments on commit 0d391e6

Please sign in to comment.