filter.GetTheoreticalLimit() to encompass the actual limit specified in the filter.
This commit is contained in:
@@ -11,11 +11,10 @@ import (
|
||||
|
||||
func (b *BleveBackend) QueryEvents(filter nostr.Filter, maxLimit int) iter.Seq[nostr.Event] {
|
||||
return func(yield func(nostr.Event) bool) {
|
||||
limit := maxLimit
|
||||
if filter.LimitZero {
|
||||
if tlimit := filter.GetTheoreticalLimit(); tlimit == 0 {
|
||||
return
|
||||
} else if filter.Limit > 0 && filter.Limit < limit {
|
||||
limit = filter.Limit
|
||||
} else if tlimit < maxLimit {
|
||||
maxLimit = tlimit
|
||||
}
|
||||
|
||||
if len(filter.Search) < 2 {
|
||||
@@ -72,7 +71,7 @@ func (b *BleveBackend) QueryEvents(filter nostr.Filter, maxLimit int) iter.Seq[n
|
||||
}
|
||||
|
||||
req := bleve.NewSearchRequest(q)
|
||||
req.Size = limit
|
||||
req.Size = maxLimit
|
||||
req.From = 0
|
||||
|
||||
result, err := b.index.Search(req)
|
||||
|
||||
@@ -30,14 +30,11 @@ func (b *BoltBackend) QueryEvents(filter nostr.Filter, maxLimit int) iter.Seq[no
|
||||
}
|
||||
|
||||
// max number of events we'll return
|
||||
if tlimit := filter.GetTheoreticalLimit(); tlimit == 0 || filter.LimitZero {
|
||||
if tlimit := filter.GetTheoreticalLimit(); tlimit == 0 {
|
||||
return
|
||||
} else if tlimit < maxLimit {
|
||||
maxLimit = tlimit
|
||||
}
|
||||
if filter.Limit > 0 && filter.Limit < maxLimit {
|
||||
maxLimit = filter.Limit
|
||||
}
|
||||
|
||||
// do a normal query based on various filters
|
||||
if err := b.DB.View(func(txn *bbolt.Tx) error {
|
||||
|
||||
@@ -31,14 +31,11 @@ func (b *LMDBBackend) QueryEvents(filter nostr.Filter, maxLimit int) iter.Seq[no
|
||||
}
|
||||
|
||||
// max number of events we'll return
|
||||
if tlimit := filter.GetTheoreticalLimit(); tlimit == 0 || filter.LimitZero {
|
||||
if tlimit := filter.GetTheoreticalLimit(); tlimit == 0 {
|
||||
return
|
||||
} else if tlimit < maxLimit {
|
||||
maxLimit = tlimit
|
||||
}
|
||||
if filter.Limit > 0 && filter.Limit < maxLimit {
|
||||
maxLimit = filter.Limit
|
||||
}
|
||||
|
||||
// do a normal query based on various filters
|
||||
if err := b.lmdbEnv.View(func(txn *lmdb.Txn) error {
|
||||
|
||||
@@ -96,14 +96,11 @@ func (il *IndexingLayer) QueryEvents(filter nostr.Filter, maxLimit int) iter.Seq
|
||||
}
|
||||
|
||||
// max number of events we'll return
|
||||
if tlimit := filter.GetTheoreticalLimit(); tlimit == 0 || filter.LimitZero {
|
||||
if tlimit := filter.GetTheoreticalLimit(); tlimit == 0 {
|
||||
return
|
||||
} else if tlimit < maxLimit {
|
||||
maxLimit = tlimit
|
||||
}
|
||||
if filter.Limit > 0 && filter.Limit < maxLimit {
|
||||
maxLimit = filter.Limit
|
||||
}
|
||||
|
||||
il.lmdbEnv.View(func(txn *lmdb.Txn) error {
|
||||
txn.RawRead = true
|
||||
|
||||
@@ -29,8 +29,10 @@ func (b *SliceStore) Close() {}
|
||||
|
||||
func (b *SliceStore) QueryEvents(filter nostr.Filter, maxLimit int) iter.Seq[nostr.Event] {
|
||||
return func(yield func(nostr.Event) bool) {
|
||||
if filter.Limit > maxLimit || (filter.Limit == 0 && !filter.LimitZero) {
|
||||
filter.Limit = maxLimit
|
||||
if tlimit := filter.GetTheoreticalLimit(); tlimit == 0 {
|
||||
return
|
||||
} else if tlimit < maxLimit {
|
||||
maxLimit = tlimit
|
||||
}
|
||||
|
||||
// efficiently determine where to start and end
|
||||
@@ -50,7 +52,7 @@ func (b *SliceStore) QueryEvents(filter nostr.Filter, maxLimit int) iter.Seq[nos
|
||||
|
||||
count := 0
|
||||
for _, event := range b.internal[start:end] {
|
||||
if count == filter.Limit {
|
||||
if count == maxLimit {
|
||||
break
|
||||
}
|
||||
|
||||
|
||||
@@ -183,5 +183,13 @@ func (filter Filter) GetTheoreticalLimit() int {
|
||||
}
|
||||
}
|
||||
|
||||
if filter.Limit > 0 {
|
||||
return filter.Limit
|
||||
}
|
||||
|
||||
if filter.LimitZero {
|
||||
return 0
|
||||
}
|
||||
|
||||
return math.MaxInt
|
||||
}
|
||||
|
||||
@@ -145,6 +145,9 @@ func TestTheoreticalLimit(t *testing.T) {
|
||||
require.Equal(t, 9, Filter{Authors: []PubKey{{'a'}, {'b'}, {'c'}}, Kinds: []Kind{3, 0, 10002}}.GetTheoreticalLimit())
|
||||
require.Equal(t, math.MaxInt, Filter{Authors: []PubKey{{'a'}}}.GetTheoreticalLimit())
|
||||
require.Equal(t, 4, Filter{Authors: []PubKey{{'a'}, {'b'}, {'c'}, {'d'}}, Kinds: []Kind{10050}}.GetTheoreticalLimit())
|
||||
require.Equal(t, 4, Filter{Limit: 18, Authors: []PubKey{{'a'}, {'b'}, {'c'}, {'d'}}, Kinds: []Kind{10050}}.GetTheoreticalLimit())
|
||||
require.Equal(t, 0, Filter{LimitZero: true}.GetTheoreticalLimit())
|
||||
require.Equal(t, 18, Filter{Limit: 18}.GetTheoreticalLimit())
|
||||
require.Equal(t, math.MaxInt, Filter{Authors: []PubKey{{'a'}, {'b'}, {'c'}, {'d'}}}.GetTheoreticalLimit())
|
||||
require.Equal(t, math.MaxInt, Filter{Kinds: []Kind{3, 0, 10002}}.GetTheoreticalLimit())
|
||||
require.Equal(t, 24, Filter{Authors: []PubKey{{'a'}, {'b'}, {'c'}, {'d'}, {'e'}, {'f'}}, Kinds: []Kind{30023, 30024}, Tags: TagMap{"d": []string{"aaa", "bbb"}}}.GetTheoreticalLimit())
|
||||
|
||||
Reference in New Issue
Block a user