forked from dgraph-io/dgraph
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mutation.go
396 lines (354 loc) · 9.95 KB
/
mutation.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
/*
* Copyright (C) 2017 Dgraph Labs, Inc. and Contributors
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package gql
import (
"errors"
"strconv"
"github.com/dgraph-io/dgraph/protos"
"github.com/dgraph-io/dgraph/types"
"github.com/dgraph-io/dgraph/x"
)
var (
ErrInvalidUID = errors.New("UID has to be greater than one.")
)
// Mutation stores the strings corresponding to set and delete operations.
type Mutation struct {
Set []*protos.NQuad
Del []*protos.NQuad
DropAll bool
Schema string
}
func (m Mutation) HasVariables() bool {
for _, n := range m.Set {
if HasVariables(n) {
return true
}
}
for _, n := range m.Del {
if HasVariables(n) {
return true
}
}
return false
}
// HasOps returns true iff the mutation has at least one non-empty
// part.
func (m Mutation) HasOps() bool {
return len(m.Set) > 0 || len(m.Del) > 0 || len(m.Schema) > 0 || m.DropAll
}
// NeededVars retrieves NQuads and variable names of NQuads that refer a variable.
func (m Mutation) NeededVars() (res []string) {
addIfVar := func(nq *protos.NQuad) {
if len(nq.SubjectVar) > 0 {
res = append(res, nq.SubjectVar)
}
if len(nq.ObjectVar) > 0 {
res = append(res, nq.ObjectVar)
}
}
for _, s := range m.Set {
addIfVar(s)
}
for _, d := range m.Del {
addIfVar(d)
}
return
}
type NQuads struct {
NQuads []*protos.NQuad
Types []protos.DirectedEdge_Op
}
func WrapNQ(s []*protos.NQuad, typ protos.DirectedEdge_Op) NQuads {
t := make([]protos.DirectedEdge_Op, len(s))
for i := range t {
t[i] = typ
}
return NQuads{NQuads: s, Types: t}
}
func (n *NQuads) SetTypes(t protos.DirectedEdge_Op) {
n.Types = make([]protos.DirectedEdge_Op, len(n.NQuads))
for i := range n.Types {
n.Types[i] = t
}
}
func (n NQuads) IsEmpty() bool {
return len(n.NQuads) == 0
}
func (n NQuads) Add(m NQuads) (res NQuads) {
res.NQuads = n.NQuads
res.Types = n.Types
res.NQuads = append(res.NQuads, m.NQuads...)
res.Types = append(res.Types, m.Types...)
x.AssertTrue(len(res.NQuads) == len(n.NQuads)+len(m.NQuads))
x.AssertTrue(len(res.Types) == len(n.Types)+len(m.Types))
return
}
// Partitions NQuads using given predicate.
func (n NQuads) Partition(by func(*protos.NQuad) bool) (t NQuads, f NQuads) {
t.NQuads = make([]*protos.NQuad, 0, 10)
f.NQuads = make([]*protos.NQuad, 0, 10)
t.Types = make([]protos.DirectedEdge_Op, 0, 10)
f.Types = make([]protos.DirectedEdge_Op, 0, 10)
p := func(nq *protos.NQuad, typ protos.DirectedEdge_Op) {
if by(nq) {
t.NQuads = append(t.NQuads, nq)
t.Types = append(t.Types, typ)
} else {
f.NQuads = append(f.NQuads, nq)
f.Types = append(f.Types, typ)
}
}
for i, s := range n.NQuads {
p(s, n.Types[i])
}
x.AssertTrue(len(t.NQuads)+len(f.NQuads) == len(n.NQuads))
x.AssertTrue(len(t.Types)+len(f.Types) == len(n.Types))
return
}
// HasVariables returns true iff given NQuad refers some variable.
func HasVariables(n *protos.NQuad) bool {
return len(n.SubjectVar) > 0 || len(n.ObjectVar) > 0
}
// Gets the uid corresponding
func ParseUid(xid string) (uint64, error) {
// If string represents a UID, convert to uint64 and return.
uid, err := strconv.ParseUint(xid, 0, 64)
if err != nil {
return 0, err
}
if uid == 0 {
return 0, ErrInvalidUID
}
return uid, nil
}
type NQuad struct {
*protos.NQuad
}
func typeValFrom(val *protos.Value) types.Val {
switch val.Val.(type) {
case *protos.Value_BytesVal:
return types.Val{types.BinaryID, val.GetBytesVal()}
case *protos.Value_IntVal:
return types.Val{types.IntID, val.GetIntVal()}
case *protos.Value_StrVal:
if val.GetStrVal() == "" {
return types.Val{types.StringID, "_nil_"}
}
return types.Val{types.StringID, val.GetStrVal()}
case *protos.Value_BoolVal:
return types.Val{types.BoolID, val.GetBoolVal()}
case *protos.Value_DoubleVal:
return types.Val{types.FloatID, val.GetDoubleVal()}
case *protos.Value_GeoVal:
return types.Val{types.GeoID, val.GetGeoVal()}
case *protos.Value_DatetimeVal:
return types.Val{types.DateTimeID, val.GetDatetimeVal()}
case *protos.Value_PasswordVal:
return types.Val{types.PasswordID, val.GetPasswordVal()}
case *protos.Value_DefaultVal:
if val.GetDefaultVal() == "" {
return types.Val{types.DefaultID, "_nil_"}
}
return types.Val{types.DefaultID, val.GetDefaultVal()}
}
return types.Val{types.StringID, ""}
}
func byteVal(nq NQuad) ([]byte, types.TypeID, error) {
// We infer object type from type of value. We set appropriate type in parse
// function or the Go client has already set.
p := typeValFrom(nq.ObjectValue)
// These three would have already been marshalled to bytes by the client or
// in parse function.
if p.Tid == types.GeoID || p.Tid == types.DateTimeID {
return p.Value.([]byte), p.Tid, nil
}
p1 := types.ValueForType(types.BinaryID)
if err := types.Marshal(p, &p1); err != nil {
return []byte{}, p.Tid, err
}
return []byte(p1.Value.([]byte)), p.Tid, nil
}
func toUid(subject string, newToUid map[string]uint64) (uid uint64, err error) {
x.AssertTrue(len(subject) > 0)
if id, err := ParseUid(subject); err == nil || err == ErrInvalidUID {
return id, err
}
// It's an xid
if id, present := newToUid[subject]; present {
return id, err
}
return 0, x.Errorf("uid not found/generated for xid %s\n", subject)
}
var emptyEdge protos.DirectedEdge
func (nq NQuad) createEdge(subjectUid uint64, newToUid map[string]uint64) (*protos.DirectedEdge, error) {
var err error
var objectUid uint64
out := &protos.DirectedEdge{
Entity: subjectUid,
Attr: nq.Predicate,
Label: nq.Label,
Lang: nq.Lang,
Facets: nq.Facets,
}
switch nq.valueType() {
case x.ValueUid:
objectUid, err = toUid(nq.ObjectId, newToUid)
if err != nil {
return out, err
}
x.AssertTrue(objectUid > 0)
out.ValueId = objectUid
case x.ValuePlain, x.ValueMulti:
if err = copyValue(out, nq); err != nil {
return &emptyEdge, err
}
default:
return &emptyEdge, errors.New("unknow value type")
}
return out, nil
}
func (nq NQuad) createEdgePrototype(subjectUid uint64) *protos.DirectedEdge {
return &protos.DirectedEdge{
Entity: subjectUid,
Attr: nq.Predicate,
Label: nq.Label,
Lang: nq.Lang,
Facets: nq.Facets,
}
}
func (nq NQuad) CreateUidEdge(subjectUid uint64, objectUid uint64) *protos.DirectedEdge {
out := nq.createEdgePrototype(subjectUid)
out.ValueId = objectUid
return out
}
func (nq NQuad) CreateValueEdge(subjectUid uint64) (*protos.DirectedEdge, error) {
var err error
out := nq.createEdgePrototype(subjectUid)
if err = copyValue(out, nq); err != nil {
return &emptyEdge, err
}
return out, nil
}
func (nq NQuad) ToDeletePredEdge() (*protos.DirectedEdge, error) {
if nq.Subject != x.Star && nq.ObjectValue.String() != x.Star {
return &emptyEdge, x.Errorf("Subject and object both should be *. Got: %+v", nq)
}
out := &protos.DirectedEdge{
// This along with edge.ObjectValue == x.Star would indicate
// that we want to delete the predicate.
Entity: 0,
Attr: nq.Predicate,
Label: nq.Label,
Lang: nq.Lang,
Facets: nq.Facets,
}
if err := copyValue(out, nq); err != nil {
return &emptyEdge, err
}
return out, nil
}
// ToEdgeUsing determines the UIDs for the provided XIDs and populates the
// xidToUid map.
func (nq NQuad) ToEdgeUsing(newToUid map[string]uint64) (*protos.DirectedEdge, error) {
var edge *protos.DirectedEdge
sUid, err := toUid(nq.Subject, newToUid)
if err != nil {
return nil, err
}
switch nq.valueType() {
case x.ValueUid:
oUid, err := toUid(nq.ObjectId, newToUid)
if err != nil {
return nil, err
}
edge = nq.CreateUidEdge(sUid, oUid)
case x.ValuePlain, x.ValueMulti:
edge, err = nq.CreateValueEdge(sUid)
default:
return &emptyEdge, x.Errorf("unknown value type for nquad: %+v", nq)
}
if err != nil {
return nil, err
}
return edge, nil
}
func (nq *NQuad) ExpandVariables(newToUid map[string]uint64, subjectUids []uint64,
objectUids []uint64) (edges []*protos.DirectedEdge, err error) {
var edge *protos.DirectedEdge
edges = make([]*protos.DirectedEdge, 0, len(subjectUids)*len(objectUids))
// Empty subject variable.
if len(subjectUids) == 0 {
if len(nq.Subject) == 0 {
return
}
sUid, err := toUid(nq.Subject, newToUid)
if err != nil {
return nil, err
}
subjectUids = []uint64{sUid}[:]
}
// Empty object variable.
if len(objectUids) == 0 {
// No object id given, lets return
if len(nq.ObjectId) == 0 {
if nq.ObjectValue == nil {
return edges, nil
}
} else {
oUid, err := toUid(nq.ObjectId, newToUid)
if err != nil {
return edges, err
}
objectUids = append(objectUids, oUid)
}
}
switch nq.valueType() {
case x.ValueUid:
for _, sUid := range subjectUids {
for _, oUid := range objectUids {
edge = nq.CreateUidEdge(sUid, oUid)
edges = append(edges, edge)
}
}
case x.ValuePlain, x.ValueMulti:
for _, sUid := range subjectUids {
edge, err = nq.CreateValueEdge(sUid)
if err != nil {
return edges, err
}
edges = append(edges, edge)
}
default:
return edges, x.Errorf("unknown value type for nquad: %+v", nq)
}
return edges, nil
}
func copyValue(out *protos.DirectedEdge, nq NQuad) error {
var err error
var t types.TypeID
if out.Value, t, err = byteVal(nq); err != nil {
return err
}
out.ValueType = uint32(t)
return nil
}
func (nq NQuad) valueType() x.ValueTypeInfo {
hasValue := nq.ObjectValue != nil
hasLang := len(nq.Lang) > 0
hasSpecialId := len(nq.ObjectId) == 0 && len(nq.ObjectVar) == 0
return x.ValueType(hasValue, hasLang, hasSpecialId)
}