gpu-compute-chain/x/colinear-core/memdb/biddb.go

177 lines
3.6 KiB
Go
Raw Normal View History

2022-08-28 17:42:43 -07:00
package memdb
import (
"bytes"
"colinear/x/colinear-core/types"
2022-08-28 17:42:43 -07:00
"encoding/gob"
"errors"
2022-08-28 17:42:43 -07:00
"log"
"math/big"
badger "github.com/dgraph-io/badger/v3"
2022-08-28 17:42:43 -07:00
)
type bidDB struct {
db *badger.DB
}
var BidDB bidDB
// Mount Db & initialize encoder/decoder
func (b *bidDB) Mount() {
db, err := badger.Open(badger.DefaultOptions("").WithInMemory(true))
if err != nil {
// must force crash here, since db is absolutely required
log.Fatalf("Failed to mount in-memory db: %s", err)
}
b.db = db
// initialize encode/decode stuff
gob.Register([]types.Bid{})
}
// Add a bid to the bid list under specified auction key.
func (b *bidDB) AddBid(auctionId string, bid *types.Bid) error {
k := []byte(auctionId)
err := b.db.Update(func(txn *badger.Txn) error {
var bids []*types.Bid
bidsOld, err := txn.Get(k)
if errors.Is(err, badger.ErrKeyNotFound) {
2022-08-28 17:42:43 -07:00
// key not found -> just create a new Bid array
bids = []*types.Bid{}
} else {
if err != nil {
return err
}
// key found -> decode contents to bids
bidsOld.Value(func(val []byte) error {
2022-08-28 18:03:09 -07:00
dec := gob.NewDecoder(bytes.NewReader(val))
2022-08-28 17:42:43 -07:00
if err := dec.Decode(&bids); err != nil {
return err
}
return nil
})
}
// append bid
bids = append(bids, bid)
// encode new list
2022-08-28 18:03:09 -07:00
buf := bytes.NewBuffer(nil)
enc := gob.NewEncoder(buf)
2022-08-28 17:42:43 -07:00
if err := enc.Encode(&bids); err != nil {
return err
}
// set under auction key in db
if err := txn.Set(k, buf.Bytes()); err != nil {
return err
}
return nil
})
return err
}
// Get the highest bid in the list under specified auction key.
func (b *bidDB) GetLowestBid(auctionId string) (*types.Bid, error) {
2022-08-28 17:42:43 -07:00
k := []byte(auctionId)
var bid *types.Bid
err := b.db.View(func(txn *badger.Txn) error {
bidData, err := txn.Get(k)
if err != nil {
if !errors.Is(err, badger.ErrKeyNotFound) {
return err
} else {
return nil
}
2022-08-28 17:42:43 -07:00
}
err = bidData.Value(func(val []byte) error {
var bids []*types.Bid
2022-08-28 18:03:09 -07:00
dec := gob.NewDecoder(bytes.NewReader(val))
2022-08-28 17:42:43 -07:00
if err := dec.Decode(&bids); err != nil {
return err
}
if bids == nil {
return nil
}
minAmt := big.NewInt(0)
refAmt := big.NewInt(0)
for _, currBid := range bids {
2022-08-28 17:42:43 -07:00
if bid == nil {
bid = currBid
minAmt.SetString(currBid.Amount, 10)
2022-08-28 17:42:43 -07:00
} else {
// set ref amt.
refAmt.SetString(currBid.Amount, 10)
// if ref amt is less, then we have a new min
if refAmt.Cmp(minAmt) == -1 {
bid = currBid
2022-08-28 17:42:43 -07:00
}
}
}
return nil
})
return err
})
return bid, err
}
2022-08-29 16:23:07 -07:00
2022-08-30 14:43:26 -07:00
func (b *bidDB) GetBids(auctionId string) ([]*types.Bid, error) {
k := []byte(auctionId)
var bids []*types.Bid
err := b.db.View(func(txn *badger.Txn) error {
res, err := txn.Get(k)
if err != nil {
return err
2022-08-30 14:43:26 -07:00
} else {
err := res.Value(func(val []byte) error {
dec := gob.NewDecoder(bytes.NewReader(val))
err := dec.Decode(&bids)
return err
})
return err
}
})
return bids, err
}
2022-08-29 16:23:07 -07:00
func (b *bidDB) ClearAuction(auctionId string) error {
k := []byte(auctionId)
err := b.db.Update(func(txn *badger.Txn) error {
return txn.Delete(k)
})
return err
}
2022-08-31 11:43:45 -07:00
// Iterate over all auction keys in memory. VIEW-ONLY.
func (b *bidDB) ForEachAuction(viewFunc func(string) error) error {
err := b.db.View(func(txn *badger.Txn) error {
opts := badger.DefaultIteratorOptions
// can customize options down here if we want
iter := txn.NewIterator(opts)
defer iter.Close()
for iter.Rewind(); iter.Valid(); iter.Next() {
item := iter.Item()
key := string(item.Key())
err := viewFunc(key)
if err != nil {
return err
}
}
return nil
})
return err
}