From 8dced272d22c6a9d1069d81b7397d95fa1620d75 Mon Sep 17 00:00:00 2001 From: Leonard Lyubich Date: Wed, 23 Sep 2020 16:33:54 +0300 Subject: [PATCH] [#43] cmd/neofs-node: Implement in-memory bucket Signed-off-by: Leonard Lyubich --- cmd/neofs-node/object.go | 68 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 68 insertions(+) diff --git a/cmd/neofs-node/object.go b/cmd/neofs-node/object.go index 4d943174c..31560abc1 100644 --- a/cmd/neofs-node/object.go +++ b/cmd/neofs-node/object.go @@ -3,16 +3,84 @@ package main import ( "context" "errors" + "sync" + "github.com/mr-tron/base58" "github.com/nspcc-dev/neofs-api-go/v2/object" objectGRPC "github.com/nspcc-dev/neofs-api-go/v2/object/grpc" "github.com/nspcc-dev/neofs-api-go/v2/session" + "github.com/nspcc-dev/neofs-node/pkg/local_object_storage/bucket" objectTransportGRPC "github.com/nspcc-dev/neofs-node/pkg/network/transport/object/grpc" objectService "github.com/nspcc-dev/neofs-node/pkg/services/object" ) type objectExecutor struct{} +type inMemBucket struct { + bucket.Bucket + *sync.RWMutex + items map[string][]byte +} + +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 +} + func (*objectExecutor) Get(context.Context, *object.GetRequestBody) (objectService.GetObjectBodyStreamer, error) { return nil, errors.New("unimplemented service call") }