Skip to content

Commit 7476fa3

Browse files
author
Pawan Rawal
committed
Merge branch 'master' of github.com:dgraph-io/dgraph
2 parents 97a0e29 + f92d520 commit 7476fa3

File tree

9 files changed

+662
-243
lines changed

9 files changed

+662
-243
lines changed

client/client.go

Lines changed: 101 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -28,22 +28,23 @@ import (
2828
"github.com/twpayne/go-geom/encoding/geojson"
2929
)
3030

31-
type Op int
31+
type opType int
3232

3333
const (
3434
// SET indicates a Set mutation.
35-
SET Op = iota
35+
SET opType = iota
3636
// DEL indicates a Delete mutation.
3737
DEL
3838
)
3939

40-
// Req wraps the protos.Request so that helper methods can be defined on it.
40+
// A Req represents a single request to the backend Dgraph instance. Each request may contain
41+
// multiple set, delete and schema mutations, and a single GraphQL+- query. If the query contains
42+
// GraphQL variables, then it must be set with SetQueryWithVariables rather than SetQuery.
4143
type Req struct {
4244
gr protos.Request
4345
}
4446

45-
// Request returns the graph request object which is sent to the server to perform
46-
// a query/mutation.
47+
// Request returns the protos.Request backing the Req.
4748
func (req *Req) Request() *protos.Request {
4849
return &req.gr
4950
}
@@ -61,18 +62,23 @@ func checkSchema(schema protos.SchemaUpdate) error {
6162
return nil
6263
}
6364

64-
// SetQuery sets a query with graphQL variables as part of the request.
65+
// SetQuery sets the query in req to the given string.
66+
// The query string is not checked until the request is
67+
// run, when it is parsed and checked server-side.
6568
func (req *Req) SetQuery(q string) {
6669
req.gr.Query = q
6770
}
6871

69-
// SetQueryWithVariables sets a query with graphQL variables as part of the request.
72+
// SetQueryWithVariables sets query q (which contains graphQL variables mapped
73+
// in vars) as the query in req and sets vars as the corresponding query variables.
74+
// Neither the query string nor the variables are checked until the request is run,
75+
// when it is parsed and checked server-side.
7076
func (req *Req) SetQueryWithVariables(q string, vars map[string]string) {
7177
req.gr.Query = q
7278
req.gr.Vars = vars
7379
}
7480

