2022-09-02 19:23:33 +00:00
|
|
|
package registry
|
|
|
|
|
|
|
|
import (
|
2022-10-26 19:29:17 +00:00
|
|
|
"context"
|
|
|
|
"fmt"
|
2022-09-26 18:05:28 +00:00
|
|
|
"time"
|
2022-09-02 19:23:33 +00:00
|
|
|
|
2023-03-23 14:42:51 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/io"
|
2022-09-02 19:23:33 +00:00
|
|
|
"go.etcd.io/bbolt"
|
|
|
|
)
|
|
|
|
|
2024-06-24 10:59:12 +00:00
|
|
|
const nextObjectTimeout = 10 * time.Second
|
|
|
|
|
2022-09-26 18:05:28 +00:00
|
|
|
type ObjFilter struct {
|
|
|
|
Status string
|
|
|
|
Age int
|
|
|
|
}
|
|
|
|
|
2022-09-02 19:23:33 +00:00
|
|
|
type ObjSelector struct {
|
2022-11-10 06:23:33 +00:00
|
|
|
ctx context.Context
|
|
|
|
objChan chan *ObjectInfo
|
|
|
|
boltDB *bbolt.DB
|
|
|
|
filter *ObjFilter
|
|
|
|
cacheSize int
|
2024-04-05 10:16:31 +00:00
|
|
|
kind SelectorKind
|
2022-09-26 18:05:28 +00:00
|
|
|
}
|
|
|
|
|
2022-11-10 06:23:33 +00:00
|
|
|
// objectSelectCache is the default maximum size of a batch to select from DB.
|
|
|
|
const objectSelectCache = 1000
|
2022-10-26 19:29:17 +00:00
|
|
|
|
2022-09-26 18:05:28 +00:00
|
|
|
// NewObjSelector creates a new instance of object selector that can iterate over
|
|
|
|
// objects in the specified registry.
|
2024-04-05 10:16:31 +00:00
|
|
|
func NewObjSelector(registry *ObjRegistry, selectionSize int, kind SelectorKind, filter *ObjFilter) *ObjSelector {
|
2022-11-10 06:23:33 +00:00
|
|
|
if selectionSize <= 0 {
|
|
|
|
selectionSize = objectSelectCache
|
|
|
|
}
|
2023-03-24 12:25:25 +00:00
|
|
|
if filter == nil || filter.Status == "" {
|
|
|
|
panic("filtering without status is not supported")
|
|
|
|
}
|
2022-10-26 19:29:17 +00:00
|
|
|
objSelector := &ObjSelector{
|
2022-11-10 06:23:33 +00:00
|
|
|
ctx: registry.ctx,
|
|
|
|
boltDB: registry.boltDB,
|
|
|
|
filter: filter,
|
|
|
|
objChan: make(chan *ObjectInfo, selectionSize*2),
|
|
|
|
cacheSize: selectionSize,
|
2024-04-05 10:16:31 +00:00
|
|
|
kind: kind,
|
2022-10-26 19:29:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
go objSelector.selectLoop()
|
|
|
|
|
2022-09-26 18:05:28 +00:00
|
|
|
return objSelector
|
2022-09-02 19:23:33 +00:00
|
|
|
}
|
|
|
|
|
2022-09-26 18:05:28 +00:00
|
|
|
// NextObject returns the next object from the registry that matches filter of
|
|
|
|
// the selector. NextObject only roams forward from the current position of the
|
2022-10-26 19:29:17 +00:00
|
|
|
// selector. If there are no objects that match the filter, blocks until one of
|
|
|
|
// the following happens:
|
|
|
|
// - a "new" next object is available;
|
|
|
|
// - underlying registry context is done, nil objects will be returned on the
|
2023-07-20 12:01:57 +00:00
|
|
|
// currently blocked and every further NextObject calls.
|
|
|
|
func (o *ObjSelector) NextObject() *ObjectInfo {
|
2024-06-24 10:59:12 +00:00
|
|
|
if o.kind == SelectorOneshot {
|
|
|
|
return <-o.objChan
|
|
|
|
}
|
|
|
|
|
|
|
|
select {
|
|
|
|
case <-time.After(nextObjectTimeout):
|
|
|
|
return nil
|
|
|
|
case obj := <-o.objChan:
|
|
|
|
return obj
|
|
|
|
}
|
2022-09-26 18:05:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Count returns total number of objects that match filter of the selector.
|
|
|
|
func (o *ObjSelector) Count() (int, error) {
|
2024-03-20 08:41:03 +00:00
|
|
|
count := 0
|
2022-09-26 18:05:28 +00:00
|
|
|
err := o.boltDB.View(func(tx *bbolt.Tx) error {
|
2023-03-23 14:42:51 +00:00
|
|
|
b := tx.Bucket([]byte(o.filter.Status))
|
2022-09-26 18:05:28 +00:00
|
|
|
if b == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-03-23 14:42:51 +00:00
|
|
|
if o.filter.Age == 0 {
|
|
|
|
count = b.Stats().KeyN
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-09-26 18:05:28 +00:00
|
|
|
return b.ForEach(func(_, objBytes []byte) error {
|
|
|
|
if objBytes != nil {
|
2023-03-23 14:42:51 +00:00
|
|
|
r := io.NewBinReaderFromBuf(objBytes)
|
|
|
|
|
2022-09-26 18:05:28 +00:00
|
|
|
var obj ObjectInfo
|
2023-03-23 14:42:51 +00:00
|
|
|
obj.decodeFilterableFields(r)
|
|
|
|
if r.Err != nil {
|
2022-09-26 18:05:28 +00:00
|
|
|
// Ignore malformed objects
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
if o.filter.match(obj) {
|
|
|
|
count++
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
})
|
|
|
|
return count, err
|
|
|
|
}
|
|
|
|
|
2022-10-26 19:29:17 +00:00
|
|
|
func (o *ObjSelector) selectLoop() {
|
2022-11-10 06:23:33 +00:00
|
|
|
cache := make([]*ObjectInfo, 0, o.cacheSize)
|
2022-10-26 19:29:17 +00:00
|
|
|
var lastID uint64
|
|
|
|
defer close(o.objChan)
|
|
|
|
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-o.ctx.Done():
|
|
|
|
return
|
|
|
|
default:
|
|
|
|
}
|
|
|
|
|
|
|
|
// cache the objects
|
|
|
|
err := o.boltDB.View(func(tx *bbolt.Tx) error {
|
2023-03-23 14:42:51 +00:00
|
|
|
b := tx.Bucket([]byte(o.filter.Status))
|
2022-10-26 19:29:17 +00:00
|
|
|
if b == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
c := b.Cursor()
|
|
|
|
|
|
|
|
// Establish the start position for searching the next object:
|
|
|
|
// If we should go from the beginning (lastID=0), then we start
|
|
|
|
// from the first element. Otherwise, we start from the last
|
|
|
|
// handled ID + 1.
|
|
|
|
var keyBytes, objBytes []byte
|
|
|
|
if lastID == 0 {
|
|
|
|
keyBytes, objBytes = c.First()
|
|
|
|
} else {
|
|
|
|
keyBytes, objBytes = c.Seek(encodeId(lastID))
|
|
|
|
if keyBytes != nil && decodeId(keyBytes) == lastID {
|
|
|
|
keyBytes, objBytes = c.Next()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Iterate over objects to find the next object matching the filter.
|
2022-11-10 06:23:33 +00:00
|
|
|
for ; keyBytes != nil && len(cache) != o.cacheSize; keyBytes, objBytes = c.Next() {
|
2022-10-26 19:29:17 +00:00
|
|
|
if objBytes != nil {
|
|
|
|
var obj ObjectInfo
|
2023-03-23 13:32:25 +00:00
|
|
|
if err := obj.Unmarshal(objBytes); err != nil {
|
2022-10-26 19:29:17 +00:00
|
|
|
// Ignore malformed objects for now. Maybe it should be panic?
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if o.filter.match(obj) {
|
|
|
|
cache = append(cache, &obj)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(cache) > 0 {
|
|
|
|
lastID = cache[len(cache)-1].Id
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
panic(fmt.Errorf("fetching objects failed: %w", err))
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, obj := range cache {
|
|
|
|
select {
|
|
|
|
case <-o.ctx.Done():
|
|
|
|
return
|
|
|
|
case o.objChan <- obj:
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-04-05 10:16:31 +00:00
|
|
|
if o.kind == SelectorOneshot && len(cache) != o.cacheSize {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if o.kind != SelectorLooped && len(cache) != o.cacheSize {
|
2022-10-26 19:29:17 +00:00
|
|
|
// no more objects, wait a little; the logic could be improved.
|
|
|
|
select {
|
2023-07-19 14:23:38 +00:00
|
|
|
case <-time.After(time.Second):
|
2022-10-26 19:29:17 +00:00
|
|
|
case <-o.ctx.Done():
|
|
|
|
return
|
|
|
|
}
|
2024-03-19 14:18:22 +00:00
|
|
|
}
|
|
|
|
|
2024-04-05 10:16:31 +00:00
|
|
|
if o.kind == SelectorLooped && len(cache) != o.cacheSize {
|
2023-07-19 14:23:38 +00:00
|
|
|
lastID = 0
|
2022-10-26 19:29:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// clean handled objects
|
|
|
|
cache = cache[:0]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-26 18:05:28 +00:00
|
|
|
func (f *ObjFilter) match(o ObjectInfo) bool {
|
|
|
|
if f.Status != "" && f.Status != o.Status {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
if f.Age != 0 {
|
2023-03-23 13:32:25 +00:00
|
|
|
objAge := time.Now().UTC().Unix() - o.CreatedAt
|
|
|
|
if objAge < int64(f.Age) {
|
2022-09-26 18:05:28 +00:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|