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-03-07 13:38:26 +00:00
|
|
|
v2object "git.frostfs.info/TrueCloudLab/frostfs-api-go/v2/object"
|
2023-04-12 14:35:10 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-node/internal/logs"
|
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"
|
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-12-08 07:51:34 +00:00
|
|
|
"go.uber.org/zap"
|
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 {
|
2022-05-31 17:00:41 +00:00
|
|
|
cnr cid.ID
|
2023-07-06 12:36:41 +00:00
|
|
|
filters objectSDK.SearchFilters
|
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
|
|
|
}
|
|
|
|
|
|
|
|
// 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 {
|
2022-07-27 18:38:28 +00:00
|
|
|
res.addrList, err = db.selectObjects(tx, prm.cnr, prm.filters, currEpoch)
|
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
|
|
|
|
2023-07-06 12:36:41 +00:00
|
|
|
func (db *DB) selectObjects(tx *bbolt.Tx, cnr cid.ID, fs objectSDK.SearchFilters, currEpoch uint64) ([]oid.Address, error) {
|
2020-12-08 07:51:34 +00:00
|
|
|
group, err := groupFilters(fs)
|
|
|
|
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)
|
|
|
|
|
2022-07-27 18:38:28 +00:00
|
|
|
if objectStatus(tx, addr, currEpoch) > 0 {
|
2020-12-08 07:51:34 +00:00
|
|
|
continue // ignore removed objects
|
|
|
|
}
|
|
|
|
|
2022-07-27 18:38:28 +00:00
|
|
|
if !db.matchSlowFilters(tx, addr, group.slowFilters, currEpoch) {
|
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)
|
2024-08-19 14:20:55 +00:00
|
|
|
case v2object.FilterHeaderOwnerID,
|
|
|
|
v2object.FilterHeaderPayloadHash:
|
2024-08-19 14:13:39 +00:00
|
|
|
return // moved to slow filters
|
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)
|
2020-12-08 07:51:34 +00:00
|
|
|
default: // user attribute
|
2022-09-08 11:54:21 +00:00
|
|
|
bucketName := attributeBucketName(cnr, f.Header(), bucketName)
|
2021-02-01 21:00:40 +00:00
|
|
|
|
2023-07-06 12:36:41 +00:00
|
|
|
if f.Operation() == objectSDK.MatchNotPresent {
|
2022-09-08 11:54:21 +00:00
|
|
|
selectOutsideFKBT(tx, allBucketNames(cnr), bucketName, to, fNum)
|
2021-02-01 21:00:40 +00:00
|
|
|
} else {
|
2022-09-08 11:54:21 +00:00
|
|
|
db.selectFromFKBT(tx, bucketName, f, to, fNum)
|
2021-02-01 21:00:40 +00:00
|
|
|
}
|
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
|
|
|
}
|
|
|
|
|
2022-05-31 17:00:41 +00:00
|
|
|
func allBucketNames(cnr cid.ID) (names [][]byte) {
|
2021-02-01 21:00:40 +00:00
|
|
|
for _, fns := range mBucketNaming {
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2020-12-08 07:51:34 +00:00
|
|
|
// selectFromList looks into <fkbt> index to find list of addresses to add in
|
|
|
|
// resulting cache.
|
|
|
|
func (db *DB) selectFromFKBT(
|
|
|
|
tx *bbolt.Tx,
|
|
|
|
name []byte, // fkbt 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
|
|
|
|
) { //
|
|
|
|
matchFunc, ok := db.matchers[f.Operation()]
|
|
|
|
if !ok {
|
2023-04-12 14:35:10 +00:00
|
|
|
db.log.Debug(logs.MetabaseMissingMatcher, zap.Uint32("operation", uint32(f.Operation())))
|
2020-12-08 07:51:34 +00:00
|
|
|
|
|
|
|
return
|
2020-11-10 12:55:54 +00:00
|
|
|
}
|
|
|
|
|
2020-12-08 07:51:34 +00:00
|
|
|
fkbtRoot := tx.Bucket(name)
|
|
|
|
if fkbtRoot == nil {
|
|
|
|
return
|
|
|
|
}
|
2020-11-06 09:41:59 +00:00
|
|
|
|
2022-05-25 12:39:51 +00:00
|
|
|
err := matchFunc.matchBucket(fkbtRoot, f.Header(), f.Value(), func(k, _ []byte) error {
|
|
|
|
fkbtLeaf := fkbtRoot.Bucket(k)
|
|
|
|
if fkbtLeaf == nil {
|
|
|
|
return nil
|
2020-11-10 12:55:54 +00:00
|
|
|
}
|
2020-12-08 07:51:34 +00:00
|
|
|
|
2022-05-25 12:39:51 +00:00
|
|
|
return fkbtLeaf.ForEach(func(k, _ []byte) error {
|
2022-09-08 11:54:21 +00:00
|
|
|
markAddressInCache(to, fNum, string(k))
|
2022-05-25 12:39:51 +00:00
|
|
|
|
|
|
|
return nil
|
|
|
|
})
|
2020-12-08 07:51:34 +00:00
|
|
|
})
|
|
|
|
if err != nil {
|
2023-04-12 14:35:10 +00:00
|
|
|
db.log.Debug(logs.MetabaseErrorInFKBTSelection, zap.String("error", err.Error()))
|
2020-12-08 07:51:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-01 21:00:40 +00:00
|
|
|
// 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 {
|
2023-05-19 10:08:13 +00:00
|
|
|
_ = bktExcl.ForEachBucket(func(k []byte) error {
|
2021-02-01 21:00:40 +00:00
|
|
|
exclBktLeaf := bktExcl.Bucket(k)
|
|
|
|
return exclBktLeaf.ForEach(func(k, _ []byte) error {
|
2022-09-08 11:54:21 +00:00
|
|
|
mExcl[string(k)] = struct{}{}
|
2021-02-01 21:00:40 +00:00
|
|
|
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
for i := range incl {
|
|
|
|
bktIncl := tx.Bucket(incl[i])
|
|
|
|
if bktIncl == nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
_ = bktIncl.ForEach(func(k, _ []byte) error {
|
2022-09-08 11:54:21 +00:00
|
|
|
if _, ok := mExcl[string(k)]; !ok {
|
|
|
|
markAddressInCache(to, fNum, string(k))
|
2021-02-01 21:00:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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 {
|
2023-04-12 14:35:10 +00:00
|
|
|
db.log.Debug(logs.MetabaseCantDecodeListBucketLeaf, zap.String("error", err.Error()))
|
2021-02-01 21:00:40 +00:00
|
|
|
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 {
|
2023-04-12 14:35:10 +00:00
|
|
|
db.log.Debug(logs.MetabaseUnknownOperation, zap.Uint32("operation", uint32(op)))
|
2020-11-10 12:55:54 +00:00
|
|
|
|
2021-02-01 21:00:40 +00:00
|
|
|
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 {
|
2023-04-12 14:35:10 +00:00
|
|
|
db.log.Debug(logs.MetabaseCantDecodeListBucketLeaf,
|
2021-02-01 21:00:40 +00:00
|
|
|
zap.String("error", err.Error()),
|
|
|
|
)
|
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
lst = append(lst, l...)
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}); err != nil {
|
2023-04-12 14:35:10 +00:00
|
|
|
db.log.Debug(logs.MetabaseCantIterateOverTheBucket,
|
2021-02-01 21:00:40 +00:00
|
|
|
zap.String("error", err.Error()),
|
|
|
|
)
|
|
|
|
|
|
|
|
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 {
|
2023-04-12 14:35:10 +00:00
|
|
|
db.log.Debug(logs.MetabaseUnknownOperation,
|
2021-02-01 21:00:40 +00:00
|
|
|
zap.Uint32("operation", uint32(f.Operation())),
|
|
|
|
)
|
|
|
|
|
|
|
|
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-03-11 14:11:49 +00:00
|
|
|
err := 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
|
|
|
|
})
|
|
|
|
if err != nil {
|
2023-04-12 14:35:10 +00:00
|
|
|
db.log.Debug(logs.MetabaseCouldNotIterateOverTheBuckets,
|
2021-02-01 21:00:40 +00:00
|
|
|
zap.String("error", err.Error()),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
2020-12-08 07:51:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// matchSlowFilters return true if object header is matched by all slow filters.
|
2023-07-06 12:36:41 +00:00
|
|
|
func (db *DB) matchSlowFilters(tx *bbolt.Tx, addr oid.Address, f objectSDK.SearchFilters, currEpoch uint64) bool {
|
2020-12-08 07:51:34 +00:00
|
|
|
if len(f) == 0 {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2022-09-08 11:54:21 +00:00
|
|
|
buf := make([]byte, addressKeySize)
|
|
|
|
obj, err := db.get(tx, addr, buf, true, false, currEpoch)
|
2020-12-08 07:51:34 +00:00
|
|
|
if err != nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
for i := range f {
|
|
|
|
matchFunc, ok := db.matchers[f[i].Operation()]
|
|
|
|
if !ok {
|
|
|
|
return false
|
2020-11-10 12:55:54 +00:00
|
|
|
}
|
|
|
|
|
2020-12-08 07:51:34 +00:00
|
|
|
var data []byte
|
|
|
|
|
|
|
|
switch f[i].Header() {
|
|
|
|
case v2object.FilterHeaderVersion:
|
|
|
|
data = []byte(obj.Version().String())
|
|
|
|
case v2object.FilterHeaderHomomorphicHash:
|
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:
|
|
|
|
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:
|
|
|
|
cs, _ := obj.PayloadChecksum()
|
|
|
|
data = cs.Value()
|
2020-12-08 07:51:34 +00:00
|
|
|
default:
|
|
|
|
continue // ignore unknown search attributes
|
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()) {
|
2020-12-08 07:51:34 +00:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
// groupFilters divides filters in two groups: fast and slow. Fast filters
|
|
|
|
// processed by indexes and slow filters processed after by unmarshaling
|
|
|
|
// object headers.
|
2023-07-06 12:36:41 +00:00
|
|
|
func groupFilters(filters objectSDK.SearchFilters) (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 {
|
2022-05-31 11:43:08 +00:00
|
|
|
return filterGroup{}, fmt.Errorf("can't parse container id: %w", err)
|
2020-12-08 07:51:34 +00:00
|
|
|
}
|
2022-05-31 17:00:41 +00:00
|
|
|
|
|
|
|
res.withCnrFilter = true
|
2020-12-08 07:51:34 +00:00
|
|
|
case // slow filters
|
|
|
|
v2object.FilterHeaderVersion,
|
|
|
|
v2object.FilterHeaderCreationEpoch,
|
|
|
|
v2object.FilterHeaderPayloadLength,
|
2024-08-19 14:13:39 +00:00
|
|
|
v2object.FilterHeaderHomomorphicHash,
|
2024-08-19 14:20:55 +00:00
|
|
|
v2object.FilterHeaderOwnerID,
|
|
|
|
v2object.FilterHeaderPayloadHash:
|
2020-12-08 07:51:34 +00:00
|
|
|
res.slowFilters = append(res.slowFilters, filters[i])
|
|
|
|
default: // fast filters or user attributes if unknown
|
|
|
|
res.fastFilters = append(res.fastFilters, filters[i])
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
|
|
|
}
|