Files
orly/pkg/database/query-for-ids.go
mleku fd866c21b2 refactor(database): optimize serial querying and add utils imports
- pkg/encoders/event/codectester/divider/main.go
  - Added missing import for `orly.dev/pkg/utils`.

- pkg/crypto/encryption/nip44.go
  - Imported `orly.dev/pkg/utils`.

- pkg/crypto/ec/musig2/sign.go
  - Introduced `orly.dev/pkg/utils` import.

- pkg/crypto/keys/keys.go
  - Included `orly.dev/pkg/utils`.

- pkg/database/query-for-serials.go
  - Updated `QueryForSerials` to use `GetFullIdPubkeyBySerials` for batch retrieval.
  - Removed unnecessary `sort` package import.
  - Replaced outdated logic for serial resolution.

- pkg/database/get-fullidpubkey-by-serials.go
  - Added new implementation for `GetFullIdPubkeyBySerials` for efficient batch serial lookups.

- pkg/database/get-serial-by-id.go
  - Added placeholder for alternative serial lookup method.

- pkg/database/database.go
  - Enabled `opts.Compression = options.None` in database configuration.

- pkg/database/save-event.go
  - Replaced loop-based full ID lookup with `GetFullIdPubkeyBySerials` for efficiency.

- pkg/database/get-serials-by-range.go
  - Added missing `sort.Slice` to enforce ascending order for serials.

- pkg/crypto/ec/taproot/taproot.go
  - Imported `orly.dev/pkg/utils`.

- pkg/crypto/ec/musig2/keys.go
  - Added `orly.dev/pkg/utils` import.

- pkg/database/get-fullidpubkey-by-serial.go
  - Removed legacy `GetFullIdPubkeyBySerials` implementation.

- pkg/database/query-for-ids.go
  - Refactored `QueryForIds` to use batched lookups via `GetFullIdPubkeyBySerials`.
  - Consolidated batch result deduplication logic.
  - Simplified code by removing redundant steps and checks.
2025-08-17 17:12:24 +01:00

72 lines
1.9 KiB
Go

package database
import (
"orly.dev/pkg/database/indexes/types"
"orly.dev/pkg/encoders/filter"
"orly.dev/pkg/interfaces/store"
"orly.dev/pkg/utils/chk"
"orly.dev/pkg/utils/context"
"orly.dev/pkg/utils/errorf"
"sort"
)
// QueryForIds retrieves a list of IdPkTs based on the provided filter.
// It supports filtering by ranges and tags but disallows filtering by Ids.
// Results are sorted by timestamp in reverse chronological order.
// Returns an error if the filter contains Ids or if any operation fails.
func (d *D) QueryForIds(c context.T, f *filter.F) (
idPkTs []*store.IdPkTs, err error,
) {
if f.Ids != nil && f.Ids.Len() > 0 {
// if there is Ids in the query, this is an error for this query
err = errorf.E("query for Ids is invalid for a filter with Ids")
return
}
var idxs []Range
if idxs, err = GetIndexesFromFilter(f); chk.E(err) {
return
}
var results []*store.IdPkTs
var founds []*types.Uint40
for _, idx := range idxs {
if f.Tags != nil && f.Tags.Len() > 1 {
if founds, err = d.GetSerialsByRange(idx); chk.E(err) {
return
}
var tmp []*store.IdPkTs
if tmp, err = d.GetFullIdPubkeyBySerials(founds); chk.E(err) {
return
}
results = append(results, tmp...)
} else {
if founds, err = d.GetSerialsByRange(idx); chk.E(err) {
return
}
var tmp []*store.IdPkTs
if tmp, err = d.GetFullIdPubkeyBySerials(founds); chk.E(err) {
return
}
results = append(results, tmp...)
}
}
// deduplicate in case this somehow happened (such as two or more
// from one tag matched, only need it once)
seen := make(map[uint64]struct{})
for _, idpk := range results {
if _, ok := seen[idpk.Ser]; !ok {
seen[idpk.Ser] = struct{}{}
idPkTs = append(idPkTs, idpk)
}
}
// sort results by timestamp in reverse chronological order
sort.Slice(
idPkTs, func(i, j int) bool {
return idPkTs[i].Ts > idPkTs[j].Ts
},
)
if f.Limit != nil && len(idPkTs) > int(*f.Limit) {
idPkTs = idPkTs[:*f.Limit]
}
return
}