fix memdb encode/decode EOF issue

master
michael 2022-08-29 01:03:09 +00:00
parent 2ef71da720
commit cdd1cb8cc0
1 changed files with 4 additions and 16 deletions

View File

@ -31,9 +31,6 @@ func (b *bidDB) Mount() {
// Add a bid to the bid list under specified auction key.
func (b *bidDB) AddBid(auctionId string, bid *types.Bid) error {
buf := bytes.NewBuffer([]byte{})
enc := gob.NewEncoder(buf)
dec := gob.NewDecoder(buf)
k := []byte(auctionId)
err := b.db.Update(func(txn *badger.Txn) error {
@ -48,10 +45,7 @@ func (b *bidDB) AddBid(auctionId string, bid *types.Bid) error {
}
// key found -> decode contents to bids
bidsOld.Value(func(val []byte) error {
buf.Reset()
if _, err := buf.Read(val); err != nil {
return err
}
dec := gob.NewDecoder(bytes.NewReader(val))
if err := dec.Decode(&bids); err != nil {
return err
}
@ -62,7 +56,8 @@ func (b *bidDB) AddBid(auctionId string, bid *types.Bid) error {
// append bid
bids = append(bids, bid)
// encode new list
buf.Reset()
buf := bytes.NewBuffer(nil)
enc := gob.NewEncoder(buf)
if err := enc.Encode(&bids); err != nil {
return err
}
@ -80,8 +75,6 @@ func (b *bidDB) AddBid(auctionId string, bid *types.Bid) error {
// Get the highest bid in the list under specified auction key.
func (b *bidDB) GetHighestBid(auctionId string) (*types.Bid, error) {
k := []byte(auctionId)
buf := bytes.NewBuffer([]byte{})
dec := gob.NewDecoder(buf)
var bid *types.Bid
@ -92,13 +85,8 @@ func (b *bidDB) GetHighestBid(auctionId string) (*types.Bid, error) {
}
err = bidData.Value(func(val []byte) error {
buf.Reset()
if _, err := buf.Read(val); err != nil {
return err
}
// panic(len(val))
var bids []*types.Bid
dec := gob.NewDecoder(bytes.NewReader(val))
if err := dec.Decode(&bids); err != nil {
return err
}