Files
orly/pkg/database/get-serial-by-id.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

77 lines
1.8 KiB
Go

package database
import (
"bytes"
"github.com/dgraph-io/badger/v4"
"orly.dev/pkg/database/indexes/types"
"orly.dev/pkg/encoders/filter"
"orly.dev/pkg/encoders/tag"
"orly.dev/pkg/utils/chk"
"orly.dev/pkg/utils/errorf"
)
func (d *D) GetSerialById(id []byte) (ser *types.Uint40, err error) {
var idxs []Range
if idxs, err = GetIndexesFromFilter(&filter.F{Ids: tag.New(id)}); chk.E(err) {
return
}
if len(idxs) == 0 {
err = errorf.E("no indexes found for id %0x", id)
}
if err = d.View(
func(txn *badger.Txn) (err error) {
it := txn.NewIterator(badger.DefaultIteratorOptions)
var key []byte
defer it.Close()
it.Seek(idxs[0].Start)
if it.ValidForPrefix(idxs[0].Start) {
item := it.Item()
key = item.Key()
ser = new(types.Uint40)
buf := bytes.NewBuffer(key[len(key)-5:])
if err = ser.UnmarshalRead(buf); chk.E(err) {
return
}
} else {
// just don't return what we don't have? others may be
// found tho.
}
return
},
); chk.E(err) {
return
}
return
}
//
// func (d *D) GetSerialBytesById(id []byte) (ser []byte, err error) {
// var idxs []Range
// if idxs, err = GetIndexesFromFilter(&filter.F{Ids: tag.New(id)}); chk.E(err) {
// return
// }
// if len(idxs) == 0 {
// err = errorf.E("no indexes found for id %0x", id)
// }
// if err = d.View(
// func(txn *badger.Txn) (err error) {
// it := txn.NewIterator(badger.DefaultIteratorOptions)
// var key []byte
// defer it.Close()
// it.Seek(idxs[0].Start)
// if it.ValidForPrefix(idxs[0].Start) {
// item := it.Item()
// key = item.Key()
// ser = key[len(key)-5:]
// } else {
// // just don't return what we don't have? others may be
// // found tho.
// }
// return
// },
// ); chk.E(err) {
// return
// }
// return
// }