Nicholas Drake
Nicholas Drake

Reputation: 149

Does Erlang Mnesia select on an ordered_set give a list in Erlang Term order?

In the documentation it isn't clear to me whether I need to iterate through in order with either next or perhaps foldl (it is mentioned that foldr goes in the opposite order to ordered_set so presuambly foldl goes in the same order) or if I can use select and rely upon it being ordered (assuming ordered_set table)

Upvotes: 3

Views: 178

Answers (1)

7stud
7stud

Reputation: 48649

can I use select and rely upon it being ordered (assuming ordered_set table)

ets:select/2:

For tables of type ordered_set, objects are visited in the same order as in a first/next traversal. This means that the match specification is executed against objects with keys in the first/next order and the corresponding result list is in the order of that execution.

ets:first/1:

Returns the first key Key in table Tab. For an ordered_set table, the first key in Erlang term order is returned.

Table Traversal:

Traversals using match and select functions may not need to scan the entire table depending on how the key is specified. A match pattern with a fully bound key (without any match variables) will optimize the operation to a single key lookup without any table traversal at all. For ordered_set a partially bound key will limit the traversal to only scan a subset of the table based on term order.

It would make no sense to me for a table of type ordered_set to return search results in a random order.

Upvotes: 1

Related Questions