Skip to content

Commit

Permalink
add wrappers/count
Browse files Browse the repository at this point in the history
  • Loading branch information
fiatjaf committed Aug 20, 2024
1 parent 1b51d99 commit ce90258
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 0 deletions.
4 changes: 4 additions & 0 deletions store.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,7 @@ type Store interface {
// SaveEvent is called once Relay.AcceptEvent reports true.
SaveEvent(context.Context, *nostr.Event) error
}

type Counter interface {
CountEvents(context.Context, nostr.Filter) (int64, error)
}
34 changes: 34 additions & 0 deletions wrappers/count/count.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package count

import (
"context"

"github.com/fiatjaf/eventstore"
"github.com/nbd-wtf/go-nostr"
)

type Wrapper struct {
eventstore.Store
}

var _ eventstore.Store = (*Wrapper)(nil)

func (w Wrapper) CountEvents(ctx context.Context, filter nostr.Filter) (int64, error) {
if counter, ok := w.Store.(eventstore.Counter); ok {
return counter.CountEvents(ctx, filter)
}

ch, err := w.Store.QueryEvents(ctx, filter)
if err != nil {
return 0, err
}
if ch == nil {
return 0, nil
}

var count int64
for range ch {
count++
}
return count, nil
}

0 comments on commit ce90258

Please sign in to comment.