Skip to content

Commit d8efa29

Browse files
committed
manifest: simplify VirtualBackings interface
This commit adapts the VirtualBackings type, replacing its ForEach and Backings methods with an All method that returns an iter.Seq of table backings. Additionally, use maps.Keys and slices.Collect where applicable.
1 parent 132d6b6 commit d8efa29

File tree

3 files changed

+17
-25
lines changed

3 files changed

+17
-25
lines changed

checkpoint.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -198,9 +198,9 @@ func (d *DB) Checkpoint(
198198
optionsFileNum := d.optionsFileNum
199199

200200
virtualBackingFiles := make(map[base.DiskFileNum]struct{})
201-
d.mu.versions.latest.virtualBackings.ForEach(func(backing *manifest.TableBacking) {
201+
for backing := range d.mu.versions.latest.virtualBackings.All() {
202202
virtualBackingFiles[backing.DiskFileNum] = struct{}{}
203-
})
203+
}
204204
versionBlobFiles := d.mu.versions.latest.blobFiles.Metadatas()
205205

206206
// Acquire the logs while holding mutexes to ensure we don't race with a

internal/manifest/virtual_backings.go

Lines changed: 9 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
stdcmp "cmp"
1010
"container/heap"
1111
"fmt"
12+
"iter"
1213
"maps"
1314
"slices"
1415

@@ -274,30 +275,20 @@ func (bv *VirtualBackings) Get(n base.DiskFileNum) (_ *TableBacking, ok bool) {
274275
}
275276

276277
// ForEach calls fn on each backing, in unspecified order.
277-
func (bv *VirtualBackings) ForEach(fn func(backing *TableBacking)) {
278-
for _, v := range bv.m {
279-
fn(v.backing)
278+
func (bv *VirtualBackings) All() iter.Seq[*TableBacking] {
279+
return func(yield func(*TableBacking) bool) {
280+
for _, v := range bv.m {
281+
if !yield(v.backing) {
282+
return
283+
}
284+
}
280285
}
281286
}
282287

283288
// DiskFileNums returns disk file nums of all the backing in the set, in sorted
284289
// order.
285290
func (bv *VirtualBackings) DiskFileNums() []base.DiskFileNum {
286-
res := make([]base.DiskFileNum, 0, len(bv.m))
287-
for n := range bv.m {
288-
res = append(res, n)
289-
}
290-
slices.Sort(res)
291-
return res
292-
}
293-
294-
// Backings returns all backings in the set, in unspecified order.
295-
func (bv *VirtualBackings) Backings() []*TableBacking {
296-
res := make([]*TableBacking, 0, len(bv.m))
297-
for _, v := range bv.m {
298-
res = append(res, v.backing)
299-
}
300-
return res
291+
return slices.Sorted(maps.Keys(bv.m))
301292
}
302293

303294
// ReplacementCandidate returns the backing with the lowest ratio of data

version_set.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ package pebble
77
import (
88
"fmt"
99
"io"
10+
"slices"
1011
"sync"
1112
"sync/atomic"
1213
"time"
@@ -369,13 +370,13 @@ func (vs *versionSet) load(
369370
}
370371
}
371372
}
372-
vs.latest.virtualBackings.ForEach(func(backing *manifest.TableBacking) {
373+
for backing := range vs.latest.virtualBackings.All() {
373374
isLocal, localSize := sizeIfLocal(backing, vs.provider)
374375
vs.metrics.Table.Local.LiveSize = uint64(int64(vs.metrics.Table.Local.LiveSize) + localSize)
375376
if isLocal {
376377
vs.metrics.Table.Local.LiveCount++
377378
}
378-
})
379+
}
379380

380381
vs.setCompactionPicker(newCompactionPickerByScore(newVersion, &vs.latest, vs.opts, nil))
381382
return nil
@@ -578,7 +579,7 @@ func (vs *versionSet) UpdateVersionLocked(
578579
// We want the virtual backings *before* applying the version edit, because
579580
// the new manifest will contain the pre-apply version plus the last version
580581
// edit.
581-
newManifestVirtualBackings = vs.latest.virtualBackings.Backings()
582+
newManifestVirtualBackings = slices.Collect(vs.latest.virtualBackings.All())
582583
}
583584

584585
// Grab certain values before releasing vs.mu, in case createManifest() needs
@@ -1174,9 +1175,9 @@ func (vs *versionSet) addLiveFileNums(m map[base.DiskFileNum]struct{}) {
11741175
// are not but are still alive because of the protection mechanism (see
11751176
// manifset.VirtualBackings). This loop ensures the latter get added to the
11761177
// map.
1177-
vs.latest.virtualBackings.ForEach(func(b *manifest.TableBacking) {
1178+
for b := range vs.latest.virtualBackings.All() {
11781179
m[b.DiskFileNum] = struct{}{}
1179-
})
1180+
}
11801181
}
11811182

11821183
// addObsoleteLocked will add the fileInfo associated with obsolete backing

0 commit comments

Comments
 (0)