[#1418] writecache: Do not use pointers as parameters

Signed-off-by: Pavel Karpy <carpawell@nspcc.ru>
This commit is contained in:
Pavel Karpy 2022-05-20 18:43:05 +03:00 committed by fyrchik
parent da3ae202f0
commit 1c100fb4b0
3 changed files with 11 additions and 8 deletions

View file

@ -81,7 +81,10 @@ func (s *Shard) Dump(prm *DumpPrm) (*DumpRes, error) {
var count int var count int
if s.hasWriteCache() { if s.hasWriteCache() {
err := s.writeCache.Iterate(new(writecache.IterationPrm).WithHandler(func(data []byte) error { var iterPrm writecache.IterationPrm
iterPrm.WithIgnoreErrors(prm.ignoreErrors)
iterPrm.WithHandler(func(data []byte) error {
var size [4]byte var size [4]byte
binary.LittleEndian.PutUint32(size[:], uint32(len(data))) binary.LittleEndian.PutUint32(size[:], uint32(len(data)))
if _, err := w.Write(size[:]); err != nil { if _, err := w.Write(size[:]); err != nil {
@ -94,7 +97,9 @@ func (s *Shard) Dump(prm *DumpPrm) (*DumpRes, error) {
count++ count++
return nil return nil
}).WithIgnoreErrors(prm.ignoreErrors)) })
err := s.writeCache.Iterate(iterPrm)
if err != nil { if err != nil {
return nil, err return nil, err
} }

View file

@ -19,21 +19,19 @@ type IterationPrm struct {
} }
// WithHandler sets a callback to be executed on every object. // WithHandler sets a callback to be executed on every object.
func (p *IterationPrm) WithHandler(f func([]byte) error) *IterationPrm { func (p *IterationPrm) WithHandler(f func([]byte) error) {
p.handler = f p.handler = f
return p
} }
// WithIgnoreErrors sets a flag indicating that errors should be ignored. // WithIgnoreErrors sets a flag indicating that errors should be ignored.
func (p *IterationPrm) WithIgnoreErrors(ignore bool) *IterationPrm { func (p *IterationPrm) WithIgnoreErrors(ignore bool) {
p.ignoreErrors = ignore p.ignoreErrors = ignore
return p
} }
// Iterate iterates over all objects present in write cache. // Iterate iterates over all objects present in write cache.
// This is very difficult to do correctly unless write-cache is put in read-only mode. // This is very difficult to do correctly unless write-cache is put in read-only mode.
// Thus we silently fail if shard is not in read-only mode to avoid reporting misleading results. // Thus we silently fail if shard is not in read-only mode to avoid reporting misleading results.
func (c *cache) Iterate(prm *IterationPrm) error { func (c *cache) Iterate(prm IterationPrm) error {
c.modeMtx.RLock() c.modeMtx.RLock()
defer c.modeMtx.RUnlock() defer c.modeMtx.RUnlock()
if !c.readOnly() { if !c.readOnly() {

View file

@ -20,7 +20,7 @@ type Cache interface {
Get(address oid.Address) (*object.Object, error) Get(address oid.Address) (*object.Object, error)
Head(oid.Address) (*object.Object, error) Head(oid.Address) (*object.Object, error)
Delete(oid.Address) error Delete(oid.Address) error
Iterate(*IterationPrm) error Iterate(IterationPrm) error
Put(*object.Object) error Put(*object.Object) error
SetMode(Mode) SetMode(Mode)
DumpInfo() Info DumpInfo() Info