// Copyright (c) The FrostDB Authors.
// Licensed under the Apache License 2.0.
// Copyright (c) The Thanos Authors.
// Licensed under the Apache License 2.0.

package storage

import (
	
	
	

	
)

// Bucket is an objstore.Bucket that also supports reading files via a ReaderAt interface.
type Bucket interface {
	objstore.Bucket
	GetReaderAt(ctx context.Context, name string) (io.ReaderAt, error)
}

// FileReaderAt is a wrapper around a objstore.Bucket that implements the ReaderAt interface.
type FileReaderAt struct {
	objstore.Bucket
	name string
	ctx  context.Context
}

// BucketReaderAt implements the Bucket interface.
type BucketReaderAt struct {
	objstore.Bucket
}

// NewBucketReaderAt returns a new Bucket.
func ( objstore.Bucket) *BucketReaderAt {
	return &BucketReaderAt{Bucket: }
}

// GetReaderAt returns a io.ReaderAt for the given filename.
func ( *BucketReaderAt) ( context.Context,  string) (io.ReaderAt, error) {
	return &FileReaderAt{
		Bucket: .Bucket,
		name:   ,
		ctx:    ,
	}, nil
}

// ReadAt implements the io.ReaderAt interface.
func ( *FileReaderAt) ( []byte,  int64) ( int,  error) {
	,  := .GetRange(.ctx, .name, , int64(len()))
	if  != nil {
		return 0, 
	}
	defer func() {
		.Close()
	}()

	 := 0
	for  < len() { // Read does not guarantee the buffer will be full, but ReadAt does
		,  = .Read([:])
		 += 
		if  != nil {
			if errors.Is(, io.EOF) {
				// If io.EOF is returned it means we read the end of the file and simply return the total.
				break
			}
			return , 
		}
	}

	return , nil
}