Which data structure is suitable to store multiple keys with one Object and Complexity is O(1)

huangapple go评论73阅读模式
英文:

Which data structure is suitable to store multiple keys with one Object and Complexity is O(1)

问题

在开发交易核心系统时,给定一个订单对象如下:

type Order struct {
   name       string    // 符号名称
   id         int
   price      float64
   tp_price   float64
   sl_price   float64
   order_type int       // 0 - 市价订单,1 - 限价订单,2 - 止损订单,3 - 止损限价订单
   expiration time.Time // 过期时间
}

我们有三个触发器需要找到指定的订单并进行修改:

  1. 基于订单ID的客户端修改
  2. 定时器检查过期订单,需要基于订单ID找到订单
  3. 基于符号名称的Tick数据处理逻辑,需要找到订单

为了方便处理Tick数据,我使用了以下数据结构:
map(以符号名称为键,数据为有序数组订单数组
实际数据如下:

map['AUDCAD'] = array(order1, order2, order3, ...)
map['CADUSD'] = array(order7, order8, order9, ...)

这样,当Tick数据(卖出价/买入价)到来时,我们可以在map中轻松找到所有基于符号名称的订单。

但是,如果客户端发来一个修改订单的请求,我必须遍历整个map来找到指定的订单,这是不好的。

而且我们还需要以多线程模式处理订单。

所以我的问题是:
哪种数据结构适合我的情况:
以多个键(订单ID或符号名称)查找订单,并且复杂度为O(1)

非常感谢任何想法。

英文:

When developing a trading core system, given having an order object as :

class Order{
   name string, // symbol name
   id   int,
   price float64,
   tp_price float64,
   sl_price float64,
   type  int, // 0 - market order, 1 - limit order, 2 - stop order, 3 - stop limit order 
   expiration datetime  
}

we have three triggers that need to find specified order and modify it:

  1. User Modification from client based on id
  2. Timer checking expiration need to find order based on id
  3. Tick data processing logic need to find order based on name

So for Tick data processing easily, I use the following data structure:
map(key with symbol name, data is ordered array order array)
real data like :

map['AUDCAD'] = array(order1, order2,order3, ...)
map['CADUSD'] = array(order7, order8,order9, ...)

then when tick data(ask/bid) comes, we can find all orders based on the symbol name easily in the map.

but if an order modifying request comes from the client, I must to loop all the map to find the specified order, it is bad.

and we need to process orders in multi-threading mode as well.

So my question is :
which data structure is suitable for my case :
to find an order with multiple keys (order id or symbol name) and complexity is O(1).

Any ideas are very appreciated.

答案1

得分: 0

你可以使用一个包含两个映射的结构体:

type orderIndex struct {
   sync.RWMutex
   orderByID map[int]*Order
   orderByName map[string][]*Order
}

在插入时,使用Lock()锁定订单索引,并将订单添加到两个映射中。

在查找时,使用RLock()锁定索引,并在其中一个映射中查找订单。

如果你需要保护单个订单以进行并发访问,可以在订单结构体中添加一个Mutex,或者如果需要批量更新订单,可以使用orderIndexLock()方法。

英文:

You can use a structure with two maps:

type orderIndex struct {
   sync.RWMutex
   orderByID map[int]*Order
   orderByName map[string][]*Order
}

When inserting, Lock() order index, and add order to both maps.

When looking up, RLock the index, and lookup the order in one of the maps.

If you need to protect individual order for concurrent access, you may add a Mutex to order struct, or use the Lock() for the orderIndex if you need to bulk-update orders.

huangapple
  • 本文由 发表于 2022年8月17日 06:04:52
  • 转载请务必保留本文链接:https://go.coder-hub.com/73380862.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定