2020-08-25 13:37:10 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2020-08-25 14:44:41 +00:00
|
|
|
"context"
|
2020-09-23 13:28:42 +00:00
|
|
|
"errors"
|
2020-09-23 13:33:54 +00:00
|
|
|
"sync"
|
2020-08-25 14:44:41 +00:00
|
|
|
|
2020-09-23 13:33:54 +00:00
|
|
|
"github.com/mr-tron/base58"
|
2020-08-25 14:44:41 +00:00
|
|
|
"github.com/nspcc-dev/neofs-api-go/v2/object"
|
2020-08-25 13:37:10 +00:00
|
|
|
objectGRPC "github.com/nspcc-dev/neofs-api-go/v2/object/grpc"
|
2020-08-25 14:44:41 +00:00
|
|
|
"github.com/nspcc-dev/neofs-api-go/v2/session"
|
2020-09-23 13:33:54 +00:00
|
|
|
"github.com/nspcc-dev/neofs-node/pkg/local_object_storage/bucket"
|
2020-08-25 13:37:10 +00:00
|
|
|
objectTransportGRPC "github.com/nspcc-dev/neofs-node/pkg/network/transport/object/grpc"
|
|
|
|
objectService "github.com/nspcc-dev/neofs-node/pkg/services/object"
|
|
|
|
)
|
|
|
|
|
2020-08-25 15:45:36 +00:00
|
|
|
type objectExecutor struct{}
|
2020-08-25 14:44:41 +00:00
|
|
|
|
2020-09-23 13:33:54 +00:00
|
|
|
type inMemBucket struct {
|
|
|
|
bucket.Bucket
|
|
|
|
*sync.RWMutex
|
|
|
|
items map[string][]byte
|
|
|
|
}
|
|
|
|
|
2020-09-23 13:35:40 +00:00
|
|
|
type maxSzSrc struct {
|
|
|
|
v uint64
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *maxSzSrc) MaxObjectSize() uint64 {
|
|
|
|
return s.v
|
|
|
|
}
|
|
|
|
|
2020-09-23 13:33:54 +00:00
|
|
|
func newBucket() bucket.Bucket {
|
|
|
|
return &inMemBucket{
|
|
|
|
RWMutex: new(sync.RWMutex),
|
|
|
|
items: map[string][]byte{},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *inMemBucket) Get(key []byte) ([]byte, error) {
|
|
|
|
b.RLock()
|
|
|
|
v, ok := b.items[base58.Encode(key)]
|
|
|
|
b.RUnlock()
|
|
|
|
|
|
|
|
if !ok {
|
|
|
|
return nil, errors.New("not found")
|
|
|
|
}
|
|
|
|
|
|
|
|
return v, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *inMemBucket) Set(key, value []byte) error {
|
|
|
|
k := base58.Encode(key)
|
|
|
|
|
|
|
|
b.Lock()
|
|
|
|
b.items[k] = makeCopy(value)
|
|
|
|
b.Unlock()
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *inMemBucket) Iterate(handler bucket.FilterHandler) error {
|
|
|
|
if handler == nil {
|
|
|
|
return bucket.ErrNilFilterHandler
|
|
|
|
}
|
|
|
|
|
|
|
|
b.RLock()
|
|
|
|
for key, val := range b.items {
|
|
|
|
k, err := base58.Decode(key)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
v := makeCopy(val)
|
|
|
|
|
|
|
|
if !handler(k, v) {
|
|
|
|
return bucket.ErrIteratingAborted
|
|
|
|
}
|
|
|
|
}
|
|
|
|
b.RUnlock()
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func makeCopy(val []byte) []byte {
|
|
|
|
tmp := make([]byte, len(val))
|
|
|
|
copy(tmp, val)
|
|
|
|
|
|
|
|
return tmp
|
|
|
|
}
|
|
|
|
|
2020-08-25 14:44:41 +00:00
|
|
|
func (*objectExecutor) Get(context.Context, *object.GetRequestBody) (objectService.GetObjectBodyStreamer, error) {
|
2020-09-23 13:28:42 +00:00
|
|
|
return nil, errors.New("unimplemented service call")
|
2020-08-25 15:45:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (*objectExecutor) Put(context.Context) (objectService.PutObjectBodyStreamer, error) {
|
2020-09-23 13:28:42 +00:00
|
|
|
return nil, errors.New("unimplemented service call")
|
2020-08-25 14:44:41 +00:00
|
|
|
}
|
|
|
|
|
2020-09-23 13:28:42 +00:00
|
|
|
func (*objectExecutor) Head(context.Context, *object.HeadRequestBody) (*object.HeadResponseBody, error) {
|
|
|
|
return nil, errors.New("unimplemented service call")
|
2020-08-25 14:44:41 +00:00
|
|
|
}
|
|
|
|
|
2020-09-23 13:28:42 +00:00
|
|
|
func (s *objectExecutor) Search(context.Context, *object.SearchRequestBody) (objectService.SearchObjectBodyStreamer, error) {
|
|
|
|
return nil, errors.New("unimplemented service call")
|
2020-08-25 14:44:41 +00:00
|
|
|
}
|
|
|
|
|
2020-08-25 16:12:54 +00:00
|
|
|
func (*objectExecutor) Delete(_ context.Context, body *object.DeleteRequestBody) (*object.DeleteResponseBody, error) {
|
2020-09-23 13:28:42 +00:00
|
|
|
return nil, errors.New("unimplemented service call")
|
2020-08-25 15:58:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (*objectExecutor) GetRange(_ context.Context, body *object.GetRangeRequestBody) (objectService.GetRangeObjectBodyStreamer, error) {
|
2020-09-23 13:28:42 +00:00
|
|
|
return nil, errors.New("unimplemented service call")
|
2020-08-25 14:44:41 +00:00
|
|
|
}
|
|
|
|
|
2020-09-23 13:28:42 +00:00
|
|
|
func (*objectExecutor) GetRangeHash(context.Context, *object.GetRangeHashRequestBody) (*object.GetRangeHashResponseBody, error) {
|
|
|
|
return nil, errors.New("unimplemented service call")
|
2020-08-25 14:44:41 +00:00
|
|
|
}
|
|
|
|
|
2020-08-25 13:37:10 +00:00
|
|
|
func initObjectService(c *cfg) {
|
2020-08-25 14:44:41 +00:00
|
|
|
metaHdr := new(session.ResponseMetaHeader)
|
|
|
|
xHdr := new(session.XHeader)
|
|
|
|
xHdr.SetKey("test X-Header key for Object service")
|
|
|
|
xHdr.SetValue("test X-Header value for Object service")
|
|
|
|
metaHdr.SetXHeaders([]*session.XHeader{xHdr})
|
|
|
|
|
2020-08-25 13:37:10 +00:00
|
|
|
objectGRPC.RegisterObjectServiceServer(c.cfgGRPC.server,
|
|
|
|
objectTransportGRPC.New(
|
|
|
|
objectService.NewSignService(
|
|
|
|
c.key,
|
2020-08-25 14:44:41 +00:00
|
|
|
objectService.NewExecutionService(
|
|
|
|
new(objectExecutor),
|
|
|
|
metaHdr,
|
|
|
|
),
|
2020-08-25 13:37:10 +00:00
|
|
|
),
|
|
|
|
),
|
|
|
|
)
|
|
|
|
}
|