-
Notifications
You must be signed in to change notification settings - Fork 1.5k
/
list.go
1297 lines (1139 loc) · 34.3 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"
"encoding/binary"
"errors"
"fmt"
"log"
"math"
"sort"
"sync/atomic"
"github.com/dgryski/go-farm"
"github.com/golang/glog"
bpb "github.com/dgraph-io/badger/pb"
"github.com/dgraph-io/dgo/protos/api"
"github.com/dgraph-io/dgo/y"
"github.com/dgraph-io/dgraph/algo"
"github.com/dgraph-io/dgraph/codec"
"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"
)
var (
// ErrRetry can be triggered if the posting list got deleted from memory due to a hard commit.
// In such a case, retry.
ErrRetry = fmt.Errorf("Temporary error. Please retry")
// ErrNoValue would be returned if no value was found in the posting list.
ErrNoValue = fmt.Errorf("No value found")
ErrInvalidTxn = fmt.Errorf("Invalid transaction")
ErrStopIteration = errors.New("Stop iteration")
emptyPosting = &pb.Posting{}
maxListSize = 2000000
)
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
// Metadata Bit which is stored to find out whether the stored value is pl or byte slice.
BitSchemaPosting byte = 0x01
BitDeltaPosting byte = 0x04
BitCompletePosting byte = 0x08
BitEmptyPosting byte = 0x10
)
type List struct {
x.SafeMutex
key []byte
plist *pb.PostingList
parts []*pb.PostingList // If a multi-part list, the parts will be stored here.
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.
pendingTxns int32 // Using atomic for this, to avoid locking in SetForDeletion operation.
deleteMe int32 // Using atomic for this, to avoid expensive SetForDeletion operation.
}
func getNextPartKey(baseKey []byte, nextPartStart uint64) []byte {
keyCopy := make([]byte, len(baseKey))
copy(keyCopy, baseKey)
encNexStart := make([]byte, 8)
binary.BigEndian.PutUint64(encNexStart, nextPartStart)
keyCopy = append(keyCopy, encNexStart...)
return keyCopy
}
func appendNextStartToKey(key []byte, nextPartStart uint64) []byte {
keyCopy := make([]byte, len(key))
copy(keyCopy, key)
encNexStart := make([]byte, 8)
binary.BigEndian.PutUint64(encNexStart, nextPartStart)
keyCopy = append(keyCopy, encNexStart...)
return keyCopy
}
func replaceNextStartInKey(key []byte, nextPartStart uint64) []byte {
keyCopy := make([]byte, len(key))
copy(keyCopy, key)
rest := keyCopy[len(keyCopy)-8:]
binary.BigEndian.PutUint64(rest, nextPartStart)
return keyCopy
}
func (l *List) getNextPartKey() []byte {
if l.plist != nil {
return nil
}
if !l.plist.MultiPart {
return nil
}
if l.plist.EndUid == math.MaxUint64 {
return nil
}
if l.plist.FirstPart {
// Add the start of the next list to the end of the key.
return appendNextStartToKey(l.key, l.plist.EndUid+1)
}
// In this case, the key already includes the extra bytes to encode the start
// UID of this part. Replace those bytes with the start UID of the next part.
return replaceNextStartInKey(l.key, l.plist.EndUid+1)
}
func generateNextPartKey(currKey []byte, currPl *pb.PostingList, nextPartStart uint64) []byte {
appendToKey := currPl.FirstPart || !currPl.MultiPart
if appendToKey {
return appendNextStartToKey(currKey, nextPartStart)
}
return replaceNextStartInKey(currKey, nextPartStart)
}
func (l *List) maxVersion() uint64 {
l.RLock()
defer l.RUnlock()
return l.maxTs
}
type PIterator struct {
l *List
plist *pb.PostingList
opts PItrOpts
uidPosting *pb.Posting
pidx int // index of postings
plen int
firstPart bool
dec *codec.Decoder
uids []uint64
uidx int // Offset into the uids slice
partIndex int // Offset into the parts list (if a multi-part list).
}
type PItrOpts struct {
discardPl bool
afterUid uint64
partialIteration bool
startPart int
readTs uint64
}
func (it *PIterator) Init(l *List, opts PItrOpts) {
if l.plist.MultiPart {
it.plist = l.parts[opts.startPart]
} else {
it.plist = l.plist
}
it.l = l
it.opts = opts
it.uidPosting = &pb.Posting{}
it.dec = &codec.Decoder{Pack: it.plist.Pack}
it.uids = it.dec.Seek(opts.afterUid)
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.opts.afterUid < p.Uid
})
}
func (it *PIterator) Next() error {
if it.opts.discardPl {
return nil
}
it.uidx++
if it.uidx < len(it.uids) {
return nil
}
it.uidx = 0
it.uids = it.dec.Next()
return nil
}
func (it *PIterator) Valid() bool {
if it.opts.discardPl {
return false
}
if len(it.uids) > 0 {
return true
}
// Only iterate through this part of the list.
if it.opts.partialIteration {
return false
}
// Not a multi-part list, so nothing else to iterate through
if !it.l.plist.MultiPart {
return false
}
// No more parts to iterate through
if len(it.l.parts) == it.opts.startPart+1 {
return false
}
it.opts.startPart++
it.Init(it.l, it.opts)
// TODO: corner case, what if next part is empty but the one after that is not.
return len(it.uids) > 0
}
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 UID returned must be after this value.
Intersect *pb.List // Intersect results with this list of UIDs.
}
func NewPosting(t *pb.DirectedEdge) *pb.Posting {
var op uint32
if t.Op == pb.DirectedEdge_SET {
op = Set
} else if t.Op == pb.DirectedEdge_DEL {
op = Del
} else {
x.Fatalf("Unhandled operation: %+v", t)
}
var postingType pb.Posting_PostingType
if len(t.Lang) > 0 {
postingType = pb.Posting_VALUE_LANG
} else if t.ValueId == 0 {
postingType = pb.Posting_VALUE
} else {
postingType = pb.Posting_REF
}
return &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,
}
}
// SetForDeletion will mark this List to be deleted, so no more mutations can be applied to this.
// Ensure that we don't acquire any locks during a call to this function, so the LRU cache can
// proceed smoothly.
func (l *List) SetForDeletion() bool {
if atomic.LoadInt32(&l.pendingTxns) > 0 {
return false
}
atomic.StoreInt32(&l.deleteMe, 1)
return true
}
func hasDeleteAll(mpost *pb.Posting) bool {
return mpost.Op == Del && bytes.Equal(mpost.Value, []byte(x.Star))
}
// 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)
l.mutationMap[mpost.StartTs] = plist
return
}
plist, ok := l.mutationMap[mpost.StartTs]
if !ok {
plist := &pb.PostingList{}
plist.Postings = append(plist.Postings, mpost)
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)
}
// AddMutation adds mutation to mutation layers. Note that it does not write
// anything to disk. Some other background routine will be responsible for merging
// changes in mutation layers to BadgerDB. Returns whether any mutation happens.
func (l *List) AddMutation(ctx context.Context, txn *Txn, t *pb.DirectedEdge) error {
l.Lock()
defer l.Unlock()
return l.addMutation(ctx, txn, t)
}
// 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.
if len(t.Lang) > 0 {
id = farm.Fingerprint64([]byte(t.Lang))
} else if 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
}
// canMutateUid returns an error if all the following conditions are met.
// * Predicate is of type UidID.
// * Predicate is not set to a list of uids in the schema.
// * The existing posting list has an entry that does not match the proposed
// mutation's uid.
// In this case, the user should delete the existing predicate and retry, or mutate
// the schema to allow for multiple uids. This method is necessary to support uid
// predicates with single values because previously all uid predicates were
// considered lists.
// This functions returns a nil error in all other cases.
func (l *List) canMutateUid(txn *Txn, edge *pb.DirectedEdge) error {
l.AssertRLock()
if types.TypeID(edge.ValueType) != types.UidID {
return nil
}
if schema.State().IsList(edge.Attr) {
return nil
}
return l.iterate(txn.StartTs, 0, func(obj *pb.Posting) error {
if obj.Uid != edge.GetValueId() {
return fmt.Errorf(
"cannot add value with uid %x to predicate %s because one of the existing "+
"values does not match this uid, either delete the existing values first or "+
"modify the schema to '%s: [uid]'",
edge.GetValueId(), edge.Attr, edge.Attr)
}
return nil
})
}
func (l *List) addMutation(ctx context.Context, txn *Txn, t *pb.DirectedEdge) error {
if atomic.LoadInt32(&l.deleteMe) == 1 {
return ErrRetry
}
if txn.ShouldAbort() {
return y.ErrConflict
}
getKey := func(key []byte, uid uint64) string {
return fmt.Sprintf("%s|%d", key, uid)
}
// 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 string
if t.Attr == "_predicate_" {
// Don't check for conflict.
} else if schema.State().HasUpsert(t.Attr) {
// Consider checking to see if a email id is unique. A user adds:
// <uid> <email> "[email protected]", and there's a string equal tokenizer
// and upsert directive on the schema.
// Then keys are "<email> <uid>" and "<email> [email protected]"
// 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)
} else if x.Parse(l.key).IsData() {
// Unless upsert is specified, we don't check for index conflicts, only
// data conflicts.
// If the data is of type UID, then we use SPO for conflict detection.
// Otherwise, we use SP (for string, date, int, etc.).
typ, err := schema.State().TypeOf(t.Attr)
if err != nil {
glog.V(2).Infof("Unable to find type of attr: %s. Err: %v", t.Attr, err)
// Don't check for conflict.
} else if typ == types.UidID {
conflictKey = getKey(l.key, t.ValueId)
} else {
conflictKey = getKey(l.key, 0)
}
}
mpost := NewPosting(t)
mpost.StartTs = txn.StartTs
if mpost.PostingType != pb.Posting_REF {
t.ValueId = fingerprintEdge(t)
mpost.Uid = t.ValueId
}
l.updateMutationLayer(mpost)
atomic.AddInt32(&l.pendingTxns, 1)
txn.AddKeys(string(l.key), 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) CommitMutation(startTs, commitTs uint64) error {
l.Lock()
defer l.Unlock()
return l.commitMutation(startTs, commitTs)
}
func (l *List) commitMutation(startTs, commitTs uint64) error {
if atomic.LoadInt32(&l.deleteMe) == 1 {
return ErrRetry
}
l.AssertLock()
// Check if we still have a pending txn when we return from this function.
defer func() {
for _, plist := range l.mutationMap {
if plist.CommitTs == 0 {
return // Got a pending txn.
}
}
atomic.StoreInt32(&l.pendingTxns, 0)
}()
plist, ok := l.mutationMap[startTs]
if !ok {
// It was already committed, might be happening due to replay.
return nil
}
if commitTs == 0 {
// Abort mutation.
delete(l.mutationMap, startTs)
return nil
}
// We have a valid commit.
plist.CommitTs = commitTs
for _, mpost := range plist.Postings {
mpost.CommitTs = commitTs
}
// In general, a posting list shouldn't try to mix up it's job of keeping
// things in memory, with writing things to disk. A separate process can
// roll up and write them to disk. Posting list should only keep things in
// memory, to make it available for transactions. So, all we need to do here
// is to roll them up periodically, now being done by draft.go.
// For the PLs in memory, we roll them up after we do the disk rollup.
return nil
}
// 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)
}
func (l *List) Conflicts(readTs uint64) []uint64 {
l.RLock()
defer l.RUnlock()
var conflicts []uint64
for ts, pl := range l.mutationMap {
if pl.CommitTs > 0 {
continue
}
if ts < readTs {
conflicts = append(conflicts, ts)
}
}
return conflicts
}
func (l *List) pickPostings(readTs uint64) (uint64, []*pb.Posting) {
return l.pickPostingsInternal(readTs, false, 0)
}
func (l *List) pickPartPostings(readTs uint64, partIdx int) (uint64, []*pb.Posting) {
return l.pickPostingsInternal(readTs, true, partIdx)
}
func (l *List) pickPostingsInternal(readTs uint64, partial bool, partIdx int) (
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 deleteBelow 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 > deleteBelow {
// We're above the deleteBelow marker. We wouldn't reach here if effectiveTs is zero.
for _, mpost := range plist.Postings {
if partial && (mpost.Uid < l.parts[partIdx].StartUid ||
l.parts[partIdx].EndUid < mpost.Uid) {
continue
}
if hasDeleteAll(mpost) {
deleteBelow = effectiveTs
continue
}
posts = append(posts, mpost)
}
}
}
if deleteBelow > 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 < deleteBelow { // Do pick the posts at effectiveTs == deleteBelow.
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 deleteBelow, posts
}
func (l *List) iterate(readTs uint64, afterUid uint64, f func(obj *pb.Posting) error) error {
return l.iterateInternal(readTs, afterUid, false, 0, f)
}
func (l *List) partIterate(readTs uint64, partIdx int, f func(obj *pb.Posting) error) error {
return l.iterateInternal(readTs, 0, true, partIdx, f)
}
func (l *List) iterateInternal(readTs uint64, afterUid uint64, partial bool, partIdx int,
f func(obj *pb.Posting) error) error {
l.AssertRLock()
// If not a multi-part list iterate through the whole list.
if !l.plist.MultiPart {
partial = false
partIdx = 0
}
deleteBelow, mposts := l.pickPostingsInternal(readTs, partial, partIdx)
if readTs < l.minTs {
return x.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
var pitr PIterator
pitr.Init(l, PItrOpts{
afterUid: afterUid,
discardPl: deleteBelow > 0,
readTs: readTs,
partialIteration: partial,
startPart: partIdx,
})
prevUid := uint64(0)
var err error
for err == nil {
if midx < mlen {
mp = mposts[midx]
} else {
mp = emptyPosting
}
if pitr.Valid() {
pp = pitr.Posting()
} else {
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 := pitr.Next(); err != nil {
return err
}
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)
}
prevUid = mp.Uid
midx++
case pp.Uid == mp.Uid:
if mp.Op != Del {
err = f(mp)
}
prevUid = mp.Uid
if err := pitr.Next(); err != nil {
return err
}
midx++
default:
log.Fatalf("Unhandled case during iteration of posting list.")
}
}
if err == ErrStopIteration {
return nil
}
return err
}
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)
}
func (l *List) MarshalToKv() ([]*bpb.KV, error) {
l.Lock()
defer l.Unlock()
if err := l.rollup(math.MaxUint64); err != nil {
return nil, err
}
var kvs []*bpb.KV
kv := &bpb.KV{}
kv.Version = l.minTs
kv.Key = l.key
val, meta := marshalPostingList(l.plist)
kv.UserMeta = []byte{meta}
kv.Value = val
kvs = append(kvs, kv)
for _, part := range l.parts {
kv := &bpb.KV{}
kv.Version = l.minTs
kv.Key = getNextPartKey(l.key, part.StartUid)
val, meta := marshalPostingList(part)
kv.UserMeta = []byte{meta}
kv.Value = val
kvs = append(kvs, kv)
}
return kvs, nil
}
func marshalPostingList(plist *pb.PostingList) (data []byte, meta byte) {
if plist.Pack == nil || len(plist.Pack.Blocks) == 0 {
return nil, BitEmptyPosting
}
data, err := plist.Marshal()
x.Check(err)
return data, BitCompletePosting
}
const blockSize int = 256
func (l *List) Rollup(readTs uint64) error {
l.Lock()
defer l.Unlock()
return l.rollup(readTs)
}
// 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) error {
l.AssertLock()
// Pick all committed entries
if l.minTs > readTs {
// If we are already past the readTs, then skip the rollup.
return nil
}
if l.plist.MultiPart && !l.plist.FirstPart {
return fmt.Errorf("rollup can only be called from the first part of a multi-part list")
}
if !l.plist.MultiPart {
final := new(pb.PostingList)
enc := codec.Encoder{BlockSize: blockSize}
err := l.iterate(readTs, 0, func(p *pb.Posting) error {
// iterate already takes care of not returning entries whose commitTs
// is above curr.commitTs.
// So, we don't need to do any filtering here. In fact, doing filtering
// here could result in a bug.
enc.Add(p.Uid)
// We want to add the posting if it has facets or has a value.
if p.Facets != nil || p.PostingType != pb.Posting_REF || len(p.Label) != 0 {
// I think it's okay to take the pointer from the iterator, because
// we have a lock over List; which won't be released until final has
// been marshalled. Thus, the underlying data wouldn't be changed.
final.Postings = append(final.Postings, p)
}
return nil
})
x.Check(err)
final.Pack = enc.Done()
l.plist.Pack = final.Pack
l.plist.Postings = final.Postings
} else {
for partIdx, part := range l.parts {
final := new(pb.PostingList)
enc := codec.Encoder{BlockSize: blockSize}
err := l.partIterate(readTs, partIdx, func(p *pb.Posting) error {
// iterate already takes care of not returning entries whose commitTs
// is above curr.commitTs.
// So, we don't need to do any filtering here. In fact, doing filtering
// here could result in a bug.
enc.Add(p.Uid)
// We want to add the posting if it has facets or has a value.
if p.Facets != nil || p.PostingType != pb.Posting_REF || len(p.Label) != 0 {
// I think it's okay to take the pointer from the iterator, because
// we have a lock over List; which won't be released until final has
// been marshalled. Thus, the underlying data wouldn't be changed.
final.Postings = append(final.Postings, p)
}
return nil
})
x.Check(err)
final.Pack = enc.Done()
part.Pack = final.Pack
part.Postings = final.Postings
}
}
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.
deleteBelow, mposts := l.pickPostings(readTs)
maxCommitTs = x.Max(maxCommitTs, deleteBelow)
for _, mp := range mposts {
maxCommitTs = x.Max(maxCommitTs, mp.CommitTs)
}
}
// Keep all uncommitted Entries or postings with commitTs > l.commitTs
// in mutation map. Discard all else.
// TODO: This could be removed after LRU cache is removed.
for startTs, plist := range l.mutationMap {
cl := plist.CommitTs
if cl == 0 || cl > maxCommitTs {
// Keep this.
} else {
delete(l.mutationMap, startTs)
}
}
l.minTs = maxCommitTs
// 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.
if err := l.splitList(readTs); err != nil {
return nil
}
return nil
}
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 {
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)
})
}
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
}
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
}