2020-11-23 13:30:56 +00:00
|
|
|
package meta
|
|
|
|
|
|
|
|
import (
|
2021-02-16 15:56:59 +00:00
|
|
|
"bytes"
|
2020-11-23 13:30:56 +00:00
|
|
|
"strings"
|
|
|
|
|
2021-05-31 11:03:17 +00:00
|
|
|
cid "github.com/nspcc-dev/neofs-api-go/pkg/container/id"
|
2020-11-23 13:30:56 +00:00
|
|
|
"github.com/nspcc-dev/neofs-api-go/pkg/object"
|
2021-09-13 08:23:58 +00:00
|
|
|
"go.etcd.io/bbolt"
|
2020-11-23 13:30:56 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
/*
|
|
|
|
We might increase performance by not using string representation of
|
|
|
|
identities and addresses. String representation require base58 encoding that
|
|
|
|
slows execution. Instead we can try to marshal these structures directly into
|
|
|
|
bytes. Check it later.
|
|
|
|
*/
|
|
|
|
|
2020-12-17 08:07:51 +00:00
|
|
|
const invalidBase58String = "_"
|
|
|
|
|
2020-11-23 13:30:56 +00:00
|
|
|
var (
|
2021-01-22 11:07:08 +00:00
|
|
|
graveyardBucketName = []byte(invalidBase58String + "Graveyard")
|
|
|
|
toMoveItBucketName = []byte(invalidBase58String + "ToMoveIt")
|
|
|
|
containerVolumeBucketName = []byte(invalidBase58String + "ContainerSize")
|
2020-11-23 13:30:56 +00:00
|
|
|
|
|
|
|
zeroValue = []byte{0xFF}
|
|
|
|
|
2020-12-17 08:07:51 +00:00
|
|
|
smallPostfix = invalidBase58String + "small"
|
|
|
|
storageGroupPostfix = invalidBase58String + "SG"
|
|
|
|
tombstonePostfix = invalidBase58String + "TS"
|
|
|
|
ownerPostfix = invalidBase58String + "ownerid"
|
|
|
|
payloadHashPostfix = invalidBase58String + "payloadhash"
|
|
|
|
rootPostfix = invalidBase58String + "root"
|
|
|
|
parentPostfix = invalidBase58String + "parent"
|
|
|
|
splitPostfix = invalidBase58String + "splitid"
|
2020-11-23 13:30:56 +00:00
|
|
|
|
2020-12-17 08:07:51 +00:00
|
|
|
userAttributePostfix = invalidBase58String + "attr_"
|
2020-12-03 09:31:51 +00:00
|
|
|
|
|
|
|
splitInfoError *object.SplitInfoError // for errors.As comparisons
|
2020-11-23 13:30:56 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// primaryBucketName returns <CID>.
|
2021-05-31 11:03:17 +00:00
|
|
|
func primaryBucketName(cid *cid.ID) []byte {
|
2020-11-23 13:30:56 +00:00
|
|
|
return []byte(cid.String())
|
|
|
|
}
|
|
|
|
|
|
|
|
// tombstoneBucketName returns <CID>_TS.
|
2021-05-31 11:03:17 +00:00
|
|
|
func tombstoneBucketName(cid *cid.ID) []byte {
|
2020-11-23 13:30:56 +00:00
|
|
|
return []byte(cid.String() + tombstonePostfix)
|
|
|
|
}
|
|
|
|
|
|
|
|
// storageGroupBucketName returns <CID>_SG.
|
2021-05-31 11:03:17 +00:00
|
|
|
func storageGroupBucketName(cid *cid.ID) []byte {
|
2020-11-23 13:30:56 +00:00
|
|
|
return []byte(cid.String() + storageGroupPostfix)
|
|
|
|
}
|
|
|
|
|
|
|
|
// smallBucketName returns <CID>_small.
|
2021-05-31 11:03:17 +00:00
|
|
|
func smallBucketName(cid *cid.ID) []byte {
|
2020-11-23 13:30:56 +00:00
|
|
|
return []byte(cid.String() + smallPostfix) // consider caching output values
|
|
|
|
}
|
|
|
|
|
|
|
|
// attributeBucketName returns <CID>_attr_<attributeKey>.
|
2021-05-31 11:03:17 +00:00
|
|
|
func attributeBucketName(cid *cid.ID, attributeKey string) []byte {
|
2020-11-23 13:30:56 +00:00
|
|
|
sb := strings.Builder{} // consider getting string builders from sync.Pool
|
|
|
|
sb.WriteString(cid.String())
|
|
|
|
sb.WriteString(userAttributePostfix)
|
|
|
|
sb.WriteString(attributeKey)
|
|
|
|
|
|
|
|
return []byte(sb.String())
|
|
|
|
}
|
|
|
|
|
2021-02-16 15:56:59 +00:00
|
|
|
// returns <CID> from attributeBucketName result, nil otherwise.
|
|
|
|
func cidFromAttributeBucket(val []byte, attributeKey string) []byte {
|
|
|
|
suffix := []byte(userAttributePostfix + attributeKey)
|
|
|
|
if !bytes.HasSuffix(val, suffix) {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return val[:len(val)-len(suffix)]
|
|
|
|
}
|
|
|
|
|
2020-11-23 13:30:56 +00:00
|
|
|
// payloadHashBucketName returns <CID>_payloadhash.
|
2021-05-31 11:03:17 +00:00
|
|
|
func payloadHashBucketName(cid *cid.ID) []byte {
|
2020-11-23 13:30:56 +00:00
|
|
|
return []byte(cid.String() + payloadHashPostfix)
|
|
|
|
}
|
|
|
|
|
|
|
|
// rootBucketName returns <CID>_root.
|
2021-05-31 11:03:17 +00:00
|
|
|
func rootBucketName(cid *cid.ID) []byte {
|
2020-11-23 13:30:56 +00:00
|
|
|
return []byte(cid.String() + rootPostfix)
|
|
|
|
}
|
|
|
|
|
|
|
|
// ownerBucketName returns <CID>_ownerid.
|
2021-05-31 11:03:17 +00:00
|
|
|
func ownerBucketName(cid *cid.ID) []byte {
|
2020-11-23 13:30:56 +00:00
|
|
|
return []byte(cid.String() + ownerPostfix)
|
|
|
|
}
|
|
|
|
|
2020-11-30 08:46:18 +00:00
|
|
|
// parentBucketName returns <CID>_parent.
|
2021-05-31 11:03:17 +00:00
|
|
|
func parentBucketName(cid *cid.ID) []byte {
|
2020-11-23 13:30:56 +00:00
|
|
|
return []byte(cid.String() + parentPostfix)
|
|
|
|
}
|
|
|
|
|
2020-11-30 08:46:18 +00:00
|
|
|
// splitBucketName returns <CID>_splitid.
|
2021-05-31 11:03:17 +00:00
|
|
|
func splitBucketName(cid *cid.ID) []byte {
|
2020-11-30 08:46:18 +00:00
|
|
|
return []byte(cid.String() + splitPostfix)
|
|
|
|
}
|
|
|
|
|
2020-11-23 13:30:56 +00:00
|
|
|
// addressKey returns key for K-V tables when key is a whole address.
|
|
|
|
func addressKey(addr *object.Address) []byte {
|
|
|
|
return []byte(addr.String())
|
|
|
|
}
|
|
|
|
|
2021-02-16 07:57:43 +00:00
|
|
|
// parses object address formed by addressKey.
|
|
|
|
func addressFromKey(k []byte) (*object.Address, error) {
|
|
|
|
a := object.NewAddress()
|
|
|
|
return a, a.Parse(string(k))
|
|
|
|
}
|
|
|
|
|
2020-11-23 13:30:56 +00:00
|
|
|
// objectKey returns key for K-V tables when key is an object id.
|
|
|
|
func objectKey(oid *object.ID) []byte {
|
|
|
|
return []byte(oid.String())
|
|
|
|
}
|
2021-09-13 08:23:58 +00:00
|
|
|
|
|
|
|
// removes all bucket elements.
|
|
|
|
func resetBucket(b *bbolt.Bucket) error {
|
|
|
|
return b.ForEach(func(k, v []byte) error {
|
|
|
|
if v != nil {
|
|
|
|
return b.Delete(k)
|
|
|
|
}
|
|
|
|
|
|
|
|
return b.DeleteBucket(k)
|
|
|
|
})
|
|
|
|
}
|