fix bolt, breaking it in the process.

This commit is contained in:
fiatjaf
2024-07-23 14:35:29 -03:00
parent edf5240977
commit 62e5430a85
3 changed files with 21 additions and 11 deletions

View File

@@ -6,15 +6,23 @@ import (
"github.com/nbd-wtf/go-nostr"
bolt "go.etcd.io/bbolt"
"golang.org/x/exp/slices"
)
func (b *BoltBackend) DeleteEvent(ctx context.Context, evt *nostr.Event) error {
return b.db.Update(func(txn *bolt.Tx) error {
idPrefix8, _ := hex.DecodeString(evt.ID[0 : 8*2])
bucket := txn.Bucket(bucketId)
// check if we already do not have this
bucket := txn.Bucket(bucketId)
seqb := bucket.Get(idPrefix8)
c := bucket.Cursor()
key, _ := c.Seek(idPrefix8)
if key == nil || !slices.Equal(key[0:8], idPrefix8) {
// already do not have it
return nil
}
seqb := key[8:]
if seqb == nil {
return nil
}

View File

@@ -62,24 +62,26 @@ func (b *BoltBackend) QueryEvents(ctx context.Context, filter nostr.Filter) (cha
c := bucket.Cursor()
k, _ := c.Seek(q.startingPoint)
if k == nil {
k, _ = c.Last()
key, _ := c.Seek(q.startingPoint)
if key == nil {
key, _ = c.Last()
} else {
k, _ = c.Prev()
key, _ = c.Prev()
}
for ; k != nil && bytes.HasPrefix(k, q.prefix); k, _ = c.Prev() {
for ; key != nil && bytes.HasPrefix(key, q.prefix); key, _ = c.Prev() {
idxOffset := len(key) - 4 // this is where the idx actually starts
// "id" indexes don't contain a timestamp
if !q.skipTimestamp {
createdAt := binary.BigEndian.Uint32(k[len(k)-4:])
createdAt := binary.BigEndian.Uint32(key[idxOffset-4 : idxOffset])
if createdAt < since {
return nil
break
}
}
// fetch actual event
val := raw.Get(k[len(k)-8:])
val := raw.Get(key[len(key)-4:])
evt := &nostr.Event{}
if err := nostr_binary.Unmarshal(val, evt); err != nil {
log.Printf("bolt: value read error (id %x): %s\n", val[0:32], err)

View File

@@ -37,7 +37,7 @@ func (b *BoltBackend) SaveEvent(ctx context.Context, evt *nostr.Event) error {
// raw event store
raw := txn.Bucket(bucketRaw)
seq, _ := raw.NextSequence()
seqb := binary.BigEndian.AppendUint64(nil, seq)
seqb := binary.BigEndian.AppendUint32(nil, uint32(seq))
if err := raw.Put(seqb, bin); err != nil {
return err
}