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 13:12:34 +00:00
|
|
|
// keep processed addresses (false if address was added and excluded later)
|
|
|
|
mAddr := make(map[string]bool)
|
2020-10-28 14:49:30 +00:00
|
|
|
|
|
|
|
for _, f := range fs {
|
|
|
|
matchFunc, ok := db.matchers[f.Operation()]
|
|
|
|
if !ok {
|
|
|
|
return errors.Errorf("no function for matcher %v", f.Operation())
|
|
|
|
}
|
|
|
|
|
|
|
|
key := f.Header()
|
|
|
|
|
|
|
|
// 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
|
|
|
}
|
|
|
|
|
|
|
|
fVal := f.Value()
|
|
|
|
|
|
|
|
// 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)
|
|
|
|
}
|
|
|
|
|
|
|
|
for i := range strs {
|
2020-11-03 12:08:54 +00:00
|
|
|
if include {
|
2020-11-03 13:12:34 +00:00
|
|
|
if _, ok := mAddr[strs[i]]; !ok {
|
|
|
|
mAddr[strs[i]] = true
|
|
|
|
}
|
2020-11-03 12:08:54 +00:00
|
|
|
} else {
|
2020-11-03 13:12:34 +00:00
|
|
|
mAddr[strs[i]] = false
|
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 13:12:34 +00:00
|
|
|
for a, inc := range mAddr {
|
|
|
|
if !inc {
|
|
|
|
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
|
|
|
|
}
|