2020-10-28 14:49:30 +00:00
|
|
|
package meta
|
|
|
|
|
|
|
|
import (
|
2023-04-12 14:01:29 +00:00
|
|
|
"context"
|
2020-12-08 07:51:34 +00:00
|
|
|
"encoding/binary"
|
2021-05-18 08:12:51 +00:00
|
|
|
"errors"
|
2020-12-08 07:51:34 +00:00
|
|
|
"fmt"
|
2021-02-01 21:00:40 +00:00
|
|
|
"strings"
|
2023-06-06 09:27:19 +00:00
|
|
|
"time"
|
2020-12-08 07:51:34 +00:00
|
|
|
|
2023-06-15 10:19:36 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-node/pkg/local_object_storage/internal/metaerr"
|
2023-05-31 09:24:04 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-observability/tracing"
|
2024-11-07 14:32:10 +00:00
|
|
|
v2object "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/api/object"
|
2023-03-07 13:38:26 +00:00
|
|
|
cid "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/container/id"
|
2023-07-06 12:36:41 +00:00
|
|
|
objectSDK "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/object"
|
2023-03-07 13:38:26 +00:00
|
|
|
oid "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/object/id"
|
2020-10-28 14:49:30 +00:00
|
|
|
"go.etcd.io/bbolt"
|
2023-04-12 14:01:29 +00:00
|
|
|
"go.opentelemetry.io/otel/attribute"
|
|
|
|
"go.opentelemetry.io/otel/trace"
|
2020-10-28 14:49:30 +00:00
|
|
|
)
|
|
|
|
|
2020-12-08 07:51:34 +00:00
|
|
|
type (
|
|
|
|
// filterGroup is a structure that have search filters grouped by access
|
|
|
|
// method. We have fast filters that looks for indexes and do not unmarshal
|
|
|
|
// objects, and slow filters, that applied after fast filters created
|
|
|
|
// smaller set of objects to check.
|
|
|
|
filterGroup struct {
|
2022-05-31 17:00:41 +00:00
|
|
|
withCnrFilter bool
|
|
|
|
|
|
|
|
cnr cid.ID
|
|
|
|
|
2023-07-06 12:36:41 +00:00
|
|
|
fastFilters, slowFilters objectSDK.SearchFilters
|
2020-12-08 07:51:34 +00:00
|
|
|
}
|
|
|
|
)
|
|
|
|
|
2020-12-08 09:56:14 +00:00
|
|
|
// SelectPrm groups the parameters of Select operation.
|
|
|
|
type SelectPrm struct {
|
2024-10-02 10:04:29 +00:00
|
|
|
cnr cid.ID
|
|
|
|
filters objectSDK.SearchFilters
|
|
|
|
useAttributeIndex bool
|
2020-12-08 09:56:14 +00:00
|
|
|
}
|
|
|
|
|
2022-04-21 11:28:05 +00:00
|
|
|
// SelectRes groups the resulting values of Select operation.
|
2020-12-08 09:56:14 +00:00
|
|
|
type SelectRes struct {
|
2022-05-31 17:00:41 +00:00
|
|
|
addrList []oid.Address
|
2020-12-08 09:56:14 +00:00
|
|
|
}
|
|
|
|
|
2022-07-12 14:59:37 +00:00
|
|
|
// SetContainerID is a Select option to set the container id to search in.
|
|
|
|
func (p *SelectPrm) SetContainerID(cnr cid.ID) {
|
|
|
|
p.cnr = cnr
|
2020-12-10 13:17:45 +00:00
|
|
|
}
|
|
|
|
|
2022-07-12 14:59:37 +00:00
|
|
|
// SetFilters is a Select option to set the object filters.
|
2023-07-06 12:36:41 +00:00
|
|
|
func (p *SelectPrm) SetFilters(fs objectSDK.SearchFilters) {
|
2022-07-12 14:59:37 +00:00
|
|
|
p.filters = fs
|
2020-12-08 09:56:14 +00:00
|
|
|
}
|
|
|
|
|
2024-10-02 10:04:29 +00:00
|
|
|
func (p *SelectPrm) SetUseAttributeIndex(v bool) {
|
|
|
|
p.useAttributeIndex = v
|
|
|
|
}
|
|
|
|
|
2020-12-08 09:56:14 +00:00
|
|
|
// AddressList returns list of addresses of the selected objects.
|
2022-05-20 16:48:14 +00:00
|
|
|
func (r SelectRes) AddressList() []oid.Address {
|
2020-12-08 09:56:14 +00:00
|
|
|
return r.addrList
|
|
|
|
}
|
|
|
|
|
2020-10-28 14:49:30 +00:00
|
|
|
// Select returns list of addresses of objects that match search filters.
|
2023-04-12 14:01:29 +00:00
|
|
|
func (db *DB) Select(ctx context.Context, prm SelectPrm) (res SelectRes, err error) {
|
2023-06-06 09:27:19 +00:00
|
|
|
var (
|
|
|
|
startedAt = time.Now()
|
|
|
|
success = false
|
|
|
|
)
|
|
|
|
defer func() {
|
|
|
|
db.metrics.AddMethodDuration("Select", time.Since(startedAt), success)
|
|
|
|
}()
|
|
|
|
|
2023-04-12 14:01:29 +00:00
|
|
|
_, span := tracing.StartSpanFromContext(ctx, "metabase.Select",
|
|
|
|
trace.WithAttributes(
|
|
|
|
attribute.String("container_id", prm.cnr.EncodeToString()),
|
|
|
|
))
|
|
|
|
defer span.End()
|
|
|
|
|
2022-07-21 13:26:25 +00:00
|
|
|
db.modeMtx.RLock()
|
|
|
|
defer db.modeMtx.RUnlock()
|
|
|
|
|
2022-11-15 12:46:32 +00:00
|
|
|
if db.mode.NoMetabase() {
|
|
|
|
return res, ErrDegradedMode
|
|
|
|
}
|
|
|
|
|
2023-06-26 11:49:08 +00:00
|
|
|
if checkNonEmpty(prm.filters) {
|
2023-06-06 09:27:19 +00:00
|
|
|
success = true
|
2022-01-19 13:21:31 +00:00
|
|
|
return res, nil
|
|
|
|
}
|
|
|
|
|
2022-07-27 18:38:28 +00:00
|
|
|
currEpoch := db.epochState.CurrentEpoch()
|
|
|
|
|
2023-06-15 10:19:36 +00:00
|
|
|
return res, metaerr.Wrap(db.boltDB.View(func(tx *bbolt.Tx) error {
|
2024-10-02 10:04:29 +00:00
|
|
|
res.addrList, err = db.selectObjects(tx, prm.cnr, prm.filters, currEpoch, prm.useAttributeIndex)
|
2023-06-06 09:27:19 +00:00
|
|
|
success = err == nil
|
2020-11-06 09:41:59 +00:00
|
|
|
return err
|
2023-06-15 10:19:36 +00:00
|
|
|
}))
|
2020-11-06 09:41:59 +00:00
|
|
|
}
|
2020-11-03 12:08:54 +00:00
|
|
|
|
2024-10-02 10:04:29 +00:00
|
|
|
func (db *DB) selectObjects(tx *bbolt.Tx, cnr cid.ID, fs objectSDK.SearchFilters, currEpoch uint64, useAttributeIndex bool) ([]oid.Address, error) {
|
|
|
|
group, err := groupFilters(fs, useAttributeIndex)
|
2020-12-08 07:51:34 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2020-11-06 09:41:59 +00:00
|
|
|
}
|
|
|
|
|
2022-05-31 17:00:41 +00:00
|
|
|
// if there are conflicts in query and container then it means that there is no
|
2020-12-10 13:17:45 +00:00
|
|
|
// objects to match this query.
|
2022-05-31 17:00:41 +00:00
|
|
|
if group.withCnrFilter && !cnr.Equals(group.cnr) {
|
2020-12-10 13:17:45 +00:00
|
|
|
return nil, nil
|
2020-11-06 09:41:59 +00:00
|
|
|
}
|
|
|
|
|
2020-12-08 07:51:34 +00:00
|
|
|
// keep matched addresses in this cache
|
2020-11-06 09:41:59 +00:00
|
|
|
// value equal to number (index+1) of latest matched filter
|
|
|
|
mAddr := make(map[string]int)
|
|
|
|
|
2020-12-08 07:51:34 +00:00
|
|
|
expLen := len(group.fastFilters) // expected value of matched filters in mAddr
|
2020-11-03 12:08:54 +00:00
|
|
|
|
2020-12-08 07:51:34 +00:00
|
|
|
if len(group.fastFilters) == 0 {
|
|
|
|
expLen = 1
|
2020-11-03 12:08:54 +00:00
|
|
|
|
2022-05-12 16:37:46 +00:00
|
|
|
db.selectAll(tx, cnr, mAddr)
|
2020-12-08 07:51:34 +00:00
|
|
|
} else {
|
|
|
|
for i := range group.fastFilters {
|
2022-05-12 16:37:46 +00:00
|
|
|
db.selectFastFilter(tx, cnr, group.fastFilters[i], mAddr, i)
|
2020-11-06 09:41:59 +00:00
|
|
|
}
|
|
|
|
}
|
2020-10-28 14:49:30 +00:00
|
|
|
|
2022-05-31 17:00:41 +00:00
|
|
|
res := make([]oid.Address, 0, len(mAddr))
|
2020-10-29 11:38:50 +00:00
|
|
|
|
2020-11-06 09:41:59 +00:00
|
|
|
for a, ind := range mAddr {
|
2020-12-08 07:51:34 +00:00
|
|
|
if ind != expLen {
|
|
|
|
continue // ignore objects with unmatched fast filters
|
2020-11-06 09:41:59 +00:00
|
|
|
}
|
2020-10-28 14:49:30 +00:00
|
|
|
|
2022-09-08 11:54:21 +00:00
|
|
|
var id oid.ID
|
|
|
|
err = id.Decode([]byte(a))
|
2021-02-19 08:31:25 +00:00
|
|
|
if err != nil {
|
2020-11-06 09:41:59 +00:00
|
|
|
return nil, err
|
2020-10-28 14:49:30 +00:00
|
|
|
}
|
|
|
|
|
2022-09-08 11:54:21 +00:00
|
|
|
var addr oid.Address
|
|
|
|
addr.SetContainer(cnr)
|
|
|
|
addr.SetObject(id)
|
2024-08-20 08:59:42 +00:00
|
|
|
st, err := objectStatus(tx, addr, currEpoch)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if st > 0 {
|
2020-12-08 07:51:34 +00:00
|
|
|
continue // ignore removed objects
|
|
|
|
}
|
|
|
|
|
2024-09-12 06:53:21 +00:00
|
|
|
addr, match := db.matchSlowFilters(tx, addr, group.slowFilters, currEpoch)
|
|
|
|
if !match {
|
2020-12-08 07:51:34 +00:00
|
|
|
continue // ignore objects with unmatched slow filters
|
|
|
|
}
|
|
|
|
|
2020-11-06 09:41:59 +00:00
|
|
|
res = append(res, addr)
|
|
|
|
}
|
2020-11-03 14:24:23 +00:00
|
|
|
|
2020-11-06 09:41:59 +00:00
|
|
|
return res, nil
|
|
|
|
}
|
2020-11-03 13:12:34 +00:00
|
|
|
|
2020-12-08 07:51:34 +00:00
|
|
|
// selectAll adds to resulting cache all available objects in metabase.
|
2022-05-31 17:00:41 +00:00
|
|
|
func (db *DB) selectAll(tx *bbolt.Tx, cnr cid.ID, to map[string]int) {
|
2022-09-08 11:54:21 +00:00
|
|
|
bucketName := make([]byte, bucketKeySize)
|
|
|
|
selectAllFromBucket(tx, primaryBucketName(cnr, bucketName), to, 0)
|
|
|
|
selectAllFromBucket(tx, tombstoneBucketName(cnr, bucketName), to, 0)
|
|
|
|
selectAllFromBucket(tx, parentBucketName(cnr, bucketName), to, 0)
|
|
|
|
selectAllFromBucket(tx, bucketNameLockers(cnr, bucketName), to, 0)
|
2020-12-08 07:51:34 +00:00
|
|
|
}
|
2020-11-10 12:55:54 +00:00
|
|
|
|
2020-12-08 07:51:34 +00:00
|
|
|
// selectAllFromBucket goes through all keys in bucket and adds them in a
|
|
|
|
// resulting cache. Keys should be stringed object ids.
|
2022-09-08 11:54:21 +00:00
|
|
|
func selectAllFromBucket(tx *bbolt.Tx, name []byte, to map[string]int, fNum int) {
|
2020-12-08 07:51:34 +00:00
|
|
|
bkt := tx.Bucket(name)
|
|
|
|
if bkt == nil {
|
|
|
|
return
|
2020-11-10 12:55:54 +00:00
|
|
|
}
|
|
|
|
|
2024-03-11 14:11:49 +00:00
|
|
|
_ = bkt.ForEach(func(k, _ []byte) error {
|
2022-09-08 11:54:21 +00:00
|
|
|
markAddressInCache(to, fNum, string(k))
|
2020-10-28 14:49:30 +00:00
|
|
|
|
2020-11-10 12:55:54 +00:00
|
|
|
return nil
|
2020-12-08 07:51:34 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// selectFastFilter makes fast optimized checks for well known buckets or
|
|
|
|
// looking through user attribute buckets otherwise.
|
|
|
|
func (db *DB) selectFastFilter(
|
|
|
|
tx *bbolt.Tx,
|
2022-05-31 17:00:41 +00:00
|
|
|
cnr cid.ID, // container we search on
|
2023-07-06 12:36:41 +00:00
|
|
|
f objectSDK.SearchFilter, // fast filter
|
2020-12-08 07:51:34 +00:00
|
|
|
to map[string]int, // resulting cache
|
|
|
|
fNum int, // index of filter
|
|
|
|
) {
|
2022-07-27 18:38:28 +00:00
|
|
|
currEpoch := db.epochState.CurrentEpoch()
|
2022-09-08 11:54:21 +00:00
|
|
|
bucketName := make([]byte, bucketKeySize)
|
2020-12-08 07:51:34 +00:00
|
|
|
switch f.Header() {
|
|
|
|
case v2object.FilterHeaderObjectID:
|
2022-07-27 18:38:28 +00:00
|
|
|
db.selectObjectID(tx, f, cnr, to, fNum, currEpoch)
|
2020-12-08 07:51:34 +00:00
|
|
|
case v2object.FilterHeaderObjectType:
|
2022-05-31 17:00:41 +00:00
|
|
|
for _, bucketName := range bucketNamesForType(cnr, f.Operation(), f.Value()) {
|
2022-09-08 11:54:21 +00:00
|
|
|
selectAllFromBucket(tx, bucketName, to, fNum)
|
2020-12-08 07:51:34 +00:00
|
|
|
}
|
|
|
|
case v2object.FilterHeaderParent:
|
2022-09-08 11:54:21 +00:00
|
|
|
bucketName := parentBucketName(cnr, bucketName)
|
|
|
|
db.selectFromList(tx, bucketName, f, to, fNum)
|
2020-12-08 07:51:34 +00:00
|
|
|
case v2object.FilterHeaderSplitID:
|
2022-09-08 11:54:21 +00:00
|
|
|
bucketName := splitBucketName(cnr, bucketName)
|
|
|
|
db.selectFromList(tx, bucketName, f, to, fNum)
|
2024-05-21 11:19:22 +00:00
|
|
|
case v2object.FilterHeaderECParent:
|
2024-06-04 10:30:30 +00:00
|
|
|
bucketName := ecInfoBucketName(cnr, bucketName)
|
2024-05-21 11:19:22 +00:00
|
|
|
db.selectFromList(tx, bucketName, f, to, fNum)
|
2020-12-08 07:51:34 +00:00
|
|
|
case v2object.FilterPropertyRoot:
|
2022-09-08 11:54:21 +00:00
|
|
|
selectAllFromBucket(tx, rootBucketName(cnr, bucketName), to, fNum)
|
2020-12-08 07:51:34 +00:00
|
|
|
case v2object.FilterPropertyPhy:
|
2022-09-08 11:54:21 +00:00
|
|
|
selectAllFromBucket(tx, primaryBucketName(cnr, bucketName), to, fNum)
|
|
|
|
selectAllFromBucket(tx, tombstoneBucketName(cnr, bucketName), to, fNum)
|
|
|
|
selectAllFromBucket(tx, bucketNameLockers(cnr, bucketName), to, fNum)
|
2024-10-02 10:04:29 +00:00
|
|
|
default: // user attribute
|
|
|
|
bucketName := attributeBucketName(cnr, f.Header(), bucketName)
|
|
|
|
if f.Operation() == objectSDK.MatchNotPresent {
|
|
|
|
selectOutsideFKBT(tx, allBucketNames(cnr), bucketName, to, fNum)
|
|
|
|
} else {
|
|
|
|
db.selectFromFKBT(tx, bucketName, f, to, fNum)
|
|
|
|
}
|
2020-11-06 09:41:59 +00:00
|
|
|
}
|
2020-12-08 07:51:34 +00:00
|
|
|
}
|
2020-10-28 14:49:30 +00:00
|
|
|
|
2022-09-08 11:54:21 +00:00
|
|
|
var mBucketNaming = map[string][]func(cid.ID, []byte) []byte{
|
2023-04-25 06:48:24 +00:00
|
|
|
v2object.TypeRegular.String(): {primaryBucketName, parentBucketName},
|
|
|
|
v2object.TypeTombstone.String(): {tombstoneBucketName},
|
|
|
|
v2object.TypeLock.String(): {bucketNameLockers},
|
2021-02-01 21:00:40 +00:00
|
|
|
}
|
|
|
|
|
2024-10-02 10:04:29 +00:00
|
|
|
func allBucketNames(cnr cid.ID) (names [][]byte) {
|
|
|
|
for _, fns := range mBucketNaming {
|
|
|
|
for _, fn := range fns {
|
|
|
|
names = append(names, fn(cnr, make([]byte, bucketKeySize)))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-07-06 12:36:41 +00:00
|
|
|
func bucketNamesForType(cnr cid.ID, mType objectSDK.SearchMatchType, typeVal string) (names [][]byte) {
|
2021-02-01 21:00:40 +00:00
|
|
|
appendNames := func(key string) {
|
|
|
|
fns, ok := mBucketNaming[key]
|
|
|
|
if ok {
|
|
|
|
for _, fn := range fns {
|
2022-09-08 11:54:21 +00:00
|
|
|
names = append(names, fn(cnr, make([]byte, bucketKeySize)))
|
2021-02-01 21:00:40 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
switch mType {
|
|
|
|
default:
|
2023-07-06 12:36:41 +00:00
|
|
|
case objectSDK.MatchStringNotEqual:
|
2021-02-01 21:00:40 +00:00
|
|
|
for key := range mBucketNaming {
|
|
|
|
if key != typeVal {
|
|
|
|
appendNames(key)
|
|
|
|
}
|
|
|
|
}
|
2023-07-06 12:36:41 +00:00
|
|
|
case objectSDK.MatchStringEqual:
|
2021-02-01 21:00:40 +00:00
|
|
|
appendNames(typeVal)
|
2023-07-06 12:36:41 +00:00
|
|
|
case objectSDK.MatchCommonPrefix:
|
2021-08-18 07:26:14 +00:00
|
|
|
for key := range mBucketNaming {
|
|
|
|
if strings.HasPrefix(key, typeVal) {
|
|
|
|
appendNames(key)
|
|
|
|
}
|
|
|
|
}
|
2021-02-01 21:00:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-10-02 10:04:29 +00:00
|
|
|
func (db *DB) selectFromFKBT(
|
|
|
|
tx *bbolt.Tx,
|
|
|
|
name []byte, // fkbt root bucket name
|
|
|
|
f objectSDK.SearchFilter, // filter for operation and value
|
|
|
|
to map[string]int, // resulting cache
|
|
|
|
fNum int, // index of filter
|
|
|
|
) { //
|
|
|
|
matchFunc, ok := db.matchers[f.Operation()]
|
|
|
|
if !ok {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
fkbtRoot := tx.Bucket(name)
|
|
|
|
if fkbtRoot == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-10-07 14:19:04 +00:00
|
|
|
_ = matchFunc.matchBucket(fkbtRoot, f.Header(), f.Value(), func(k, _ []byte) error {
|
2024-10-02 10:04:29 +00:00
|
|
|
fkbtLeaf := fkbtRoot.Bucket(k)
|
|
|
|
if fkbtLeaf == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return fkbtLeaf.ForEach(func(k, _ []byte) error {
|
|
|
|
markAddressInCache(to, fNum, string(k))
|
|
|
|
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// selectOutsideFKBT looks into all incl buckets to find list of addresses outside <fkbt> to add in
|
|
|
|
// resulting cache.
|
|
|
|
func selectOutsideFKBT(
|
|
|
|
tx *bbolt.Tx,
|
|
|
|
incl [][]byte, // buckets
|
|
|
|
name []byte, // fkbt root bucket name
|
|
|
|
to map[string]int, // resulting cache
|
|
|
|
fNum int, // index of filter
|
|
|
|
) {
|
|
|
|
mExcl := make(map[string]struct{})
|
|
|
|
|
|
|
|
bktExcl := tx.Bucket(name)
|
|
|
|
if bktExcl != nil {
|
|
|
|
_ = bktExcl.ForEachBucket(func(k []byte) error {
|
|
|
|
exclBktLeaf := bktExcl.Bucket(k)
|
|
|
|
return exclBktLeaf.ForEach(func(k, _ []byte) error {
|
|
|
|
mExcl[string(k)] = struct{}{}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
for i := range incl {
|
|
|
|
bktIncl := tx.Bucket(incl[i])
|
|
|
|
if bktIncl == nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
_ = bktIncl.ForEach(func(k, _ []byte) error {
|
|
|
|
if _, ok := mExcl[string(k)]; !ok {
|
|
|
|
markAddressInCache(to, fNum, string(k))
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-08 07:51:34 +00:00
|
|
|
// selectFromList looks into <list> index to find list of addresses to add in
|
|
|
|
// resulting cache.
|
|
|
|
func (db *DB) selectFromList(
|
|
|
|
tx *bbolt.Tx,
|
|
|
|
name []byte, // list root bucket name
|
2023-07-06 12:36:41 +00:00
|
|
|
f objectSDK.SearchFilter, // filter for operation and value
|
2020-12-08 07:51:34 +00:00
|
|
|
to map[string]int, // resulting cache
|
|
|
|
fNum int, // index of filter
|
|
|
|
) { //
|
|
|
|
bkt := tx.Bucket(name)
|
|
|
|
if bkt == nil {
|
|
|
|
return
|
2020-11-10 12:55:54 +00:00
|
|
|
}
|
|
|
|
|
2021-02-01 21:00:40 +00:00
|
|
|
var (
|
|
|
|
lst [][]byte
|
|
|
|
err error
|
|
|
|
)
|
|
|
|
|
|
|
|
switch op := f.Operation(); op {
|
2023-07-06 12:36:41 +00:00
|
|
|
case objectSDK.MatchStringEqual:
|
2021-02-01 21:00:40 +00:00
|
|
|
lst, err = decodeList(bkt.Get(bucketKeyHelper(f.Header(), f.Value())))
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
2020-12-08 07:51:34 +00:00
|
|
|
default:
|
2021-02-01 21:00:40 +00:00
|
|
|
fMatch, ok := db.matchers[op]
|
|
|
|
if !ok {
|
|
|
|
return
|
|
|
|
}
|
2020-12-08 07:51:34 +00:00
|
|
|
|
2024-03-11 14:11:49 +00:00
|
|
|
if err = fMatch.matchBucket(bkt, f.Header(), f.Value(), func(_, val []byte) error {
|
2021-02-01 21:00:40 +00:00
|
|
|
l, err := decodeList(val)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
lst = append(lst, l...)
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}); err != nil {
|
|
|
|
return
|
|
|
|
}
|
2020-12-08 07:51:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for i := range lst {
|
2022-09-08 11:54:21 +00:00
|
|
|
markAddressInCache(to, fNum, string(lst[i]))
|
2020-12-08 07:51:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// selectObjectID processes objectID filter with in-place optimizations.
|
|
|
|
func (db *DB) selectObjectID(
|
|
|
|
tx *bbolt.Tx,
|
2023-07-06 12:36:41 +00:00
|
|
|
f objectSDK.SearchFilter,
|
2022-05-31 17:00:41 +00:00
|
|
|
cnr cid.ID,
|
2020-12-08 07:51:34 +00:00
|
|
|
to map[string]int, // resulting cache
|
|
|
|
fNum int, // index of filter
|
2022-07-27 18:38:28 +00:00
|
|
|
currEpoch uint64,
|
2020-12-08 07:51:34 +00:00
|
|
|
) {
|
2022-09-08 11:54:21 +00:00
|
|
|
appendOID := func(id oid.ID) {
|
2022-05-31 17:00:41 +00:00
|
|
|
var addr oid.Address
|
2022-09-08 11:54:21 +00:00
|
|
|
addr.SetContainer(cnr)
|
|
|
|
addr.SetObject(id)
|
2020-12-08 07:51:34 +00:00
|
|
|
|
2023-07-26 13:02:08 +00:00
|
|
|
var splitInfoError *objectSDK.SplitInfoError
|
2024-05-27 19:07:43 +00:00
|
|
|
ok, _, err := db.exists(tx, addr, oid.Address{}, currEpoch)
|
2021-02-01 21:00:40 +00:00
|
|
|
if (err == nil && ok) || errors.As(err, &splitInfoError) {
|
2022-09-08 11:54:21 +00:00
|
|
|
raw := make([]byte, objectKeySize)
|
|
|
|
id.Encode(raw)
|
|
|
|
markAddressInCache(to, fNum, string(raw))
|
2021-02-01 21:00:40 +00:00
|
|
|
}
|
2020-12-08 07:51:34 +00:00
|
|
|
}
|
|
|
|
|
2021-02-01 21:00:40 +00:00
|
|
|
switch op := f.Operation(); op {
|
2023-07-06 12:36:41 +00:00
|
|
|
case objectSDK.MatchStringEqual:
|
2022-09-08 11:54:21 +00:00
|
|
|
var id oid.ID
|
|
|
|
if err := id.DecodeString(f.Value()); err == nil {
|
|
|
|
appendOID(id)
|
|
|
|
}
|
2021-02-01 21:00:40 +00:00
|
|
|
default:
|
|
|
|
fMatch, ok := db.matchers[op]
|
|
|
|
if !ok {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-07-06 12:36:41 +00:00
|
|
|
for _, bucketName := range bucketNamesForType(cnr, objectSDK.MatchStringNotEqual, "") {
|
2021-02-01 21:00:40 +00:00
|
|
|
// copy-paste from DB.selectAllFrom
|
|
|
|
bkt := tx.Bucket(bucketName)
|
|
|
|
if bkt == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-10-07 14:19:04 +00:00
|
|
|
_ = fMatch.matchBucket(bkt, f.Header(), f.Value(), func(k, _ []byte) error {
|
2022-09-08 11:54:21 +00:00
|
|
|
var id oid.ID
|
|
|
|
if err := id.Decode(k); err == nil {
|
|
|
|
appendOID(id)
|
|
|
|
}
|
2021-02-01 21:00:40 +00:00
|
|
|
return nil
|
|
|
|
})
|
|
|
|
}
|
2020-12-08 07:51:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// matchSlowFilters return true if object header is matched by all slow filters.
|
2024-09-12 06:53:21 +00:00
|
|
|
func (db *DB) matchSlowFilters(tx *bbolt.Tx, addr oid.Address, f objectSDK.SearchFilters, currEpoch uint64) (oid.Address, bool) {
|
|
|
|
result := addr
|
2020-12-08 07:51:34 +00:00
|
|
|
if len(f) == 0 {
|
2024-09-12 06:53:21 +00:00
|
|
|
return result, true
|
2020-12-08 07:51:34 +00:00
|
|
|
}
|
|
|
|
|
2024-10-02 08:28:00 +00:00
|
|
|
obj, isECChunk, err := db.getObjectForSlowFilters(tx, addr, currEpoch)
|
2020-12-08 07:51:34 +00:00
|
|
|
if err != nil {
|
2024-09-12 06:53:21 +00:00
|
|
|
return result, false
|
2020-12-08 07:51:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for i := range f {
|
|
|
|
var data []byte
|
|
|
|
switch f[i].Header() {
|
|
|
|
case v2object.FilterHeaderVersion:
|
|
|
|
data = []byte(obj.Version().String())
|
|
|
|
case v2object.FilterHeaderHomomorphicHash:
|
2024-10-02 08:28:00 +00:00
|
|
|
if isECChunk {
|
|
|
|
return result, false // EC chunk and EC parent hashes are incomparable
|
|
|
|
}
|
2022-05-11 16:35:01 +00:00
|
|
|
cs, _ := obj.PayloadHomomorphicHash()
|
|
|
|
data = cs.Value()
|
2020-12-08 07:51:34 +00:00
|
|
|
case v2object.FilterHeaderCreationEpoch:
|
|
|
|
data = make([]byte, 8)
|
|
|
|
binary.LittleEndian.PutUint64(data, obj.CreationEpoch())
|
|
|
|
case v2object.FilterHeaderPayloadLength:
|
2024-10-02 08:28:00 +00:00
|
|
|
if isECChunk {
|
|
|
|
return result, false // EC chunk and EC parent payload lengths are incomparable
|
|
|
|
}
|
2020-12-08 07:51:34 +00:00
|
|
|
data = make([]byte, 8)
|
|
|
|
binary.LittleEndian.PutUint64(data, obj.PayloadSize())
|
2024-08-19 14:13:39 +00:00
|
|
|
case v2object.FilterHeaderOwnerID:
|
|
|
|
data = []byte(obj.OwnerID().EncodeToString())
|
2024-08-19 14:20:55 +00:00
|
|
|
case v2object.FilterHeaderPayloadHash:
|
2024-10-02 08:28:00 +00:00
|
|
|
if isECChunk {
|
|
|
|
return result, false // EC chunk and EC parent payload hashes are incomparable
|
|
|
|
}
|
2024-08-19 14:20:55 +00:00
|
|
|
cs, _ := obj.PayloadChecksum()
|
|
|
|
data = cs.Value()
|
2024-08-20 09:59:59 +00:00
|
|
|
default: // user attribute
|
|
|
|
v, ok := attributeValue(obj, f[i].Header())
|
|
|
|
if ok {
|
2024-09-12 06:53:21 +00:00
|
|
|
if ech := obj.ECHeader(); ech != nil {
|
|
|
|
result.SetObject(ech.Parent())
|
|
|
|
}
|
2024-08-20 09:59:59 +00:00
|
|
|
data = []byte(v)
|
|
|
|
} else {
|
2024-09-12 06:53:21 +00:00
|
|
|
return result, f[i].Operation() == objectSDK.MatchNotPresent
|
2024-08-20 09:59:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
matchFunc, ok := db.matchers[f[i].Operation()]
|
|
|
|
if !ok {
|
2024-09-12 06:53:21 +00:00
|
|
|
return result, false
|
2020-11-10 12:55:54 +00:00
|
|
|
}
|
2020-11-06 09:41:59 +00:00
|
|
|
|
2022-05-25 12:39:51 +00:00
|
|
|
if !matchFunc.matchSlow(f[i].Header(), data, f[i].Value()) {
|
2024-09-12 06:53:21 +00:00
|
|
|
return result, false
|
2020-12-08 07:51:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-09-12 06:53:21 +00:00
|
|
|
return result, true
|
2020-12-08 07:51:34 +00:00
|
|
|
}
|
|
|
|
|
2024-10-02 08:28:00 +00:00
|
|
|
func (db *DB) getObjectForSlowFilters(tx *bbolt.Tx, addr oid.Address, currEpoch uint64) (*objectSDK.Object, bool, error) {
|
|
|
|
buf := make([]byte, addressKeySize)
|
|
|
|
obj, err := db.get(tx, addr, buf, true, false, currEpoch)
|
|
|
|
if err != nil {
|
|
|
|
var ecInfoError *objectSDK.ECInfoError
|
|
|
|
if errors.As(err, &ecInfoError) {
|
|
|
|
for _, chunk := range ecInfoError.ECInfo().Chunks {
|
|
|
|
var objID oid.ID
|
|
|
|
if err = objID.ReadFromV2(chunk.ID); err != nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
addr.SetObject(objID)
|
|
|
|
obj, err = db.get(tx, addr, buf, true, false, currEpoch)
|
|
|
|
if err == nil {
|
|
|
|
return obj, true, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil, false, err
|
|
|
|
}
|
|
|
|
return obj, false, nil
|
|
|
|
}
|
|
|
|
|
2024-08-20 09:59:59 +00:00
|
|
|
func attributeValue(obj *objectSDK.Object, attribute string) (string, bool) {
|
|
|
|
objectAttributes := obj.Attributes()
|
|
|
|
if ech := obj.ECHeader(); ech != nil {
|
|
|
|
objectAttributes = ech.ParentAttributes()
|
|
|
|
}
|
|
|
|
for _, attr := range objectAttributes {
|
|
|
|
if attr.Key() == attribute {
|
|
|
|
return attr.Value(), true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return "", false
|
|
|
|
}
|
|
|
|
|
2020-12-08 07:51:34 +00:00
|
|
|
// groupFilters divides filters in two groups: fast and slow. Fast filters
|
|
|
|
// processed by indexes and slow filters processed after by unmarshaling
|
|
|
|
// object headers.
|
2024-10-02 10:04:29 +00:00
|
|
|
func groupFilters(filters objectSDK.SearchFilters, useAttributeIndex bool) (filterGroup, error) {
|
2022-05-31 11:43:08 +00:00
|
|
|
res := filterGroup{
|
2023-07-06 12:36:41 +00:00
|
|
|
fastFilters: make(objectSDK.SearchFilters, 0, len(filters)),
|
|
|
|
slowFilters: make(objectSDK.SearchFilters, 0, len(filters)),
|
2020-11-06 09:41:59 +00:00
|
|
|
}
|
2020-10-28 14:49:30 +00:00
|
|
|
|
2020-12-08 07:51:34 +00:00
|
|
|
for i := range filters {
|
|
|
|
switch filters[i].Header() {
|
2020-12-10 13:17:45 +00:00
|
|
|
case v2object.FilterHeaderContainerID: // support deprecated field
|
2022-05-31 17:00:41 +00:00
|
|
|
err := res.cnr.DecodeString(filters[i].Value())
|
2020-12-08 07:51:34 +00:00
|
|
|
if err != nil {
|
2024-12-18 06:38:14 +00:00
|
|
|
return filterGroup{}, fmt.Errorf("parse container id: %w", err)
|
2020-12-08 07:51:34 +00:00
|
|
|
}
|
2022-05-31 17:00:41 +00:00
|
|
|
|
|
|
|
res.withCnrFilter = true
|
2024-08-20 09:59:59 +00:00
|
|
|
case // fast filters
|
|
|
|
v2object.FilterHeaderObjectID,
|
|
|
|
v2object.FilterHeaderObjectType,
|
|
|
|
v2object.FilterHeaderParent,
|
|
|
|
v2object.FilterHeaderSplitID,
|
|
|
|
v2object.FilterHeaderECParent,
|
|
|
|
v2object.FilterPropertyRoot,
|
|
|
|
v2object.FilterPropertyPhy:
|
2020-12-08 07:51:34 +00:00
|
|
|
res.fastFilters = append(res.fastFilters, filters[i])
|
2024-08-20 09:59:59 +00:00
|
|
|
default:
|
2024-10-02 10:04:29 +00:00
|
|
|
if useAttributeIndex && IsAtrributeIndexed(filters[i].Header()) {
|
|
|
|
res.fastFilters = append(res.fastFilters, filters[i])
|
|
|
|
} else {
|
|
|
|
res.slowFilters = append(res.slowFilters, filters[i])
|
|
|
|
}
|
2020-12-08 07:51:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return res, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func markAddressInCache(cache map[string]int, fNum int, addr string) {
|
|
|
|
if num := cache[addr]; num == fNum {
|
|
|
|
cache[addr] = num + 1
|
|
|
|
}
|
2020-10-28 14:49:30 +00:00
|
|
|
}
|
2021-02-01 21:00:40 +00:00
|
|
|
|
2023-06-26 11:49:08 +00:00
|
|
|
// Returns true if at least 1 object can satisfy fs.
|
2023-07-06 12:36:41 +00:00
|
|
|
func checkNonEmpty(fs objectSDK.SearchFilters) bool {
|
2021-02-01 21:00:40 +00:00
|
|
|
for i := range fs {
|
2023-07-06 12:36:41 +00:00
|
|
|
if fs[i].Operation() == objectSDK.MatchNotPresent && isSystemKey(fs[i].Header()) {
|
2021-02-01 21:00:40 +00:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
// returns true if string key is a reserved system filter key.
|
|
|
|
func isSystemKey(key string) bool {
|
|
|
|
return strings.HasPrefix(key, v2object.ReservedFilterPrefix)
|
|
|
|
}
|