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-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-09-23 13:42:50 +00:00
|
|
|
headsvc "github.com/nspcc-dev/neofs-node/pkg/services/object/head/v2"
|
|
|
|
putsvc "github.com/nspcc-dev/neofs-node/pkg/services/object/put/v2"
|
|
|
|
searchsvc "github.com/nspcc-dev/neofs-node/pkg/services/object/search/v2"
|
2020-08-25 13:37:10 +00:00
|
|
|
)
|
|
|
|
|
2020-09-23 13:42:50 +00:00
|
|
|
type objectSvc struct {
|
|
|
|
put *putsvc.Service
|
|
|
|
|
|
|
|
search *searchsvc.Service
|
|
|
|
|
|
|
|
head *headsvc.Service
|
|
|
|
}
|
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-09-23 13:42:50 +00:00
|
|
|
func (s *objectSvc) Put(ctx context.Context) (object.PutObjectStreamer, error) {
|
|
|
|
return s.put.Put(ctx)
|
2020-08-25 15:45:36 +00:00
|
|
|
}
|
|
|
|
|
2020-09-23 13:42:50 +00:00
|
|
|
func (s *objectSvc) Head(ctx context.Context, req *object.HeadRequest) (*object.HeadResponse, error) {
|
|
|
|
return s.head.Head(ctx, req)
|
2020-08-25 14:44:41 +00:00
|
|
|
}
|
|
|
|
|
2020-09-23 13:42:50 +00:00
|
|
|
func (s *objectSvc) Search(ctx context.Context, req *object.SearchRequest) (object.SearchObjectStreamer, error) {
|
|
|
|
return s.search.Search(ctx, req)
|
2020-08-25 14:44:41 +00:00
|
|
|
}
|
|
|
|
|
2020-09-23 13:42:50 +00:00
|
|
|
func (*objectSvc) Get(context.Context, *object.GetRequest) (object.GetObjectStreamer, 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:42:50 +00:00
|
|
|
func (*objectSvc) Delete(context.Context, *object.DeleteRequest) (*object.DeleteResponse, error) {
|
2020-09-23 13:28:42 +00:00
|
|
|
return nil, errors.New("unimplemented service call")
|
2020-08-25 15:58:31 +00:00
|
|
|
}
|
|
|
|
|
2020-09-23 13:42:50 +00:00
|
|
|
func (*objectSvc) GetRange(context.Context, *object.GetRangeRequest) (object.GetRangeObjectStreamer, 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:42:50 +00:00
|
|
|
func (*objectSvc) GetRangeHash(context.Context, *object.GetRangeHashRequest) (*object.GetRangeHashResponse, 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-08-25 13:37:10 +00:00
|
|
|
func initObjectService(c *cfg) {
|
2020-09-23 13:42:50 +00:00
|
|
|
svc := new(objectSvc)
|
2020-08-25 14:44:41 +00:00
|
|
|
|
2020-08-25 13:37:10 +00:00
|
|
|
objectGRPC.RegisterObjectServiceServer(c.cfgGRPC.server,
|
|
|
|
objectTransportGRPC.New(
|
|
|
|
objectService.NewSignService(
|
|
|
|
c.key,
|
2020-09-23 13:42:50 +00:00
|
|
|
svc,
|
2020-08-25 13:37:10 +00:00
|
|
|
),
|
|
|
|
),
|
|
|
|
)
|
|
|
|
}
|