types: implement lookup method

This commit is contained in:
c9s 2022-12-20 17:33:40 +08:00
parent bbe58f51cf
commit 130cf2468d
No known key found for this signature in database
GPG Key ID: 7385E7E464CB0A54

View File

@ -16,17 +16,29 @@ func (m OrderMap) Backup() (orderForms []SubmitOrder) {
return orderForms return orderForms
} }
// Add the order the the map
func (m OrderMap) Add(o Order) { func (m OrderMap) Add(o Order) {
m[o.OrderID] = o m[o.OrderID] = o
} }
// Update only updates the order when the order exists in the map // Update only updates the order when the order ID exists in the map
func (m OrderMap) Update(o Order) { func (m OrderMap) Update(o Order) {
if _, ok := m[o.OrderID]; ok { if _, ok := m[o.OrderID]; ok {
m[o.OrderID] = o m[o.OrderID] = o
} }
} }
func (m OrderMap) Lookup(f func(o Order) bool) *Order {
for _, order := range m {
if f(order) {
// copy and return
o := order
return &o
}
}
return nil
}
func (m OrderMap) Remove(orderID uint64) { func (m OrderMap) Remove(orderID uint64) {
delete(m, orderID) delete(m, orderID)
} }
@ -153,6 +165,12 @@ func (m *SyncOrderMap) Exists(orderID uint64) (exists bool) {
return exists return exists
} }
func (m *SyncOrderMap) Lookup(f func(o Order) bool) *Order {
m.Lock()
defer m.Unlock()
return m.orders.Lookup(f)
}
func (m *SyncOrderMap) Len() int { func (m *SyncOrderMap) Len() int {
m.Lock() m.Lock()
defer m.Unlock() defer m.Unlock()