eventstore: skip replacing when it's the exact same id.

This commit is contained in:
fiatjaf
2025-11-30 22:11:49 -03:00
parent a355f27adb
commit fb3b14c69c
3 changed files with 27 additions and 0 deletions

View File

@@ -11,6 +11,14 @@ import (
func (b *BoltBackend) ReplaceEvent(evt nostr.Event) error { func (b *BoltBackend) ReplaceEvent(evt nostr.Event) error {
return b.DB.Update(func(txn *bbolt.Tx) error { return b.DB.Update(func(txn *bbolt.Tx) error {
rawBucket := txn.Bucket(rawEventStore)
// check if we already have this id
bin := rawBucket.Get(evt.ID[16:24])
if bin != nil {
return nil
}
filter := nostr.Filter{Kinds: []nostr.Kind{evt.Kind}, Authors: []nostr.PubKey{evt.PubKey}} filter := nostr.Filter{Kinds: []nostr.Kind{evt.Kind}, Authors: []nostr.PubKey{evt.PubKey}}
if evt.Kind.IsAddressable() { if evt.Kind.IsAddressable() {
// when addressable, add the "d" tag to the filter // when addressable, add the "d" tag to the filter

View File

@@ -11,6 +11,15 @@ import (
func (b *LMDBBackend) ReplaceEvent(evt nostr.Event) error { func (b *LMDBBackend) ReplaceEvent(evt nostr.Event) error {
return b.lmdbEnv.Update(func(txn *lmdb.Txn) error { return b.lmdbEnv.Update(func(txn *lmdb.Txn) error {
// check if we already have this id
_, existsErr := txn.Get(b.indexId, evt.ID[0:8])
if existsErr == nil {
return nil
}
if operr, ok := existsErr.(*lmdb.OpError); !ok || operr.Errno != lmdb.NotFound {
return existsErr
}
filter := nostr.Filter{Kinds: []nostr.Kind{evt.Kind}, Authors: []nostr.PubKey{evt.PubKey}} filter := nostr.Filter{Kinds: []nostr.Kind{evt.Kind}, Authors: []nostr.PubKey{evt.PubKey}}
if evt.Kind.IsAddressable() { if evt.Kind.IsAddressable() {
// when addressable, add the "d" tag to the filter // when addressable, add the "d" tag to the filter

View File

@@ -7,6 +7,7 @@ import (
"fiatjaf.com/nostr" "fiatjaf.com/nostr"
"fiatjaf.com/nostr/eventstore/internal" "fiatjaf.com/nostr/eventstore/internal"
"github.com/PowerDNS/lmdb-go/lmdb"
) )
func (il *IndexingLayer) ReplaceEvent(evt nostr.Event) error { func (il *IndexingLayer) ReplaceEvent(evt nostr.Event) error {
@@ -47,6 +48,15 @@ func (il *IndexingLayer) ReplaceEvent(evt nostr.Event) error {
}() }()
iltxn.RawRead = true iltxn.RawRead = true
// check if we already have this id
_, existsErr := mmmtxn.Get(il.mmmm.indexId, evt.ID[0:8])
if existsErr == nil {
return nil
}
if !lmdb.IsNotFound(existsErr) {
return fmt.Errorf("error checking existence: %w", existsErr)
}
// now we fetch the past events, whatever they are, delete them and then save the new // now we fetch the past events, whatever they are, delete them and then save the new
var results iter.Seq[nostr.Event] = func(yield func(nostr.Event) bool) { var results iter.Seq[nostr.Event] = func(yield func(nostr.Event) bool) {
err = il.query(iltxn, filter, 10 /* in theory limit could be just 1 and this should work */, yield) err = il.query(iltxn, filter, 10 /* in theory limit could be just 1 and this should work */, yield)