Mnesia query: less or equal than given key

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

Mnesia query: less or equal than given key

Erik Reitsma (RY/ETM)-2
Hi all,

Using mnesia, is there an efficient way to find the smallest entry in a table with a key equal to or less than a given key?

So suppose that I have keys 10, 23, 33, 34, 56, 100 and I look for 26, I want to get 23 back. If I look for 23, I should get 23.

I could select all entries smaller than or equal to my key, and then take the largest of those, but it seems like I would get an intermediate list that may consist of the entire table (i.e. if my key is larger than the largest key in the table).

I could make my own binary search (the keys are integers anyway), but I wondered if there would be a more elegant way.

Regards,
*Erik.



Reply | Threaded
Open this post in threaded view
|

Mnesia query: less or equal than given key

Sean Hinde-2
Hi,

You could try first looking up the exact key, and if not found then  
do a mnesia:dirty_prev/2 to get the next smallest. You would need to  
be careful of transaction safety..

Sean

On 19 Sep 2005, at 15:06, Erik Reitsma (RY/ETM) wrote:

> Hi all,
>
> Using mnesia, is there an efficient way to find the smallest entry  
> in a table with a key equal to or less than a given key?
>
> So suppose that I have keys 10, 23, 33, 34, 56, 100 and I look for  
> 26, I want to get 23 back. If I look for 23, I should get 23.
>
> I could select all entries smaller than or equal to my key, and  
> then take the largest of those, but it seems like I would get an  
> intermediate list that may consist of the entire table (i.e. if my  
> key is larger than the largest key in the table).
>
> I could make my own binary search (the keys are integers anyway),  
> but I wondered if there would be a more elegant way.
>
> Regards,
> *Erik.
>
>



Reply | Threaded
Open this post in threaded view
|

Mnesia query: less or equal than given key

chandru
In reply to this post by Erik Reitsma (RY/ETM)-2
Hi,

>From the man page for mnesia

       dirty_prev(Tab, Key) -> Key | exit({aborted, Reason})

              This  function works exactly mnesia:dirty_next/2 but returns the
              previous object in Erlang term order for the  ordered_set  table
              type.  For  all  other table types, mnesia:dirty_next/2 and mne-
              sia:dirty_prev/2 are synonyms.

cheers
Chandru

On 19/09/05, Erik Reitsma (RY/ETM) <erik.reitsma> wrote:

> Hi all,
>
> Using mnesia, is there an efficient way to find the smallest entry in a table with a key equal to or less than a given key?
>
> So suppose that I have keys 10, 23, 33, 34, 56, 100 and I look for 26, I want to get 23 back. If I look for 23, I should get 23.
>
> I could select all entries smaller than or equal to my key, and then take the largest of those, but it seems like I would get an intermediate list that may consist of the entire table (i.e. if my key is larger than the largest key in the table).
>
> I could make my own binary search (the keys are integers anyway), but I wondered if there would be a more elegant way.
>
> Regards,
> *Erik.
>
>