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

@@ -35,12 +35,21 @@ func (b *LMDBBackend) QueryEvents(ctx context.Context, filter nostr.Filter) (cha
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
}
ch := make(chan *nostr.Event)
go func() {
defer close(ch)
for _, q := range queries {
q := q
pulled := 0 // this will be hard-capped at the global limit of the query
go b.lmdbEnv.View(func(txn *lmdb.Txn) error {
txn.RawRead = true
defer close(q.results)
@@ -105,6 +114,10 @@ func (b *LMDBBackend) QueryEvents(ctx context.Context, filter nostr.Filter) (cha
if extraFilter == nil || extraFilter.Matches(evt) {
select {
case q.results <- evt:
pulled++
if pulled >= limit {
break
}
case <-ctx.Done():
break
}
@@ -120,12 +133,6 @@ func (b *LMDBBackend) QueryEvents(ctx context.Context, filter nostr.Filter) (cha
log.Printf("lmdb: error on cursor iteration: %v\n", err)
}
// 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