|
72 | 72 | } |
73 | 73 | ) |
74 | 74 |
|
75 | | -var memoryLayer *MemoryLayer |
| 75 | +var MemLayerInstance *MemoryLayer |
76 | 76 |
|
77 | 77 | func init() { |
78 | 78 | x.AssertTrue(len(IncrRollup.priorityKeys) == 2) |
@@ -325,12 +325,12 @@ func (txn *Txn) CommitToDisk(writer *TxnWriter, commitTs uint64) error { |
325 | 325 | } |
326 | 326 |
|
327 | 327 | func ResetCache() { |
328 | | - memoryLayer.clear() |
| 328 | + MemLayerInstance.clear() |
329 | 329 | } |
330 | 330 |
|
331 | 331 | // RemoveCacheFor will delete the list corresponding to the given key. |
332 | 332 | func RemoveCacheFor(key []byte) { |
333 | | - memoryLayer.del(key) |
| 333 | + MemLayerInstance.del(key) |
334 | 334 | } |
335 | 335 |
|
336 | 336 | type Cache struct { |
@@ -405,8 +405,102 @@ func (ml *MemoryLayer) del(key []byte) { |
405 | 405 | ml.cache.del(key) |
406 | 406 | } |
407 | 407 |
|
| 408 | +type IterateDiskArgs struct { |
| 409 | + Prefix []byte |
| 410 | + Prefetch bool |
| 411 | + AllVersions bool |
| 412 | + ReadTs uint64 |
| 413 | + Reverse bool |
| 414 | + CheckInclusion func(uint64) error |
| 415 | + Function func(l *List, pk x.ParsedKey) error |
| 416 | + |
| 417 | + StartKey []byte |
| 418 | +} |
| 419 | + |
| 420 | +func (ml *MemoryLayer) IterateDisk(ctx context.Context, f IterateDiskArgs) error { |
| 421 | + txn := pstore.NewTransactionAt(f.ReadTs, false) |
| 422 | + defer txn.Discard() |
| 423 | + |
| 424 | + itOpt := badger.DefaultIteratorOptions |
| 425 | + itOpt.PrefetchValues = f.Prefetch |
| 426 | + itOpt.AllVersions = f.AllVersions |
| 427 | + itOpt.Reverse = f.Reverse |
| 428 | + itOpt.Prefix = f.Prefix |
| 429 | + it := txn.NewIterator(itOpt) |
| 430 | + defer it.Close() |
| 431 | + |
| 432 | + var prevKey []byte |
| 433 | + |
| 434 | + count := 0 |
| 435 | + |
| 436 | + for it.Seek(f.StartKey); it.Valid(); { |
| 437 | + item := it.Item() |
| 438 | + if bytes.Equal(item.Key(), prevKey) { |
| 439 | + it.Next() |
| 440 | + continue |
| 441 | + } |
| 442 | + prevKey = append(prevKey[:0], item.Key()...) |
| 443 | + |
| 444 | + // Parse the key upfront, otherwise ReadPostingList would advance the |
| 445 | + // iterator. |
| 446 | + pk, err := x.Parse(item.Key()) |
| 447 | + if err != nil { |
| 448 | + return err |
| 449 | + } |
| 450 | + |
| 451 | + if pk.HasStartUid { |
| 452 | + // The keys holding parts of a split key should not be accessed here because |
| 453 | + // they have a different prefix. However, the check is being added to guard |
| 454 | + // against future bugs. |
| 455 | + continue |
| 456 | + } |
| 457 | + |
| 458 | + if item.UserMeta()&BitEmptyPosting > 0 { |
| 459 | + // This is an empty posting list. So, it should not be included. |
| 460 | + continue |
| 461 | + } |
| 462 | + |
| 463 | + err = f.CheckInclusion(pk.Uid) |
| 464 | + switch { |
| 465 | + case err == ErrNoValue: |
| 466 | + continue |
| 467 | + case err != nil: |
| 468 | + return err |
| 469 | + } |
| 470 | + |
| 471 | + count++ |
| 472 | + |
| 473 | + if count%100000 == 0 { |
| 474 | + select { |
| 475 | + case <-ctx.Done(): |
| 476 | + return ctx.Err() |
| 477 | + default: |
| 478 | + } |
| 479 | + } |
| 480 | + |
| 481 | + l, err := ReadPostingList(item.KeyCopy(nil), it) |
| 482 | + if err != nil { |
| 483 | + return err |
| 484 | + } |
| 485 | + empty, err := l.IsEmpty(f.ReadTs, 0) |
| 486 | + switch { |
| 487 | + case err != nil: |
| 488 | + return err |
| 489 | + case !empty: |
| 490 | + err = f.Function(l, pk) |
| 491 | + if err != nil && err != ErrStopIteration { |
| 492 | + return err |
| 493 | + } |
| 494 | + if err == ErrStopIteration { |
| 495 | + return nil |
| 496 | + } |
| 497 | + } |
| 498 | + } |
| 499 | + return nil |
| 500 | +} |
| 501 | + |
408 | 502 | func GetStatsHolder() *StatsHolder { |
409 | | - return memoryLayer.statsHolder |
| 503 | + return MemLayerInstance.statsHolder |
410 | 504 | } |
411 | 505 |
|
412 | 506 | func initMemoryLayer(cacheSize int64, removeOnUpdate bool) *MemoryLayer { |
@@ -510,9 +604,9 @@ func (txn *Txn) UpdateCachedKeys(commitTs uint64) { |
510 | 604 | return |
511 | 605 | } |
512 | 606 |
|
513 | | - memoryLayer.wait() |
| 607 | + MemLayerInstance.wait() |
514 | 608 | for key, delta := range txn.cache.deltas { |
515 | | - memoryLayer.updateItemInCache(key, delta, txn.StartTs, commitTs) |
| 609 | + MemLayerInstance.updateItemInCache(key, delta, txn.StartTs, commitTs) |
516 | 610 | } |
517 | 611 | } |
518 | 612 |
|
@@ -746,7 +840,7 @@ func getNew(key []byte, pstore *badger.DB, readTs uint64, readUids bool) (*List, |
746 | 840 | return nil, badger.ErrDBClosed |
747 | 841 | } |
748 | 842 |
|
749 | | - l, err := memoryLayer.ReadData(key, pstore, readTs, readUids) |
| 843 | + l, err := MemLayerInstance.ReadData(key, pstore, readTs, readUids) |
750 | 844 | if err != nil { |
751 | 845 | return l, err |
752 | 846 | } |
|
0 commit comments