forked from dgraph-io/dgraph
-
Notifications
You must be signed in to change notification settings - Fork 0
/
parse.go
295 lines (261 loc) · 8.13 KB
/
parse.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
/*
* Copyright 2015 DGraph Labs, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package rdf
import (
"log"
"strconv"
"strings"
"unicode"
farm "github.com/dgryski/go-farm"
"github.com/dgraph-io/dgraph/lex"
"github.com/dgraph-io/dgraph/query/graph"
"github.com/dgraph-io/dgraph/task"
"github.com/dgraph-io/dgraph/types"
"github.com/dgraph-io/dgraph/x"
)
var emptyEdge task.DirectedEdge
// Gets the uid corresponding to an xid from the posting list which stores the
// mapping.
func GetUid(xid string) (uint64, error) {
// If string represents a UID, convert to uint64 and return.
if strings.HasPrefix(xid, "_uid_:") {
return strconv.ParseUint(xid[6:], 0, 64)
}
return farm.Fingerprint64([]byte(xid)), nil
}
type NQuad struct {
*graph.NQuad
}
func typeValFrom(val *graph.Value) types.Val {
switch val.Val.(type) {
case *graph.Value_BytesVal:
return types.Val{types.BinaryID, val.GetBytesVal()}
case *graph.Value_IntVal:
return types.Val{types.Int32ID, val.GetIntVal()}
case *graph.Value_StrVal:
return types.Val{types.StringID, val.GetStrVal()}
case *graph.Value_BoolVal:
return types.Val{types.BoolID, val.GetBoolVal()}
case *graph.Value_DoubleVal:
return types.Val{types.FloatID, val.GetDoubleVal()}
case *graph.Value_GeoVal:
return types.Val{types.GeoID, val.GetGeoVal()}
case *graph.Value_DateVal:
return types.Val{types.DateID, val.GetDateVal()}
case *graph.Value_DatetimeVal:
return types.Val{types.DateTimeID, val.GetDatetimeVal()}
}
return types.Val{types.StringID, ""}
}
func byteVal(nq NQuad) ([]byte, 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.DateID || p.Tid == types.DateTimeID {
return p.Value.([]byte), nil
}
p1 := types.ValueForType(types.BinaryID)
if err := types.Marshal(p, &p1); err != nil {
return []byte{}, err
}
return []byte(p1.Value.([]byte)), nil
}
// ToEdge is useful when you want to find the UID corresponding to XID for
// just one edge. The method doesn't automatically generate a UID for an XID.
func (nq NQuad) ToEdge() (*task.DirectedEdge, error) {
sid, err := GetUid(nq.Subject)
if err != nil {
return &emptyEdge, err
}
out := &task.DirectedEdge{
Attr: nq.Predicate,
Label: nq.Label,
Entity: sid,
}
// An edge can have an id or a value.
if len(nq.ObjectId) > 0 {
oid, err := GetUid(nq.ObjectId)
if err != nil {
return &emptyEdge, err
}
out.ValueId = oid
} else {
if out.Value, err = byteVal(nq); err != nil {
return &emptyEdge, err
}
out.ValueType = uint32(nq.ObjectType)
}
return out, nil
}
func toUid(xid string, newToUid map[string]uint64) (uid uint64, rerr error) {
if id, present := newToUid[xid]; present {
return id, nil
}
return GetUid(xid)
}
// ToEdgeUsing determines the UIDs for the provided XIDs and populates the
// xidToUid map.
func (nq NQuad) ToEdgeUsing(newToUid map[string]uint64) (*task.DirectedEdge, error) {
uid, err := toUid(nq.Subject, newToUid)
if err != nil {
return &emptyEdge, err
}
out := &task.DirectedEdge{
Entity: uid,
Attr: nq.Predicate,
Label: nq.Label,
}
if len(nq.ObjectId) == 0 {
if out.Value, err = byteVal(nq); err != nil {
return &emptyEdge, err
}
out.ValueType = uint32(nq.ObjectType)
} else {
uid, err = toUid(nq.ObjectId, newToUid)
if err != nil {
return &emptyEdge, err
}
out.ValueId = uid
}
return out, nil
}
// This function is used to extract an IRI from an IRIREF.
func stripBracketsAndTrim(val string) string {
if val[0] != '<' && val[len(val)-1] != '>' {
return strings.Trim(val, " ")
}
return strings.Trim(val[1:len(val)-1], " ")
}
// Function to do sanity check for subject, predicate, object and label strings.
func sane(s string) bool {
// Label and ObjectId can be "", we already check that subject and predicate
// shouldn't be empty.
if len(s) == 0 {
return true
}
// s should have atleast one alphanumeric character.
for _, r := range s {
if unicode.IsLetter(r) || unicode.IsDigit(r) {
return true
}
}
return false
}
// Parse parses a mutation string and returns the NQuad representation for it.
func Parse(line string) (rnq graph.NQuad, rerr error) {
l := &lex.Lexer{}
l.Init(line)
go run(l)
var oval string
var vend bool
// We read items from the l.Items channel to which the lexer sends items.
for item := range l.Items {
switch item.Typ {
case itemSubject:
rnq.Subject = stripBracketsAndTrim(item.Val)
case itemPredicate:
rnq.Predicate = stripBracketsAndTrim(item.Val)
case itemObject:
rnq.ObjectId = stripBracketsAndTrim(item.Val)
case itemLiteral:
oval = item.Val
if oval == "" {
oval = "_nil_"
}
case itemLanguage:
rnq.Predicate += "." + item.Val
case itemObjectType:
if len(oval) == 0 {
log.Fatalf(
"itemObject should be emitted before itemObjectType. Input: [%s]",
line)
}
val := stripBracketsAndTrim(item.Val)
// TODO: Check if this condition is required.
if strings.Trim(val, " ") == "*" {
return rnq, x.Errorf("itemObject can't be *")
}
if t, ok := typeMap[val]; ok {
if oval == "_nil_" && t != types.StringID {
return rnq, x.Errorf("Invalid ObjectValue")
}
rnq.ObjectType = int32(t)
p := types.ValueForType(t)
src := types.ValueForType(types.StringID)
src.Value = []byte(oval)
err := types.Convert(src, &p)
if err != nil {
return rnq, err
}
if rnq.ObjectValue, err = types.ObjectValue(t, p.Value); err != nil {
return rnq, err
}
oval = ""
} else {
oval += "@@" + val
}
case lex.ItemError:
return rnq, x.Errorf(item.Val)
case itemValidEnd:
vend = true
case itemLabel:
rnq.Label = stripBracketsAndTrim(item.Val)
}
}
if !vend {
return rnq, x.Errorf("Invalid end of input. Input: [%s]", line)
}
if len(oval) > 0 {
rnq.ObjectValue = &graph.Value{&graph.Value_StrVal{oval}}
// If no type is specified, we default to string.
rnq.ObjectType = int32(0)
}
if len(rnq.Subject) == 0 || len(rnq.Predicate) == 0 {
return rnq, x.Errorf("Empty required fields in NQuad. Input: [%s]", line)
}
if len(rnq.ObjectId) == 0 && rnq.ObjectValue == nil {
return rnq, x.Errorf("No Object in NQuad. Input: [%s]", line)
}
if !sane(rnq.Subject) || !sane(rnq.Predicate) || !sane(rnq.ObjectId) ||
!sane(rnq.Label) {
return rnq, x.Errorf("NQuad failed sanity check:%+v", rnq)
}
return rnq, nil
}
func isNewline(r rune) bool {
return r == '\n' || r == '\r'
}
var typeMap = map[string]types.TypeID{
"xs:string": types.StringID,
"xs:dateTime": types.DateTimeID,
"xs:date": types.DateID,
"xs:int": types.Int32ID,
"xs:boolean": types.BoolID,
"xs:double": types.FloatID,
"xs:float": types.FloatID,
"geo:geojson": types.GeoID,
"http://www.w3.org/2001/XMLSchema#string": types.StringID,
"http://www.w3.org/2001/XMLSchema#dateTime": types.DateTimeID,
"http://www.w3.org/2001/XMLSchema#date": types.DateID,
"http://www.w3.org/2001/XMLSchema#int": types.Int32ID,
"http://www.w3.org/2001/XMLSchema#boolean": types.BoolID,
"http://www.w3.org/2001/XMLSchema#double": types.FloatID,
"http://www.w3.org/2001/XMLSchema#float": types.FloatID,
"http://www.w3.org/2001/XMLSchema#gYear": types.DateID,
"http://www.w3.org/2001/XMLSchema#gYearMonth": types.DateID,
}