Migrate package imports from next.orly.dev to new orly domain structure; add new varint and binary encoders with comprehensive tests; enhance existing tag and envelope implementations with additional methods, validations, and test coverage; introduce shared test.sh script for streamlined testing across modules.
This commit is contained in:
77
pkg/database/get-serial-by-id.go
Normal file
77
pkg/database/get-serial-by-id.go
Normal file
@@ -0,0 +1,77 @@
|
||||
package database
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
|
||||
"database.orly/indexes/types"
|
||||
"encoders.orly/filter"
|
||||
"encoders.orly/tag"
|
||||
"github.com/dgraph-io/badger/v4"
|
||||
"lol.mleku.dev/chk"
|
||||
"lol.mleku.dev/errorf"
|
||||
)
|
||||
|
||||
func (d *D) GetSerialById(id []byte) (ser *types.Uint40, err error) {
|
||||
var idxs []Range
|
||||
if idxs, err = GetIndexesFromFilter(&filter.F{Ids: tag.NewFromBytesSlice(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
|
||||
// }
|
||||
Reference in New Issue
Block a user