MCPcopy
hub / github.com/dgraph-io/dgraph / assign

Method assign

dgraph/cmd/zero/http.go:45–102  ·  view source on GitHub ↗
(w http.ResponseWriter, r *http.Request)

Source from the content-addressed store, hash-verified

43}
44
45func (st *state) assign(w http.ResponseWriter, r *http.Request) {
46 x.AddCorsHeaders(w)
47 w.Header().Set("Content-Type", "application/json")
48 if r.Method == "OPTIONS" {
49 return
50 }
51 if r.Method != http.MethodGet {
52 w.WriteHeader(http.StatusBadRequest)
53 x.SetStatus(w, x.ErrorInvalidMethod, "Invalid method")
54 return
55 }
56 val, ok := intFromQueryParam(w, r, "num")
57 if !ok {
58 return
59 }
60
61 num := &pb.Num{Val: val}
62 ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
63 defer cancel()
64
65 var ids *pb.AssignedIds
66 var err error
67 what := r.URL.Query().Get("what")
68 switch what {
69 case "uids":
70 num.Type = pb.Num_UID
71 ids, err = st.zero.AssignIds(ctx, num)
72 case "timestamps":
73 num.Type = pb.Num_TXN_TS
74 if num.Val == 0 {
75 num.ReadOnly = true
76 }
77 ids, err = st.zero.Timestamps(ctx, num)
78 case "nsids":
79 num.Type = pb.Num_NS_ID
80 ids, err = st.zero.AssignIds(ctx, num)
81 default:
82 x.SetStatus(w, x.Error,
83 fmt.Sprintf("Invalid what: [%s]. Must be one of: [uids, timestamps, nsids]", what))
84 return
85 }
86 if err != nil {
87 x.SetStatus(w, x.Error, err.Error())
88 return
89 }
90
91 m := protojson.MarshalOptions{EmitUnpopulated: true}
92 buf, err := m.Marshal(ids)
93 if err != nil {
94 x.SetStatus(w, x.ErrorNoData, err.Error())
95 return
96 }
97
98 if _, err := w.Write(buf); err != nil {
99 x.SetStatus(w, x.Error, err.Error())
100 return
101 }
102}

Callers

nothing calls this directly

Calls 11

AddCorsHeadersFunction · 0.92
SetStatusFunction · 0.92
intFromQueryParamFunction · 0.85
SetMethod · 0.65
WriteHeaderMethod · 0.65
GetMethod · 0.65
AssignIdsMethod · 0.65
TimestampsMethod · 0.65
WriteMethod · 0.65
QueryMethod · 0.45
ErrorMethod · 0.45

Tested by

no test coverage detected