Efficient way to select a range of data between 2 values?

Alexander Uvarov alexander.uvarov@REDACTED
Mon Aug 31 18:25:50 CEST 2009


I have ~100K ip2country records like {ip_address_map, {range_from,  
range_to, country_code}} and I am searching for fastest way to lookup  
country_code. There should be matching guard like range_from =< IP >=  
range_to. Any ideas, guys? I am not satisfied with ets (lookup time  
from 0.06s up to 1.6s).

This is my current code (original http://code.google.com/p/ip2country-erlang/source/browse/trunk/ip2country.erl) 
:

-record(ip_address_map, {range_from, range_to, country_code}).

start() ->
     ets:new(ip, [named_table, public, {keypos, 2}]).

lookup(IP) ->
   	MatchHead = #ip_address_map{range_from='$1', range_to='$2',  
country_code='$3'},
   	Guard = [{'=<', '$1', IP}, {'>=', '$2', IP}],
   	Result = '$3',
	ets:select(ip, [{MatchHead, Guard, [Result]}], 1).



More information about the erlang-questions mailing list