Files
loki/pkg/dataobj/range_reader.go
Robert Fratto a237444f0d refactor(dataobj): invert dependency between dataobj and sections (#17762)
Originally, the dataobj package was a higher-level API around sections. This
design caused it to become a bottleneck:

* Implementing any new public behaviour for a section required bubbling it up
  to the dataobj API for it to be exposed, making it tedious to add new
  sections or update existing ones.

* The `dataobj.Builder` pattern was focused on constructing dataobjs for
  storing log data, which will cause friction as we build objects around other
  use cases. 

This PR builds on top of the foundation laid out by #17704 and #17708, fully
inverting the dependency between dataobj and sections:

* The `dataobj` package has no knowledge of what sections exist, and can now be
  used for writing and reading generic sections. Section packages now create
  higher-level APIs around the abstractions provided by `dataobj`.

* Section packages are now public, and callers interact directly with these
  packages for writing and reading section-specific data.

* All logic for a section (encoding, decoding, buffering, reading) is now fully
  self-contained inside the section package. Previously, the implementation of
  each section was spread across three packages
  (`pkg/dataobj/internal/encoding`, `pkg/dataobj/internal/sections/SECTION`,
  `pkg/dataobj`).

* Cutting a section is now a decision made by the caller rather than the
  section implementation. Previously, the logs section builder would create
  multiple sections. 

For the most part, this change is a no-op, with two exceptions:

1. Section cutting is now performed by the caller; however, this shouldn't
   result in any issues. 

2. Removing the high-level `dataobj.Stream` and `dataobj.Record` types will
   temporarily reduce the allocation gains from #16988. I will address this after
   this PR is merged.
2025-05-21 08:04:35 -04:00

54 lines
1.4 KiB
Go

package dataobj
import (
"context"
"fmt"
"io"
"math"
"github.com/thanos-io/objstore"
)
// rangeReader is an interface that can read a range of bytes from an object.
type rangeReader interface {
// Size returns the full size of the object.
Size(ctx context.Context) (int64, error)
// ReadRange returns a reader over a range of bytes. Callers may create
// multiple current instance of ReadRange.
ReadRange(ctx context.Context, offset int64, length int64) (io.ReadCloser, error)
}
type bucketRangeReader struct {
bucket objstore.BucketReader
path string
}
func (rr *bucketRangeReader) Size(ctx context.Context) (int64, error) {
attrs, err := rr.bucket.Attributes(ctx, rr.path)
if err != nil {
return 0, fmt.Errorf("reading attributes: %w", err)
}
return attrs.Size, nil
}
func (rr *bucketRangeReader) ReadRange(ctx context.Context, offset int64, length int64) (io.ReadCloser, error) {
return rr.bucket.GetRange(ctx, rr.path, offset, length)
}
type readerAtRangeReader struct {
size int64
r io.ReaderAt
}
func (rr *readerAtRangeReader) Size(_ context.Context) (int64, error) {
return rr.size, nil
}
func (rr *readerAtRangeReader) ReadRange(_ context.Context, offset int64, length int64) (io.ReadCloser, error) {
if length > math.MaxInt {
return nil, fmt.Errorf("length too large: %d", length)
}
return io.NopCloser(io.NewSectionReader(rr.r, offset, length)), nil
}