@@ -233,6 +233,18 @@ type countParams struct {
233
233
reverse bool
234
234
}
235
235
236
+ // When we want to update count edges, we should set them with OVR instead of SET as SET will mess with count
237
+ func shouldAddCountEdge (found bool , edge * pb.DirectedEdge ) bool {
238
+ if found {
239
+ if edge .Op != pb .DirectedEdge_DEL {
240
+ edge .Op = pb .DirectedEdge_OVR
241
+ }
242
+ return true
243
+ } else {
244
+ return edge .Op != pb .DirectedEdge_DEL
245
+ }
246
+ }
247
+
236
248
func (txn * Txn ) addReverseMutationHelper (ctx context.Context , plist * List ,
237
249
hasCountIndex bool , edge * pb.DirectedEdge ) (countParams , error ) {
238
250
countBefore , countAfter := 0 , 0
@@ -242,12 +254,14 @@ func (txn *Txn) addReverseMutationHelper(ctx context.Context, plist *List,
242
254
defer plist .Unlock ()
243
255
if hasCountIndex {
244
256
countBefore , found , _ = plist .getPostingAndLengthNoSort (txn .StartTs , 0 , edge .ValueId )
245
- if countBefore == - 1 {
257
+ if countBefore < 0 {
246
258
return emptyCountParams , errors .Wrapf (ErrTsTooOld , "Adding reverse mutation helper count" )
247
259
}
248
260
}
249
- if err := plist .addMutationInternal (ctx , txn , edge ); err != nil {
250
- return emptyCountParams , err
261
+ if ! (hasCountIndex && ! shouldAddCountEdge (found , edge )) {
262
+ if err := plist .addMutationInternal (ctx , txn , edge ); err != nil {
263
+ return emptyCountParams , err
264
+ }
251
265
}
252
266
if hasCountIndex {
253
267
countAfter = countAfterMutation (countBefore , found , edge .Op )
@@ -311,7 +325,7 @@ func (txn *Txn) addReverseAndCountMutation(ctx context.Context, t *pb.DirectedEd
311
325
// entries for this key in the index are removed.
312
326
pred , ok := schema .State ().Get (ctx , t .Attr )
313
327
isSingleUidUpdate := ok && ! pred .GetList () && pred .GetValueType () == pb .Posting_UID &&
314
- t .Op == pb .DirectedEdge_SET && t .ValueId != 0
328
+ t .Op != pb .DirectedEdge_DEL && t .ValueId != 0
315
329
if isSingleUidUpdate {
316
330
dataKey := x .DataKey (t .Attr , t .Entity )
317
331
dataList , err := getFn (dataKey )
@@ -458,7 +472,7 @@ func (txn *Txn) updateCount(ctx context.Context, params countParams) error {
458
472
}
459
473
460
474
func countAfterMutation (countBefore int , found bool , op pb.DirectedEdge_Op ) int {
461
- if ! found && op == pb .DirectedEdge_SET {
475
+ if ! found && op != pb .DirectedEdge_DEL {
462
476
return countBefore + 1
463
477
} else if found && op == pb .DirectedEdge_DEL {
464
478
return countBefore - 1
@@ -531,8 +545,10 @@ func (txn *Txn) addMutationHelper(ctx context.Context, l *List, doUpdateIndex bo
531
545
}
532
546
}
533
547
534
- if err = l .addMutationInternal (ctx , txn , t ); err != nil {
535
- return val , found , emptyCountParams , err
548
+ if ! (hasCountIndex && ! shouldAddCountEdge (found && currPost .Op != Del , t )) {
549
+ if err = l .addMutationInternal (ctx , txn , t ); err != nil {
550
+ return val , found , emptyCountParams , err
551
+ }
536
552
}
537
553
538
554
if found && doUpdateIndex {
@@ -596,7 +612,7 @@ func (l *List) AddMutationWithIndex(ctx context.Context, edge *pb.DirectedEdge,
596
612
return err
597
613
}
598
614
}
599
- if edge .Op == pb .DirectedEdge_SET {
615
+ if edge .Op != pb .DirectedEdge_DEL {
600
616
val = types.Val {
601
617
Tid : types .TypeID (edge .ValueType ),
602
618
Value : edge .Value ,
@@ -895,15 +911,13 @@ func (r *rebuilder) Run(ctx context.Context) error {
895
911
// We set it to 1 in case there are no keys found and NewStreamAt is called with ts=0.
896
912
var counter uint64 = 1
897
913
898
- var txn * Txn
899
-
900
914
tmpWriter := tmpDB .NewManagedWriteBatch ()
901
915
stream := pstore .NewStreamAt (r .startTs )
902
916
stream .LogPrefix = fmt .Sprintf ("Rebuilding index for predicate %s (1/2):" , r .attr )
903
917
stream .Prefix = r .prefix
904
918
//TODO We need to create a single transaction irrespective of the type of the predicate
905
919
if pred .ValueType == pb .Posting_VFLOAT {
906
- txn = NewTxn ( r . startTs )
920
+ x . AssertTrue ( false )
907
921
}
908
922
stream .KeyToList = func (key []byte , itr * badger.Iterator ) (* bpb.KVList , error ) {
909
923
// We should return quickly if the context is no longer valid.
@@ -923,44 +937,25 @@ func (r *rebuilder) Run(ctx context.Context) error {
923
937
return nil , errors .Wrapf (err , "error reading posting list from disk" )
924
938
}
925
939
926
- // We are using different transactions in each call to KeyToList function. This could
927
- // be a problem for computing reverse count indexes if deltas for same key are added
928
- // in different transactions. Such a case doesn't occur for now.
929
- // TODO: Maybe we can always use txn initialized in rebuilder.Run().
930
- streamTxn := txn
931
- if streamTxn == nil {
932
- streamTxn = NewTxn (r .startTs )
933
- }
934
- edges , err := r .fn (pk .Uid , l , streamTxn )
940
+ kvs , err := l .Rollup (nil , r .startTs )
935
941
if err != nil {
936
942
return nil , err
937
943
}
938
944
939
- if txn != nil {
940
- kvs := make ([]* bpb.KV , 0 , len (edges ))
941
- for _ , edge := range edges {
942
- version := atomic .AddUint64 (& counter , 1 )
943
- key := x .DataKey (edge .Attr , edge .Entity )
944
- pl , err := txn .GetFromDelta (key )
945
- if err != nil {
946
- return & bpb.KVList {}, nil
947
- }
948
- data := pl .getMutation (r .startTs )
949
- kv := bpb.KV {
950
- Key : x .DataKey (edge .Attr , edge .Entity ),
951
- Value : data ,
952
- UserMeta : []byte {BitDeltaPosting },
953
- Version : version ,
954
- }
955
- kvs = append (kvs , & kv )
956
- }
957
- return & bpb.KVList {Kv : kvs }, nil
945
+ for _ , kv := range kvs {
946
+ version := atomic .AddUint64 (& counter , 1 )
947
+ kv .Version = version
948
+ }
949
+
950
+ streamTxn := NewTxn (r .startTs )
951
+ _ , err = r .fn (pk .Uid , l , streamTxn )
952
+ if err != nil {
953
+ return nil , err
958
954
}
959
955
960
956
// Convert data into deltas.
961
957
streamTxn .Update ()
962
958
// txn.cache.Lock() is not required because we are the only one making changes to txn.
963
- kvs := make ([]* bpb.KV , 0 , len (streamTxn .cache .deltas ))
964
959
for key , data := range streamTxn .cache .deltas {
965
960
version := atomic .AddUint64 (& counter , 1 )
966
961
kv := bpb.KV {
0 commit comments