-
Notifications
You must be signed in to change notification settings - Fork 1.5k
/
list.go
1392 lines (1227 loc) · 37.6 KB
/
list.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 2015-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"
"log"
"math"
"sort"
"github.com/dgryski/go-farm"
bpb "github.com/dgraph-io/badger/v2/pb"
"github.com/dgraph-io/dgraph/algo"
"github.com/dgraph-io/dgraph/codec"
"github.com/dgraph-io/dgraph/dgraph/cmd/zero"
"github.com/dgraph-io/dgraph/protos/pb"
"github.com/dgraph-io/dgraph/schema"
"github.com/dgraph-io/dgraph/types"
"github.com/dgraph-io/dgraph/types/facets"
"github.com/dgraph-io/dgraph/x"
"github.com/pkg/errors"
)
var (
// ErrRetry can be triggered if the posting list got deleted from memory due to a hard commit.
// In such a case, retry.
ErrRetry = errors.New("Temporary error. Please retry")
// ErrNoValue would be returned if no value was found in the posting list.
ErrNoValue = errors.New("No value found")
// ErrStopIteration is returned when an iteration is terminated early.
ErrStopIteration = errors.New("Stop iteration")
emptyPosting = &pb.Posting{}
maxListSize = mb / 2
)
const (
// Set means overwrite in mutation layer. It contributes 0 in Length.
Set uint32 = 0x01
// Del means delete in mutation layer. It contributes -1 in Length.
Del uint32 = 0x02
// BitSchemaPosting signals that the value stores a schema or type.
BitSchemaPosting byte = 0x01
// BitDeltaPosting signals that the value stores the delta of a posting list.
BitDeltaPosting byte = 0x04
// BitCompletePosting signals that the values stores a complete posting list.
BitCompletePosting byte = 0x08
// BitEmptyPosting signals that the value stores an empty posting list.
BitEmptyPosting byte = 0x10
)
// List stores the in-memory representation of a posting list.
type List struct {
x.SafeMutex
key []byte
plist *pb.PostingList
mutationMap map[uint64]*pb.PostingList
minTs uint64 // commit timestamp of immutable layer, reject reads before this ts.
maxTs uint64 // max commit timestamp seen for this list.
}
func (l *List) maxVersion() uint64 {
l.RLock()
defer l.RUnlock()
return l.maxTs
}
type pIterator struct {
l *List
plist *pb.PostingList
uidPosting *pb.Posting
pidx int // index of postings
plen int
dec *codec.Decoder
uids []uint64
uidx int // Offset into the uids slice
afterUid uint64
splitIdx int
// The timestamp of a delete marker in the mutable layer. If this value is greater
// than zero, then the immutable posting list should not be traversed.
deleteBelowTs uint64
}
func (it *pIterator) init(l *List, afterUid, deleteBelowTs uint64) error {
if deleteBelowTs > 0 && deleteBelowTs <= l.minTs {
return errors.Errorf("deleteBelowTs (%d) must be greater than the minTs in the list (%d)",
deleteBelowTs, l.minTs)
}
it.l = l
it.splitIdx = it.selectInitialSplit(afterUid)
if len(it.l.plist.Splits) > 0 {
plist, err := l.readListPart(it.l.plist.Splits[it.splitIdx])
if err != nil {
return err
}
it.plist = plist
} else {
it.plist = l.plist
}
it.afterUid = afterUid
it.deleteBelowTs = deleteBelowTs
if deleteBelowTs > 0 {
// We don't need to iterate over the immutable layer if this is > 0. Returning here would
// mean it.uids is empty and valid() would return false.
return nil
}
it.uidPosting = &pb.Posting{}
it.dec = &codec.Decoder{Pack: it.plist.Pack}
it.uids = it.dec.Seek(it.afterUid, codec.SeekCurrent)
it.uidx = 0
it.plen = len(it.plist.Postings)
it.pidx = sort.Search(it.plen, func(idx int) bool {
p := it.plist.Postings[idx]
return it.afterUid < p.Uid
})
return nil
}
func (it *pIterator) selectInitialSplit(afterUid uint64) int {
if afterUid == 0 {
return 0
}
for i, startUid := range it.l.plist.Splits {
// If startUid == afterUid, the current block should be selected.
if startUid == afterUid {
return i
}
// If this split starts at an UID greater than afterUid, there might be
// elements in the previous split that need to be checked.
if startUid > afterUid {
return i - 1
}
}
// In case no split's startUid is greater or equal than afterUid, start the
// iteration at the start of the last split.
return len(it.l.plist.Splits) - 1
}
// moveToNextPart re-initializes the iterator at the start of the next list part.
func (it *pIterator) moveToNextPart() error {
it.splitIdx++
plist, err := it.l.readListPart(it.l.plist.Splits[it.splitIdx])
if err != nil {
return err
}
it.plist = plist
it.dec = &codec.Decoder{Pack: it.plist.Pack}
// codec.SeekCurrent makes sure we skip returning afterUid during seek.
it.uids = it.dec.Seek(it.afterUid, codec.SeekCurrent)
it.uidx = 0
it.plen = len(it.plist.Postings)
it.pidx = sort.Search(it.plen, func(idx int) bool {
p := it.plist.Postings[idx]
return it.afterUid < p.Uid
})
return nil
}
// moveToNextValidPart moves the iterator to the next part that contains valid data.
// This is used to skip over parts of the list that might not contain postings.
func (it *pIterator) moveToNextValidPart() error {
// Not a multi-part list, the iterator has reached the end of the list.
if len(it.l.plist.Splits) == 0 {
return nil
}
// If there are no more UIDs to iterate over, move to the next part of the
// list that contains valid data.
if len(it.uids) == 0 {
for it.splitIdx <= len(it.l.plist.Splits)-2 {
// moveToNextPart will increment it.splitIdx. Therefore, the for loop must only
// continue until len(splits) - 2.
if err := it.moveToNextPart(); err != nil {
return err
}
if len(it.uids) > 0 {
return nil
}
}
}
return nil
}
func (it *pIterator) next() error {
if it.deleteBelowTs > 0 {
it.uids = nil
return nil
}
it.uidx++
if it.uidx < len(it.uids) {
return nil
}
it.uidx = 0
it.uids = it.dec.Next()
return it.moveToNextValidPart()
}
func (it *pIterator) valid() (bool, error) {
if len(it.uids) > 0 {
return true, nil
}
err := it.moveToNextValidPart()
switch {
case err != nil:
return false, err
case len(it.uids) > 0:
return true, nil
default:
return false, nil
}
}
func (it *pIterator) posting() *pb.Posting {
uid := it.uids[it.uidx]
for it.pidx < it.plen {
p := it.plist.Postings[it.pidx]
if p.Uid > uid {
break
}
if p.Uid == uid {
return p
}
it.pidx++
}
it.uidPosting.Uid = uid
return it.uidPosting
}
// ListOptions is used in List.Uids (in posting) to customize our output list of
// UIDs, for each posting list. It should be pb.to this package.
type ListOptions struct {
ReadTs uint64
AfterUid uint64 // Any UIDs returned must be after this value.
Intersect *pb.List // Intersect results with this list of UIDs.
}
// NewPosting takes the given edge and returns its equivalent representation as a posting.
func NewPosting(t *pb.DirectedEdge) *pb.Posting {
var op uint32
switch t.Op {
case pb.DirectedEdge_SET:
op = Set
case pb.DirectedEdge_DEL:
op = Del
default:
x.Fatalf("Unhandled operation: %+v", t)
}
var postingType pb.Posting_PostingType
switch {
case len(t.Lang) > 0:
postingType = pb.Posting_VALUE_LANG
case t.ValueId == 0:
postingType = pb.Posting_VALUE
default:
postingType = pb.Posting_REF
}
p := &pb.Posting{
Uid: t.ValueId,
Value: t.Value,
ValType: t.ValueType,
PostingType: postingType,
LangTag: []byte(t.Lang),
Label: t.Label,
Op: op,
Facets: t.Facets,
}
return p
}
func hasDeleteAll(mpost *pb.Posting) bool {
return mpost.Op == Del && bytes.Equal(mpost.Value, []byte(x.Star)) && len(mpost.LangTag) == 0
}
// Ensure that you either abort the uncommitted postings or commit them before calling me.
func (l *List) updateMutationLayer(mpost *pb.Posting) {
l.AssertLock()
x.AssertTrue(mpost.Op == Set || mpost.Op == Del)
// If we have a delete all, then we replace the map entry with just one.
if hasDeleteAll(mpost) {
plist := &pb.PostingList{}
plist.Postings = append(plist.Postings, mpost)
if l.mutationMap == nil {
l.mutationMap = make(map[uint64]*pb.PostingList)
}
l.mutationMap[mpost.StartTs] = plist
return
}
plist, ok := l.mutationMap[mpost.StartTs]
if !ok {
plist := &pb.PostingList{}
plist.Postings = append(plist.Postings, mpost)
if l.mutationMap == nil {
l.mutationMap = make(map[uint64]*pb.PostingList)
}
l.mutationMap[mpost.StartTs] = plist
return
}
// Even if we have a delete all in this transaction, we should still pick up any updates since.
for i, prev := range plist.Postings {
if prev.Uid == mpost.Uid {
plist.Postings[i] = mpost
return
}
}
plist.Postings = append(plist.Postings, mpost)
}
// TypeID returns the typeid of destination vertex
func TypeID(edge *pb.DirectedEdge) types.TypeID {
if edge.ValueId != 0 {
return types.UidID
}
return types.TypeID(edge.ValueType)
}
func fingerprintEdge(t *pb.DirectedEdge) uint64 {
// There could be a collision if the user gives us a value with Lang = "en" and later gives
// us a value = "en" for the same predicate. We would end up overwritting his older lang
// value.
// All edges with a value without LANGTAG, have the same UID. In other words,
// an (entity, attribute) can only have one untagged value.
var id uint64 = math.MaxUint64
// Value with a lang type.
switch {
case len(t.Lang) > 0:
id = farm.Fingerprint64([]byte(t.Lang))
case schema.State().IsList(t.Attr):
// TODO - When values are deleted for list type, then we should only delete the UID from
// index if no other values produces that index token.
// Value for list type.
id = farm.Fingerprint64(t.Value)
}
return id
}
func (l *List) addMutation(ctx context.Context, txn *Txn, t *pb.DirectedEdge) error {
l.Lock()
defer l.Unlock()
return l.addMutationInternal(ctx, txn, t)
}
func (l *List) addMutationInternal(ctx context.Context, txn *Txn, t *pb.DirectedEdge) error {
l.AssertLock()
if txn.ShouldAbort() {
return zero.ErrConflict
}
getKey := func(key []byte, uid uint64) uint64 {
// Instead of creating a string first and then doing a fingerprint, let's do a fingerprint
// here to save memory allocations.
// Not entirely sure about effect on collision chances due to this simple XOR with uid.
return farm.Fingerprint64(key) ^ uid
}
mpost := NewPosting(t)
mpost.StartTs = txn.StartTs
if mpost.PostingType != pb.Posting_REF {
t.ValueId = fingerprintEdge(t)
mpost.Uid = t.ValueId
}
l.updateMutationLayer(mpost)
if x.WorkerConfig.LudicrousMode {
return nil
}
// We ensure that commit marks are applied to posting lists in the right
// order. We can do so by proposing them in the same order as received by the Oracle delta
// stream from Zero, instead of in goroutines.
var conflictKey uint64
pk, err := x.Parse(l.key)
if err != nil {
return err
}
switch {
case schema.State().HasNoConflict(t.Attr):
break
case schema.State().HasUpsert(t.Attr):
// Consider checking to see if a email id is unique. A user adds:
// <uid> <email> "email@email.org", and there's a string equal tokenizer
// and upsert directive on the schema.
// Then keys are "<email> <uid>" and "<email> email@email.org"
// The first key won't conflict, because two different UIDs can try to
// get the same email id. But, the second key would. Thus, we ensure
// that two users don't set the same email id.
conflictKey = getKey(l.key, 0)
case pk.IsData() && schema.State().IsList(t.Attr):
// Data keys, irrespective of whether they are UID or values, should be judged based on
// whether they are lists or not. For UID, t.ValueId = UID. For value, t.ValueId =
// fingerprint(value) or could be fingerprint(lang) or something else.
//
// For singular uid predicate, like partner: uid // no list.
// a -> b
// a -> c
// Run concurrently, only one of them should succeed.
// But for friend: [uid], both should succeed.
//
// Similarly, name: string
// a -> "x"
// a -> "y"
// This should definitely have a conflict.
// But, if name: [string], then they can both succeed.
conflictKey = getKey(l.key, t.ValueId)
case pk.IsData(): // NOT a list. This case must happen after the above case.
conflictKey = getKey(l.key, 0)
case pk.IsIndex() || pk.IsCountOrCountRev():
// Index keys are by default of type [uid].
conflictKey = getKey(l.key, t.ValueId)
default:
// Don't assign a conflictKey.
}
txn.addConflictKey(conflictKey)
return nil
}
// getMutation returns a marshaled version of posting list mutation stored internally.
func (l *List) getMutation(startTs uint64) []byte {
l.RLock()
defer l.RUnlock()
if pl, ok := l.mutationMap[startTs]; ok {
data, err := pl.Marshal()
x.Check(err)
return data
}
return nil
}
func (l *List) setMutation(startTs uint64, data []byte) {
pl := new(pb.PostingList)
x.Check(pl.Unmarshal(data))
l.Lock()
if l.mutationMap == nil {
l.mutationMap = make(map[uint64]*pb.PostingList)
}
l.mutationMap[startTs] = pl
l.Unlock()
}
// Iterate will allow you to iterate over this posting List, while having acquired a read lock.
// So, please keep this iteration cheap, otherwise mutations would get stuck.
// The iteration will start after the provided UID. The results would not include this uid.
// The function will loop until either the posting List is fully iterated, or you return a false
// in the provided function, which will indicate to the function to break out of the iteration.
//
// pl.Iterate(..., func(p *pb.posting) error {
// // Use posting p
// return nil // to continue iteration.
// return errStopIteration // to break iteration.
// })
func (l *List) Iterate(readTs uint64, afterUid uint64, f func(obj *pb.Posting) error) error {
l.RLock()
defer l.RUnlock()
return l.iterate(readTs, afterUid, f)
}
// pickPostings goes through the mutable layer and returns the appropriate postings,
// along with the timestamp of the delete marker, if any. If this timestamp is greater
// than zero, it indicates that the immutable layer should be ignored during traversals.
// If greater than zero, this timestamp must thus be greater than l.minTs.
func (l *List) pickPostings(readTs uint64) (uint64, []*pb.Posting) {
// This function would return zero ts for entries above readTs.
effective := func(start, commit uint64) uint64 {
if commit > 0 && commit <= readTs {
// Has been committed and below the readTs.
return commit
}
if start == readTs {
// This mutation is by ME. So, I must be able to read it.
return start
}
return 0
}
// First pick up the postings.
var deleteBelowTs uint64
var posts []*pb.Posting
for startTs, plist := range l.mutationMap {
// Pick up the transactions which are either committed, or the one which is ME.
effectiveTs := effective(startTs, plist.CommitTs)
if effectiveTs > deleteBelowTs {
// We're above the deleteBelowTs marker. We wouldn't reach here if effectiveTs is zero.
for _, mpost := range plist.Postings {
if hasDeleteAll(mpost) {
deleteBelowTs = effectiveTs
continue
}
posts = append(posts, mpost)
}
}
}
if deleteBelowTs > 0 {
// There was a delete all marker. So, trim down the list of postings.
result := posts[:0]
for _, post := range posts {
effectiveTs := effective(post.StartTs, post.CommitTs)
if effectiveTs < deleteBelowTs { // Do pick the posts at effectiveTs == deleteBelowTs.
continue
}
result = append(result, post)
}
posts = result
}
// Sort all the postings by UID (inc order), then by commit/startTs in dec order.
sort.Slice(posts, func(i, j int) bool {
pi := posts[i]
pj := posts[j]
if pi.Uid == pj.Uid {
ei := effective(pi.StartTs, pi.CommitTs)
ej := effective(pj.StartTs, pj.CommitTs)
return ei > ej // Pick the higher, so we can discard older commits for the same UID.
}
return pi.Uid < pj.Uid
})
return deleteBelowTs, posts
}
func (l *List) iterate(readTs uint64, afterUid uint64, f func(obj *pb.Posting) error) error {
l.AssertRLock()
deleteBelowTs, mposts := l.pickPostings(readTs)
if readTs < l.minTs {
return errors.Errorf("readTs: %d less than minTs: %d for key: %q", readTs, l.minTs, l.key)
}
midx, mlen := 0, len(mposts)
if afterUid > 0 {
midx = sort.Search(mlen, func(idx int) bool {
mp := mposts[idx]
return afterUid < mp.Uid
})
}
var (
mp, pp *pb.Posting
pitr pIterator
prevUid uint64
err error
)
err = pitr.init(l, afterUid, deleteBelowTs)
if err != nil {
return err
}
loop:
for err == nil {
if midx < mlen {
mp = mposts[midx]
} else {
mp = emptyPosting
}
valid, err := pitr.valid()
switch {
case err != nil:
break loop
case valid:
pp = pitr.posting()
default:
pp = emptyPosting
}
switch {
case mp.Uid > 0 && mp.Uid == prevUid:
// Only pick the latest version of this posting.
// mp.Uid can be zero if it's an empty posting.
midx++
case pp.Uid == 0 && mp.Uid == 0:
// Reached empty posting for both iterators.
return nil
case mp.Uid == 0 || (pp.Uid > 0 && pp.Uid < mp.Uid):
// Either mp is empty, or pp is lower than mp.
err = f(pp)
if err != nil {
break loop
}
if err = pitr.next(); err != nil {
break loop
}
case pp.Uid == 0 || (mp.Uid > 0 && mp.Uid < pp.Uid):
// Either pp is empty, or mp is lower than pp.
if mp.Op != Del {
err = f(mp)
if err != nil {
break loop
}
}
prevUid = mp.Uid
midx++
case pp.Uid == mp.Uid:
if mp.Op != Del {
err = f(mp)
if err != nil {
break loop
}
}
prevUid = mp.Uid
if err = pitr.next(); err != nil {
break loop
}
midx++
default:
log.Fatalf("Unhandled case during iteration of posting list.")
}
}
if err == ErrStopIteration {
return nil
}
return err
}
// IsEmpty returns true if there are no uids at the given timestamp after the given UID.
func (l *List) IsEmpty(readTs, afterUid uint64) (bool, error) {
l.RLock()
defer l.RUnlock()
var count int
err := l.iterate(readTs, afterUid, func(p *pb.Posting) error {
count++
return ErrStopIteration
})
if err != nil {
return false, err
}
return count == 0, nil
}
func (l *List) length(readTs, afterUid uint64) int {
l.AssertRLock()
count := 0
err := l.iterate(readTs, afterUid, func(p *pb.Posting) error {
count++
return nil
})
if err != nil {
return -1
}
return count
}
// Length iterates over the mutation layer and counts number of elements.
func (l *List) Length(readTs, afterUid uint64) int {
l.RLock()
defer l.RUnlock()
return l.length(readTs, afterUid)
}
// Rollup performs the rollup process, merging the immutable and mutable layers
// and outputting the resulting list so it can be written to disk.
// During this process, the list might be split into multiple lists if the main
// list or any of the existing parts become too big.
//
// A normal list has the following format:
// <key> -> <posting list with all the data for this list>
//
// A multi-part list is stored in multiple keys. The keys for the parts will be generated by
// appending the first UID in the part to the key. The list will have the following format:
// <key> -> <posting list that includes no postings but a list of each part's start UID>
// <key, 1> -> <first part of the list with all the data for this part>
// <key, next start UID> -> <second part of the list with all the data for this part>
// ...
// <key, last start UID> -> <last part of the list with all its data>
//
// The first part of a multi-part list always has start UID 1 and will be the last part
// to be deleted, at which point the entire list will be marked for deletion.
// As the list grows, existing parts might be split if they become too big.
func (l *List) Rollup() ([]*bpb.KV, error) {
l.RLock()
defer l.RUnlock()
out, err := l.rollup(math.MaxUint64, true)
if err != nil {
return nil, err
}
if out == nil {
return nil, nil
}
var kvs []*bpb.KV
kv := &bpb.KV{}
kv.Version = out.newMinTs
kv.Key = l.key
val, meta := marshalPostingList(out.plist)
kv.UserMeta = []byte{meta}
kv.Value = val
kvs = append(kvs, kv)
for startUid, plist := range out.parts {
// Any empty posting list would still have BitEmpty set. And the main posting list
// would NOT have that posting list startUid in the splits list.
kv := out.marshalPostingListPart(l.key, startUid, plist)
kvs = append(kvs, kv)
}
return kvs, nil
}
// SingleListRollup works like rollup but generates a single list with no splits.
// It's used during backup so that each backed up posting list is stored in a single key.
func (l *List) SingleListRollup() (*bpb.KV, error) {
l.RLock()
defer l.RUnlock()
out, err := l.rollup(math.MaxUint64, false)
if err != nil {
return nil, err
}
// out is only nil when the list's minTs is greater than readTs but readTs
// is math.MaxUint64 so that's not possible. Assert that's true.
x.AssertTrue(out != nil)
kv := &bpb.KV{}
kv.Version = out.newMinTs
kv.Key = l.key
val, meta := marshalPostingList(out.plist)
kv.UserMeta = []byte{meta}
kv.Value = val
return kv, nil
}
func (out *rollupOutput) marshalPostingListPart(
baseKey []byte, startUid uint64, plist *pb.PostingList) *bpb.KV {
kv := &bpb.KV{}
kv.Version = out.newMinTs
key, err := x.GetSplitKey(baseKey, startUid)
x.Check(err)
kv.Key = key
val, meta := marshalPostingList(plist)
kv.UserMeta = []byte{meta}
kv.Value = val
return kv
}
func marshalPostingList(plist *pb.PostingList) ([]byte, byte) {
if isPlistEmpty(plist) {
return nil, BitEmptyPosting
}
data, err := plist.Marshal()
x.Check(err)
return data, BitCompletePosting
}
const blockSize int = 256
type rollupOutput struct {
plist *pb.PostingList
parts map[uint64]*pb.PostingList
newMinTs uint64
}
// Merge all entries in mutation layer with commitTs <= l.commitTs into
// immutable layer. Note that readTs can be math.MaxUint64, so do NOT use it
// directly. It should only serve as the read timestamp for iteration.
func (l *List) rollup(readTs uint64, split bool) (*rollupOutput, error) {
l.AssertRLock()
// Pick all committed entries
if l.minTs > readTs {
// If we are already past the readTs, then skip the rollup.
return nil, nil
}
out := &rollupOutput{
plist: &pb.PostingList{
Splits: l.plist.Splits,
},
parts: make(map[uint64]*pb.PostingList),
}
var plist *pb.PostingList
var enc codec.Encoder
var startUid, endUid uint64
var splitIdx int
// Method to properly initialize the variables above
// when a multi-part list boundary is crossed.
initializeSplit := func() {
enc = codec.Encoder{BlockSize: blockSize}
// Load the corresponding part and set endUid to correctly detect the end of the list.
startUid = l.plist.Splits[splitIdx]
if splitIdx+1 == len(l.plist.Splits) {
endUid = math.MaxUint64
} else {
endUid = l.plist.Splits[splitIdx+1] - 1
}
plist = &pb.PostingList{}
}
// If not a multi-part list, all UIDs go to the same encoder.
if len(l.plist.Splits) == 0 || !split {
plist = out.plist
endUid = math.MaxUint64
} else {
initializeSplit()
}
err := l.iterate(readTs, 0, func(p *pb.Posting) error {
if p.Uid > endUid && split {
plist.Pack = enc.Done()
out.parts[startUid] = plist
splitIdx++
initializeSplit()
}
enc.Add(p.Uid)
if p.Facets != nil || p.PostingType != pb.Posting_REF || len(p.Label) != 0 {
plist.Postings = append(plist.Postings, p)
}
return nil
})
// Finish writing the last part of the list (or the whole list if not a multi-part list).
x.Check(err)
plist.Pack = enc.Done()
if len(l.plist.Splits) > 0 {
out.parts[startUid] = plist
}
maxCommitTs := l.minTs
{
// We can't rely upon iterate to give us the max commit timestamp, because it can skip over
// postings which had deletions to provide a sorted view of the list. Therefore, the safest
// way to get the max commit timestamp is to pick all the relevant postings for the given
// readTs and calculate the maxCommitTs.
// If deleteBelowTs is greater than zero, there was a delete all marker. The list of
// postings has been trimmed down.
deleteBelowTs, mposts := l.pickPostings(readTs)
maxCommitTs = x.Max(maxCommitTs, deleteBelowTs)
for _, mp := range mposts {
maxCommitTs = x.Max(maxCommitTs, mp.CommitTs)
}
}
if split {
// Check if the list (or any of it's parts if it's been previously split) have
// become too big. Split the list if that is the case.
out.newMinTs = maxCommitTs
out.splitUpList()
out.removeEmptySplits()
} else {
out.plist.Splits = nil
}
return out, nil
}
// ApproxLen returns an approximate count of the UIDs in the posting list.
func (l *List) ApproxLen() int {
l.RLock()
defer l.RUnlock()
return len(l.mutationMap) + codec.ApproxLen(l.plist.Pack)
}
// Uids returns the UIDs given some query params.
// We have to apply the filtering before applying (offset, count).
// WARNING: Calling this function just to get UIDs is expensive
func (l *List) Uids(opt ListOptions) (*pb.List, error) {
// Pre-assign length to make it faster.
l.RLock()
// Use approximate length for initial capacity.
res := make([]uint64, 0, len(l.mutationMap)+codec.ApproxLen(l.plist.Pack))
out := &pb.List{}
if len(l.mutationMap) == 0 && opt.Intersect != nil && len(l.plist.Splits) == 0 {
if opt.ReadTs < l.minTs {
l.RUnlock()
return out, ErrTsTooOld
}
algo.IntersectCompressedWith(l.plist.Pack, opt.AfterUid, opt.Intersect, out)
l.RUnlock()
return out, nil
}
err := l.iterate(opt.ReadTs, opt.AfterUid, func(p *pb.Posting) error {
if p.PostingType == pb.Posting_REF {
res = append(res, p.Uid)
}
return nil
})
l.RUnlock()
if err != nil {
return out, err
}
// Do The intersection here as it's optimized.
out.Uids = res
if opt.Intersect != nil {
algo.IntersectWith(out, opt.Intersect, out)
}
return out, nil
}
// Postings calls postFn with the postings that are common with
// UIDs in the opt ListOptions.
func (l *List) Postings(opt ListOptions, postFn func(*pb.Posting) error) error {
l.RLock()
defer l.RUnlock()
return l.iterate(opt.ReadTs, opt.AfterUid, func(p *pb.Posting) error {
if p.PostingType != pb.Posting_REF {
return nil
}
return postFn(p)
})
}
// AllUntaggedValues returns all the values in the posting list with no language tag.
func (l *List) AllUntaggedValues(readTs uint64) ([]types.Val, error) {
l.RLock()
defer l.RUnlock()
var vals []types.Val
err := l.iterate(readTs, 0, func(p *pb.Posting) error {
if len(p.LangTag) == 0 {
vals = append(vals, types.Val{
Tid: types.TypeID(p.ValType),
Value: p.Value,
})
}
return nil
})
return vals, err
}
// allUntaggedFacets returns facets for all untagged values. Since works well only for
// fetching facets for list predicates as lang tag in not allowed for list predicates.
func (l *List) allUntaggedFacets(readTs uint64) ([]*pb.Facets, error) {
l.AssertRLock()
var facets []*pb.Facets
err := l.iterate(readTs, 0, func(p *pb.Posting) error {
if len(p.LangTag) == 0 {
facets = append(facets, &pb.Facets{Facets: p.Facets})
}
return nil
})
return facets, err
}
// AllValues returns all the values in the posting list.
func (l *List) AllValues(readTs uint64) ([]types.Val, error) {
l.RLock()
defer l.RUnlock()
var vals []types.Val
err := l.iterate(readTs, 0, func(p *pb.Posting) error {
vals = append(vals, types.Val{
Tid: types.TypeID(p.ValType),
Value: p.Value,
})
return nil
})
return vals, err