limit index scans to the maximum of total returnable events.
This commit is contained in:
@@ -34,12 +34,21 @@ func (b BadgerBackend) QueryEvents(ctx context.Context, filter nostr.Filter) (ch
|
|||||||
return nil, err
|
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() {
|
go func() {
|
||||||
defer close(ch)
|
defer close(ch)
|
||||||
|
|
||||||
// actually iterate
|
// actually iterate
|
||||||
for _, q := range queries {
|
for _, q := range queries {
|
||||||
q := q
|
q := q
|
||||||
|
|
||||||
|
pulled := 0 // this query will be hardcapped at this global limit
|
||||||
|
|
||||||
go b.View(func(txn *badger.Txn) error {
|
go b.View(func(txn *badger.Txn) error {
|
||||||
// iterate only through keys and in reverse order
|
// iterate only through keys and in reverse order
|
||||||
opts := badger.IteratorOptions{
|
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
|
// check if this matches the other filters that were not part of the index
|
||||||
if extraFilter == nil || extraFilter.Matches(evt) {
|
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
|
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
|
// receive results and ensure we only return the most recent ones always
|
||||||
emittedEvents := 0
|
emittedEvents := 0
|
||||||
|
|
||||||
|
|||||||
@@ -34,12 +34,21 @@ func (b *BoltBackend) QueryEvents(ctx context.Context, filter nostr.Filter) (cha
|
|||||||
return nil, err
|
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)
|
ch := make(chan *nostr.Event)
|
||||||
go func() {
|
go func() {
|
||||||
defer close(ch)
|
defer close(ch)
|
||||||
|
|
||||||
for _, q := range queries {
|
for _, q := range queries {
|
||||||
q := q
|
q := q
|
||||||
|
|
||||||
|
pulled := 0 // this query will be hardcapped at this global limit
|
||||||
|
|
||||||
go b.db.View(func(txn *bolt.Tx) error {
|
go b.db.View(func(txn *bolt.Tx) error {
|
||||||
defer close(q.results)
|
defer close(q.results)
|
||||||
|
|
||||||
@@ -76,6 +85,10 @@ func (b *BoltBackend) QueryEvents(ctx context.Context, filter nostr.Filter) (cha
|
|||||||
if extraFilter == nil || extraFilter.Matches(evt) {
|
if extraFilter == nil || extraFilter.Matches(evt) {
|
||||||
select {
|
select {
|
||||||
case q.results <- evt:
|
case q.results <- evt:
|
||||||
|
pulled++
|
||||||
|
if pulled > limit {
|
||||||
|
break
|
||||||
|
}
|
||||||
case <-ctx.Done():
|
case <-ctx.Done():
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
@@ -85,12 +98,6 @@ func (b *BoltBackend) QueryEvents(ctx context.Context, filter nostr.Filter) (cha
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
// 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
|
// receive results and ensure we only return the most recent ones always
|
||||||
emittedEvents := 0
|
emittedEvents := 0
|
||||||
|
|
||||||
|
|||||||
@@ -35,12 +35,21 @@ func (b *LMDBBackend) QueryEvents(ctx context.Context, filter nostr.Filter) (cha
|
|||||||
return nil, err
|
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)
|
ch := make(chan *nostr.Event)
|
||||||
go func() {
|
go func() {
|
||||||
defer close(ch)
|
defer close(ch)
|
||||||
|
|
||||||
for _, q := range queries {
|
for _, q := range queries {
|
||||||
q := q
|
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 {
|
go b.lmdbEnv.View(func(txn *lmdb.Txn) error {
|
||||||
txn.RawRead = true
|
txn.RawRead = true
|
||||||
defer close(q.results)
|
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) {
|
if extraFilter == nil || extraFilter.Matches(evt) {
|
||||||
select {
|
select {
|
||||||
case q.results <- evt:
|
case q.results <- evt:
|
||||||
|
pulled++
|
||||||
|
if pulled >= limit {
|
||||||
|
break
|
||||||
|
}
|
||||||
case <-ctx.Done():
|
case <-ctx.Done():
|
||||||
break
|
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)
|
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
|
// receive results and ensure we only return the most recent ones always
|
||||||
emittedEvents := 0
|
emittedEvents := 0
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user