define a nostr.Kind type for event kinds, make adjustments everywhere and fix some horrible bugs with mmm, lmdb and badger querying and deleting.

This commit is contained in:
fiatjaf
2025-04-20 11:14:39 -03:00
parent 27f40c2cf2
commit 15c6093c9b
74 changed files with 689 additions and 404 deletions

View File

@@ -60,9 +60,9 @@ func (b *BadgerBackend) CountEvents(filter nostr.Filter) (uint32, error) {
return err
}
err = item.Value(func(val []byte) error {
err = item.Value(func(bin []byte) error {
evt := nostr.Event{}
if err := betterbinary.Unmarshal(val, &evt); err != nil {
if err := betterbinary.Unmarshal(bin, &evt); err != nil {
return err
}
@@ -135,15 +135,15 @@ func (b *BadgerBackend) CountEventsHLL(filter nostr.Filter, offset int) (uint32,
return err
}
err = item.Value(func(val []byte) error {
err = item.Value(func(bin []byte) error {
if extraFilter == nil {
hll.AddBytes([32]byte(val[32:64]))
hll.AddBytes(betterbinary.GetPubKey(bin))
count++
return nil
}
evt := nostr.Event{}
if err := betterbinary.Unmarshal(val, &evt); err != nil {
if err := betterbinary.Unmarshal(bin, &evt); err != nil {
return err
}
if extraFilter.Matches(evt) {

View File

@@ -12,6 +12,8 @@ import (
var serialDelete uint32 = 0
func (b *BadgerBackend) DeleteEvent(id nostr.ID) error {
fmt.Println("...", id)
deletionHappened := false
err := b.Update(func(txn *badger.Txn) error {
@@ -52,21 +54,24 @@ func (b *BadgerBackend) delete(txn *badger.Txn, id nostr.ID) (bool, error) {
var evt nostr.Event
it := txn.NewIterator(opts)
defer it.Close()
it.Seek(prefix)
if it.ValidForPrefix(prefix) {
idx = append(idx, it.Item().Key()[1+8:]...)
if err := it.Item().Value(func(val []byte) error {
return betterbinary.Unmarshal(val, &evt)
}); err != nil {
return false, fmt.Errorf("failed to unmarshal event %x to delete: %w", id[:], err)
idx = append(idx, it.Item().Key()[1+8:1+8+4]...)
item, err := txn.Get(idx)
if err == badger.ErrKeyNotFound {
// this event doesn't exist or is already deleted
return false, nil
} else if err != nil {
return false, fmt.Errorf("failed to fetch event %x to delete: %w", id[:], err)
} else {
if err := item.Value(func(bin []byte) error {
return betterbinary.Unmarshal(bin, &evt)
}); err != nil {
return false, fmt.Errorf("failed to unmarshal event %x to delete: %w", id[:], err)
}
}
}
it.Close()
// if no idx was found, end here, this event doesn't exist
if len(idx) == 1 {
return false, nil
}
// calculate all index keys we have for this event and delete them
for k := range b.getIndexKeysForEvent(evt, idx[1:]) {

View File

@@ -70,11 +70,11 @@ func FuzzQuery(f *testing.F) {
Authors: make([]nostr.PubKey, authors),
Limit: int(limit),
}
var maxKind uint16 = 1
var maxKind nostr.Kind = 1
if kinds > 0 {
filter.Kinds = make([]uint16, kinds)
filter.Kinds = make([]nostr.Kind, kinds)
for i := range filter.Kinds {
filter.Kinds[i] = uint16(kindFactor) * uint16(i)
filter.Kinds[i] = nostr.Kind(kindFactor) * nostr.Kind(i)
}
maxKind = filter.Kinds[len(filter.Kinds)-1]
}
@@ -96,7 +96,7 @@ func FuzzQuery(f *testing.F) {
CreatedAt: nostr.Timestamp(skseed)*nostr.Timestamp(timestampAuthorFactor) + nostr.Timestamp(i),
Content: fmt.Sprintf("unbalanced %d", i),
Tags: nostr.Tags{},
Kind: uint16(i) % maxKind,
Kind: nostr.Kind(i) % maxKind,
}
err := evt.Sign(sk)
require.NoError(t, err)

View File

@@ -19,7 +19,7 @@ func getTagIndexPrefix(tagValue string) ([]byte, int) {
// store value in the new special "a" tag index
k = make([]byte, 1+2+8+len(d)+4+4)
k[0] = indexTagAddrPrefix
binary.BigEndian.PutUint16(k[1:], kind)
binary.BigEndian.PutUint16(k[1:], uint16(kind))
copy(k[1+2:], pkb[0:8])
copy(k[1+2+8:], d)
offset = 1 + 2 + 8 + len(d)
@@ -137,12 +137,12 @@ func (b *BadgerBackend) getIndexKeysForEvent(evt nostr.Event, idx []byte) iter.S
}
}
func getAddrTagElements(tagValue string) (kind uint16, pkb []byte, d string) {
func getAddrTagElements(tagValue string) (kind nostr.Kind, pkb []byte, d string) {
spl := strings.Split(tagValue, ":")
if len(spl) == 3 {
if pkb, _ := hex.DecodeString(spl[1]); len(pkb) == 32 {
if kind, err := strconv.ParseUint(spl[0], 10, 16); err == nil {
return uint16(kind), pkb, spl[2]
return nostr.Kind(kind), pkb, spl[2]
}
}
}

View File

@@ -161,26 +161,26 @@ func (b *BadgerBackend) query(txn *badger.Txn, filter nostr.Filter, limit int) (
return nil, err
}
if err := item.Value(func(val []byte) error {
// fmt.Println(" event", hex.EncodeToString(val[0:4]), "kind", binary.BigEndian.Uint16(val[132:134]), "author", hex.EncodeToString(val[32:36]), "ts", nostr.Timestamp(binary.BigEndian.Uint32(val[128:132])))
if err := item.Value(func(bin []byte) error {
// fmt.Println(" event", betterbinary.GetID(bin ), "kind", betterbinary.GetKind(bin ).Num(), "author", betterbinary.GetPubKey(bin ), "ts", betterbinary.GetCreatedAt(bin ), hex.EncodeToString(it.key), it.valIdx)
// check it against pubkeys without decoding the entire thing
if extraFilter != nil && extraFilter.Authors != nil &&
!nostr.ContainsPubKey(extraFilter.Authors, nostr.PubKey(val[32:64])) {
!nostr.ContainsPubKey(extraFilter.Authors, betterbinary.GetPubKey(bin)) {
// fmt.Println(" skipped (authors)")
return nil
}
// check it against kinds without decoding the entire thing
if extraFilter != nil && extraFilter.Kinds != nil &&
!slices.Contains(extraFilter.Kinds, binary.BigEndian.Uint16(val[132:134])) {
!slices.Contains(extraFilter.Kinds, betterbinary.GetKind(bin)) {
// fmt.Println(" skipped (kinds)")
return nil
}
event := nostr.Event{}
if err := betterbinary.Unmarshal(val, &event); err != nil {
log.Printf("badger: value read error (id %x): %s\n", val[0:32], err)
if err := betterbinary.Unmarshal(bin, &event); err != nil {
log.Printf("badger: value read error (id %x): %s\n", betterbinary.GetID(bin), err)
return err
}

View File

@@ -16,8 +16,8 @@ func (b *BadgerBackend) ReplaceEvent(evt nostr.Event) error {
}
return b.Update(func(txn *badger.Txn) error {
filter := nostr.Filter{Limit: 1, Kinds: []uint16{evt.Kind}, Authors: []nostr.PubKey{evt.PubKey}}
if nostr.IsAddressableKind(evt.Kind) {
filter := nostr.Filter{Limit: 1, Kinds: []nostr.Kind{evt.Kind}, Authors: []nostr.PubKey{evt.PubKey}}
if evt.Kind.IsAddressable() {
// when addressable, add the "d" tag to the filter
filter.Tags = nostr.TagMap{"d": []string{evt.Tags.GetD()}}
}