mmm: querying by ids is restricted to the layer it was initiated.

This commit is contained in:
fiatjaf
2025-09-06 21:25:35 -03:00
parent fd53d7309f
commit 4754518278

View File

@@ -20,7 +20,7 @@ func (b *MultiMmapManager) GetByID(id nostr.ID) (*nostr.Event, IndexingLayers) {
layers := b.queryByIDs([]nostr.ID{id}, func(evt nostr.Event) bool { layers := b.queryByIDs([]nostr.ID{id}, func(evt nostr.Event) bool {
event = &evt event = &evt
return false return false
}, true) }, nil, true)
if event != nil { if event != nil {
present := make([]*IndexingLayer, len(layers)) present := make([]*IndexingLayer, len(layers))
@@ -37,6 +37,7 @@ func (b *MultiMmapManager) GetByID(id nostr.ID) (*nostr.Event, IndexingLayers) {
func (b *MultiMmapManager) queryByIDs( func (b *MultiMmapManager) queryByIDs(
ids []nostr.ID, ids []nostr.ID,
yield func(nostr.Event) bool, yield func(nostr.Event) bool,
restrictToLayer *uint16, // pass -1 if not restricted
withLayers bool, withLayers bool,
) (layers []uint16) { ) (layers []uint16) {
b.lmdbEnv.View(func(txn *lmdb.Txn) error { b.lmdbEnv.View(func(txn *lmdb.Txn) error {
@@ -51,16 +52,27 @@ func (b *MultiMmapManager) queryByIDs(
panic(fmt.Errorf("failed to decode event from %v: %w", pos, err)) panic(fmt.Errorf("failed to decode event from %v: %w", pos, err))
} }
keepGoing := yield(evt) restrictionSatisfied := restrictToLayer == nil
if withLayers { if withLayers {
layers = make([]uint16, 0, (len(val)-12)/2) layers = make([]uint16, 0, (len(val)-12)/2)
}
if withLayers || !restrictionSatisfied {
for s := 12; s < len(val); s += 2 { for s := 12; s < len(val); s += 2 {
layers = append(layers, binary.BigEndian.Uint16(val[s:s+2])) layer := binary.BigEndian.Uint16(val[s : s+2])
if withLayers {
layers = append(layers, layer)
}
if !restrictionSatisfied && layer == *restrictToLayer {
restrictionSatisfied = true
}
} }
} }
if !keepGoing { if !restrictionSatisfied {
continue
}
if !yield(evt) {
return nil return nil
} }
} }
@@ -75,7 +87,7 @@ func (b *MultiMmapManager) queryByIDs(
func (il *IndexingLayer) QueryEvents(filter nostr.Filter, maxLimit int) iter.Seq[nostr.Event] { func (il *IndexingLayer) 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.IDs != nil { if filter.IDs != nil {
il.mmmm.queryByIDs(filter.IDs, yield, false) il.mmmm.queryByIDs(filter.IDs, yield, &il.id, false)
return return
} }