2020-12-03 11:29:33 +00:00
|
|
|
package meta
|
|
|
|
|
|
|
|
import (
|
2021-01-22 11:07:08 +00:00
|
|
|
"encoding/binary"
|
2020-12-03 11:29:33 +00:00
|
|
|
"strings"
|
|
|
|
|
2021-11-10 07:08:33 +00:00
|
|
|
cid "github.com/nspcc-dev/neofs-sdk-go/container/id"
|
2020-12-03 11:29:33 +00:00
|
|
|
"go.etcd.io/bbolt"
|
|
|
|
)
|
|
|
|
|
2021-05-31 11:03:17 +00:00
|
|
|
func (db *DB) Containers() (list []*cid.ID, err error) {
|
2020-12-03 11:29:33 +00:00
|
|
|
err = db.boltDB.View(func(tx *bbolt.Tx) error {
|
|
|
|
list, err = db.containers(tx)
|
|
|
|
|
|
|
|
return err
|
|
|
|
})
|
|
|
|
|
|
|
|
return list, err
|
|
|
|
}
|
|
|
|
|
2021-05-31 11:03:17 +00:00
|
|
|
func (db *DB) containers(tx *bbolt.Tx) ([]*cid.ID, error) {
|
|
|
|
result := make([]*cid.ID, 0)
|
2021-10-27 10:52:12 +00:00
|
|
|
unique := make(map[string]struct{})
|
2020-12-03 11:29:33 +00:00
|
|
|
|
|
|
|
err := tx.ForEach(func(name []byte, _ *bbolt.Bucket) error {
|
2021-10-27 10:52:12 +00:00
|
|
|
id := parseContainerID(name, unique)
|
2020-12-03 11:29:33 +00:00
|
|
|
|
|
|
|
if id != nil {
|
|
|
|
result = append(result, id)
|
2021-10-27 10:52:12 +00:00
|
|
|
unique[id.String()] = struct{}{}
|
2020-12-03 11:29:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
|
|
|
|
return result, err
|
|
|
|
}
|
|
|
|
|
2021-05-31 11:03:17 +00:00
|
|
|
func (db *DB) ContainerSize(id *cid.ID) (size uint64, err error) {
|
2021-01-22 11:07:08 +00:00
|
|
|
err = db.boltDB.Update(func(tx *bbolt.Tx) error {
|
|
|
|
size, err = db.containerSize(tx, id)
|
|
|
|
|
|
|
|
return err
|
|
|
|
})
|
|
|
|
|
|
|
|
return size, err
|
|
|
|
}
|
|
|
|
|
2021-05-31 11:03:17 +00:00
|
|
|
func (db *DB) containerSize(tx *bbolt.Tx, id *cid.ID) (uint64, error) {
|
2021-01-22 11:07:08 +00:00
|
|
|
containerVolume, err := tx.CreateBucketIfNotExists(containerVolumeBucketName)
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
|
|
|
|
key := id.ToV2().GetValue()
|
|
|
|
|
|
|
|
return parseContainerSize(containerVolume.Get(key)), nil
|
|
|
|
}
|
|
|
|
|
2021-10-27 10:52:12 +00:00
|
|
|
func parseContainerID(name []byte, ignore map[string]struct{}) *cid.ID {
|
|
|
|
containerID := cid.New()
|
|
|
|
strContainerID := strings.Split(string(name), invalidBase58String)[0]
|
2020-12-03 11:29:33 +00:00
|
|
|
|
2021-10-27 10:52:12 +00:00
|
|
|
if _, ok := ignore[strContainerID]; ok {
|
|
|
|
return nil
|
2020-12-03 11:29:33 +00:00
|
|
|
}
|
|
|
|
|
2021-10-27 10:52:12 +00:00
|
|
|
if err := containerID.Parse(strContainerID); err != nil {
|
|
|
|
return nil
|
|
|
|
}
|
2020-12-03 11:29:33 +00:00
|
|
|
|
2021-10-27 10:52:12 +00:00
|
|
|
return containerID
|
2020-12-03 11:29:33 +00:00
|
|
|
}
|
2021-01-22 11:07:08 +00:00
|
|
|
|
|
|
|
func parseContainerSize(v []byte) uint64 {
|
|
|
|
if len(v) == 0 {
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
|
|
|
return binary.LittleEndian.Uint64(v)
|
|
|
|
}
|
|
|
|
|
2021-05-31 11:03:17 +00:00
|
|
|
func changeContainerSize(tx *bbolt.Tx, id *cid.ID, delta uint64, increase bool) error {
|
2021-01-22 11:07:08 +00:00
|
|
|
containerVolume, err := tx.CreateBucketIfNotExists(containerVolumeBucketName)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
key := id.ToV2().GetValue()
|
|
|
|
size := parseContainerSize(containerVolume.Get(key))
|
|
|
|
|
|
|
|
if increase {
|
|
|
|
size += delta
|
|
|
|
} else if size > delta {
|
|
|
|
size -= delta
|
|
|
|
} else {
|
|
|
|
size = 0
|
|
|
|
}
|
|
|
|
|
|
|
|
buf := make([]byte, 8) // consider using sync.Pool to decrease allocations
|
|
|
|
binary.LittleEndian.PutUint64(buf, size)
|
|
|
|
|
|
|
|
return containerVolume.Put(key, buf)
|
|
|
|
}
|