[erlang-questions] Mnesia sorting/limiting

Ola Andersson A ola.a.andersson@REDACTED
Wed Jun 10 10:30:47 CEST 2009


I was surprised to discover that there is no priority queue
implementation in OTP.
This was a few years back when I needed one. I don't know if that has
changed lately.
It seems like an obvious extension to the queue module.
I couldn't find anything using google either so I had to resort to
rolling my own using gb_trees.
That worked ok for my purposes but it could probably be made more
efficient and generalized.
Any volounteers?
/OLA. 

> -----Original Message-----
> From: erlang-questions@REDACTED 
> [mailto:erlang-questions@REDACTED] On Behalf Of Barry Mitchelson
> Sent: den 9 juni 2009 22:17
> To: erlang-questions@REDACTED
> Subject: [erlang-questions] Mnesia sorting/limiting
> 
> Hi,
> 
> I'm relatively new to Erlang, and as a mini project to work 
> on as I learn, I'm writing a simple message queue. I'm having 
> some problems retreiving data from the mnesia table I am 
> using to store my messages.
> 
> My job records are defined as :
> 
> -record(job, { id, body, due = 0, priority = 1 }).
> 
> and after much playing around, the code to retreive the job 
> with the highest priority I have is
> 
> Q1 = qlc:q([ X || X <- mnesia:table(job)]),
> Q2 = qlc:sort(Q1, {order, fun(Job1, Job2) -> 
> Job1#job.priority > Job2#job.priority end}), C = 
> qlc:cursor(Q2), R = qlc:next_answers(C,1),
> 
> I know this is not the most efficient way, but I'm struggling 
> to find a fast way to do this, and this is without also 
> sorting by the due date which I also want to be able to do. 
> In SQL, the query would be something like
> 
> SELECT * FROM jobs ORDER BY priority, due_at LIMIT 1
> 
> which with the right indexing would return a result very 
> quickly even with a large amount of rows. The code above 
> takes a few seconds with 100,000 records in the table.
> 
> I feel like I'm missing something somewhere, but am running 
> out of ideas.
> 
> Thanks,
> 
> Barry
> 
> ________________________________________________________________
> erlang-questions mailing list. See 
> http://www.erlang.org/faq.html erlang-questions (at) erlang.org
> 
> 


More information about the erlang-questions mailing list