lmdb: implement simple query by id part.

This commit is contained in:
fiatjaf
2025-08-04 17:39:59 -03:00
parent 1cd48343d6
commit a4ebd32477
2 changed files with 42 additions and 18 deletions

View File

@@ -14,13 +14,19 @@ import (
func (b *LMDBBackend) QueryEvents(filter nostr.Filter, maxLimit int) iter.Seq[nostr.Event] { func (b *LMDBBackend) QueryEvents(filter nostr.Filter, maxLimit int) iter.Seq[nostr.Event] {
return func(yield func(nostr.Event) bool) { return func(yield func(nostr.Event) bool) {
if filter.Search != "" { if filter.IDs != nil {
return // when there are ids we ignore everything else and just fetch the ids
if err := b.lmdbEnv.View(func(txn *lmdb.Txn) error {
txn.RawRead = true
return b.queryByIds(txn, filter.IDs, yield)
}); err != nil {
log.Printf("lmdb: unexpected id query error: %s\n", err)
}
} }
if filter.IDs != nil { // ignore search queries
// do a special id query if filter.Search != "" {
// TODO return
} }
// max number of events we'll return // max number of events we'll return
@@ -33,11 +39,40 @@ func (b *LMDBBackend) QueryEvents(filter nostr.Filter, maxLimit int) iter.Seq[no
maxLimit = filter.Limit maxLimit = filter.Limit
} }
b.lmdbEnv.View(func(txn *lmdb.Txn) error { // do a normal query based on various filters
if err := b.lmdbEnv.View(func(txn *lmdb.Txn) error {
txn.RawRead = true txn.RawRead = true
return b.query(txn, filter, maxLimit, yield) return b.query(txn, filter, maxLimit, yield)
}) }); err != nil {
log.Printf("lmdb: unexpected query error: %s\n", err)
} }
}
}
func (b *LMDBBackend) queryByIds(txn *lmdb.Txn, ids []nostr.ID, yield func(nostr.Event) bool) error {
for _, id := range ids {
idx, err := txn.Get(b.indexId, id[0:8])
if err != nil {
continue
}
txn.Get(b.rawEventStore, idx)
bin, err := txn.Get(b.rawEventStore, idx)
if err != nil {
continue
}
event := nostr.Event{}
if err := betterbinary.Unmarshal(bin, &event); err != nil {
continue
}
if !yield(event) {
return nil
}
}
return nil
} }
func (b *LMDBBackend) query(txn *lmdb.Txn, filter nostr.Filter, limit int, yield func(nostr.Event) bool) error { func (b *LMDBBackend) query(txn *lmdb.Txn, filter nostr.Filter, limit int, yield func(nostr.Event) bool) error {

View File

@@ -47,17 +47,6 @@ func (b *LMDBBackend) prepareQueries(filter nostr.Filter) (
} }
}() }()
// if filter.IDs != nil {
// // when there are ids we ignore everything else
// queries = make([]query, len(filter.IDs))
// for i, id := range filter.IDs {
// prefix := make([]byte, 8)
// copy(prefix[0:8], id[0:8])
// queries[i] = query{i: i, dbi: b.indexId, prefix: prefix[0:8], keySize: 8}
// }
// return queries, nil, nil, "", nil, 0, nil
// }
// this is where we'll end the iteration // this is where we'll end the iteration
if filter.Since != 0 { if filter.Since != 0 {
if fs := uint32(filter.Since); fs > since { if fs := uint32(filter.Since); fs > since {