acl  3.5.3.0
redis_geo.hpp
浏览该文件的文档.
1 #pragma once
2 #include "../acl_cpp_define.hpp"
3 #include <vector>
4 #include <map>
5 #include "../stdlib/string.hpp"
6 #include "redis_command.hpp"
7 
8 #if !defined(ACL_CLIENT_ONLY) && !defined(ACL_REDIS_DISABLE)
9 
10 namespace acl
11 {
12 
13 #define GEO_INVALID 360
14 #define GEO_LONGITUDE_MIN -180
15 #define GEO_LONGITUDE_MAX 180
16 #define GEO_LATITUDE_MIN -85.05112878
17 #define GEO_LATITUDE_MAX 85.05112878
18 
19 enum
20 {
25 };
26 
27 enum
28 {
29  GEO_WITH_COORD = 1 << 0,
30  GEO_WITH_DIST = 1 << 1,
31  GEO_WITH_HASH = 1 << 2,
32 };
33 
34 enum
35 {
39 };
40 
42 {
43 public:
44  geo_member(const char* name);
45  geo_member(const geo_member& member);
46  ~geo_member(void);
47 
48  void set_name(const char* name);
49  const char* get_name() const
50  {
51  return name_.c_str();
52  }
53 
54  void set_dist(double dist);
55  double get_dist() const
56  {
57  return dist_;
58  }
59 
60 #if defined(_WIN32) || defined(_WIN64)
61  void set_hash(__int64 hash);
62  __int64 get_hash() const
63 #else
64  void set_hash(long long int hash);
65  long long int get_hash() const
66 #endif // defined(_WIN32) || defined(_WIN64)
67  {
68  return hash_;
69  }
70 
71  void set_coordinate(double longitude, double latitude);
72  double get_longitude() const
73  {
74  return longitude_;
75  }
76 
77  double get_latitude() const
78  {
79  return latitude_;
80  }
81 
82 private:
83  string name_;
84  double dist_;
85 #if defined(_WIN32) || defined(_WIN64)
86  __int64 hash_;
87 #else
88  long long int hash_;
89 #endif // defined(_WIN32) || defined(_WIN64)
90 
91  double longitude_;
92  double latitude_;
93 };
94 
95 class ACL_CPP_API redis_geo : virtual public redis_command
96 {
97 public:
98  /**
99  * see redis_command::redis_command()
100  */
101  redis_geo();
102 
103  /**
104  * see redis_command::redis_command(redis_client*)
105  */
106  redis_geo(redis_client* conn);
107 
108  /**
109  * see redis_command::redis_command(redis_client_cluster*)
110  */
112 
114  redis_geo(redis_client_cluster* cluster, size_t max_conns);
115 
116  redis_geo(redis_client_pipeline* pipeline);
117 
118  virtual ~redis_geo();
119 
120  /////////////////////////////////////////////////////////////////////
121 
122  /**
123  * 添加一个指定的地理位置坐标至指定的 key 中
124  * Add the specified geospatial item (latitude, logitude, name)
125  * to the specified key.
126  * @param key {const char*} 对应的键值
127  * the specified key
128  * @param member {const char*} 该地理坐标的标识符
129  * the geospatial's identifier
130  * @param longitude {double} 经度
131  * the geospatial's loginitude
132  * @param latitude {double} 纬度
133  * the geospatial's latitude
134  * @return {int} 1:添加成功,0:该地理坐标标识符已存在,即使对其值进行了修改,
135  * 也将返回 0,-1:表示出错。
136  * the return value as below:
137  * 1: add one new member successfully
138  * 0: the member already existed, and the geospatial may be changed
139  * -1: some erro happened
140  */
141  int geoadd(const char* key, const char* member,
142  double longitude, double latitude);
143 
144  /**
145  * 给指定 key 添加一组地址位置坐标数据
146  * Add the specified geospatial items (latitude, logitude, name)
147  * to the specified key.
148  * @param key {const char*} 对应的键值
149  * the specified key
150  * @param size {size_t} 数组的长度
151  * the array's size
152  * @param members {const char* []} 成员数组,其长度由 size 指定
153  * the members array, which's length was specified by size parameter
154  * @param longitudes {const double[]} 经度数据数组,其长度由 size 指定
155  * the logintitudes array, which's length was specifed by size parameter
156  * @param latitudes {const double[]} 纬度数据数组,其长度由 size 指定
157  * the lattitudes array, which's length was specifed by size parameter
158  * @return {int} 添加成功的成员数量,返回值含义如下:
159  * return the successfully added members's count:
160  * > 0: 表示成功添加的成员数量;
161  * represent the successfully added members's count
162  * 0: 这些成员都已经存在
163  * the members's belong the key already existing
164  * -1: 表示出错,可以通过 result_error 函数查看出错原因
165  * some error happened, the result_error function can be used
166  * to find the error's reason
167  */
168  int geoadd(const char* key, size_t size, const char* members[],
169  const double longitudes[], const double latitudes[]);
170 
171  /**
172  * 给指定 key 添加一组地址位置坐标数据
173  * Add the specified geospatial items (latitude, logitude, name)
174  * to the specified key.
175  * @param key {const char*} 对应的键值
176  * the specified key
177  * @param members {const std::vector<string>&} 成员数组
178  * the members array
179  * @param longitudes {const std::vector<double>&} 经度数据数组
180  * the logintitudes array
181  * @param latitudes {const std::vector<double>&} 纬度数据数组
182  * the lattitudes array
183  * @return {int} 添加成功的成员数量,返回值含义如下:
184  * return the successfully added members's count:
185  * > 0: 表示成功添加的成员数量;
186  * represent the successfully added members's count
187  * 0: 这些成员都已经存在
188  * the members's belong the key already existing
189  * -1: 表示出错,可以通过 result_error 函数查看出错原因
190  * some error happened, the result_error function can be used
191  * to find the error's reason
192  * 注意:三个数组(members, longitudes, latitudes)的数组长度必须相等
193  * Notice: the three array's length must be equal between members,
194  * longitudes and latitudes
195  */
196  int geoadd(const char* key, const std::vector<string>& members,
197  const std::vector<double>& longitudes,
198  const std::vector<double>& latitudes);
199 
200  /**
201  * 以字符串方式返回指定成员的 GEOHASH 值
202  * Returns members of a geospatial index as standard geohash strings.
203  * @param key {const char*} 对应的键值
204  * the specified key
205  * @param members {const std::vector<string>&} 成员数组
206  * the members array
207  * @param results {std::vector<string>&} 存储结果集合
208  * store the result
209  * @return {bool} 操作是否成功
210  * if the operation was successful.
211  */
212  bool geohash(const char* key, const std::vector<string>& members,
213  std::vector<string>& results);
214 
215  /**
216  * 以字符串方式返回指定成员的 GEOHASH 值
217  * Returns members of a geospatial index as standard geohash strings.
218  * @param key {const char*} 对应的键值
219  * the specified key
220  * @param member {const char*} 成员名
221  * the member of a geospatial index
222  * @param result {std::vector<string>&} 存储结果
223  * store the result
224  * @return {bool} 操作是否成功
225  * if the operation was successful.
226  */
227  bool geohash(const char* key, const char* member, string& result);
228 
229  /**
230  * 获得指定成员的地理位置坐标
231  * Returns longitude and latitude of members of a geospatial index
232  * @param key {const char*} 对应的键值
233  * the specified key
234  * @param members {const std::vector<string>&} 成员数组
235  * the members array
236  * @param results {std::vector<std::pair<double, double> >&} 存储结果集
237  * store the results
238  * @return {bool} 操作是否成功
239  * if the operation was successful.
240  */
241  bool geopos(const char* key, const std::vector<string>& members,
242  std::vector<std::pair<double, double> >& results);
243 
244  /**
245  * 获得某个指定成员的地理位置坐标
246  * Returns longitude and latitude of the one member of
247  * a geospatial index
248  * @param key {const char*} 指定键值
249  * the specifed key
250  * @param member {const char*} 指定成员名
251  * the specified member
252  * @param result {std::pair<double, double>&} 存储坐标点结果
253  * store the result of longitude and latitude of the member
254  * @return {bool} 操作是否成功
255  * if the operation was successful.
256  */
257  bool geopos(const char* key, const char* member,
258  std::pair<double, double>& result);
259 
260  /**
261  * 获得两个地理位置坐标之间的距离
262  * Returns the distance between two members of a geospatial index
263  * @param key {const char*} 对应的键值
264  * the specified key
265  * @param member1 {const char*} 地理坐标成员
266  * one member of a geospatial index
267  * @param member2 {const char*} 地理坐标成员
268  * another member of a geospatial index
269  * @param unit {int} 返回的距离的单位值
270  * @return {double} 两个坐标之间的长度,返回值 < 0 表示出错
271  * returns the distance between two members, which was less than 0
272  * if some error happened.
273  */
274  double geodist(const char* key, const char* member1,
275  const char* member2, int unit = GEO_UNIT_M);
276 
277  /**
278  * 获得距离某指定坐标位置在给定距离范围内的所有坐标点
279  * Query a sorted set representing a geospatial index to fetch
280  * members matching a given maximum distance from a point
281  * @param key {const char*} 对应的键值
282  * the specified key
283  * @param longitude {double} 指定坐标点的经度值
284  * the longitude of the specified geospatial coordinate
285  * @param latitude {double} 指定坐标点的纬度值
286  * the latitude of the specified geospatial coordinate
287  * @param radius {double} 限定的距离范围大小
288  * the distance from the specified coordinate
289  * @param unit {int} radius 距离的单位类型
290  * the unit type of the raidus
291  * @param with {int} 查询条件选项,参见上面的定义:GEO_WITH_XXX
292  * the serach operations, defined as GEO_WITH_XXX above
293  * @param sort {int} 查询结果的排序方式,定义参见:GEO_SORT_XXX
294  * the sorted type of the results, defined as GEO_SORT_XXX above
295  * @return {const std::vector<geo_member>&} 符合条件的坐标点的结果集
296  * Returns the results according the searching conditions.
297  */
298  const std::vector<geo_member>& georadius(const char* key,
299  double longitude, double latitude, double radius,
300  int unit = GEO_UNIT_M,
301  int with = GEO_WITH_COORD | GEO_WITH_DIST,
302  int sort = GEO_SORT_ASC);
303 
304  /**
305  * 获得距离某指定坐标位置在给定距离范围内的所有坐标点
306  * Query a sorted set representing a geospatial index to fetch
307  * members matching a given maximum distance from a member
308  * @param key {const char*} 对应的键值
309  * the specified key
310  * @param member {const char*} 某个指定的坐标点成员
311  * the specified member of a geospatial index
312  * @param radius {double} 限定的距离范围大小
313  * the distance from the specified coordinate
314  * @param unit {int} radius 距离的单位类型
315  * the unit type of the raidus
316  * @param with {int} 查询条件选项,参见上面的定义:GEO_WITH_XXX
317  * the serach operations, defined as GEO_WITH_XXX above
318  * @param sort {int} 查询结果的排序方式,定义参见:GEO_SORT_XXX
319  * the sorted type of the results, defined as GEO_SORT_XXX above
320  * @return {const std::vector<geo_member>&} 符合条件的坐标点的结果集
321  * Returns the results according the searching conditions.
322  */
323  const std::vector<geo_member>& georadiusbymember(const char* key,
324  const char* member, double radius,
325  int unit = GEO_UNIT_M,
326  int with = GEO_WITH_COORD | GEO_WITH_DIST,
327  int sort = GEO_SORT_ASC);
328 
329 private:
330  std::vector<geo_member> positions_;
331 
332  void add_one_pos(const redis_result& rr);
333  static const char* get_unit(int unit);
334 };
335 
336 } // namespace acl
337 
338 #endif // !defined(ACL_CLIENT_ONLY) && !defined(ACL_REDIS_DISABLE)
HTTP_API void const char * name
Definition: lib_http.h:620
double get_latitude() const
Definition: redis_geo.hpp:77
#define ACL_CPP_DEPRECATED
Definition: atomic.hpp:86
double get_longitude() const
Definition: redis_geo.hpp:72
const char * get_name() const
Definition: redis_geo.hpp:49
long long int get_hash() const
Definition: redis_geo.hpp:65
double get_dist() const
Definition: redis_geo.hpp:55
#define ACL_CPP_API