[erlang-questions] ETS ordering

lud <>
Fri Feb 28 17:55:59 CET 2014


I need some help setting up ETS tables for an application :

I would like to store users' sell-orders and buy-orders in an ETS Table. 
I would like them to be ordered.

(tl;dr please see last paragraph.)

Tuples must contain the following values :
  - Whose order it is
  - 'buy' or 'sell'
  - The quantity
  - The product id
  - The price

So, as an example :

(prices are per unit)

If some user wants to sell 20 apples a minimum price of 20.00, the three 
orders match, but I would like to iterate from highest price to lower.

I want the user to get the maximum profit, so the records must come in 
this ordering :

I will sell 20 apples : 6 to jack, 8 to joe and 6 to william.

When I want to buy, I would like the sell orders to come in the opposite 
ordering : cheapest first !

So i've thought of having two tables, buy-orders on one side, 
sell-orders on the other. I could use ordered_set table type and 
ets:select/3 to fold over the matchspec results in order. For buy 
orders, I can store negative prices so higher prices come first.

My main question is on the key : do you think it's okay/good to have 
tuples with compound keys such as the following in buy_orders table ?
	{{apple, -36.00, 1}, 6,  jack}
	{{apple, -35.00, 2}, 8,  joe}
	{{apple, -23.00, 3}, 20, william}
I must have OrderID (which is a dummy value) because there is no 
ordered_bag table type and users may sell/buy same items at the same 
price, and one user could sell/buy the same item at two different prices 
(e.g. testing the market).

Thank you

More information about the erlang-questions mailing list