75-
func (req *Req) addMutation(e Edge, op Op) {
81+
func (req *Req) addMutation(e Edge, op opType) {
7682
if req.gr.Mutation == nil {
7783
req.gr.Mutation = new(protos.Mutation)
7884
}
@@ -84,23 +90,41 @@ func (req *Req) addMutation(e Edge, op Op) {
8490
}
8591
}
8692

87-
func (req *Req) Set(e Edge) {
93+
// Set adds edge e to the set mutation of request req, thus scheduling the edge to be added to the
94+
// graph when the request is run. The edge must have a valid target (a Node or value), otherwise
95+
// an error is returned. The edge is not checked agaist the schema until the request is
96+
// run --- so setting a UID edge to a value, for example, doesn't result in an error until
97+
// the request is run.
98+
func (req *Req) Set(e Edge) error {
99+
if err := e.validate(); err != nil {
100+
return err
101+
}
88102
req.addMutation(e, SET)
103+
return nil
89104
}
90105

91-
func (req *Req) Delete(e Edge) {
106+
// Delete adds edge e to the delete mutation of request req, thus scheduling the edge to be removed
107+
// from the graph when the request is run. The edge must have a valid target (a Node or value),
108+
// otherwise an error is returned. The edge need not represent
109+
// an edge in the graph --- applying such a mutation simply has no effect.
110+
func (req *Req) Delete(e Edge) error {
111+
if err := e.validate(); err != nil {
112+
return err
113+
}
92114
req.addMutation(e, DEL)
115+
return nil
93116
}
94117

95-
// AddSchema sets the schema mutations
96-
func (req *Req) addSchema(s protos.SchemaUpdate) error {
118+
// AddSchema adds the single schema mutation s to the request.
119+
func (req *Req) AddSchema(s protos.SchemaUpdate) error {
97120
if req.gr.Mutation == nil {
98121
req.gr.Mutation = new(protos.Mutation)
99122
}
100123
req.gr.Mutation.Schema = append(req.gr.Mutation.Schema, &s)
101124
return nil
102125
}
103126

127+
104128
func (req *Req) size() int {
105129
if req.gr.Mutation == nil {
106130
return 0
@@ -117,22 +141,25 @@ func (req *Req) reset() {
117141

118142
type nquadOp struct {
119143
e Edge
120-
op Op
144+
op opType
121145
}
122146

147+
// Node represents a single node in the graph.
123148
type Node struct {
124149
uid uint64
125150
// We can do variables in mutations.
126151
varName string
127152
}
128153

154+
// String returns Node n as a string
129155
func (n Node) String() string {
130156
if n.uid != 0 {
131157
return fmt.Sprintf("%#x", uint64(n.uid))
132158
}
133159
return n.varName
134160
}
135161

162+
// ConnectTo creates an edge labelled pred from Node n to Node n1
136163
func (n *Node) ConnectTo(pred string, n1 Node) Edge {
137164
e := Edge{}
138165
if len(n.varName) != 0 {
@@ -145,6 +172,11 @@ func (n *Node) ConnectTo(pred string, n1 Node) Edge {
145172
return e
146173
}
147174

175+
// Edge create an edge with source Node n and predicate pred, but without a target.
176+
// The edge needs to be completed by calling Edge.ConnectTo() if the edge is a
177+
// UID edge, or one of the Edge.SetValue...() functions if the edge is of a scalar type.
178+
// The edge can't be committed to the store --- calling Req.Set() to add the edge to
179+
// a request will result in an error --- until it is completed.
148180
func (n *Node) Edge(pred string) Edge {
149181
e := Edge{}
150182
if len(n.varName) != 0 {
@@ -156,14 +188,21 @@ func (n *Node) Edge(pred string) Edge {
156188
return e
157189
}
158190

191+
// An Edge represents an edge between a source node and a target (either a node or a value).
192+
// Facets are stored in the edge. See Node.Edge(), Node.ConnectTo(), Edge.ConnecTo(),
193+
// Edge.AddFacet and the Edge.SetValue...() functions to
194+
// make a valid edge for a set or delete mutation.
159195
type Edge struct {
160196
nq protos.NQuad
161197
}
162198

199+
// NewEdge creates an Edge from an NQuad.
163200
func NewEdge(nq protos.NQuad) Edge {
164201
return Edge{nq}
165202
}
166203

204+
// ConnectTo adds Node n as the target of the edge. If the edge already has a known scalar type,
205+
// for example if Edge.SetValue...() had been called on the edge, then an error is returned.
167206
func (e *Edge) ConnectTo(n Node) error {
168207
if e.nq.ObjectType > 0 {
169208
return ErrValue
@@ -176,6 +215,15 @@ func (e *Edge) ConnectTo(n Node) error {
176215
return nil
177216
}
178217

218+
func (e *Edge) validate() error {
219+
// Edge should be connected to a value in which case ObjectType would be > 0.
220+
// Or it needs to connect to a Node (ObjectId > 0) or it should be connected to a variable.
221+
if e.nq.ObjectValue != nil || len(e.nq.ObjectId) > 0 || len(e.nq.ObjectVar) > 0 {
222+
return nil
223+
}
224+
return ErrNotConnected
225+
}
226+
179227
func validateStr(val string) error {
180228
for idx, c := range val {
181229
if c == '"' && (idx == 0 || val[idx-1] != '\\') {
@@ -185,6 +233,11 @@ func validateStr(val string) error {
185233
return nil
186234
}
187235

236+
// SetValueString sets the value of Edge e as string val and sets the type of the edge to
237+
// types.StringID. If the edge had previous been assigned another value (even of another type),
238+
// the value and type are overwritten. If the edge has previously been connected to a node, the
239+
// edge and type are left unchanged and ErrConnected is returned. The string must
240+
// escape " with \, otherwise the edge and type are left unchanged and an error returned.
188241
func (e *Edge) SetValueString(val string) error {
189242
if len(e.nq.ObjectId) > 0 {
190243
return ErrConnected
@@ -202,6 +255,10 @@ func (e *Edge) SetValueString(val string) error {
202255
return nil
203256
}
204257

258+
// SetValueInt sets the value of Edge e as int64 val and sets the type of the edge to types.IntID.
259+
// If the edge had previous been assigned another value (even of another type), the value and type
260+
// are overwritten. If the edge has previously been connected to a node, the edge and type are
261+
// left unchanged and ErrConnected is returned.
205262
func (e *Edge) SetValueInt(val int64) error {
206263
if len(e.nq.ObjectId) > 0 {
207264
return ErrConnected
@@ -215,6 +272,10 @@ func (e *Edge) SetValueInt(val int64) error {
215272
return nil
216273
}
217274

275+
// SetValueFloat sets the value of Edge e as float64 val and sets the type of the edge to
276+
// types.FloatID. If the edge had previous been assigned another value (even of another type),
277+
// the value and type are overwritten. If the edge has previously been connected to a node, the
278+
// edge and type are left unchanged and ErrConnected is returned.
218279
func (e *Edge) SetValueFloat(val float64) error {
219280
if len(e.nq.ObjectId) > 0 {
220281
return ErrConnected
@@ -228,6 +289,10 @@ func (e *Edge) SetValueFloat(val float64) error {
228289
return nil
229290
}
230291

292+
// SetValueBool sets the value of Edge e as bool val and sets the type of the edge to types.BoolID.
293+
// If the edge had previous been assigned another value (even of another type), the value and type
294+
// are overwritten. If the edge has previously been connected to a node, the edge and type are
295+
// left unchanged and ErrConnected is returned.
231296
func (e *Edge) SetValueBool(val bool) error {
232297
if len(e.nq.ObjectId) > 0 {
233298
return ErrConnected
@@ -241,6 +306,10 @@ func (e *Edge) SetValueBool(val bool) error {
241306
return nil
242307
}
243308

309+
// SetValuePassword sets the value of Edge e as password string val and sets the type of the edge
310+
// to types.PasswordID. If the edge had previous been assigned another value (even of another
311+
// type), the value and type are overwritten. If the edge has previously been connected to a
312+
// node, the edge and type are left unchanged and ErrConnected is returned.
244313
func (e *Edge) SetValuePassword(val string) error {
245314
if len(e.nq.ObjectId) > 0 {
246315
return ErrConnected
@@ -254,6 +323,10 @@ func (e *Edge) SetValuePassword(val string) error {
254323
return nil
255324
}
256325

326+
// SetValueDatetime sets the value of Edge e as time.Time dateTime and sets the type of the edge
327+
// to types.DateTimeID. If the edge had previous been assigned another value (even of another
328+
// type), the value and type are overwritten. If the edge has previously been connected to a node,
329+
// the edge and type are left unchanged and ErrConnected is returned.
257330
func (e *Edge) SetValueDatetime(dateTime time.Time) error {
258331
if len(e.nq.ObjectId) > 0 {
259332
return ErrConnected
@@ -267,6 +340,11 @@ func (e *Edge) SetValueDatetime(dateTime time.Time) error {
267340
return nil
268341
}
269342

343+
// SetValueGeoJson sets the value of Edge e as the GeoJSON object parsed from json string and sets
344+
// the type of the edge to types.GeoID. If the edge had previous been assigned another value (even
345+
// of another type), the value and type are overwritten. If the edge has previously been connected
346+
// to a node, the edge and type are left unchanged and ErrConnected is returned. If the string
347+
// fails to parse with geojson.Unmarshal() the edge is left unchanged and an error returned.
270348
func (e *Edge) SetValueGeoJson(json string) error {
271349
if len(e.nq.ObjectId) > 0 {
272350
return ErrConnected
@@ -288,6 +366,11 @@ func (e *Edge) SetValueGeoJson(json string) error {
288366
return nil
289367
}
290368

369+
// SetValueDefault sets the value of Edge e as string val and sets the type of the edge to
370+
// types.DefaultID. If the edge had previous been assigned another value (even of another
371+
// type), the value and type are overwritten. If the edge has previously been connected to
372+
// a node, the edge and type are left unchanged and ErrConnected is returned.
373+
// The string must escape " with \, otherwise the edge and type are left unchanged and an error returned.
291374
func (e *Edge) SetValueDefault(val string) error {
292375
if len(e.nq.ObjectId) > 0 {
293376
return ErrConnected
@@ -305,6 +388,10 @@ func (e *Edge) SetValueDefault(val string) error {
305388
return nil
306389
}
307390

391+
// SetValueBytes allows setting the value of an edge to raw bytes and sets the type of the edge
392+
// to types.BinaryID. If the edge had previous been assigned another value (even of another type),
393+
// the value and type are overwritten. If the edge has previously been connected to a node, the
394+
// edge and type are left unchanged and ErrConnected is returned. the bytes are encoded as base64.
308395
func (e *Edge) SetValueBytes(val []byte) error {
309396
if len(e.nq.ObjectId) > 0 {
310397
return ErrConnected
@@ -320,6 +407,7 @@ func (e *Edge) SetValueBytes(val []byte) error {
320407
return nil
321408
}
322409

410+
// AddFacet adds the key, value pair as facets on Edge e. No checking is done.
323411
func (e *Edge) AddFacet(key, val string) {
324412
e.nq.Facets = append(e.nq.Facets, &protos.Facet{
325413
Key: key,

client/client_test.go

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020
"testing"
2121

2222
"github.com/dgraph-io/dgraph/protos"
23+
"github.com/dgraph-io/dgraph/x"
2324
"github.com/stretchr/testify/assert"
2425
)
2526

@@ -31,24 +32,27 @@ func TestSetMutation(t *testing.T) {
3132
req := Req{}
3233

3334
s := graphValue("Alice")
34-
req.Set(Edge{protos.NQuad{
35+
err := req.Set(Edge{protos.NQuad{
3536
Subject: "alice",
3637
Predicate: "name",
3738
ObjectValue: s,
3839
}})
40+
x.Check(err)
3941

4042
s = graphValue("rabbithole")
41-
req.Set(Edge{protos.NQuad{
43+
err = req.Set(Edge{protos.NQuad{
4244
Subject: "alice",
4345
Predicate: "falls.in",
4446
ObjectValue: s,
4547
}})
48+
x.Check(err)
4649

47-
req.Delete(Edge{protos.NQuad{
50+
err = req.Delete(Edge{protos.NQuad{
4851
Subject: "alice",
4952
Predicate: "falls.in",
5053
ObjectValue: s,
5154
}})
55+
x.Check(err)
5256

5357
assert.Equal(t, len(req.gr.Mutation.Set), 2, "Set should have 2 entries")
5458
assert.Equal(t, len(req.gr.Mutation.Del), 1, "Del should have 1 entry")
@@ -57,7 +61,7 @@ func TestSetMutation(t *testing.T) {
5761
func TestAddSchema(t *testing.T) {
5862
req := Req{}
5963

60-
if err := req.addSchema(protos.SchemaUpdate{Predicate: "name"}); err != nil {
64+
if err := req.AddSchema(protos.SchemaUpdate{Predicate: "name"}); err != nil {
6165
t.Fatal(err)
6266
}
6367

0 commit comments

Comments
 (0)