2020-10-28 14:49:30 +00:00
|
|
|
package meta
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/nspcc-dev/neofs-api-go/pkg/object"
|
|
|
|
"github.com/pkg/errors"
|
|
|
|
"go.etcd.io/bbolt"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Select returns list of addresses of objects that match search filters.
|
|
|
|
func (db *DB) Select(fs object.SearchFilters) ([]*object.Address, error) {
|
|
|
|
res := make([]*object.Address, 0)
|
|
|
|
|
|
|
|
err := db.boltDB.View(func(tx *bbolt.Tx) error {
|
|
|
|
// get indexed bucket
|
|
|
|
indexBucket := tx.Bucket(indexBucket)
|
|
|
|
if indexBucket == nil {
|
|
|
|
// empty storage
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-11-03 12:08:54 +00:00
|
|
|
if len(fs) == 0 {
|
|
|
|
// get primary bucket
|
|
|
|
primaryBucket := tx.Bucket(primaryBucket)
|
|
|
|
if primaryBucket == nil {
|
|
|
|
// empty storage
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// iterate over all stored addresses
|
|
|
|
return primaryBucket.ForEach(func(k, v []byte) error {
|
|
|
|
// check if object marked as deleted
|
|
|
|
if objectRemoved(tx, k) {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
addr := object.NewAddress()
|
|
|
|
if err := addr.Parse(string(k)); err != nil {
|
|
|
|
// TODO: storage was broken, so we need to handle it
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
res = append(res, addr)
|
|
|
|
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-11-03 14:24:23 +00:00
|
|
|
// keep processed addresses
|
|
|
|
// value equal to number (index+1) of latest matched filter
|
|
|
|
mAddr := make(map[string]int)
|
2020-10-28 14:49:30 +00:00
|
|
|
|
2020-11-03 14:24:23 +00:00
|
|
|
for fNum := range fs {
|
|
|
|
matchFunc, ok := db.matchers[fs[fNum].Operation()]
|
2020-10-28 14:49:30 +00:00
|
|
|
if !ok {
|
2020-11-03 14:24:23 +00:00
|
|
|
return errors.Errorf("no function for matcher %v", fs[fNum].Operation())
|
2020-10-28 14:49:30 +00:00
|
|
|
}
|
|
|
|
|
2020-11-03 14:24:23 +00:00
|
|
|
key := fs[fNum].Header()
|
2020-10-28 14:49:30 +00:00
|
|
|
|
|
|
|
// get bucket with values
|
|
|
|
keyBucket := indexBucket.Bucket([]byte(key))
|
|
|
|
if keyBucket == nil {
|
2020-11-02 08:07:53 +00:00
|
|
|
// no object has this attribute => empty result
|
|
|
|
return nil
|
2020-10-28 14:49:30 +00:00
|
|
|
}
|
|
|
|
|
2020-11-03 14:24:23 +00:00
|
|
|
fVal := fs[fNum].Value()
|
2020-10-28 14:49:30 +00:00
|
|
|
|
|
|
|
// iterate over all existing values for the key
|
2020-11-03 12:12:56 +00:00
|
|
|
if err := keyBucket.ForEach(func(k, v []byte) error {
|
2020-11-03 12:08:54 +00:00
|
|
|
include := matchFunc(string(key), string(cutKeyBytes(k)), fVal)
|
2020-10-29 11:38:50 +00:00
|
|
|
|
2020-11-03 12:12:56 +00:00
|
|
|
strs, err := decodeAddressList(v)
|
|
|
|
if err != nil {
|
|
|
|
return errors.Wrapf(err, "(%T) could not decode address list", db)
|
|
|
|
}
|
|
|
|
|
2020-11-03 14:24:23 +00:00
|
|
|
for j := range strs {
|
|
|
|
if num := mAddr[strs[j]]; num != fNum {
|
|
|
|
// than object does not match some previous filter
|
|
|
|
continue
|
|
|
|
} else if include {
|
|
|
|
mAddr[strs[j]] = fNum + 1
|
2020-11-03 12:08:54 +00:00
|
|
|
}
|
2020-11-03 12:12:56 +00:00
|
|
|
}
|
2020-10-28 14:49:30 +00:00
|
|
|
|
2020-11-03 12:12:56 +00:00
|
|
|
return nil
|
2020-10-28 14:49:30 +00:00
|
|
|
}); err != nil {
|
|
|
|
return errors.Wrapf(err, "(%T) could not iterate bucket %s", db, key)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-03 14:24:23 +00:00
|
|
|
fLen := len(fs)
|
|
|
|
|
|
|
|
for a, ind := range mAddr {
|
|
|
|
if ind != fLen {
|
2020-11-03 13:12:34 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2020-10-29 12:03:55 +00:00
|
|
|
// check if object marked as deleted
|
2020-11-03 12:08:54 +00:00
|
|
|
if objectRemoved(tx, []byte(a)) {
|
2020-11-03 14:09:59 +00:00
|
|
|
continue
|
2020-10-29 12:03:55 +00:00
|
|
|
}
|
|
|
|
|
2020-10-28 14:49:30 +00:00
|
|
|
addr := object.NewAddress()
|
2020-11-03 12:08:54 +00:00
|
|
|
if err := addr.Parse(a); err != nil {
|
2020-10-28 14:49:30 +00:00
|
|
|
// TODO: storage was broken, so we need to handle it
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
res = append(res, addr)
|
2020-11-03 12:08:54 +00:00
|
|
|
}
|
2020-10-28 14:49:30 +00:00
|
|
|
|
2020-11-03 12:08:54 +00:00
|
|
|
return nil
|
2020-10-28 14:49:30 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
return res, err
|
|
|
|
}
|