forked from dgraph-io/dgraph
/
server.go
917 lines (821 loc) · 23.4 KB
/
server.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
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
/*
* 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 edgraph
import (
"bytes"
"encoding/json"
"fmt"
"log"
"math/rand"
"os"
"strconv"
"strings"
"sync"
"time"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
"golang.org/x/net/context"
"golang.org/x/net/trace"
"github.com/dgraph-io/badger"
"github.com/dgraph-io/badger/options"
"github.com/dgraph-io/dgraph/gql"
"github.com/dgraph-io/dgraph/protos/api"
"github.com/dgraph-io/dgraph/protos/intern"
"github.com/dgraph-io/dgraph/query"
"github.com/dgraph-io/dgraph/rdf"
"github.com/dgraph-io/dgraph/schema"
"github.com/dgraph-io/dgraph/types"
"github.com/dgraph-io/dgraph/types/facets"
"github.com/dgraph-io/dgraph/worker"
"github.com/dgraph-io/dgraph/x"
"github.com/dgraph-io/dgraph/y"
"github.com/pkg/errors"
geom "github.com/twpayne/go-geom"
"github.com/twpayne/go-geom/encoding/geojson"
)
type ServerState struct {
FinishCh chan struct{} // channel to wait for all pending reqs to finish.
ShutdownCh chan struct{} // channel to signal shutdown.
Pstore *badger.ManagedDB
WALstore *badger.ManagedDB
vlogTicker *time.Ticker // runs every 1m, check size of vlog and run GC conditionally.
mandatoryVlogTicker *time.Ticker // runs every 10m, we always run vlog GC.
mu sync.Mutex
needTs []chan uint64
notify chan struct{}
}
// TODO(tzdybal) - remove global
var State ServerState
func InitServerState() {
Config.validate()
State.FinishCh = make(chan struct{})
State.ShutdownCh = make(chan struct{})
State.notify = make(chan struct{}, 1)
State.initStorage()
go State.fillTimestampRequests()
}
func (s *ServerState) runVlogGC(store *badger.ManagedDB) {
// Get initial size on start.
_, lastVlogSize := store.Size()
const GB = int64(1 << 30)
for {
select {
case <-s.vlogTicker.C:
_, currentVlogSize := store.Size()
if currentVlogSize < lastVlogSize+GB {
continue
}
// If size increased by 3.5 GB, then we run this 3 times.
numTimes := (currentVlogSize - lastVlogSize) / GB
for i := 0; i < int(numTimes); i++ {
store.RunValueLogGC(0.5)
}
_, lastVlogSize = store.Size()
case <-s.mandatoryVlogTicker.C:
store.RunValueLogGC(0.5)
}
}
}
func (s *ServerState) initStorage() {
// Write Ahead Log directory
x.Checkf(os.MkdirAll(Config.WALDir, 0700), "Error while creating WAL dir.")
kvOpt := badger.DefaultOptions
kvOpt.SyncWrites = true
kvOpt.Dir = Config.WALDir
kvOpt.ValueDir = Config.WALDir
kvOpt.TableLoadingMode = options.MemoryMap
var err error
s.WALstore, err = badger.OpenManaged(kvOpt)
x.Checkf(err, "Error while creating badger KV WAL store")
// Postings directory
// All the writes to posting store should be synchronous. We use batched writers
// for posting lists, so the cost of sync writes is amortized.
x.Check(os.MkdirAll(Config.PostingDir, 0700))
opt := badger.DefaultOptions
opt.SyncWrites = true
opt.Dir = Config.PostingDir
opt.ValueDir = Config.PostingDir
switch Config.PostingTables {
case "memorymap":
opt.TableLoadingMode = options.MemoryMap
case "loadtoram":
opt.TableLoadingMode = options.LoadToRAM
case "fileio":
opt.TableLoadingMode = options.FileIO
default:
x.Fatalf("Invalid Posting Tables options")
}
s.Pstore, err = badger.OpenManaged(opt)
x.Checkf(err, "Error while creating badger KV posting store")
s.vlogTicker = time.NewTicker(1 * time.Minute)
s.mandatoryVlogTicker = time.NewTicker(10 * time.Minute)
go s.runVlogGC(s.Pstore)
go s.runVlogGC(s.WALstore)
}
func (s *ServerState) Dispose() error {
if err := s.Pstore.Close(); err != nil {
return errors.Wrapf(err, "While closing postings store")
}
if err := s.WALstore.Close(); err != nil {
return errors.Wrapf(err, "While closing WAL store")
}
s.vlogTicker.Stop()
s.mandatoryVlogTicker.Stop()
return nil
}
// Server implements protos.DgraphServer
type Server struct{}
// TODO(pawan) - Remove this logic from client after client doesn't have to fetch ts
// for Commit API.
func (s *ServerState) fillTimestampRequests() {
var chs []chan uint64
const (
initDelay = 10 * time.Millisecond
maxDelay = 10 * time.Second
)
delay := initDelay
for range s.notify {
RETRY:
s.mu.Lock()
chs = append(chs, s.needTs...)
s.needTs = s.needTs[:0]
s.mu.Unlock()
if len(chs) == 0 {
continue
}
num := &intern.Num{Val: uint64(len(chs))}
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
ts, err := worker.Timestamps(ctx, num)
cancel()
if err != nil {
log.Printf("Error while retrieving timestamps: %v. Will retry...\n", err)
time.Sleep(delay)
delay *= 2
if delay > maxDelay {
delay = maxDelay
}
goto RETRY
}
delay = initDelay
x.AssertTrue(ts.EndId-ts.StartId+1 == uint64(len(chs)))
for i, ch := range chs {
ch <- ts.StartId + uint64(i)
}
chs = chs[:0]
}
}
func (s *ServerState) getTimestamp() uint64 {
ch := make(chan uint64)
s.mu.Lock()
s.needTs = append(s.needTs, ch)
s.mu.Unlock()
select {
case s.notify <- struct{}{}:
default:
}
return <-ch
}
func (s *Server) Alter(ctx context.Context, op *api.Operation) (*api.Payload, error) {
if op.Schema == "" && op.DropAttr == "" && !op.DropAll {
// Must have at least one field set. This helps users if they attempt
// to set a field but use the wrong name (could be decoded from JSON).
return nil, x.Errorf("Operation must have at least one field set")
}
empty := &api.Payload{}
if err := x.HealthCheck(); err != nil {
if tr, ok := trace.FromContext(ctx); ok {
tr.LazyPrintf("Request rejected %v", err)
}
return empty, err
}
if !isMutationAllowed(ctx) {
return nil, x.Errorf("No mutations allowed.")
}
// StartTs is not needed if the predicate to be dropped lies on this server but is required
// if it lies on some other machine. Let's get it for safety.
m := &intern.Mutations{StartTs: State.getTimestamp()}
if op.DropAll {
m.DropAll = true
_, err := query.ApplyMutations(ctx, m)
return empty, err
}
if len(op.DropAttr) > 0 {
nq := &api.NQuad{
Subject: x.Star,
Predicate: op.DropAttr,
ObjectValue: &api.Value{&api.Value_StrVal{x.Star}},
}
wnq := &gql.NQuad{nq}
edge, err := wnq.ToDeletePredEdge()
if err != nil {
return empty, err
}
edges := []*intern.DirectedEdge{edge}
m.Edges = edges
_, err = query.ApplyMutations(ctx, m)
return empty, err
}
updates, err := schema.Parse(op.Schema)
if err != nil {
return empty, err
}
x.Printf("Got schema: %+v\n", updates)
// TODO: Maybe add some checks about the schema.
m.Schema = updates
_, err = query.ApplyMutations(ctx, m)
return empty, err
}
func (s *Server) Mutate(ctx context.Context, mu *api.Mutation) (resp *api.Assigned, err error) {
resp = &api.Assigned{}
if err := x.HealthCheck(); err != nil {
if tr, ok := trace.FromContext(ctx); ok {
tr.LazyPrintf("Request rejected %v", err)
}
return resp, err
}
if !isMutationAllowed(ctx) {
return nil, x.Errorf("No mutations allowed.")
}
if mu.StartTs == 0 {
mu.StartTs = State.getTimestamp()
}
emptyMutation :=
len(mu.GetSetJson()) == 0 && len(mu.GetDeleteJson()) == 0 &&
len(mu.Set) == 0 && len(mu.Del) == 0 &&
len(mu.SetNquads) == 0 && len(mu.DelNquads) == 0
if emptyMutation {
return resp, fmt.Errorf("empty mutation")
}
if rand.Float64() < worker.Config.Tracing {
var tr trace.Trace
tr, ctx = x.NewTrace("GrpcMutate", ctx)
defer tr.Finish()
}
var l query.Latency
l.Start = time.Now()
gmu, err := parseMutationObject(mu)
if err != nil {
return resp, err
}
parseEnd := time.Now()
l.Parsing = parseEnd.Sub(l.Start)
defer func() {
l.Processing = time.Since(parseEnd)
resp.Latency = &api.Latency{
ParsingNs: uint64(l.Parsing.Nanoseconds()),
ProcessingNs: uint64(l.Processing.Nanoseconds()),
}
}()
newUids, err := query.AssignUids(ctx, gmu.Set)
if err != nil {
return resp, err
}
resp.Uids = query.ConvertUidsToHex(query.StripBlankNode(newUids))
edges, err := query.ToInternal(gmu, newUids)
if err != nil {
return resp, err
}
m := &intern.Mutations{
Edges: edges,
StartTs: mu.StartTs,
}
resp.Context, err = query.ApplyMutations(ctx, m)
if !mu.CommitNow {
if err == y.ErrConflict {
err = status.Errorf(codes.FailedPrecondition, err.Error())
}
return resp, err
}
// The following logic is for committing immediately.
if err != nil {
// ApplyMutations failed. We now want to abort the transaction,
// ignoring any error that might occur during the abort (the user would
// care more about the previous error).
ctxn := resp.Context
ctxn.Aborted = true
_, _ = worker.CommitOverNetwork(ctx, ctxn)
if err == y.ErrConflict {
// We have already aborted the transaction, so the error message should reflect that.
return resp, y.ErrAborted
}
return resp, err
}
tr, ok := trace.FromContext(ctx)
if ok {
tr.LazyPrintf("Prewrites err: %v. Attempting to commit/abort immediately.", err)
}
ctxn := resp.Context
// zero would assign the CommitTs
cts, err := worker.CommitOverNetwork(ctx, ctxn)
if ok {
tr.LazyPrintf("Status of commit at ts: %d: %v", ctxn.StartTs, err)
}
if err != nil {
if err == y.ErrAborted {
err = status.Errorf(codes.Aborted, err.Error())
resp.Context.Aborted = true
}
return resp, err
}
// CommitNow was true, no need to send keys.
resp.Context.Keys = resp.Context.Keys[:0]
resp.Context.CommitTs = cts
return resp, nil
}
// This method is used to execute the query and return the response to the
// client as a protocol buffer message.
func (s *Server) Query(ctx context.Context, req *api.Request) (resp *api.Response, err error) {
if err := x.HealthCheck(); err != nil {
if tr, ok := trace.FromContext(ctx); ok {
tr.LazyPrintf("Request rejected %v", err)
}
return resp, err
}
x.PendingQueries.Add(1)
x.NumQueries.Add(1)
defer x.PendingQueries.Add(-1)
if ctx.Err() != nil {
return resp, ctx.Err()
}
if rand.Float64() < worker.Config.Tracing {
var tr trace.Trace
tr, ctx = x.NewTrace("GrpcQuery", ctx)
defer tr.Finish()
}
resp = new(api.Response)
if len(req.Query) == 0 {
if tr, ok := trace.FromContext(ctx); ok {
tr.LazyPrintf("Empty query")
}
return resp, fmt.Errorf("empty query")
}
if Config.DebugMode {
x.Printf("Received query: %+v\n", req.Query)
}
var l query.Latency
l.Start = time.Now()
if tr, ok := trace.FromContext(ctx); ok {
tr.LazyPrintf("Query received: %v, variables: %v", req.Query, req.Vars)
}
parsedReq, err := gql.Parse(gql.Request{
Str: req.Query,
Variables: req.Vars,
})
if err != nil {
return resp, err
}
if req.StartTs == 0 {
req.StartTs = State.getTimestamp()
}
resp.Txn = &api.TxnContext{
StartTs: req.StartTs,
}
var queryRequest = query.QueryRequest{
Latency: &l,
GqlQuery: &parsedReq,
ReadTs: req.StartTs,
LinRead: req.LinRead,
}
var er query.ExecuteResult
if er, err = queryRequest.Process(ctx); err != nil {
if tr, ok := trace.FromContext(ctx); ok {
tr.LazyPrintf("Error while processing query: %+v", err)
}
return resp, x.Wrap(err)
}
resp.Schema = er.SchemaNode
json, err := query.ToJson(&l, er.Subgraphs)
if err != nil {
if tr, ok := trace.FromContext(ctx); ok {
tr.LazyPrintf("Error while converting to protocol buffer: %+v", err)
}
return resp, err
}
resp.Json = json
gl := &api.Latency{
ParsingNs: uint64(l.Parsing.Nanoseconds()),
ProcessingNs: uint64(l.Processing.Nanoseconds()),
EncodingNs: uint64(l.Json.Nanoseconds()),
}
resp.Latency = gl
resp.Txn.LinRead = queryRequest.LinRead
return resp, err
}
func (s *Server) CommitOrAbort(ctx context.Context, tc *api.TxnContext) (*api.TxnContext,
error) {
if err := x.HealthCheck(); err != nil {
if tr, ok := trace.FromContext(ctx); ok {
tr.LazyPrintf("Request rejected %v", err)
}
return &api.TxnContext{}, err
}
tctx := &api.TxnContext{}
commitTs, err := worker.CommitOverNetwork(ctx, tc)
if err == y.ErrAborted {
tctx.Aborted = true
return tctx, status.Errorf(codes.Aborted, err.Error())
}
tctx.CommitTs = commitTs
return tctx, err
}
func (s *Server) CheckVersion(ctx context.Context, c *api.Check) (v *api.Version, err error) {
if err := x.HealthCheck(); err != nil {
if tr, ok := trace.FromContext(ctx); ok {
tr.LazyPrintf("request rejected %v", err)
}
return v, err
}
v = new(api.Version)
v.Tag = x.Version()
return v, nil
}
//-------------------------------------------------------------------------------------------------
// HELPER FUNCTIONS
//-------------------------------------------------------------------------------------------------
func isMutationAllowed(ctx context.Context) bool {
if !Config.Nomutations {
return true
}
shareAllowed, ok := ctx.Value("_share_").(bool)
if !ok || !shareAllowed {
return false
}
return true
}
func parseFacets(m map[string]interface{}, prefix string) ([]*api.Facet, error) {
// This happens at root.
if prefix == "" {
return nil, nil
}
var facetsForPred []*api.Facet
var fv interface{}
for fname, facetVal := range m {
if facetVal == nil {
continue
}
if !strings.HasPrefix(fname, prefix) {
continue
}
if len(fname) <= len(prefix) {
return nil, x.Errorf("Facet key is invalid: %s", fname)
}
// Prefix includes colon, predicate:
f := &api.Facet{Key: fname[len(prefix):]}
switch v := facetVal.(type) {
case string:
if t, err := types.ParseTime(v); err == nil {
f.ValType = api.Facet_DATETIME
fv = t
} else {
f.ValType = api.Facet_STRING
fv = v
}
case float64:
// Could be int too, but we just store it as float.
fv = v
f.ValType = api.Facet_FLOAT
case bool:
fv = v
f.ValType = api.Facet_BOOL
default:
return nil, x.Errorf("Facet value for key: %s can only be string/float64/bool.",
fname)
}
// convert facet val interface{} to binary
tid := facets.TypeIDFor(&api.Facet{ValType: f.ValType})
fVal := &types.Val{Tid: types.BinaryID}
if err := types.Marshal(types.Val{Tid: tid, Value: fv}, fVal); err != nil {
return nil, err
}
fval, ok := fVal.Value.([]byte)
if !ok {
return nil, x.Errorf("Error while marshalling types.Val into binary.")
}
f.Value = fval
facetsForPred = append(facetsForPred, f)
}
return facetsForPred, nil
}
// This is the response for a map[string]interface{} i.e. a struct.
type mapResponse struct {
nquads []*api.NQuad // nquads at this level including the children.
uid string // uid retrieved or allocated for the node.
fcts []*api.Facet // facets on the edge connecting this node to the source if any.
}
func handleBasicType(k string, v interface{}, op int, nq *api.NQuad) error {
switch v.(type) {
case string:
predWithLang := strings.SplitN(k, "@", 2)
if len(predWithLang) == 2 && predWithLang[0] != "" {
nq.Predicate = predWithLang[0]
nq.Lang = predWithLang[1]
}
// Default value is considered as S P * deletion.
if v == "" && op == delete {
nq.ObjectValue = &api.Value{&api.Value_DefaultVal{x.Star}}
return nil
}
nq.ObjectValue = &api.Value{&api.Value_StrVal{v.(string)}}
case float64:
if v == 0 && op == delete {
nq.ObjectValue = &api.Value{&api.Value_DefaultVal{x.Star}}
return nil
}
nq.ObjectValue = &api.Value{&api.Value_DoubleVal{v.(float64)}}
case bool:
if v == false && op == delete {
nq.ObjectValue = &api.Value{&api.Value_DefaultVal{x.Star}}
return nil
}
nq.ObjectValue = &api.Value{&api.Value_BoolVal{v.(bool)}}
default:
return x.Errorf("Unexpected type for val for attr: %s while converting to nquad", k)
}
return nil
}
func checkForDeletion(mr *mapResponse, m map[string]interface{}, op int) {
// Since uid is the only key, this must be S * * deletion.
if op == delete && len(mr.uid) > 0 && len(m) == 1 {
mr.nquads = append(mr.nquads, &api.NQuad{
Subject: mr.uid,
Predicate: x.Star,
ObjectValue: &api.Value{&api.Value_DefaultVal{x.Star}},
})
}
}
func tryParseAsGeo(b []byte, nq *api.NQuad) (bool, error) {
var g geom.T
err := geojson.Unmarshal(b, &g)
if err == nil {
geo, err := types.ObjectValue(types.GeoID, g)
if err != nil {
return false, x.Errorf("Couldn't convert value: %s to geo type", string(b))
}
nq.ObjectValue = geo
return true, nil
}
return false, nil
}
// TODO - Abstract these parameters to a struct.
func mapToNquads(m map[string]interface{}, idx *int, op int, parentPred string) (mapResponse, error) {
var mr mapResponse
// Check field in map.
if uidVal, ok := m["uid"]; ok {
var uid uint64
if id, ok := uidVal.(float64); ok {
uid = uint64(id)
// We need to check for length of id as empty string would give an error while
// calling ParseUint. We should assign a new uid if len == 0.
} else if id, ok := uidVal.(string); ok && len(id) > 0 {
if ok := strings.HasPrefix(id, "_:"); ok {
mr.uid = id
} else if u, err := strconv.ParseUint(id, 0, 64); err != nil {
return mr, err
} else {
uid = u
}
}
if uid > 0 {
mr.uid = fmt.Sprintf("%d", uid)
}
}
if len(mr.uid) == 0 {
if op == delete {
// Delete operations with a non-nil value must have a uid specified.
return mr, x.Errorf("uid must be present and non-zero while deleting edges.")
}
mr.uid = fmt.Sprintf("_:blank-%d", *idx)
*idx++
}
for pred, v := range m {
// We have already extracted the uid above so we skip that edge.
// v can be nil if user didn't set a value and if omitEmpty was not supplied as JSON
// option.
// We also skip facets here because we parse them with the corresponding predicate.
if pred == "uid" || strings.Index(pred, query.FacetDelimeter) > 0 {
continue
}
if op == delete {
// This corresponds to edge deletion.
if v == nil {
mr.nquads = append(mr.nquads, &api.NQuad{
Subject: mr.uid,
Predicate: pred,
ObjectValue: &api.Value{&api.Value_DefaultVal{x.Star}},
})
continue
}
}
prefix := pred + query.FacetDelimeter
// TODO - Maybe do an initial pass and build facets for all predicates. Then we don't have
// to call parseFacets everytime.
fts, err := parseFacets(m, prefix)
if err != nil {
return mr, err
}
nq := api.NQuad{
Subject: mr.uid,
Predicate: pred,
Facets: fts,
}
if v == nil {
if op == delete {
nq.ObjectValue = &api.Value{&api.Value_DefaultVal{x.Star}}
mr.nquads = append(mr.nquads, &nq)
}
continue
}
switch v.(type) {
case string, float64, bool:
if err := handleBasicType(pred, v, op, &nq); err != nil {
return mr, err
}
mr.nquads = append(mr.nquads, &nq)
case map[string]interface{}:
val := v.(map[string]interface{})
if len(val) == 0 {
continue
}
// Geojson geometry should have type and coordinates.
_, hasType := val["type"]
_, hasCoordinates := val["coordinates"]
if len(val) == 2 && hasType && hasCoordinates {
b, err := json.Marshal(val)
if err != nil {
return mr, x.Errorf("Error while trying to parse "+
"value: %+v as geo val", val)
}
ok, err := tryParseAsGeo(b, &nq)
if err != nil {
return mr, err
}
if ok {
mr.nquads = append(mr.nquads, &nq)
continue
}
}
cr, err := mapToNquads(v.(map[string]interface{}), idx, op, pred)
if err != nil {
return mr, err
}
// Add the connecting edge beteween the entities.
nq.ObjectId = cr.uid
nq.Facets = cr.fcts
mr.nquads = append(mr.nquads, &nq)
// Add the nquads that we got for the connecting entity.
mr.nquads = append(mr.nquads, cr.nquads...)
case []interface{}:
for _, item := range v.([]interface{}) {
nq := api.NQuad{
Subject: mr.uid,
Predicate: pred,
}
switch iv := item.(type) {
case string, float64:
if err := handleBasicType(pred, iv, op, &nq); err != nil {
return mr, err
}
mr.nquads = append(mr.nquads, &nq)
case map[string]interface{}:
cr, err := mapToNquads(iv, idx, op, pred)
if err != nil {
return mr, err
}
nq.ObjectId = cr.uid
nq.Facets = cr.fcts
mr.nquads = append(mr.nquads, &nq)
// Add the nquads that we got for the connecting entity.
mr.nquads = append(mr.nquads, cr.nquads...)
default:
return mr,
x.Errorf("Got unsupported type for list: %s", pred)
}
}
default:
return mr, x.Errorf("Unexpected type for val for attr: %s while converting to nquad", pred)
}
}
fts, err := parseFacets(m, parentPred+query.FacetDelimeter)
mr.fcts = fts
return mr, err
}
const (
set = iota
delete
)
func nquadsFromJson(b []byte, op int) ([]*api.NQuad, error) {
ms := make(map[string]interface{})
var list []interface{}
if err := json.Unmarshal(b, &ms); err != nil {
// Couldn't parse as map, lets try to parse it as a list.
if err = json.Unmarshal(b, &list); err != nil {
return nil, err
}
}
if len(list) == 0 && len(ms) == 0 {
return nil, fmt.Errorf("Couldn't parse json as a map or an array.")
}
var idx int
var nquads []*api.NQuad
if len(list) > 0 {
for _, obj := range list {
if _, ok := obj.(map[string]interface{}); !ok {
return nil, x.Errorf("Only array of map allowed at root.")
}
mr, err := mapToNquads(obj.(map[string]interface{}), &idx, op, "")
if err != nil {
return mr.nquads, err
}
checkForDeletion(&mr, obj.(map[string]interface{}), op)
nquads = append(nquads, mr.nquads...)
}
return nquads, nil
}
mr, err := mapToNquads(ms, &idx, op, "")
checkForDeletion(&mr, ms, op)
return mr.nquads, err
}
func parseNQuads(b []byte) ([]*api.NQuad, error) {
var nqs []*api.NQuad
for _, line := range bytes.Split(b, []byte{'\n'}) {
line = bytes.TrimSpace(line)
nq, err := rdf.Parse(string(line))
if err == rdf.ErrEmpty {
continue
}
if err != nil {
return nil, err
}
nqs = append(nqs, &nq)
}
return nqs, nil
}
func parseMutationObject(mu *api.Mutation) (*gql.Mutation, error) {
res := &gql.Mutation{}
if len(mu.SetJson) > 0 {
nqs, err := nquadsFromJson(mu.SetJson, set)
if err != nil {
return nil, err
}
res.Set = append(res.Set, nqs...)
}
if len(mu.DeleteJson) > 0 {
nqs, err := nquadsFromJson(mu.DeleteJson, delete)
if err != nil {
return nil, err
}
res.Del = append(res.Del, nqs...)
}
if len(mu.SetNquads) > 0 {
nqs, err := parseNQuads(mu.SetNquads)
if err != nil {
return nil, err
}
res.Set = append(res.Set, nqs...)
}
if len(mu.DelNquads) > 0 {
nqs, err := parseNQuads(mu.DelNquads)
if err != nil {
return nil, err
}
res.Del = append(res.Del, nqs...)
}
res.Set = append(res.Set, mu.Set...)
res.Del = append(res.Del, mu.Del...)
return res, validWildcards(res.Set, res.Del)
}
func validWildcards(set, del []*api.NQuad) error {
for _, nq := range set {
var ostar bool
if o, ok := nq.ObjectValue.GetVal().(*api.Value_DefaultVal); ok {
ostar = o.DefaultVal == x.Star
}
if nq.Subject == x.Star || nq.Predicate == x.Star || ostar {
return x.Errorf("Cannot use star in set n-quad: %+v", nq)
}
}
for _, nq := range del {
var ostar bool
if o, ok := nq.ObjectValue.GetVal().(*api.Value_DefaultVal); ok {
ostar = o.DefaultVal == x.Star
}
if nq.Subject == x.Star || (nq.Predicate == x.Star && !ostar) {
return x.Errorf("Only valid wildcard delete patterns are 'S * *' and 'S P *': %v", nq)
}
}
return nil
}