Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Stop erlang qlc query at first matching entry

Tags:

erlang

mnesia

How do I write a qlc query that traverses table until it finds the first matching entry? For instance, this query returns all entries in the table that match criteria:

qlc:q([E#stuff.data || E <- mnesia:table(stuff), E#stuff.type == 123]).

How to modify this or the qlc:e call to stop and return only the first matching entry? The motivation for this is performance - I'm not interested in all entries, just want to take a look at how entries of specific type look like.

like image 850
Sergey Evstifeev Avatar asked Dec 03 '25 04:12

Sergey Evstifeev


1 Answers

You need to use a qlc cursor and qlc:next_answers/2, take a look at the example in qlc:cursor/1. It should look something like

QH=qlc:q([E#stuff.data || E <- mnesia:table(stuff), E#stuff.type == 123]).
QC = qlc:cursor(QH).
Result=qlc:next_answers(QC, 1). % Only return 1 answer
qlc:delete_cursor(QC).
like image 163
johlo Avatar answered Dec 05 '25 03:12

johlo



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!