lmdb/badger/bolt: limit index scans to the maximum of total returnable events.

This commit is contained in:
fiatjaf
2024-04-16 17:11:39 -03:00
parent 13db9b84a7
commit fc2884d7aa
3 changed files with 44 additions and 19 deletions

View File

@@ -34,12 +34,21 @@ func (b BadgerBackend) QueryEvents(ctx context.Context, filter nostr.Filter) (ch
return nil, err
}
// max number of events we'll return
limit := b.MaxLimit / 4
if filter.Limit > 0 && filter.Limit < b.MaxLimit {
limit = filter.Limit
}
go func() {
defer close(ch)
// actually iterate
for _, q := range queries {
q := q
pulled := 0 // this query will be hardcapped at this global limit
go b.View(func(txn *badger.Txn) error {
// iterate only through keys and in reverse order
opts := badger.IteratorOptions{
@@ -87,7 +96,15 @@ func (b BadgerBackend) QueryEvents(ctx context.Context, filter nostr.Filter) (ch
// check if this matches the other filters that were not part of the index
if extraFilter == nil || extraFilter.Matches(evt) {
q.results <- evt
select {
case q.results <- evt:
pulled++
if pulled > limit {
break
}
case <-ctx.Done():
break
}
}
return nil
@@ -98,12 +115,6 @@ func (b BadgerBackend) QueryEvents(ctx context.Context, filter nostr.Filter) (ch
})
}
// max number of events we'll return
limit := b.MaxLimit
if filter.Limit > 0 && filter.Limit < limit {
limit = filter.Limit
}
// receive results and ensure we only return the most recent ones always
emittedEvents := 0