forked from TrueCloudLab/frostfs-node
ddaed283e9
1. Remove in-memory cache. It doesn't persist objects and if we want more speed, `NoSync` option can be used for the bolt DB. 2. Put to the metabase in a synchronous fashion. This considerably simplifies overall logic and plays nicely with the metabase bolt DB batch settings. Signed-off-by: Evgenii Stratonikov <evgeniy@nspcc.ru>
73 lines
1.8 KiB
Go
73 lines
1.8 KiB
Go
package writecache
|
|
|
|
import (
|
|
"github.com/nspcc-dev/neofs-node/pkg/local_object_storage/blobstor/common"
|
|
apistatus "github.com/nspcc-dev/neofs-sdk-go/client/status"
|
|
objectSDK "github.com/nspcc-dev/neofs-sdk-go/object"
|
|
oid "github.com/nspcc-dev/neofs-sdk-go/object/id"
|
|
"go.etcd.io/bbolt"
|
|
)
|
|
|
|
// Get returns object from write-cache.
|
|
//
|
|
// Returns an error of type apistatus.ObjectNotFound if the requested object is missing in write-cache.
|
|
func (c *cache) Get(addr oid.Address) (*objectSDK.Object, error) {
|
|
saddr := addr.EncodeToString()
|
|
|
|
value, err := Get(c.db, []byte(saddr))
|
|
if err == nil {
|
|
obj := objectSDK.New()
|
|
c.flushed.Get(saddr)
|
|
return obj, obj.Unmarshal(value)
|
|
}
|
|
|
|
data, err := c.fsTree.Get(common.GetPrm{Address: addr})
|
|
if err != nil {
|
|
var errNotFound apistatus.ObjectNotFound
|
|
|
|
return nil, errNotFound
|
|
}
|
|
|
|
obj := objectSDK.New()
|
|
if err := obj.Unmarshal(data); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
c.flushed.Get(saddr)
|
|
return obj, nil
|
|
}
|
|
|
|
// Head returns object header from write-cache.
|
|
//
|
|
// Returns an error of type apistatus.ObjectNotFound if the requested object is missing in write-cache.
|
|
func (c *cache) Head(addr oid.Address) (*objectSDK.Object, error) {
|
|
obj, err := c.Get(addr)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return obj.CutPayload(), nil
|
|
}
|
|
|
|
// Get fetches object from the underlying database.
|
|
// Key should be a stringified address.
|
|
//
|
|
// Returns an error of type apistatus.ObjectNotFound if the requested object is missing in db.
|
|
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 {
|
|
var errNotFound apistatus.ObjectNotFound
|
|
|
|
return errNotFound
|
|
}
|
|
value = cloneBytes(value)
|
|
return nil
|
|
})
|
|
return value, err
|
|
}
|