Skip to content

Commit

Permalink
manifest: add a helper for iterating through all levels
Browse files Browse the repository at this point in the history
This helper can be used when we need to use an iterator for each L0
sublevel and for each level L1+.
  • Loading branch information
RaduBerinde committed Mar 29, 2024
1 parent a9087af commit 1c7bcd1
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 19 deletions.
24 changes: 5 additions & 19 deletions ingest_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -804,30 +804,16 @@ func TestExcise(t *testing.T) {
d.mu.versions.logLock()
d.mu.Unlock()
current := d.mu.versions.currentVersion()
for _, sublevel := range current.L0Sublevels.Levels {
iter := sublevel.Iter()
for m := iter.SeekGE(d.cmp, exciseSpan.Start); m != nil && d.cmp(m.Smallest.UserKey, exciseSpan.End) < 0; m = iter.Next() {
_, err := d.excise(exciseSpan.UserKeyBounds(), m, ve, 0 /* level */)
if err != nil {
d.mu.Lock()
d.mu.versions.logUnlock()
d.mu.Unlock()
return fmt.Sprintf("error when excising %s: %s", m.FileNum, err.Error())
}
}
}
for level := 1; level < manifest.NumLevels; level++ {
iter := current.Levels[level].Iter()

current.IterAllLevelsAndSublevels(func(iter manifest.LevelIterator, level, _ int) {
for m := iter.SeekGE(d.cmp, exciseSpan.Start); m != nil && d.cmp(m.Smallest.UserKey, exciseSpan.End) < 0; m = iter.Next() {
_, err := d.excise(exciseSpan.UserKeyBounds(), m, ve, level)
if err != nil {
d.mu.Lock()
d.mu.versions.logUnlock()
d.mu.Unlock()
return fmt.Sprintf("error when excising %s: %s", m.FileNum, err.Error())
td.Fatalf(t, "error when excising %s: %s", m.FileNum, err.Error())
}
}
}
})

d.mu.Lock()
d.mu.versions.logUnlock()
d.mu.Unlock()
Expand Down
11 changes: 11 additions & 0 deletions internal/manifest/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -1489,6 +1489,17 @@ func (v *Version) Overlaps(level int, bounds base.UserKeyBounds) LevelSlice {
return overlaps(v.Levels[level].Iter(), v.cmp.Compare, bounds)
}

// IterAllLevelsAndSublevels calls fn with an iterator for each L0 sublevel
// (from top to bottom), then once for each level below L0.
func (v *Version) IterAllLevelsAndSublevels(fn func(it LevelIterator, level int, sublevel int)) {
for sublevel := len(v.L0SublevelFiles) - 1; sublevel >= 0; sublevel-- {
fn(v.L0SublevelFiles[sublevel].Iter(), 0, sublevel)
}
for level := 1; level < NumLevels; level++ {
fn(v.Levels[level].Iter(), level, invalidSublevel)
}
}

// CheckOrdering checks that the files are consistent with respect to
// increasing file numbers (for level 0 files) and increasing and non-
// overlapping internal key ranges (for level non-0 files).
Expand Down

0 comments on commit 1c7bcd1

Please sign in to comment.