Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
fiatjaf committed Nov 16, 2024
1 parent 99e4503 commit 214288e
Show file tree
Hide file tree
Showing 7 changed files with 48 additions and 10 deletions.
13 changes: 12 additions & 1 deletion envelopes.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package nostr

import (
"bytes"
"encoding/hex"
"encoding/json"
"fmt"
"strconv"
Expand Down Expand Up @@ -142,7 +143,8 @@ func (v ReqEnvelope) MarshalJSON() ([]byte, error) {
type CountEnvelope struct {
SubscriptionID string
Filters
Count *int64
Count *int64
HyperLogLog []byte
}

func (_ CountEnvelope) Label() string { return "COUNT" }
Expand All @@ -161,9 +163,11 @@ func (v *CountEnvelope) UnmarshalJSON(data []byte) error {

var countResult struct {
Count *int64 `json:"count"`
HLL string `json:"hll"`
}
if err := json.Unmarshal([]byte(arr[2].Raw), &countResult); err == nil && countResult.Count != nil {
v.Count = countResult.Count
v.HyperLogLog, _ = hex.DecodeString(countResult.HLL)
return nil
}

Expand All @@ -189,6 +193,13 @@ func (v CountEnvelope) MarshalJSON() ([]byte, error) {
if v.Count != nil {
w.RawString(`,{"count":`)
w.RawString(strconv.FormatInt(*v.Count, 10))
if v.HyperLogLog != nil {
w.RawString(`,"hll":"`)
hllHex := make([]byte, 0, 512)
hex.Encode(hllHex, v.HyperLogLog)
w.Buffer.AppendBytes(hllHex)
w.RawString(`"`)
}
w.RawString(`}`)
} else {
for _, filter := range v.Filters {
Expand Down
2 changes: 1 addition & 1 deletion nip45/helpers.go → nip45/hyperloglog/helpers.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package nip45
package hyperloglog

import (
"math"
Expand Down
2 changes: 1 addition & 1 deletion nip45/hll.go → nip45/hyperloglog/hll.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package nip45
package hyperloglog

import (
"encoding/binary"
Expand Down
2 changes: 1 addition & 1 deletion nip45/hll_test.go → nip45/hyperloglog/hll_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package nip45
package hyperloglog

import (
"encoding/hex"
Expand Down
19 changes: 19 additions & 0 deletions pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"sync"
"time"

"github.com/nbd-wtf/go-nostr/nip45/hyperloglog"
"github.com/puzpuzpuz/xsync/v3"
)

Expand Down Expand Up @@ -468,6 +469,24 @@ func (pool *SimplePool) subManyEose(
return events
}

// CountMany aggregates count results from multiple relays using HyperLogLog
func (pool *SimplePool) CountMany(
ctx context.Context,
urls []string,
filter Filter,
opts []SubscriptionOption,
) int {
hll := hyperloglog.New()

for _, url := range urls {
go func(nm string) {
relay, err := pool.EnsureRelay(url)

Check failure on line 483 in pool.go

View workflow job for this annotation

GitHub Actions / test

declared and not used: relay

Check failure on line 483 in pool.go

View workflow job for this annotation

GitHub Actions / test

declared and not used: err
}(NormalizeURL(url))
}

return int(hll.Count())
}

// QuerySingle returns the first event returned by the first relay, cancels everything else.
func (pool *SimplePool) QuerySingle(ctx context.Context, urls []string, filter Filter) *RelayEvent {
ctx, cancel := context.WithCancel(ctx)
Expand Down
16 changes: 12 additions & 4 deletions relay.go
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,7 @@ func (r *Relay) ConnectWithTLS(ctx context.Context, tlsConfig *tls.Config) error
}
case *CountEnvelope:
if subscription, ok := r.Subscriptions.Load(subIdToSerial(env.SubscriptionID)); ok && env.Count != nil && subscription.countResult != nil {
subscription.countResult <- *env.Count
subscription.countResult <- *env
}
case *OKEnvelope:
if okCallback, exist := r.okCallbacks.Load(env.EventID); exist {
Expand Down Expand Up @@ -478,11 +478,19 @@ func (r *Relay) QuerySync(ctx context.Context, filter Filter) ([]*Event, error)
}

func (r *Relay) Count(ctx context.Context, filters Filters, opts ...SubscriptionOption) (int64, error) {
v, err := r.countInternal(ctx, filters, opts...)
if err != nil {
return 0, err
}
return *v.Count, nil
}

func (r *Relay) countInternal(ctx context.Context, filters Filters, opts ...SubscriptionOption) (CountEnvelope, error) {
sub := r.PrepareSubscription(ctx, filters, opts...)
sub.countResult = make(chan int64)
sub.countResult = make(chan CountEnvelope)

if err := sub.Fire(); err != nil {
return 0, err
return CountEnvelope{}, err
}

defer sub.Unsub()
Expand All @@ -499,7 +507,7 @@ func (r *Relay) Count(ctx context.Context, filters Filters, opts ...Subscription
case count := <-sub.countResult:
return count, nil
case <-ctx.Done():
return 0, ctx.Err()
return CountEnvelope{}, ctx.Err()
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions subscription.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ type Subscription struct {
Filters Filters

// for this to be treated as a COUNT and not a REQ this must be set
countResult chan int64
countResult chan CountEnvelope

// the Events channel emits all EVENTs that come in a Subscription
// will be closed when the subscription ends
Expand Down Expand Up @@ -152,7 +152,7 @@ func (sub *Subscription) Fire() error {
if sub.countResult == nil {
reqb, _ = ReqEnvelope{sub.id, sub.Filters}.MarshalJSON()
} else {
reqb, _ = CountEnvelope{sub.id, sub.Filters, nil}.MarshalJSON()
reqb, _ = CountEnvelope{sub.id, sub.Filters, nil, nil}.MarshalJSON()
}

sub.live.Store(true)
Expand Down

0 comments on commit 214288e

Please sign in to comment.