Skip to content

Commit

Permalink
use update time instead of decoding order
Browse files Browse the repository at this point in the history
  • Loading branch information
buck54321 committed Oct 17, 2024
1 parent b77d86a commit 2078315
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 17 deletions.
15 changes: 10 additions & 5 deletions client/db/bolt/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -470,7 +470,7 @@ func (db *BoltDB) pruneArchivedOrders() error {

type orderStamp struct {
oid []byte
stamp int64
stamp uint64
}
deletes := make([]*orderStamp, 0, toClear)
sortDeletes := func() {
Expand All @@ -484,11 +484,16 @@ func (db *BoltDB) pruneArchivedOrders() error {
if _, found := oidsWithActiveMatches[oid]; found {
return nil
}
ord, err := decodeOrderBucket(oidB, archivedOB.Bucket(oidB))
if err != nil {
return fmt.Errorf("error decoding order %x: %v", oid, err)
oBkt := archivedOB.Bucket(oidB)
if oBkt == nil {
return fmt.Errorf("no order bucket iterated order %x", oidB)
}
stampB := oBkt.Get(updateTimeKey)
if stampB == nil {
// Highly improbable.
stampB = make([]byte, 8)
}
stamp := ord.Order.Prefix().ClientTime.Unix()
stamp := intCoder.Uint64(stampB)
if len(deletes) < toClear {
deletes = append(deletes, &orderStamp{
stamp: stamp,
Expand Down
23 changes: 11 additions & 12 deletions client/db/bolt/db_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1641,13 +1641,11 @@ func TestPruneArchivedOrders(t *testing.T) {
return n
}

var ordStampI int64
addOrder := func(optStamp int64) order.OrderID {
var ordStampI uint64
addOrder := func(stamp uint64) order.OrderID {
ord, _ := ordertest.RandomLimitOrder()
if optStamp != 0 {
ord.P.ClientTime = time.Unix(optStamp, 0)
} else {
ord.P.ClientTime = time.Unix(ordStampI, 0)
if stamp == 0 {
stamp = ordStampI
ordStampI++
}
boltdb.UpdateOrder(&db.MetaOrder{
Expand All @@ -1658,7 +1656,12 @@ func TestPruneArchivedOrders(t *testing.T) {
},
Order: ord,
})
return ord.ID()
oid := ord.ID()
boltdb.ordersUpdate(func(ob, archivedOB *bbolt.Bucket) error {
archivedOB.Bucket(oid[:]).Put(updateTimeKey, uint64Bytes(stamp))
return nil
})
return oid
}
for i := 0; i < archiveSizeLimit*2; i++ {
addOrder(0)
Expand All @@ -1680,11 +1683,7 @@ func TestPruneArchivedOrders(t *testing.T) {
if err := boltdb.View(func(tx *bbolt.Tx) error {
bkt := tx.Bucket(archivedOrdersBucket)
return bkt.ForEach(func(oidB, _ []byte) error {
ord, err := decodeOrderBucket(oidB, bkt.Bucket(oidB))
if err != nil {
return fmt.Errorf("error decoding order %x: %v", oidB, err)
}
if stamp := ord.Order.Prefix().ClientTime.Unix(); stamp < int64(archiveSizeLimit) {
if stamp := intCoder.Uint64(bkt.Bucket(oidB).Get(updateTimeKey)); stamp < archiveSizeLimit {
return fmt.Errorf("order stamp %d should have been pruned", stamp)
}
return nil
Expand Down

0 comments on commit 2078315

Please sign in to comment.