From 98c01381ed6dfc9085c946c2f0c720abad24ad10 Mon Sep 17 00:00:00 2001 From: Luke Shumaker Date: Tue, 23 Aug 2022 23:41:24 -0600 Subject: Begone with allsums.go --- lib/btrfs/btrfssum/shortsum.go | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) (limited to 'lib/btrfs') diff --git a/lib/btrfs/btrfssum/shortsum.go b/lib/btrfs/btrfssum/shortsum.go index 2d995b5..537e0ce 100644 --- a/lib/btrfs/btrfssum/shortsum.go +++ b/lib/btrfs/btrfssum/shortsum.go @@ -8,6 +8,7 @@ import ( "context" "fmt" "io" + "math" "strings" "git.lukeshu.com/go/lowmemjson" @@ -175,6 +176,19 @@ func (sg SumRunWithGaps[Addr]) PctFull() float64 { return float64(full) / float64(total) } +func (sg SumRunWithGaps[Addr]) RunForAddr(addr Addr) (SumRun[Addr], Addr, bool) { + for _, run := range sg.Runs { + if run.Addr > addr { + return SumRun[Addr]{}, run.Addr, false + } + if run.Addr.Add(run.Size()) <= addr { + continue + } + return run, 0, true + } + return SumRun[Addr]{}, math.MaxInt64, false +} + func (sg SumRunWithGaps[Addr]) SumForAddr(addr Addr) (ShortSum, error) { if addr < sg.Addr || addr >= sg.Addr.Add(sg.Size) { return "", io.EOF @@ -192,6 +206,15 @@ func (sg SumRunWithGaps[Addr]) SumForAddr(addr Addr) (ShortSum, error) { return "", diskio.ErrWildcard } +func (sg SumRunWithGaps[Addr]) Walk(ctx context.Context, fn func(Addr, ShortSum) error) error { + for _, run := range sg.Runs { + if err := run.Walk(ctx, fn); err != nil { + return err + } + } + return nil +} + // Get implements diskio.Sequence[int, ShortSum] func (sg SumRunWithGaps[Addr]) Get(sumIdx int64) (ShortSum, error) { addr := sg.Addr.Add(btrfsvol.AddrDelta(sumIdx) * BlockSize) -- cgit v1.2.3-2-g168b