问题描述
我正在尝试使用Xodus实现“附近”过滤器,其代码如下:
AtomicReference<EntityIterable> referenceToScope = ...;
PropertyNearbyCondition propertyNearbyCondition = (PropertyNearbyCondition) entityCondition;
String propertyName = propertyNearbyCondition.propertyName();
Double longitude = propertyNearbyCondition.longitude();
Double latitude = propertyNearbyCondition.latitude();
Double distance = propertyNearbyCondition.distance();
EntityIterable entities =
referenceToScope.get().intersect(txn.findWithProp(entityType,propertyName));
List<Entity> entityList = new ArrayList<>();
entities.forEach(entity -> {
GeoPoint reference = (GeoPoint) entity.getProperty(propertyName);
double instantaneousDistance =
MathHelper.distFrom(latitude,longitude,reference.getLatitude(),reference.getLatitude());
if (distance >= instantaneousDistance) {
entityList.add(entity);
}
});
EntityIterable scoped = referenceToScope.get().intersect(build(entityList));
EntityIterable build(List<Entity> entityList) {
// TODO: Build a new EntityIterable from the entityList
}
该算法可能不是最好的算法,但是,这里的主要问题是如何根据多个Entity对象构建新的EntityIterable
?有可能吗?
我基本上收集“附近”实体的解决方案是使用自定义GeoPoint
属性遍历所有实体,然后对找到的每个实体进行比较,比较其GeoPoint属性的距离,如果命中,则所有这些实体应该收集到一个EntityIterable.
如何从EntityIterable
个对象列表中构建一个Entity
?
更新:
逐步解释其工作原理:
下面的这段代码获取具有给定属性名称的所有实体,例如geoLocation
EntityIterable entities =
referenceToScope.get().intersect(txn.findWithProp(entityType,propertyName));
然后,对于具有此类geoLocation属性的所有实体,例如对其进行迭代以计算其是否满足距离目标:
List<Entity> entityList = new ArrayList<>();
entities.forEach(entity -> {
// compute the distance target
});
在达到目标的新List
中添加实体。
从这里开始,需要删除EntityIterable entities
中所有与entityList
中匹配的 entities 的ID不相等的实体,或者删除将这些匹配的实体与referenceToScope.get()
相交而不与EntityIterable entities
相交(为避免混淆,此entities
iterable 只是临时的)
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)