2020-10-28 14:49:30 +00:00
|
|
|
package meta
|
|
|
|
|
|
|
|
import (
|
|
|
|
objectSDK "github.com/nspcc-dev/neofs-api-go/pkg/object"
|
|
|
|
v2object "github.com/nspcc-dev/neofs-api-go/v2/object"
|
|
|
|
"github.com/nspcc-dev/neofs-node/pkg/core/object"
|
|
|
|
"github.com/pkg/errors"
|
|
|
|
"go.etcd.io/bbolt"
|
|
|
|
)
|
|
|
|
|
|
|
|
type bucketItem struct {
|
|
|
|
key, val string
|
|
|
|
}
|
|
|
|
|
|
|
|
var (
|
|
|
|
primaryBucket = []byte("objects")
|
|
|
|
indexBucket = []byte("index")
|
|
|
|
)
|
|
|
|
|
|
|
|
// Put saves object in DB.
|
|
|
|
//
|
|
|
|
// Object payload expected to be cut.
|
|
|
|
func (db *DB) Put(obj *object.Object) error {
|
|
|
|
return db.boltDB.Update(func(tx *bbolt.Tx) error {
|
2020-10-29 16:12:38 +00:00
|
|
|
par := false
|
2020-10-28 14:49:30 +00:00
|
|
|
|
2020-10-29 16:12:38 +00:00
|
|
|
for ; obj != nil; obj, par = obj.GetParent(), true {
|
|
|
|
// create primary bucket (addr: header)
|
|
|
|
primaryBucket, err := tx.CreateBucketIfNotExists(primaryBucket)
|
|
|
|
if err != nil {
|
|
|
|
return errors.Wrapf(err, "(%T) could not create primary bucket", db)
|
|
|
|
}
|
2020-10-28 14:49:30 +00:00
|
|
|
|
2020-11-16 10:26:35 +00:00
|
|
|
data, err := obj.Marshal()
|
2020-10-28 14:49:30 +00:00
|
|
|
if err != nil {
|
2020-10-29 16:12:38 +00:00
|
|
|
return errors.Wrapf(err, "(%T) could not marshal the object", db)
|
2020-10-28 14:49:30 +00:00
|
|
|
}
|
|
|
|
|
2020-10-29 16:12:38 +00:00
|
|
|
addrKey := addressKey(obj.Address())
|
2020-10-28 14:49:30 +00:00
|
|
|
|
2020-11-06 09:41:59 +00:00
|
|
|
if !par {
|
|
|
|
// put header to primary bucket
|
|
|
|
if err := primaryBucket.Put(addrKey, data); err != nil {
|
|
|
|
return errors.Wrapf(err, "(%T) could not put item to primary bucket", db)
|
|
|
|
}
|
2020-10-29 16:12:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// create bucket for indices
|
|
|
|
indexBucket, err := tx.CreateBucketIfNotExists(indexBucket)
|
2020-10-28 14:49:30 +00:00
|
|
|
if err != nil {
|
2020-10-29 16:12:38 +00:00
|
|
|
return errors.Wrapf(err, "(%T) could not create index bucket", db)
|
2020-10-28 14:49:30 +00:00
|
|
|
}
|
|
|
|
|
2020-10-29 16:12:38 +00:00
|
|
|
// calculate indexed values for object
|
|
|
|
indices := objectIndices(obj, par)
|
|
|
|
|
|
|
|
for i := range indices {
|
|
|
|
// create index bucket
|
|
|
|
keyBucket, err := indexBucket.CreateBucketIfNotExists([]byte(indices[i].key))
|
|
|
|
if err != nil {
|
|
|
|
return errors.Wrapf(err, "(%T) could not create bucket for header key", db)
|
|
|
|
}
|
|
|
|
|
2020-11-09 14:58:22 +00:00
|
|
|
v := []byte(indices[i].val)
|
2020-10-29 16:12:38 +00:00
|
|
|
|
2020-11-09 14:58:22 +00:00
|
|
|
// create address bucket for the value
|
|
|
|
valBucket, err := keyBucket.CreateBucketIfNotExists(nonEmptyKeyBytes(v))
|
2020-10-29 16:12:38 +00:00
|
|
|
if err != nil {
|
2020-11-09 14:58:22 +00:00
|
|
|
return errors.Wrapf(err, "(%T) could not create bucket for header value", db)
|
2020-10-29 16:12:38 +00:00
|
|
|
}
|
|
|
|
|
2020-11-09 14:58:22 +00:00
|
|
|
// put object address to value bucket
|
|
|
|
if err := valBucket.Put(addrKey, nil); err != nil {
|
2020-10-29 16:12:38 +00:00
|
|
|
return errors.Wrapf(err, "(%T) could not put item to header bucket", db)
|
|
|
|
}
|
2020-10-28 14:49:30 +00:00
|
|
|
}
|
2020-10-29 16:12:38 +00:00
|
|
|
|
2020-10-28 14:49:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-10-29 11:40:53 +00:00
|
|
|
func nonEmptyKeyBytes(key []byte) []byte {
|
|
|
|
return append([]byte{0}, key...)
|
2020-10-28 14:49:30 +00:00
|
|
|
}
|
|
|
|
|
2020-10-29 11:38:50 +00:00
|
|
|
func cutKeyBytes(key []byte) []byte {
|
|
|
|
return key[1:]
|
2020-10-28 14:49:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func addressKey(addr *objectSDK.Address) []byte {
|
|
|
|
return []byte(addr.String())
|
|
|
|
}
|
|
|
|
|
2020-10-29 16:12:38 +00:00
|
|
|
func objectIndices(obj *object.Object, parent bool) []bucketItem {
|
2020-11-16 09:43:52 +00:00
|
|
|
as := obj.Attributes()
|
2020-10-28 14:49:30 +00:00
|
|
|
|
2020-11-26 09:15:15 +00:00
|
|
|
res := make([]bucketItem, 0, 6+len(as)) // 6 predefined buckets and object attributes
|
2020-10-28 14:49:30 +00:00
|
|
|
|
|
|
|
res = append(res,
|
|
|
|
bucketItem{
|
|
|
|
key: v2object.FilterHeaderVersion,
|
2020-11-16 09:43:52 +00:00
|
|
|
val: obj.Version().String(),
|
2020-10-28 14:49:30 +00:00
|
|
|
},
|
|
|
|
bucketItem{
|
|
|
|
key: v2object.FilterHeaderContainerID,
|
2020-11-16 09:43:52 +00:00
|
|
|
val: obj.ContainerID().String(),
|
2020-10-28 14:49:30 +00:00
|
|
|
},
|
|
|
|
bucketItem{
|
|
|
|
key: v2object.FilterHeaderOwnerID,
|
2020-11-16 09:43:52 +00:00
|
|
|
val: obj.OwnerID().String(),
|
2020-10-28 14:49:30 +00:00
|
|
|
},
|
2020-11-05 16:54:24 +00:00
|
|
|
bucketItem{
|
|
|
|
key: v2object.FilterHeaderParent,
|
2020-11-16 09:43:52 +00:00
|
|
|
val: obj.ParentID().String(),
|
2020-11-05 16:54:24 +00:00
|
|
|
},
|
2020-11-20 07:43:41 +00:00
|
|
|
bucketItem{
|
|
|
|
key: v2object.FilterHeaderObjectID,
|
|
|
|
val: obj.ID().String(),
|
|
|
|
},
|
2020-10-28 14:49:30 +00:00
|
|
|
// TODO: add remaining fields after neofs-api#72
|
|
|
|
)
|
|
|
|
|
2020-11-16 09:43:52 +00:00
|
|
|
if obj.Type() == objectSDK.TypeRegular && !obj.HasParent() {
|
2020-11-10 12:55:54 +00:00
|
|
|
res = append(res, bucketItem{
|
|
|
|
key: v2object.FilterPropertyRoot,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
if !parent {
|
|
|
|
res = append(res, bucketItem{
|
|
|
|
key: v2object.FilterPropertyPhy,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-10-28 14:49:30 +00:00
|
|
|
for _, a := range as {
|
|
|
|
res = append(res, bucketItem{
|
2020-11-16 09:43:52 +00:00
|
|
|
key: a.Key(),
|
|
|
|
val: a.Value(),
|
2020-10-28 14:49:30 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
return res
|
|
|
|
}
|