frostfs-node/pkg/services/object/common/target/builder.go
Airat Arifullin 90cf89b26c
All checks were successful
DCO action / DCO (pull_request) Successful in 5m1s
Tests and linters / Run gofumpt (pull_request) Successful in 5m31s
Pre-commit hooks / Pre-commit (pull_request) Successful in 5m54s
Vulncheck / Vulncheck (pull_request) Successful in 5m39s
Build / Build Components (1.22) (pull_request) Successful in 6m6s
Build / Build Components (1.23) (pull_request) Successful in 6m7s
Tests and linters / Tests (1.22) (pull_request) Successful in 6m0s
Tests and linters / Tests (1.23) (pull_request) Successful in 6m9s
Tests and linters / Staticcheck (pull_request) Successful in 6m9s
Tests and linters / Lint (pull_request) Successful in 6m19s
Tests and linters / Tests with -race (pull_request) Successful in 6m33s
Tests and linters / gopls check (pull_request) Successful in 6m46s
[#1310] object: Move target initialization to separate package
* Split the logic of write target initialization to different packages;
* Refactor patch and put services: since both service initialize the target
  themselves.

Signed-off-by: Airat Arifullin <a.arifullin@yadro.com>
2024-09-04 10:21:10 +03:00

54 lines
1.2 KiB
Go

package writetarget
import (
"context"
objectSDK "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/object"
"git.frostfs.info/TrueCloudLab/frostfs-sdk-go/object/transformer"
)
var _ transformer.ChunkedObjectWriter = (*inMemoryObjectBuilder)(nil)
type inMemoryObjectBuilder struct {
objectWriter transformer.ObjectWriter
payload *payload
obj *objectSDK.Object
}
func newInMemoryObjectBuilder(objectWriter transformer.ObjectWriter) *inMemoryObjectBuilder {
return &inMemoryObjectBuilder{
objectWriter: objectWriter,
payload: getPayload(),
}
}
func (b *inMemoryObjectBuilder) Close(ctx context.Context) (*transformer.AccessIdentifiers, error) {
defer func() {
putPayload(b.payload)
b.payload = nil
}()
b.obj.SetPayload(b.payload.Data)
if err := b.objectWriter.WriteObject(ctx, b.obj); err != nil {
return nil, err
}
id, _ := b.obj.ID()
return &transformer.AccessIdentifiers{
SelfID: id,
}, nil
}
func (b *inMemoryObjectBuilder) Write(_ context.Context, p []byte) (int, error) {
b.payload.Data = append(b.payload.Data, p...)
return len(p), nil
}
func (b *inMemoryObjectBuilder) WriteHeader(_ context.Context, obj *objectSDK.Object) error {
b.obj = obj
return nil
}