Merge branch 'hyperloglog'

This commit is contained in:
fiatjaf
2024-12-07 00:13:23 -03:00
9 changed files with 426 additions and 7 deletions

View File

@@ -2,6 +2,7 @@ package nostr
import (
"bytes"
"encoding/hex"
"fmt"
"strconv"
@@ -141,7 +142,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" }
@@ -160,9 +162,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
}
@@ -188,6 +192,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, 512)
hex.Encode(hllHex, v.HyperLogLog)
w.Buffer.AppendBytes(hllHex)
w.RawString(`"`)
}
w.RawString(`}`)
} else {
for _, filter := range v.Filters {