Evgenii Stratonikov
4c520be9f1
All checks were successful
Vulncheck / Vulncheck (pull_request) Successful in 1m59s
DCO action / DCO (pull_request) Successful in 1m53s
Build / Build Components (1.21) (pull_request) Successful in 2m20s
Build / Build Components (1.22) (pull_request) Successful in 2m26s
Pre-commit hooks / Pre-commit (pull_request) Successful in 2m56s
Tests and linters / Staticcheck (pull_request) Successful in 2m47s
Tests and linters / Tests (1.21) (pull_request) Successful in 2m56s
Tests and linters / Tests (1.22) (pull_request) Successful in 2m57s
Tests and linters / Lint (pull_request) Successful in 3m7s
Tests and linters / Tests with -race (pull_request) Successful in 3m7s
Tests and linters / gopls check (pull_request) Successful in 3m46s
Signed-off-by: Evgenii Stratonikov <e.stratonikov@yadro.com>
88 lines
2 KiB
Go
88 lines
2 KiB
Go
package blobovnicza
|
|
|
|
import (
|
|
"bytes"
|
|
"context"
|
|
"errors"
|
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-observability/tracing"
|
|
apistatus "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/client/status"
|
|
oid "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/object/id"
|
|
"go.etcd.io/bbolt"
|
|
"go.opentelemetry.io/otel/attribute"
|
|
"go.opentelemetry.io/otel/trace"
|
|
)
|
|
|
|
// GetPrm groups the parameters of Get operation.
|
|
type GetPrm struct {
|
|
addr oid.Address
|
|
}
|
|
|
|
// GetRes groups the resulting values of Get operation.
|
|
type GetRes struct {
|
|
obj []byte
|
|
}
|
|
|
|
// SetAddress sets the address of the requested object.
|
|
func (p *GetPrm) SetAddress(addr oid.Address) {
|
|
p.addr = addr
|
|
}
|
|
|
|
// Object returns binary representation of the requested object.
|
|
func (p GetRes) Object() []byte {
|
|
return p.obj
|
|
}
|
|
|
|
// special error for normal bbolt.Tx.ForEach interruption.
|
|
var errInterruptForEach = errors.New("interrupt for-each")
|
|
|
|
// Get reads an object from Blobovnicza by address.
|
|
//
|
|
// Returns any error encountered that
|
|
// did not allow to completely read the object.
|
|
//
|
|
// Returns an error of type apistatus.ObjectNotFound if the requested object is not
|
|
// presented in Blobovnicza.
|
|
func (b *Blobovnicza) Get(ctx context.Context, prm GetPrm) (GetRes, error) {
|
|
_, span := tracing.StartSpanFromContext(ctx, "Blobovnicza.Get",
|
|
trace.WithAttributes(
|
|
attribute.String("path", b.path),
|
|
attribute.String("address", prm.addr.EncodeToString()),
|
|
))
|
|
defer span.End()
|
|
|
|
b.controlMtx.RLock()
|
|
defer b.controlMtx.RUnlock()
|
|
|
|
var (
|
|
data []byte
|
|
addrKey = addressKey(prm.addr)
|
|
)
|
|
|
|
if err := b.boltDB.View(func(tx *bbolt.Tx) error {
|
|
return tx.ForEach(func(bucketName []byte, buck *bbolt.Bucket) error {
|
|
if isNonDataBucket(bucketName) {
|
|
return nil
|
|
}
|
|
|
|
data = buck.Get(addrKey)
|
|
if data == nil {
|
|
return nil
|
|
}
|
|
|
|
data = bytes.Clone(data)
|
|
|
|
return errInterruptForEach
|
|
})
|
|
}); err != nil && err != errInterruptForEach {
|
|
return GetRes{}, err
|
|
}
|
|
|
|
if data == nil {
|
|
return GetRes{}, new(apistatus.ObjectNotFound)
|
|
}
|
|
|
|
return GetRes{
|
|
obj: data,
|
|
}, nil
|
|
}
|