2021-04-06 10:56:06 +00:00
|
|
|
package writecache
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
|
|
|
|
"github.com/nspcc-dev/neofs-node/pkg/core/object"
|
2021-09-06 14:28:55 +00:00
|
|
|
storagelog "github.com/nspcc-dev/neofs-node/pkg/local_object_storage/internal/log"
|
2021-04-06 10:56:06 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// ErrBigObject is returned when object is too big to be placed in cache.
|
|
|
|
var ErrBigObject = errors.New("too big object")
|
|
|
|
|
|
|
|
// Put puts object to write-cache.
|
|
|
|
func (c *cache) Put(o *object.Object) error {
|
2022-01-18 12:47:16 +00:00
|
|
|
c.modeMtx.RLock()
|
|
|
|
defer c.modeMtx.RUnlock()
|
|
|
|
if c.mode == ModeReadOnly {
|
|
|
|
return ErrReadOnly
|
|
|
|
}
|
|
|
|
|
2021-04-06 10:56:06 +00:00
|
|
|
sz := uint64(o.ToV2().StableSize())
|
|
|
|
if sz > c.maxObjectSize {
|
|
|
|
return ErrBigObject
|
|
|
|
}
|
|
|
|
|
2021-11-10 07:08:33 +00:00
|
|
|
data, err := o.Marshal()
|
2021-04-06 10:56:06 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
oi := objectInfo{
|
|
|
|
addr: o.Address().String(),
|
|
|
|
obj: o,
|
|
|
|
data: data,
|
|
|
|
}
|
|
|
|
|
|
|
|
c.mtx.Lock()
|
|
|
|
|
2021-10-27 11:48:33 +00:00
|
|
|
if sz <= c.smallObjectSize && c.curMemSize+sz <= c.maxMemSize {
|
2021-04-06 10:56:06 +00:00
|
|
|
c.curMemSize += sz
|
|
|
|
c.mem = append(c.mem, oi)
|
|
|
|
|
|
|
|
c.mtx.Unlock()
|
2021-09-06 14:28:55 +00:00
|
|
|
|
|
|
|
storagelog.Write(c.log, storagelog.AddressField(oi.addr), storagelog.OpField("in-mem PUT"))
|
|
|
|
|
2021-04-06 10:56:06 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
c.mtx.Unlock()
|
|
|
|
|
2021-10-27 11:48:33 +00:00
|
|
|
if sz <= c.smallObjectSize {
|
|
|
|
c.persistSmallObjects([]objectInfo{oi})
|
|
|
|
} else {
|
|
|
|
c.persistBigObject(oi)
|
|
|
|
}
|
2021-04-06 10:56:06 +00:00
|
|
|
return nil
|
|
|
|
}
|