query-for-serials.go raw
1 //go:build !(js && wasm)
2
3 package database
4
5 import (
6 "context"
7
8 "next.orly.dev/pkg/lol/chk"
9 "next.orly.dev/pkg/database/indexes/types"
10 "next.orly.dev/pkg/nostr/encoders/filter"
11 "next.orly.dev/pkg/interfaces/store"
12 )
13
14 // QueryForSerials takes a filter and returns the serials of events that match,
15 // sorted in reverse chronological order.
16 func (d *D) QueryForSerials(c context.Context, f *filter.F) (
17 sers types.Uint40s, err error,
18 ) {
19 var founds []*types.Uint40
20 var idPkTs []*store.IdPkTs
21 if f.Ids != nil && f.Ids.Len() > 0 {
22 // Use batch lookup to minimize transactions when resolving IDs to serials
23 var serialMap map[string]*types.Uint40
24 if serialMap, err = d.GetSerialsByIds(f.Ids); chk.E(err) {
25 return
26 }
27 for _, ser := range serialMap {
28 founds = append(founds, ser)
29 }
30 var tmp []*store.IdPkTs
31 if tmp, err = d.GetFullIdPubkeyBySerials(founds); chk.E(err) {
32 return
33 }
34 idPkTs = append(idPkTs, tmp...)
35 } else {
36 if idPkTs, err = d.QueryForIds(c, f); chk.E(err) {
37 return
38 }
39 }
40 // extract the serials
41 for _, idpk := range idPkTs {
42 ser := new(types.Uint40)
43 if err = ser.Set(idpk.Ser); chk.E(err) {
44 continue
45 }
46 sers = append(sers, ser)
47 }
48 return
49 }
50