2020-09-02 12:50:51 +00:00
|
|
|
package localstore
|
|
|
|
|
|
|
|
import (
|
2020-09-16 12:07:23 +00:00
|
|
|
objectSDK "github.com/nspcc-dev/neofs-api-go/pkg/object"
|
2020-09-02 12:50:51 +00:00
|
|
|
"github.com/nspcc-dev/neofs-node/pkg/core/object"
|
|
|
|
"github.com/pkg/errors"
|
|
|
|
"go.uber.org/zap"
|
|
|
|
)
|
|
|
|
|
2020-09-16 12:11:54 +00:00
|
|
|
func addressBytes(a *objectSDK.Address) ([]byte, error) {
|
|
|
|
return a.ToV2().StableMarshal(nil)
|
|
|
|
}
|
|
|
|
|
|
|
|
func objectBytes(o *object.Object) ([]byte, error) {
|
|
|
|
return o.ToV2().StableMarshal(nil)
|
|
|
|
}
|
|
|
|
|
2020-09-02 12:50:51 +00:00
|
|
|
func (s *Storage) Put(obj *object.Object) error {
|
2020-09-16 12:11:54 +00:00
|
|
|
addrBytes, err := addressBytes(obj.Address())
|
2020-09-02 12:50:51 +00:00
|
|
|
if err != nil {
|
|
|
|
return errors.Wrap(err, "could not marshal object address")
|
|
|
|
}
|
|
|
|
|
2020-09-16 12:11:54 +00:00
|
|
|
objBytes, err := objectBytes(obj)
|
2020-09-02 12:50:51 +00:00
|
|
|
if err != nil {
|
|
|
|
return errors.Wrap(err, "could not marshal the object")
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := s.blobBucket.Set(addrBytes, objBytes); err != nil {
|
|
|
|
return errors.Wrap(err, "could no save object in BLOB storage")
|
|
|
|
}
|
|
|
|
|
2020-10-29 17:16:49 +00:00
|
|
|
if err := s.metaBase.Put(object.NewRawFromObject(obj).CutPayload().Object()); err != nil {
|
2020-09-02 12:50:51 +00:00
|
|
|
return errors.Wrap(err, "could not save object in meta storage")
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-09-16 12:07:23 +00:00
|
|
|
func (s *Storage) Delete(addr *objectSDK.Address) error {
|
2020-09-16 12:11:54 +00:00
|
|
|
addrBytes, err := addressBytes(addr)
|
2020-09-02 12:50:51 +00:00
|
|
|
if err != nil {
|
|
|
|
return errors.Wrap(err, "could not marshal object address")
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := s.blobBucket.Del(addrBytes); err != nil {
|
|
|
|
s.log.Warn("could not remove object from BLOB storage",
|
|
|
|
zap.Error(err),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2020-10-29 17:16:49 +00:00
|
|
|
if err := s.metaBase.Delete(addr); err != nil {
|
2020-09-02 12:50:51 +00:00
|
|
|
return errors.Wrap(err, "could not remove object from meta storage")
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-09-16 12:07:23 +00:00
|
|
|
func (s *Storage) Get(addr *objectSDK.Address) (*object.Object, error) {
|
2020-09-16 12:11:54 +00:00
|
|
|
addrBytes, err := addressBytes(addr)
|
2020-09-02 12:50:51 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrap(err, "could not marshal object address")
|
|
|
|
}
|
|
|
|
|
|
|
|
objBytes, err := s.blobBucket.Get(addrBytes)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrap(err, "could not get object from BLOB storage")
|
|
|
|
}
|
|
|
|
|
2020-11-16 09:43:52 +00:00
|
|
|
obj := object.New()
|
|
|
|
|
|
|
|
return obj, obj.Unmarshal(objBytes)
|
2020-09-02 12:50:51 +00:00
|
|
|
}
|
|
|
|
|
2020-10-29 17:16:49 +00:00
|
|
|
func (s *Storage) Head(addr *objectSDK.Address) (*object.Object, error) {
|
|
|
|
return s.metaBase.Get(addr)
|
2020-09-02 12:50:51 +00:00
|
|
|
}
|
|
|
|
|
2020-10-29 17:16:49 +00:00
|
|
|
func (s *Storage) Select(fs objectSDK.SearchFilters) ([]*objectSDK.Address, error) {
|
|
|
|
return s.metaBase.Select(fs)
|
2020-09-02 12:50:51 +00:00
|
|
|
}
|