2021-04-06 10:56:06 +00:00
|
|
|
package writecache
|
|
|
|
|
|
|
|
import (
|
2022-07-07 12:56:05 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/util/slice"
|
2022-07-05 14:07:40 +00:00
|
|
|
"github.com/nspcc-dev/neofs-node/pkg/local_object_storage/blobstor/common"
|
2022-03-17 08:03:58 +00:00
|
|
|
apistatus "github.com/nspcc-dev/neofs-sdk-go/client/status"
|
2022-03-03 14:19:05 +00:00
|
|
|
objectSDK "github.com/nspcc-dev/neofs-sdk-go/object"
|
2022-05-31 17:00:41 +00:00
|
|
|
oid "github.com/nspcc-dev/neofs-sdk-go/object/id"
|
2021-04-06 10:56:06 +00:00
|
|
|
"go.etcd.io/bbolt"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Get returns object from write-cache.
|
2022-03-17 08:03:58 +00:00
|
|
|
//
|
2022-04-21 11:28:05 +00:00
|
|
|
// Returns an error of type apistatus.ObjectNotFound if the requested object is missing in write-cache.
|
2022-05-31 17:00:41 +00:00
|
|
|
func (c *cache) Get(addr oid.Address) (*objectSDK.Object, error) {
|
|
|
|
saddr := addr.EncodeToString()
|
2021-04-06 10:56:06 +00:00
|
|
|
|
2021-10-13 11:23:03 +00:00
|
|
|
value, err := Get(c.db, []byte(saddr))
|
|
|
|
if err == nil {
|
2022-03-03 14:19:05 +00:00
|
|
|
obj := objectSDK.New()
|
2021-04-06 10:56:06 +00:00
|
|
|
c.flushed.Get(saddr)
|
|
|
|
return obj, obj.Unmarshal(value)
|
|
|
|
}
|
|
|
|
|
2022-07-05 14:07:40 +00:00
|
|
|
data, err := c.fsTree.Get(common.GetPrm{Address: addr})
|
2021-04-06 10:56:06 +00:00
|
|
|
if err != nil {
|
2022-03-17 08:03:58 +00:00
|
|
|
var errNotFound apistatus.ObjectNotFound
|
|
|
|
|
|
|
|
return nil, errNotFound
|
2021-04-06 10:56:06 +00:00
|
|
|
}
|
|
|
|
|
2022-03-03 14:19:05 +00:00
|
|
|
obj := objectSDK.New()
|
2021-04-06 10:56:06 +00:00
|
|
|
if err := obj.Unmarshal(data); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
c.flushed.Get(saddr)
|
|
|
|
return obj, nil
|
|
|
|
}
|
2021-08-31 10:52:09 +00:00
|
|
|
|
|
|
|
// Head returns object header from write-cache.
|
2022-03-17 08:03:58 +00:00
|
|
|
//
|
2022-04-21 11:28:05 +00:00
|
|
|
// Returns an error of type apistatus.ObjectNotFound if the requested object is missing in write-cache.
|
2022-05-31 17:00:41 +00:00
|
|
|
func (c *cache) Head(addr oid.Address) (*objectSDK.Object, error) {
|
2021-08-31 10:52:09 +00:00
|
|
|
obj, err := c.Get(addr)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2022-03-03 14:19:05 +00:00
|
|
|
return obj.CutPayload(), nil
|
2021-08-31 10:52:09 +00:00
|
|
|
}
|
2021-10-13 11:23:03 +00:00
|
|
|
|
|
|
|
// Get fetches object from the underlying database.
|
|
|
|
// Key should be a stringified address.
|
2022-03-17 08:03:58 +00:00
|
|
|
//
|
2022-04-21 11:28:05 +00:00
|
|
|
// Returns an error of type apistatus.ObjectNotFound if the requested object is missing in db.
|
2021-10-13 11:23:03 +00:00
|
|
|
func Get(db *bbolt.DB, key []byte) ([]byte, error) {
|
|
|
|
var value []byte
|
|
|
|
err := db.View(func(tx *bbolt.Tx) error {
|
|
|
|
b := tx.Bucket(defaultBucket)
|
|
|
|
if b == nil {
|
|
|
|
return ErrNoDefaultBucket
|
|
|
|
}
|
|
|
|
value = b.Get(key)
|
|
|
|
if value == nil {
|
2022-03-17 08:03:58 +00:00
|
|
|
var errNotFound apistatus.ObjectNotFound
|
|
|
|
|
|
|
|
return errNotFound
|
2021-10-13 11:23:03 +00:00
|
|
|
}
|
2022-07-07 12:56:05 +00:00
|
|
|
value = slice.Copy(value)
|
2021-10-13 11:23:03 +00:00
|
|
|
return nil
|
|
|
|
})
|
|
|
|
return value, err
|
|
|
|
}
|