-
Notifications
You must be signed in to change notification settings - Fork 1.5k
/
index.go
1108 lines (976 loc) · 31 KB
/
index.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
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright 2016-2018 Dgraph Labs, Inc. and Contributors
*
* 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 posting
import (
"bytes"
"context"
"encoding/hex"
"fmt"
"io/ioutil"
"math"
"os"
"sync/atomic"
"time"
"github.com/golang/glog"
ostats "go.opencensus.io/stats"
otrace "go.opencensus.io/trace"
"github.com/dgraph-io/badger/v2"
"github.com/dgraph-io/badger/v2/options"
bpb "github.com/dgraph-io/badger/v2/pb"
"github.com/dgraph-io/dgraph/protos/pb"
"github.com/dgraph-io/dgraph/schema"
"github.com/dgraph-io/dgraph/tok"
"github.com/dgraph-io/dgraph/types"
"github.com/dgraph-io/dgraph/x"
"github.com/pkg/errors"
)
var emptyCountParams countParams
type indexMutationInfo struct {
tokenizers []tok.Tokenizer
edge *pb.DirectedEdge // Represents the original uid -> value edge.
val types.Val
op pb.DirectedEdge_Op
}
// indexTokensforTokenizers return tokens, without the predicate prefix and
// index rune, for specific tokenizers.
func indexTokens(info *indexMutationInfo) ([]string, error) {
attr := info.edge.Attr
lang := info.edge.GetLang()
schemaType, err := schema.State().TypeOf(attr)
if err != nil || !schemaType.IsScalar() {
return nil, errors.Errorf("Cannot index attribute %s of type object.", attr)
}
if !schema.State().IsIndexed(attr) {
return nil, errors.Errorf("Attribute %s is not indexed.", attr)
}
sv, err := types.Convert(info.val, schemaType)
if err != nil {
return nil, err
}
var tokens []string
for _, it := range info.tokenizers {
toks, err := tok.BuildTokens(sv.Value, tok.GetLangTokenizer(it, lang))
if err != nil {
return tokens, err
}
tokens = append(tokens, toks...)
}
return tokens, nil
}
// addIndexMutations adds mutation(s) for a single term, to maintain the index,
// but only for the given tokenizers.
// TODO - See if we need to pass op as argument as t should already have Op.
func (txn *Txn) addIndexMutations(ctx context.Context, info *indexMutationInfo) error {
if info.tokenizers == nil {
info.tokenizers = schema.State().Tokenizer(info.edge.Attr)
}
attr := info.edge.Attr
uid := info.edge.Entity
x.AssertTrue(uid != 0)
tokens, err := indexTokens(info)
if err != nil {
// This data is not indexable
return err
}
// Create a value token -> uid edge.
edge := &pb.DirectedEdge{
ValueId: uid,
Attr: attr,
Op: info.op,
}
for _, token := range tokens {
if err := txn.addIndexMutation(ctx, edge, token); err != nil {
return err
}
}
return nil
}
func (txn *Txn) addIndexMutation(ctx context.Context, edge *pb.DirectedEdge,
token string) error {
key := x.IndexKey(edge.Attr, token)
plist, err := txn.cache.GetFromDelta(key)
if err != nil {
return err
}
x.AssertTrue(plist != nil)
if err = plist.addMutation(ctx, txn, edge); err != nil {
return err
}
ostats.Record(ctx, x.NumEdges.M(1))
return nil
}
// countParams is sent to updateCount function. It is used to update the count index.
// It deletes the uid from the key corresponding to <attr, countBefore> and adds it
// to <attr, countAfter>.
type countParams struct {
attr string
countBefore int
countAfter int
entity uint64
reverse bool
}
func (txn *Txn) addReverseMutationHelper(ctx context.Context, plist *List,
hasCountIndex bool, edge *pb.DirectedEdge) (countParams, error) {
countBefore, countAfter := 0, 0
if hasCountIndex {
countBefore = plist.Length(txn.StartTs, 0)
if countBefore == -1 {
return emptyCountParams, ErrTsTooOld
}
}
if err := plist.addMutation(ctx, txn, edge); err != nil {
return emptyCountParams, err
}
if hasCountIndex {
countAfter = plist.Length(txn.StartTs, 0)
if countAfter == -1 {
return emptyCountParams, ErrTsTooOld
}
return countParams{
attr: edge.Attr,
countBefore: countBefore,
countAfter: countAfter,
entity: edge.Entity,
reverse: true,
}, nil
}
return emptyCountParams, nil
}
func (txn *Txn) addReverseMutation(ctx context.Context, t *pb.DirectedEdge) error {
key := x.ReverseKey(t.Attr, t.ValueId)
hasCountIndex := schema.State().HasCount(t.Attr)
var getFn func(key []byte) (*List, error)
if hasCountIndex {
// We need to retrieve the full posting list from disk, to allow us to get the length of the
// posting list for the counts.
getFn = txn.Get
} else {
// We are just adding a reverse edge. No need to read the list from disk.
getFn = txn.GetFromDelta
}
plist, err := getFn(key)
if err != nil {
return err
}
x.AssertTrue(plist != nil)
// We must create a copy here.
edge := &pb.DirectedEdge{
Entity: t.ValueId,
ValueId: t.Entity,
Attr: t.Attr,
Op: t.Op,
Facets: t.Facets,
}
cp, err := txn.addReverseMutationHelper(ctx, plist, hasCountIndex, edge)
if err != nil {
return err
}
ostats.Record(ctx, x.NumEdges.M(1))
if hasCountIndex && cp.countAfter != cp.countBefore {
if err := txn.updateCount(ctx, cp); err != nil {
return err
}
}
return nil
}
func (l *List) handleDeleteAll(ctx context.Context, edge *pb.DirectedEdge,
txn *Txn) error {
isReversed := schema.State().IsReversed(edge.Attr)
isIndexed := schema.State().IsIndexed(edge.Attr)
hasCount := schema.State().HasCount(edge.Attr)
delEdge := &pb.DirectedEdge{
Attr: edge.Attr,
Op: edge.Op,
Entity: edge.Entity,
}
// To calculate length of posting list. Used for deletion of count index.
var plen int
err := l.Iterate(txn.StartTs, 0, func(p *pb.Posting) error {
plen++
switch {
case isReversed:
// Delete reverse edge for each posting.
delEdge.ValueId = p.Uid
return txn.addReverseMutation(ctx, delEdge)
case isIndexed:
// Delete index edge of each posting.
val := types.Val{
Tid: types.TypeID(p.ValType),
Value: p.Value,
}
return txn.addIndexMutations(ctx, &indexMutationInfo{
tokenizers: schema.State().Tokenizer(edge.Attr),
edge: edge,
val: val,
op: pb.DirectedEdge_DEL,
})
default:
return nil
}
})
if err != nil {
return err
}
if hasCount {
// Delete uid from count index. Deletion of reverses is taken care by addReverseMutation
// above.
if err := txn.updateCount(ctx, countParams{
attr: edge.Attr,
countBefore: plen,
countAfter: 0,
entity: edge.Entity,
}); err != nil {
return err
}
}
return l.addMutation(ctx, txn, edge)
}
func (txn *Txn) addCountMutation(ctx context.Context, t *pb.DirectedEdge, count uint32,
reverse bool) error {
key := x.CountKey(t.Attr, count, reverse)
plist, err := txn.cache.GetFromDelta(key)
if err != nil {
return err
}
x.AssertTruef(plist != nil, "plist is nil [%s] %d",
t.Attr, t.ValueId)
if err = plist.addMutation(ctx, txn, t); err != nil {
return err
}
ostats.Record(ctx, x.NumEdges.M(1))
return nil
}
func (txn *Txn) updateCount(ctx context.Context, params countParams) error {
edge := pb.DirectedEdge{
ValueId: params.entity,
Attr: params.attr,
Op: pb.DirectedEdge_DEL,
}
if err := txn.addCountMutation(ctx, &edge, uint32(params.countBefore),
params.reverse); err != nil {
return err
}
if params.countAfter > 0 {
edge.Op = pb.DirectedEdge_SET
if err := txn.addCountMutation(ctx, &edge, uint32(params.countAfter),
params.reverse); err != nil {
return err
}
}
return nil
}
func (txn *Txn) addMutationHelper(ctx context.Context, l *List, doUpdateIndex bool,
hasCountIndex bool, t *pb.DirectedEdge) (types.Val, bool, countParams, error) {
var val types.Val
var found bool
var err error
t1 := time.Now()
l.Lock()
defer l.Unlock()
if dur := time.Since(t1); dur > time.Millisecond {
span := otrace.FromContext(ctx)
span.Annotatef([]otrace.Attribute{otrace.BoolAttribute("slow-lock", true)},
"Acquired lock %v %v %v", dur, t.Attr, t.Entity)
}
if doUpdateIndex {
// Check original value BEFORE any mutation actually happens.
val, found, err = l.findValue(txn.StartTs, fingerprintEdge(t))
if err != nil {
return val, found, emptyCountParams, err
}
}
// If the predicate schema is not a list, ignore delete triples whose object is not a star or
// a value that does not match the existing value.
if !schema.State().IsList(t.Attr) && t.Op == pb.DirectedEdge_DEL && string(t.Value) != x.Star {
newPost := NewPosting(t)
pFound, currPost, err := l.findPosting(txn.StartTs, fingerprintEdge(t))
if err != nil {
return val, found, emptyCountParams, err
}
// This is a scalar value of non-list type and a delete edge mutation, so if the value
// given by the user doesn't match the value we have, we return found to be false, to avoid
// deleting the uid from index posting list.
// This second check is required because we fingerprint the scalar values as math.MaxUint64,
// so even though they might be different the check in the doUpdateIndex block above would
// return found to be true.
if pFound && !(bytes.Equal(currPost.Value, newPost.Value) &&
types.TypeID(currPost.ValType) == types.TypeID(newPost.ValType)) {
return val, false, emptyCountParams, nil
}
}
countBefore, countAfter := 0, 0
if hasCountIndex {
countBefore = l.length(txn.StartTs, 0)
if countBefore == -1 {
return val, found, emptyCountParams, ErrTsTooOld
}
}
if err = l.addMutationInternal(ctx, txn, t); err != nil {
return val, found, emptyCountParams, err
}
if hasCountIndex {
countAfter = l.length(txn.StartTs, 0)
if countAfter == -1 {
return val, found, emptyCountParams, ErrTsTooOld
}
return val, found, countParams{
attr: t.Attr,
countBefore: countBefore,
countAfter: countAfter,
entity: t.Entity,
}, nil
}
return val, found, emptyCountParams, nil
}
// AddMutationWithIndex is addMutation with support for indexing. It also
// supports reverse edges.
func (l *List) AddMutationWithIndex(ctx context.Context, edge *pb.DirectedEdge,
txn *Txn) error {
if len(edge.Attr) == 0 {
return errors.Errorf("Predicate cannot be empty for edge with subject: [%v], object: [%v]"+
" and value: [%v]", edge.Entity, edge.ValueId, edge.Value)
}
if edge.Op == pb.DirectedEdge_DEL && string(edge.Value) == x.Star {
return l.handleDeleteAll(ctx, edge, txn)
}
doUpdateIndex := pstore != nil && schema.State().IsIndexed(edge.Attr)
hasCountIndex := schema.State().HasCount(edge.Attr)
val, found, cp, err := txn.addMutationHelper(ctx, l, doUpdateIndex, hasCountIndex, edge)
if err != nil {
return err
}
ostats.Record(ctx, x.NumEdges.M(1))
if hasCountIndex && cp.countAfter != cp.countBefore {
if err := txn.updateCount(ctx, cp); err != nil {
return err
}
}
if doUpdateIndex {
// Exact matches.
if found && val.Value != nil {
if err := txn.addIndexMutations(ctx, &indexMutationInfo{
tokenizers: schema.State().Tokenizer(edge.Attr),
edge: edge,
val: val,
op: pb.DirectedEdge_DEL,
}); err != nil {
return err
}
}
if edge.Op == pb.DirectedEdge_SET {
val = types.Val{
Tid: types.TypeID(edge.ValueType),
Value: edge.Value,
}
if err := txn.addIndexMutations(ctx, &indexMutationInfo{
tokenizers: schema.State().Tokenizer(edge.Attr),
edge: edge,
val: val,
op: pb.DirectedEdge_SET,
}); err != nil {
return err
}
}
}
// Add reverse mutation irrespective of hasMutated, server crash can happen after
// mutation is synced and before reverse edge is synced
if (pstore != nil) && (edge.ValueId != 0) && schema.State().IsReversed(edge.Attr) {
if err := txn.addReverseMutation(ctx, edge); err != nil {
return err
}
}
return nil
}
// deleteTokensFor deletes the index for the given attribute and token.
func deleteTokensFor(attr, tokenizerName string, hasLang bool) error {
pk := x.ParsedKey{Attr: attr}
prefix := pk.IndexPrefix()
tokenizer, ok := tok.GetTokenizer(tokenizerName)
if !ok {
return errors.Errorf("Could not find valid tokenizer for %s", tokenizerName)
}
if hasLang {
// We just need the tokenizer identifier for ExactTokenizer having language.
// It will be same for all the language.
tokenizer = tok.GetLangTokenizer(tokenizer, "en")
}
prefix = append(prefix, tokenizer.Identifier())
if err := pstore.DropPrefix(prefix); err != nil {
return err
}
// Also delete all the parts of any list that has been split into multiple parts.
// Such keys have a different prefix (the last byte is set to 1).
prefix = pk.IndexPrefix()
prefix[len(prefix)-1] = x.ByteSplit
prefix = append(prefix, tokenizer.Identifier())
return pstore.DropPrefix(prefix)
}
func deleteReverseEdges(attr string) error {
pk := x.ParsedKey{Attr: attr}
prefix := pk.ReversePrefix()
if err := pstore.DropPrefix(prefix); err != nil {
return err
}
// Also delete all the parts of any list that has been split into multiple parts.
// Such keys have a different prefix (the last byte is set to 1).
prefix = pk.ReversePrefix()
prefix[len(prefix)-1] = x.ByteSplit
return pstore.DropPrefix(prefix)
}
func deleteCountIndex(attr string) error {
pk := x.ParsedKey{Attr: attr}
if err := pstore.DropPrefix(pk.CountPrefix(false)); err != nil {
return err
}
if err := pstore.DropPrefix(pk.CountPrefix(true)); err != nil {
return err
}
// Also delete all the parts of any list that has been split into multiple parts.
// Such keys have a different prefix (the last byte is set to 1).
prefix := pk.CountPrefix(false)
prefix[len(prefix)-1] = x.ByteSplit
if err := pstore.DropPrefix(prefix); err != nil {
return err
}
prefix = pk.CountPrefix(true)
prefix[len(prefix)-1] = x.ByteSplit
return pstore.DropPrefix(prefix)
}
// rebuilder handles the process of rebuilding an index.
type rebuilder struct {
attr string
prefix []byte
startTs uint64
// The posting list passed here is the on disk version. It is not coming
// from the LRU cache.
fn func(uid uint64, pl *List, txn *Txn) error
}
func (r *rebuilder) Run(ctx context.Context) error {
// All the temp indexes go into the following directory. We delete the whole
// directory after the indexing step is complete. This deletes any other temp
// indexes that may have been left around in case defer wasn't executed.
tmpParentDir := "dgraph_index"
// We write the index in a temporary badger first and then,
// merge entries before writing them to p directory.
if err := os.MkdirAll(tmpParentDir, os.ModePerm); err != nil {
return errors.Wrap(err, "error creating in temp dir for reindexing")
}
tmpIndexDir, err := ioutil.TempDir(tmpParentDir, "")
if err != nil {
return errors.Wrap(err, "error creating temp dir for reindexing")
}
defer os.RemoveAll(tmpParentDir)
glog.V(1).Infof("Rebuilding indexes using the temp folder %s\n", tmpIndexDir)
dbOpts := badger.DefaultOptions(tmpIndexDir).
WithSyncWrites(false).
WithNumVersionsToKeep(math.MaxInt64).
WithCompression(options.None).
WithEventLogging(false).
WithLogRotatesToFlush(10).
WithMaxCacheSize(50) // TODO(Aman): Disable cache altogether
// TODO(Ibrahim): Remove this once badger is updated.
dbOpts.ZSTDCompressionLevel = 1
tmpDB, err := badger.OpenManaged(dbOpts)
if err != nil {
return errors.Wrap(err, "error opening temp badger for reindexing")
}
defer tmpDB.Close()
glog.V(1).Infof(
"Rebuilding index for predicate %s: Starting process. StartTs=%d. Prefix=\n%s\n",
r.attr, r.startTs, hex.Dump(r.prefix))
// Counter is used here to ensure that all keys are commited at different timestamp.
// We set it to 1 in case there are no keys found and NewStreamAt is called with ts=0.
var counter uint64 = 1
// Todo(Aman): Replace TxnWriter with WriteBatch. While we do that we should ensure that
// WriteBatch has a mechanism for throttling. Also, find other places where TxnWriter
// could be replaced with WriteBatch in the code
// Todo(Aman): Replace TxnWriter with WriteBatch. While we do that we should ensure that
// WriteBatch has a mechanism for throttling. Also, find other places where TxnWriter
// could be replaced with WriteBatch in the code.
tmpWriter := NewTxnWriter(tmpDB)
stream := pstore.NewStreamAt(r.startTs)
stream.LogPrefix = fmt.Sprintf("Rebuilding index for predicate %s (1/2):", r.attr)
stream.Prefix = r.prefix
stream.KeyToList = func(key []byte, itr *badger.Iterator) (*bpb.KVList, error) {
// We should return quickly if the context is no longer valid.
select {
case <-ctx.Done():
return nil, ctx.Err()
default:
}
pk, err := x.Parse(key)
if err != nil {
return nil, errors.Wrapf(err, "could not parse key %s", hex.Dump(key))
}
l, err := ReadPostingList(key, itr)
if err != nil {
return nil, errors.Wrapf(err, "error reading posting list from disk")
}
txn := NewTxn(r.startTs)
if err := r.fn(pk.Uid, l, txn); err != nil {
return nil, err
}
// Convert data into deltas.
txn.Update()
// txn.cache.Lock() is not required because we are the only one making changes to txn.
kvs := make([]*bpb.KV, 0, len(txn.cache.deltas))
for key, data := range txn.cache.deltas {
version := atomic.AddUint64(&counter, 1)
kv := bpb.KV{
Key: []byte(key),
Value: data,
UserMeta: []byte{BitDeltaPosting},
Version: version,
}
kvs = append(kvs, &kv)
}
return &bpb.KVList{Kv: kvs}, nil
}
stream.Send = func(kvList *bpb.KVList) error {
if err := tmpWriter.Write(kvList); err != nil {
return errors.Wrap(err, "error setting entries in temp badger")
}
return nil
}
start := time.Now()
if err := stream.Orchestrate(ctx); err != nil {
return err
}
if err := tmpWriter.Flush(); err != nil {
return err
}
glog.V(1).Infof("Rebuilding index for predicate %s: building temp index took: %v\n",
r.attr, time.Since(start))
// Now we write all the created posting lists to disk.
glog.V(1).Infof("Rebuilding index for predicate %s: writing index to badger", r.attr)
start = time.Now()
defer func() {
glog.V(1).Infof("Rebuilding index for predicate %s: writing index took: %v\n",
r.attr, time.Since(start))
}()
writer := NewTxnWriter(pstore)
tmpStream := tmpDB.NewStreamAt(counter)
tmpStream.LogPrefix = fmt.Sprintf("Rebuilding index for predicate %s (2/2):", r.attr)
tmpStream.KeyToList = func(key []byte, itr *badger.Iterator) (*bpb.KVList, error) {
l, err := ReadPostingList(key, itr)
if err != nil {
return nil, errors.Wrap(err, "error in reading posting list from pstore")
}
// No need to write a loop after ReadPostingList to skip unread entries
// for a given key because we only wrote BitDeltaPosting to temp badger.
kvs, err := l.Rollup()
if err != nil {
return nil, err
}
return &bpb.KVList{Kv: kvs}, nil
}
tmpStream.Send = func(kvList *bpb.KVList) error {
for _, kv := range kvList.Kv {
if len(kv.Value) == 0 {
continue
}
// We choose to write the PL at r.startTs, so it won't be read by txns,
// which occurred before this schema mutation.
if err := writer.SetAt(kv.Key, kv.Value, BitCompletePosting, r.startTs); err != nil {
return errors.Wrap(err, "error in writing index to pstore")
}
}
return nil
}
if err := tmpStream.Orchestrate(ctx); err != nil {
return err
}
glog.V(1).Infof("Rebuilding index for predicate %s: Flushing all writes.\n", r.attr)
return writer.Flush()
}
// IndexRebuild holds the info needed to initiate a rebuilt of the indices.
type IndexRebuild struct {
Attr string
StartTs uint64
OldSchema *pb.SchemaUpdate
CurrentSchema *pb.SchemaUpdate
}
type indexOp int
const (
indexNoop indexOp = iota // Index should be left alone.
indexDelete = iota // Index should be deleted.
indexRebuild = iota // Index should be deleted and rebuilt.
)
// Run rebuilds all indices that need it.
func (rb *IndexRebuild) Run(ctx context.Context) error {
if err := rebuildListType(ctx, rb); err != nil {
return err
}
if err := rebuildIndex(ctx, rb); err != nil {
return err
}
if err := rebuildReverseEdges(ctx, rb); err != nil {
return err
}
return rebuildCountIndex(ctx, rb)
}
type indexRebuildInfo struct {
op indexOp
tokenizersToDelete []string
tokenizersToRebuild []string
}
func (rb *IndexRebuild) needsIndexRebuild() indexRebuildInfo {
x.AssertTruef(rb.CurrentSchema != nil, "Current schema cannot be nil.")
// If the old schema is nil, we can treat it as an empty schema. Copy it
// first to avoid overwriting it in rb.
old := rb.OldSchema
if old == nil {
old = &pb.SchemaUpdate{}
}
currIndex := rb.CurrentSchema.Directive == pb.SchemaUpdate_INDEX
prevIndex := old.Directive == pb.SchemaUpdate_INDEX
// Index does not need to be rebuilt or deleted if the scheme directive
// did not require an index before and now.
if !currIndex && !prevIndex {
return indexRebuildInfo{
op: indexNoop,
}
}
// Index only needs to be deleted if the schema directive changed and the
// new directive does not require an index. Predicate is not checking
// prevIndex since the previous if statement guarantees both values are
// different.
if !currIndex {
return indexRebuildInfo{
op: indexDelete,
tokenizersToDelete: old.Tokenizer,
}
}
// All tokenizers in the index need to be deleted and rebuilt if the value
// types have changed.
if currIndex && rb.CurrentSchema.ValueType != old.ValueType {
return indexRebuildInfo{
op: indexRebuild,
tokenizersToDelete: old.Tokenizer,
tokenizersToRebuild: rb.CurrentSchema.Tokenizer,
}
}
// Index needs to be rebuilt if the tokenizers have changed
prevTokens := make(map[string]struct{})
for _, t := range old.Tokenizer {
prevTokens[t] = struct{}{}
}
currTokens := make(map[string]struct{})
for _, t := range rb.CurrentSchema.Tokenizer {
currTokens[t] = struct{}{}
}
newTokenizers, deletedTokenizers := x.Diff(currTokens, prevTokens)
// If the tokenizers are the same, nothing needs to be done.
if len(newTokenizers) == 0 && len(deletedTokenizers) == 0 {
return indexRebuildInfo{
op: indexNoop,
}
}
return indexRebuildInfo{
op: indexRebuild,
tokenizersToDelete: deletedTokenizers,
tokenizersToRebuild: newTokenizers,
}
}
// rebuildIndex rebuilds index for a given attribute.
// We commit mutations with startTs and ignore the errors.
func rebuildIndex(ctx context.Context, rb *IndexRebuild) error {
// Exit early if indices do not need to be rebuilt.
rebuildInfo := rb.needsIndexRebuild()
if rebuildInfo.op == indexNoop {
return nil
}
glog.Infof("Deleting index for attr %s and tokenizers %s", rb.Attr,
rebuildInfo.tokenizersToDelete)
for _, tokenizer := range rebuildInfo.tokenizersToDelete {
if err := deleteTokensFor(rb.Attr, tokenizer, false); err != nil {
return err
}
if tokenizer != "exact" {
continue
}
if err := deleteTokensFor(rb.Attr, tokenizer, true); err != nil {
return err
}
}
// Exit early if the index only need to be deleted and not rebuilt.
if rebuildInfo.op == indexDelete {
return nil
}
// Exit early if there are no tokenizers to rebuild.
if len(rebuildInfo.tokenizersToRebuild) == 0 {
return nil
}
glog.Infof("Rebuilding index for attr %s and tokenizers %s", rb.Attr,
rebuildInfo.tokenizersToRebuild)
// Before rebuilding, the existing index needs to be deleted.
for _, tokenizer := range rebuildInfo.tokenizersToRebuild {
if err := deleteTokensFor(rb.Attr, tokenizer, false); err != nil {
return err
}
if tokenizer != "exact" {
continue
}
if err := deleteTokensFor(rb.Attr, tokenizer, true); err != nil {
return err
}
}
tokenizers, err := tok.GetTokenizers(rebuildInfo.tokenizersToRebuild)
if err != nil {
return err
}
pk := x.ParsedKey{Attr: rb.Attr}
builder := rebuilder{attr: rb.Attr, prefix: pk.DataPrefix(), startTs: rb.StartTs}
builder.fn = func(uid uint64, pl *List, txn *Txn) error {
edge := pb.DirectedEdge{Attr: rb.Attr, Entity: uid}
return pl.Iterate(txn.StartTs, 0, func(p *pb.Posting) error {
// Add index entries based on p.
val := types.Val{
Value: p.Value,
Tid: types.TypeID(p.ValType),
}
edge.Lang = string(p.LangTag)
for {
err := txn.addIndexMutations(ctx, &indexMutationInfo{
tokenizers: tokenizers,
edge: &edge,
val: val,
op: pb.DirectedEdge_SET,
})
switch err {
case ErrRetry:
time.Sleep(10 * time.Millisecond)
default:
return err
}
}
})
}
return builder.Run(ctx)
}
func (rb *IndexRebuild) needsCountIndexRebuild() indexOp {
x.AssertTruef(rb.CurrentSchema != nil, "Current schema cannot be nil.")
// If the old schema is nil, treat it as an empty schema. Copy it to avoid
// overwriting it in rb.
old := rb.OldSchema
if old == nil {
old = &pb.SchemaUpdate{}
}
// Do nothing if the schema directive did not change.
if rb.CurrentSchema.Count == old.Count {
return indexNoop
}
// If the new schema does not require an index, delete the current index.
if !rb.CurrentSchema.Count {
return indexDelete
}
// Otherwise, the index needs to be rebuilt.
return indexRebuild
}
// rebuildCountIndex rebuilds the count index for a given attribute.
func rebuildCountIndex(ctx context.Context, rb *IndexRebuild) error {
op := rb.needsCountIndexRebuild()
if op == indexNoop {
return nil
}
glog.Infof("Deleting count index for %s", rb.Attr)
if err := deleteCountIndex(rb.Attr); err != nil {
return err
}
// Exit early if attribute is index only needed to be deleted.
if op == indexDelete {
return nil
}
glog.Infof("Rebuilding count index for %s", rb.Attr)
var reverse bool
fn := func(uid uint64, pl *List, txn *Txn) error {
t := &pb.DirectedEdge{
ValueId: uid,
Attr: rb.Attr,
Op: pb.DirectedEdge_SET,
}
sz := pl.Length(rb.StartTs, 0)
if sz == -1 {
return nil
}
for {
err := txn.addCountMutation(ctx, t, uint32(sz), reverse)
switch err {
case ErrRetry:
time.Sleep(10 * time.Millisecond)
default:
return err
}
}
}
// Create the forward index.
pk := x.ParsedKey{Attr: rb.Attr}
builder := rebuilder{attr: rb.Attr, prefix: pk.DataPrefix(), startTs: rb.StartTs}
builder.fn = fn
if err := builder.Run(ctx); err != nil {
return err
}
// Create the reverse index. The count reverse index is created if this
// predicate has both a count and reverse directive in the schema. It's safe
// to call builder.Run even if that's not the case as the reverse prefix
// will be empty.
reverse = true
builder = rebuilder{attr: rb.Attr, prefix: pk.ReversePrefix(), startTs: rb.StartTs}
builder.fn = fn
return builder.Run(ctx)
}
func (rb *IndexRebuild) needsReverseEdgesRebuild() indexOp {
x.AssertTruef(rb.CurrentSchema != nil, "Current schema cannot be nil.")
// If old schema is nil, treat it as an empty schema. Copy it to avoid
// overwriting it in rb.
old := rb.OldSchema
if old == nil {
old = &pb.SchemaUpdate{}
}
currIndex := rb.CurrentSchema.Directive == pb.SchemaUpdate_REVERSE
prevIndex := old.Directive == pb.SchemaUpdate_REVERSE
// If the schema directive did not change, return indexNoop.
if currIndex == prevIndex {
return indexNoop
}
// If the current schema requires an index, index should be rebuilt.
if currIndex {
return indexRebuild
}
// Otherwise, index should only be deleted.
return indexDelete
}
// rebuildReverseEdges rebuilds the reverse edges for a given attribute.
func rebuildReverseEdges(ctx context.Context, rb *IndexRebuild) error {
op := rb.needsReverseEdgesRebuild()
if op == indexNoop {
return nil
}
glog.Infof("Deleting reverse index for %s", rb.Attr)
if err := deleteReverseEdges(rb.Attr); err != nil {
return err
}
// Exit early if index only needed to be deleted.
if op == indexDelete {
return nil
}
glog.Infof("Rebuilding reverse index for %s", rb.Attr)
pk := x.ParsedKey{Attr: rb.Attr}
builder := rebuilder{attr: rb.Attr, prefix: pk.DataPrefix(), startTs: rb.StartTs}
builder.fn = func(uid uint64, pl *List, txn *Txn) error {
edge := pb.DirectedEdge{Attr: rb.Attr, Entity: uid}
return pl.Iterate(txn.StartTs, 0, func(pp *pb.Posting) error {
puid := pp.Uid
// Add reverse entries based on p.
edge.ValueId = puid
edge.Op = pb.DirectedEdge_SET