2021-04-06 10:56:06 +00:00
|
|
|
package writecache
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
|
|
|
|
objectSDK "github.com/nspcc-dev/neofs-api-go/pkg/object"
|
|
|
|
"github.com/nspcc-dev/neofs-node/pkg/core/object"
|
|
|
|
"github.com/nspcc-dev/neofs-node/pkg/local_object_storage/blobstor/fstree"
|
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
|
|
|
"go.etcd.io/bbolt"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Delete removes object from write-cache.
|
|
|
|
func (c *cache) Delete(addr *objectSDK.Address) error {
|
|
|
|
saddr := addr.String()
|
|
|
|
|
|
|
|
// Check memory cache.
|
|
|
|
c.mtx.Lock()
|
|
|
|
for i := range c.mem {
|
|
|
|
if saddr == c.mem[i].addr {
|
|
|
|
copy(c.mem[i:], c.mem[i+1:])
|
|
|
|
c.mem = c.mem[:len(c.mem)-1]
|
2021-06-21 09:42:09 +00:00
|
|
|
c.curMemSize -= uint64(len(c.mem[i].data))
|
2021-04-06 10:56:06 +00:00
|
|
|
c.mtx.Unlock()
|
2021-09-06 14:28:55 +00:00
|
|
|
storagelog.Write(c.log, storagelog.AddressField(saddr), storagelog.OpField("in-mem DELETE"))
|
2021-04-06 10:56:06 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
c.mtx.Unlock()
|
|
|
|
|
|
|
|
// Check disk cache.
|
2021-06-21 09:42:09 +00:00
|
|
|
var has int
|
2021-04-06 10:56:06 +00:00
|
|
|
_ = c.db.View(func(tx *bbolt.Tx) error {
|
|
|
|
b := tx.Bucket(defaultBucket)
|
2021-06-21 09:42:09 +00:00
|
|
|
has = len(b.Get([]byte(saddr)))
|
2021-04-06 10:56:06 +00:00
|
|
|
return nil
|
|
|
|
})
|
|
|
|
|
2021-06-21 09:42:09 +00:00
|
|
|
if 0 < has {
|
|
|
|
err := c.db.Update(func(tx *bbolt.Tx) error {
|
2021-04-06 10:56:06 +00:00
|
|
|
b := tx.Bucket(defaultBucket)
|
2021-06-21 09:42:09 +00:00
|
|
|
err := b.Delete([]byte(saddr))
|
|
|
|
return err
|
2021-04-06 10:56:06 +00:00
|
|
|
})
|
2021-06-21 09:42:09 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-09-06 14:28:55 +00:00
|
|
|
storagelog.Write(c.log, storagelog.AddressField(saddr), storagelog.OpField("db DELETE"))
|
2021-09-08 09:32:20 +00:00
|
|
|
c.objCounters.DecDB()
|
2021-06-21 09:42:09 +00:00
|
|
|
return nil
|
2021-04-06 10:56:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
err := c.fsTree.Delete(addr)
|
|
|
|
if errors.Is(err, fstree.ErrFileNotFound) {
|
|
|
|
err = object.ErrNotFound
|
|
|
|
}
|
|
|
|
|
2021-09-06 14:28:55 +00:00
|
|
|
if err == nil {
|
|
|
|
storagelog.Write(c.log, storagelog.AddressField(saddr), storagelog.OpField("fstree DELETE"))
|
2021-09-08 09:32:20 +00:00
|
|
|
c.objCounters.DecFS()
|
2021-09-06 14:28:55 +00:00
|
|
|
}
|
|
|
|
|
2021-04-06 10:56:06 +00:00
|
|
|
return err
|
|
|
|
}
|