2020-11-26 14:26:53 +00:00
|
|
|
package blobovnicza
|
|
|
|
|
|
|
|
import (
|
2023-06-20 08:24:14 +00:00
|
|
|
"context"
|
2021-05-18 08:12:51 +00:00
|
|
|
"fmt"
|
|
|
|
|
2023-03-07 13:38:26 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-node/pkg/local_object_storage/util/logicerr"
|
2023-06-20 08:24:14 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-observability/tracing"
|
2023-03-07 13:38:26 +00:00
|
|
|
oid "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/object/id"
|
2020-11-26 14:26:53 +00:00
|
|
|
"go.etcd.io/bbolt"
|
2023-06-20 08:24:14 +00:00
|
|
|
"go.opentelemetry.io/otel/attribute"
|
|
|
|
"go.opentelemetry.io/otel/trace"
|
2020-11-26 14:26:53 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// PutPrm groups the parameters of Put operation.
|
|
|
|
type PutPrm struct {
|
2022-05-31 17:00:41 +00:00
|
|
|
addr oid.Address
|
2020-11-26 14:26:53 +00:00
|
|
|
|
|
|
|
objData []byte
|
|
|
|
}
|
|
|
|
|
2022-04-21 11:28:05 +00:00
|
|
|
// PutRes groups the resulting values of Put operation.
|
2020-11-26 14:26:53 +00:00
|
|
|
type PutRes struct {
|
|
|
|
}
|
|
|
|
|
2022-04-21 11:28:05 +00:00
|
|
|
// SetAddress sets the address of the saving object.
|
2022-05-31 17:00:41 +00:00
|
|
|
func (p *PutPrm) SetAddress(addr oid.Address) {
|
2020-11-26 14:26:53 +00:00
|
|
|
p.addr = addr
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetMarshaledObject sets binary representation of the object.
|
|
|
|
func (p *PutPrm) SetMarshaledObject(data []byte) {
|
|
|
|
p.objData = data
|
|
|
|
}
|
|
|
|
|
2022-04-21 11:28:05 +00:00
|
|
|
// Put saves an object in Blobovnicza.
|
2020-11-26 14:26:53 +00:00
|
|
|
//
|
|
|
|
// If binary representation of the object is not set,
|
|
|
|
// it is calculated via Marshal method.
|
|
|
|
//
|
|
|
|
// The size of the object MUST BE less that or equal to
|
|
|
|
// the size specified in WithObjectSizeLimit option.
|
|
|
|
//
|
|
|
|
// Returns any error encountered that
|
|
|
|
// did not allow to completely save the object.
|
|
|
|
//
|
|
|
|
// Returns ErrFull if blobovnicza is filled.
|
2021-09-14 14:44:07 +00:00
|
|
|
//
|
|
|
|
// Should not be called in read-only configuration.
|
2023-06-20 08:24:14 +00:00
|
|
|
func (b *Blobovnicza) Put(ctx context.Context, prm PutPrm) (PutRes, error) {
|
|
|
|
_, span := tracing.StartSpanFromContext(ctx, "Blobovnicza.Put",
|
|
|
|
trace.WithAttributes(
|
|
|
|
attribute.String("path", b.path),
|
|
|
|
attribute.String("address", prm.addr.EncodeToString()),
|
|
|
|
attribute.Int("size", len(prm.objData)),
|
|
|
|
))
|
|
|
|
defer span.End()
|
|
|
|
|
2022-03-21 13:16:08 +00:00
|
|
|
sz := uint64(len(prm.objData))
|
2023-10-12 18:35:08 +00:00
|
|
|
bucketName := bucketForSize(sz)
|
2022-05-31 17:00:41 +00:00
|
|
|
key := addressKey(prm.addr)
|
2023-10-12 18:35:08 +00:00
|
|
|
recordSize := sz + uint64(len(key))
|
2022-03-21 13:16:08 +00:00
|
|
|
|
2021-04-09 09:18:38 +00:00
|
|
|
err := b.boltDB.Batch(func(tx *bbolt.Tx) error {
|
2022-03-21 13:16:08 +00:00
|
|
|
buck := tx.Bucket(bucketName)
|
2020-11-26 14:26:53 +00:00
|
|
|
if buck == nil {
|
|
|
|
// expected to happen:
|
|
|
|
// - before initialization step (incorrect usage by design)
|
|
|
|
// - if DB is corrupted (in future this case should be handled)
|
2022-11-09 10:59:24 +00:00
|
|
|
return logicerr.Wrap(fmt.Errorf("(%T) bucket for size %d not created", b, sz))
|
2020-11-26 14:26:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// save the object in bucket
|
2022-03-21 13:16:08 +00:00
|
|
|
if err := buck.Put(key, prm.objData); err != nil {
|
2021-05-18 08:12:51 +00:00
|
|
|
return fmt.Errorf("(%T) could not save object in bucket: %w", b, err)
|
2020-11-26 14:26:53 +00:00
|
|
|
}
|
|
|
|
|
2023-10-12 12:25:16 +00:00
|
|
|
return updateMeta(tx, func(count, size uint64) (uint64, uint64) {
|
2023-10-12 18:35:08 +00:00
|
|
|
return count + 1, size + recordSize
|
2023-10-12 12:25:16 +00:00
|
|
|
})
|
2020-11-26 14:26:53 +00:00
|
|
|
})
|
2022-03-21 13:16:08 +00:00
|
|
|
if err == nil {
|
2023-10-12 18:35:08 +00:00
|
|
|
b.itemAdded(recordSize)
|
2022-03-21 13:16:08 +00:00
|
|
|
}
|
2020-11-26 14:26:53 +00:00
|
|
|
|
2022-05-31 12:22:32 +00:00
|
|
|
return PutRes{}, err
|
2020-11-26 14:26:53 +00:00
|
|
|
}
|
|
|
|
|
2022-05-31 17:00:41 +00:00
|
|
|
func addressKey(addr oid.Address) []byte {
|
|
|
|
return []byte(addr.EncodeToString())
|
2020-11-26 14:26:53 +00:00
|
|
|
}
|
2021-09-14 14:14:25 +00:00
|
|
|
|
2022-05-31 17:00:41 +00:00
|
|
|
func addressFromKey(dst *oid.Address, data []byte) error {
|
|
|
|
return dst.DecodeString(string(data))
|
2021-09-14 14:14:25 +00:00
|
|
|
}
|