[#43] cmd/neofs-node: Implement in-memory bucket
Signed-off-by: Leonard Lyubich <leonard@nspcc.ru>
This commit is contained in:
parent
480362b02f
commit
8dced272d2
1 changed files with 68 additions and 0 deletions
|
@ -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")
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue