From 8acc6bd9a1b13fce664098993e04a7dd1f81eafc Mon Sep 17 00:00:00 2001 From: Aman Mangal Date: Wed, 20 Nov 2019 21:23:09 +0530 Subject: [PATCH] address comments --- dgraph/cmd/alpha/http.go | 122 +++++++++++---------------------------- edgraph/server.go | 9 ++- 2 files changed, 37 insertions(+), 94 deletions(-) diff --git a/dgraph/cmd/alpha/http.go b/dgraph/cmd/alpha/http.go index 52f71f60b66..23a2b68ac7a 100644 --- a/dgraph/cmd/alpha/http.go +++ b/dgraph/cmd/alpha/http.go @@ -157,26 +157,6 @@ func writeResponse(w http.ResponseWriter, r *http.Request, b []byte) (int, error return out.Write(b) } -func writeEntry(out *bytes.Buffer, key string, js []byte) error { - if _, err := out.WriteRune('"'); err != nil { - return err - } - if _, err := out.WriteString(key); err != nil { - return err - } - if _, err := out.WriteRune('"'); err != nil { - return err - } - if _, err := out.WriteRune(':'); err != nil { - return err - } - if _, err := out.Write(js); err != nil { - return err - } - - return nil -} - // This method should just build the request and proxy it to the Query method of dgraph.Server. // It can then encode the response as appropriate before sending it back to the user. func queryHandler(w http.ResponseWriter, r *http.Request) { @@ -272,7 +252,6 @@ func queryHandler(w http.ResponseWriter, r *http.Request) { return } - var out bytes.Buffer e := query.Extensions{ Txn: resp.Txn, Latency: resp.Latency, @@ -284,26 +263,19 @@ func queryHandler(w http.ResponseWriter, r *http.Request) { return } - if _, err := out.WriteRune('{'); err != nil { - x.SetStatusWithData(w, x.Error, err.Error()) - return - } - if err := writeEntry(&out, "data", resp.Json); err != nil { - x.SetStatusWithData(w, x.Error, err.Error()) - return - } - if _, err := out.WriteRune(','); err != nil { - x.SetStatusWithData(w, x.Error, err.Error()) - return - } - if err := writeEntry(&out, "extensions", js); err != nil { - x.SetStatusWithData(w, x.Error, err.Error()) - return - } - if _, err := out.WriteRune('}'); err != nil { - x.SetStatusWithData(w, x.Error, err.Error()) - return - } + var out bytes.Buffer + writeEntry := func(key string, js []byte) { + out.WriteRune('"') + out.WriteString(key) + out.WriteRune('"') + out.WriteRune(':') + out.Write(js) + } + out.WriteRune('{') + writeEntry("data", resp.Json) + out.WriteRune(',') + writeEntry("extensions", js) + out.WriteRune('}') if _, err := writeResponse(w, r, out.Bytes()); err != nil { // If client crashes before server could write response, writeResponse will error out, @@ -409,61 +381,33 @@ func mutationHandler(w http.ResponseWriter, r *http.Request) { e.Txn.Keys = e.Txn.Keys[:0] } - var out bytes.Buffer - // If response is either empty or only contains empty braces {}, it's a no-op - if len(resp.Json) > 2 { - var i int - for i = len(resp.Json) - 1; i >= 0; i-- { - if resp.Json[i] == '}' { - break - } - } + response := map[string]interface{}{} + response["extensions"] = e + mp := map[string]interface{}{} + mp["code"] = x.Success + mp["message"] = "Done" + mp["uids"] = resp.Uids - out = *bytes.NewBuffer(resp.Json[:i]) - if _, err := out.WriteRune(','); err != nil { - x.SetStatusWithData(w, x.Error, err.Error()) - return - } - } else { - if _, err := out.WriteRune('{'); err != nil { + // add query response if any + l := len(resp.Json) + if l > 0 && resp.Json[l-1] == '}' { + data, err := json.Marshal(mp) + if err != nil { x.SetStatusWithData(w, x.Error, err.Error()) return } - } - if err := writeEntry(&out, "code", []byte("\""+x.Success+"\"")); err != nil { - x.SetStatusWithData(w, x.Error, err.Error()) - return - } - if _, err := out.WriteRune(','); err != nil { - x.SetStatusWithData(w, x.Error, err.Error()) - return - } - if err := writeEntry(&out, "message", []byte("\"Done\"")); err != nil { - x.SetStatusWithData(w, x.Error, err.Error()) - return - } - if _, err := out.WriteRune(','); err != nil { - x.SetStatusWithData(w, x.Error, err.Error()) - return - } - uids, err := json.Marshal(resp.Uids) - if err != nil { - x.SetStatusWithData(w, x.Error, err.Error()) - return - } - if err := writeEntry(&out, "uids", uids); err != nil { - x.SetStatusWithData(w, x.Error, err.Error()) - return - } - if _, err := out.WriteRune('}'); err != nil { - x.SetStatusWithData(w, x.Error, err.Error()) - return + var out bytes.Buffer + out = *bytes.NewBuffer(resp.Json[:(l - 1)]) + out.WriteRune(',') + + // data[0] must be '{' + out.Write(data[1:]) + response["data"] = json.RawMessage(out.Bytes()) + } else { + response["data"] = mp } - response := map[string]interface{}{} - response["extensions"] = e - response["data"] = json.RawMessage(out.Bytes()) js, err := json.Marshal(response) if err != nil { x.SetStatusWithData(w, x.Error, err.Error()) diff --git a/edgraph/server.go b/edgraph/server.go index ee134f68d35..4470fbb8d34 100644 --- a/edgraph/server.go +++ b/edgraph/server.go @@ -882,7 +882,6 @@ func (s *Server) doQuery(ctx context.Context, req *api.Request, authorize int) ( if qc.req.StartTs == 0 { start := time.Now() - // For mutations, BestEffort doesn't make sense. We simply ignore the parameter. if qc.req.BestEffort && !isMutation { qc.req.StartTs = posting.Oracle().MaxAssigned() @@ -1013,9 +1012,9 @@ func processQuery(ctx context.Context, qc *queryContext) (*api.Response, error) // parseRequest parses the incoming request func parseRequest(qc *queryContext) error { - parsingStartTime := time.Now() + start := time.Now() defer func() { - qc.latency.Parsing = time.Since(parsingStartTime) + qc.latency.Parsing = time.Since(start) }() var needVars []string @@ -1066,8 +1065,8 @@ func authorizeRequest(ctx context.Context, qc *queryContext) error { return err } - if len(qc.gmuList) > 0 { - if err := authorizeMutation(ctx, qc.gmuList[0]); err != nil { + for _, gmu := range qc.gmuList { + if err := authorizeMutation(ctx, gmu); err != nil { return err } }