所以我将项目存储在ets/dets表中。目前我正在使用postgres来做这件事,我想看看它是否可以在没有它的情况下完成,或者它是否会慢得多。
我想做一个基本的空间查询。基本上是检查物品是否在当前位置的X米范围内。
ets = ets.new(:table, [:named_table])
# Data looks like this: {id, lat, lng, data}
# This is the location of the requester, this changed on every query.
current_location = {lat, lng}如果我想从ets的所有项目在50M内的请求。
ets |> :ets.tab2list |> Enum.filter(fn {_, rlat, rlng, _} = row ->
Haversine.distance({rlat, rlng}, current_location) < 50
end)有没有更好的方法来做到这一点?我不能像Haversine公式那样使用卫士:math.sin/asin/sqrt。
基本上问题是。有没有办法通过函数查询ETS/DETS表?或者我需要先把它提取到一个列表中?
发布于 2017-09-09 23:24:15
我在这里使用:ets.foldr,函数收集所有符合您的条件的元素,忽略其余的:
:ets.foldr(fn {_, rlat, rlng, _}, row ->
if Haversine.distance({rlat, rlng}, current_location) < 50 do
[row | acc]
else
acc
end
end, [], ets)这将节省您首先为表中的所有行构造一个列表的开销,而这正是:ets.tab2list所做的。
( ets中有一些match*函数,但它们只接受match spec,您的函数不能转换为匹配规范,因为它使用了数学规范中不允许的匹配操作。)
https://stackoverflow.com/questions/46131827
复制相似问题