Skip to content

Commit

Permalink
bolt: fix count.
Browse files Browse the repository at this point in the history
  • Loading branch information
fiatjaf committed Aug 20, 2024
1 parent c30ee92 commit 1b51d99
Showing 1 changed file with 13 additions and 3 deletions.
16 changes: 13 additions & 3 deletions bolt/count.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,20 @@ func (b *BoltBackend) CountEvents(ctx context.Context, filter nostr.Filter) (int
raw := txn.Bucket(bucketRaw)

c := bucket.Cursor()
for k, v := c.Seek(q.startingPoint); k != nil && bytes.HasPrefix(k, q.prefix); k, v = c.Prev() {

key, _ := c.Seek(q.startingPoint)
if key == nil {
key, _ = c.Last()
} else {
key, _ = c.Prev()
}

for ; key != nil && bytes.HasPrefix(key, q.prefix); key, _ = c.Prev() {
idxOffset := len(key) - 4 // this is where the idx actually starts

// "id" indexes don't contain a timestamp
if !q.skipTimestamp {
createdAt := binary.BigEndian.Uint32(k[len(k)-4:])
createdAt := binary.BigEndian.Uint32(key[idxOffset-4 : idxOffset])
if createdAt < since {
break
}
Expand All @@ -39,7 +49,7 @@ func (b *BoltBackend) CountEvents(ctx context.Context, filter nostr.Filter) (int
count++
} else {
// fetch actual event
val := raw.Get(v)
val := raw.Get(key[len(key)-4:])
evt := &nostr.Event{}
if err := nostr_binary.Unmarshal(val, evt); err != nil {
log.Printf("bolt: value read error (id %x): %s\n", val[0:32], err)
Expand Down

0 comments on commit 1b51d99

Please sign in to comment.