-
Notifications
You must be signed in to change notification settings - Fork 1.5k
/
Copy pathmutation.go
217 lines (190 loc) · 6.07 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
/*
* SPDX-FileCopyrightText: © Hypermode Inc. <[email protected]>
* SPDX-License-Identifier: Apache-2.0
*/
package dql
import (
"strconv"
"github.com/pkg/errors"
"github.com/dgraph-io/dgo/v250/protos/api"
"github.com/hypermodeinc/dgraph/v25/protos/pb"
"github.com/hypermodeinc/dgraph/v25/types"
"github.com/hypermodeinc/dgraph/v25/x"
)
var (
errInvalidUID = errors.New("UID must to be greater than 0")
)
// Mutation stores the strings corresponding to set and delete operations.
type Mutation struct {
Cond string
Set []*api.NQuad
Del []*api.NQuad
AllowedPreds []string
Metadata *pb.Metadata
}
// ParseUid parses the given string into an UID. This method returns with an error
// if the string cannot be parsed or the parsed UID is zero.
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
}
// NQuad is an alias for the NQuad type in the API protobuf library.
type NQuad struct {
*api.NQuad
}
func TypeValFrom(val *api.Value) types.Val {
switch val.Val.(type) {
case *api.Value_BytesVal:
return types.Val{Tid: types.BinaryID, Value: val.GetBytesVal()}
case *api.Value_IntVal:
return types.Val{Tid: types.IntID, Value: val.GetIntVal()}
case *api.Value_StrVal:
return types.Val{Tid: types.StringID, Value: val.GetStrVal()}
case *api.Value_BoolVal:
return types.Val{Tid: types.BoolID, Value: val.GetBoolVal()}
case *api.Value_DoubleVal:
return types.Val{Tid: types.FloatID, Value: val.GetDoubleVal()}
case *api.Value_GeoVal:
return types.Val{Tid: types.GeoID, Value: val.GetGeoVal()}
case *api.Value_BigfloatVal:
return types.Val{Tid: types.BigFloatID, Value: val.GetBigfloatVal()}
case *api.Value_DatetimeVal:
return types.Val{Tid: types.DateTimeID, Value: val.GetDatetimeVal()}
case *api.Value_PasswordVal:
return types.Val{Tid: types.PasswordID, Value: val.GetPasswordVal()}
case *api.Value_Vfloat32Val:
return types.Val{
Tid: types.VFloatID,
Value: types.BytesAsFloatArray(val.GetVfloat32Val()),
}
case *api.Value_DefaultVal:
return types.Val{Tid: types.DefaultID, Value: val.GetDefaultVal()}
}
return types.Val{Tid: types.StringID, Value: ""}
}
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 || p.Tid == types.BigFloatID {
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 p1.Value.([]byte), p.Tid, nil
}
func toUid(subject string, newToUid map[string]uint64) (uid uint64, err error) {
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, errors.Errorf("UID not found/generated for xid %s\n", subject)
}
var emptyEdge pb.DirectedEdge
func (nq NQuad) createEdgePrototype(subjectUid uint64) *pb.DirectedEdge {
return &pb.DirectedEdge{
Entity: subjectUid,
Attr: nq.Predicate,
Namespace: nq.Namespace,
Lang: nq.Lang,
Facets: nq.Facets,
}
}
// CreateUidEdge returns a Directed edge connecting the given subject and object UIDs.
func (nq NQuad) CreateUidEdge(subjectUid uint64, objectUid uint64) *pb.DirectedEdge {
out := nq.createEdgePrototype(subjectUid)
out.ValueId = objectUid
out.ValueType = pb.Posting_UID
return out
}
// CreateValueEdge returns a DirectedEdge with the given subject. The predicate,
// language, and facet values are derived from the NQuad.
func (nq NQuad) CreateValueEdge(subjectUid uint64) (*pb.DirectedEdge, error) {
var err error
out := nq.createEdgePrototype(subjectUid)
if err = copyValue(out, nq); err != nil {
return &emptyEdge, err
}
return out, nil
}
// ToDeletePredEdge takes an NQuad of the form '* p *' and returns the equivalent
// directed edge. Returns an error if the NQuad does not have the expected form.
func (nq NQuad) ToDeletePredEdge() (*pb.DirectedEdge, error) {
if nq.Subject != x.Star && nq.ObjectValue.String() != x.Star {
return &emptyEdge, errors.Errorf("Subject and object both should be *. Got: %+v", nq)
}
out := &pb.DirectedEdge{
// This along with edge.ObjectValue == x.Star would indicate
// that we want to delete the predicate.
Entity: 0,
Attr: nq.Predicate,
Namespace: nq.Namespace,
Lang: nq.Lang,
Facets: nq.Facets,
Op: pb.DirectedEdge_DEL,
}
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) (*pb.DirectedEdge, error) {
var edge *pb.DirectedEdge
sUid, err := toUid(nq.Subject, newToUid)
if err != nil {
return nil, err
}
if sUid == 0 {
return nil, errors.Errorf("Subject should be > 0 for nquad: %+v", nq)
}
switch nq.valueType() {
case x.ValueUid:
oUid, err := toUid(nq.ObjectId, newToUid)
if err != nil {
return nil, err
}
if oUid == 0 {
return nil, errors.Errorf("ObjectId should be > 0 for nquad: %+v", nq)
}
edge = nq.CreateUidEdge(sUid, oUid)
case x.ValuePlain, x.ValueMulti:
edge, err = nq.CreateValueEdge(sUid)
default:
return &emptyEdge, errors.Errorf("Unknown value type for nquad: %+v", nq)
}
if err != nil {
return nil, err
}
return edge, nil
}
func copyValue(out *pb.DirectedEdge, nq NQuad) error {
var err error
var t types.TypeID
if out.Value, t, err = byteVal(nq); err != nil {
return err
}
out.ValueType = t.Enum()
return nil
}
func (nq NQuad) valueType() x.ValueTypeInfo {
hasValue := nq.ObjectValue != nil
hasLang := len(nq.Lang) > 0
hasSpecialId := len(nq.ObjectId) == 0
return x.ValueType(hasValue, hasLang, hasSpecialId)
}