MCPcopy
hub / github.com/dgraph-io/badger / compactBuildTables

Method compactBuildTables

levels.go:481–721  ·  view source on GitHub ↗

compactBuildTables merges topTables and botTables to form a list of new tables.

(
	lev int, cd compactDef)

Source from the content-addressed store, hash-verified

479
480// compactBuildTables merges topTables and botTables to form a list of new tables.
481func (s *levelsController) compactBuildTables(
482 lev int, cd compactDef) ([]*table.Table, func() error, error) {
483 topTables := cd.top
484 botTables := cd.bot
485
486 // Check overlap of the top level with the levels which are not being
487 // compacted in this compaction. We don't need to check overlap of the bottom
488 // tables with other levels because if the top tables overlap with any of the lower
489 // levels, it implies bottom level also overlaps because top and bottom tables
490 // overlap with each other.
491 hasOverlap := s.checkOverlap(cd.top, cd.nextLevel.level+1)
492
493 // Try to collect stats so that we can inform value log about GC. That would help us find which
494 // value log file should be GCed.
495 discardStats := make(map[uint32]int64)
496 updateStats := func(vs y.ValueStruct) {
497 // We don't need to store/update discard stats when badger is running in Disk-less mode.
498 if s.kv.opt.InMemory {
499 return
500 }
501 if vs.Meta&bitValuePointer > 0 {
502 var vp valuePointer
503 vp.Decode(vs.Value)
504 discardStats[vp.Fid] += int64(vp.Len)
505 }
506 }
507
508 // Create iterators across all the tables involved first.
509 var iters []y.Iterator
510 switch {
511 case lev == 0:
512 iters = appendIteratorsReversed(iters, topTables, false)
513 case len(topTables) > 0:
514 y.AssertTrue(len(topTables) == 1)
515 iters = []y.Iterator{topTables[0].NewIterator(false)}
516 }
517
518 // Next level has level>=1 and we can use ConcatIterator as key ranges do not overlap.
519 var valid []*table.Table
520
521nextTable:
522 for _, table := range botTables {
523 if len(cd.dropPrefixes) > 0 {
524 for _, prefix := range cd.dropPrefixes {
525 if bytes.HasPrefix(table.Smallest(), prefix) &&
526 bytes.HasPrefix(table.Biggest(), prefix) {
527 // All the keys in this table have the dropPrefix. So, this
528 // table does not need to be in the iterator and can be
529 // dropped immediately.
530 continue nextTable
531 }
532 }
533 }
534 valid = append(valid, table)
535 }
536 iters = append(iters, table.NewConcatIterator(valid, false))
537 it := table.NewMergeIterator(iters, false)
538 defer it.Close() // Important to close the iterator to do ref counting.

Callers 1

runCompactDefMethod · 0.95

Calls 15

checkOverlapMethod · 0.95
DecodeMethod · 0.95
CloseMethod · 0.95
RewindMethod · 0.95
ValidMethod · 0.95
NextMethod · 0.95
KeyMethod · 0.95
ValueMethod · 0.95
reserveFileIDMethod · 0.95
AssertTrueFunction · 0.92
NewConcatIteratorFunction · 0.92
NewMergeIteratorFunction · 0.92

Tested by

no test coverage detected