-
Notifications
You must be signed in to change notification settings - Fork 952
Expand file tree
/
Copy pathredis_geo.hpp
More file actions
348 lines (312 loc) · 11.6 KB
/
redis_geo.hpp
File metadata and controls
348 lines (312 loc) · 11.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
#pragma once
#include "../acl_cpp_define.hpp"
#include <vector>
#include <map>
#include "../stdlib/string.hpp"
#include "redis_command.hpp"
#if !defined(ACL_CLIENT_ONLY) && !defined(ACL_REDIS_DISABLE)
namespace acl {
#define GEO_INVALID 360
#define GEO_LONGITUDE_MIN -180
#define GEO_LONGITUDE_MAX 180
#define GEO_LATITUDE_MIN -85.05112878
#define GEO_LATITUDE_MAX 85.05112878
enum {
GEO_UNIT_FT,
GEO_UNIT_M,
GEO_UNIT_MI,
GEO_UNIT_KM,
};
enum {
GEO_WITH_COORD = 1 << 0,
GEO_WITH_DIST = 1 << 1,
GEO_WITH_HASH = 1 << 2,
};
enum {
GEO_SORT_NONE,
GEO_SORT_ASC,
GEO_SORT_DESC,
};
class ACL_CPP_API geo_member {
public:
geo_member(const char* name);
geo_member(const geo_member& member);
~geo_member();
void set_name(const char* name);
const char* get_name() const
{
return name_.c_str();
}
void set_dist(double dist);
double get_dist() const
{
return dist_;
}
#if defined(_WIN32) || defined(_WIN64)
void set_hash(__int64 hash);
__int64 get_hash() const
#else
void set_hash(long long int hash);
long long int get_hash() const
#endif // defined(_WIN32) || defined(_WIN64)
{
return hash_;
}
void set_coordinate(double longitude, double latitude);
double get_longitude() const
{
return longitude_;
}
double get_latitude() const
{
return latitude_;
}
private:
string name_;
double dist_;
#if defined(_WIN32) || defined(_WIN64)
__int64 hash_;
#else
long long int hash_;
#endif // defined(_WIN32) || defined(_WIN64)
double longitude_;
double latitude_;
};
class ACL_CPP_API redis_geo : virtual public redis_command
{
public:
/**
* see redis_command::redis_command()
*/
redis_geo();
/**
* see redis_command::redis_command(redis_client*)
*/
redis_geo(redis_client* conn);
/**
* see redis_command::redis_command(redis_client_cluster*)
*/
redis_geo(redis_client_cluster* cluster);
ACL_CPP_DEPRECATED
redis_geo(redis_client_cluster* cluster, size_t max_conns);
redis_geo(redis_client_pipeline* pipeline);
virtual ~redis_geo();
/////////////////////////////////////////////////////////////////////
/**
* Add a specified geospatial coordinate to specified key
* Add the specified geospatial item (latitude, logitude, name)
* to the specified key.
* @param key {const char*} Corresponding key value
* the specified key
* @param member {const char*} Identifier of this geospatial coordinate
* the geospatial's identifier
* @param longitude {double} Longitude
* the geospatial's loginitude
* @param latitude {double} Latitude
* the geospatial's latitude
* @return {int} 1: Add successful, 0: This geospatial coordinate identifier
* already exists. Even if its value was modified,
* will also return 0. -1: Indicates error.
* the return value as below:
* 1: add one new member successfully
* 0: the member already existed, and the geospatial may be changed
* -1: some erro happened
*/
int geoadd(const char* key, const char* member,
double longitude, double latitude);
/**
* Add a group of geospatial coordinate data to specified key
* Add the specified geospatial items (latitude, logitude, name)
* to the specified key.
* @param key {const char*} Corresponding key value
* the specified key
* @param size {size_t} Array length
* the array's size
* @param members {const char* []} Member array, its length is specified by
* size
* the members array, which's length was specified by size parameter
* @param longitudes {const double[]} Longitude data array, its length is
* specified by size
* the logintitudes array, which's length was specifed by size parameter
* @param latitudes {const double[]} Latitude data array, its length is
* specified by size
* the lattitudes array, which's length was specifed by size parameter
* @return {int} Number of successfully added members. Return value meanings:
* return the successfully added members's count:
* > 0: Indicates number of successfully added members;
* represent the successfully added members's count
* 0: These members already exist
* the members's belong the key already existing
* -1: Indicates error. Can check error reason through result_error function
* some error happened, the result_error function can be used
* to find the error's reason
*/
int geoadd(const char* key, size_t size, const char* members[],
const double longitudes[], const double latitudes[]);
/**
* Add a group of geospatial coordinate data to specified key
* Add the specified geospatial items (latitude, logitude, name)
* to the specified key.
* @param key {const char*} Corresponding key value
* the specified key
* @param members {const std::vector<string>&} Member array
* the members array
* @param longitudes {const std::vector<double>&} Longitude data array
* the logintitudes array
* @param latitudes {const std::vector<double>&} Latitude data array
* the lattitudes array
* @return {int} Number of successfully added members. Return value meanings:
* return the successfully added members's count:
* > 0: Indicates number of successfully added members;
* represent the successfully added members's count
* 0: These members already exist
* the members's belong the key already existing
* -1: Indicates error. Can check error reason through result_error function
* some error happened, the result_error function can be used
* to find the error's reason
* Note: Three arrays (members, longitudes, latitudes) must have equal array
* lengths
* Notice: the three array's length must be equal between members,
* longitudes and latitudes
*/
int geoadd(const char* key, const std::vector<string>& members,
const std::vector<double>& longitudes,
const std::vector<double>& latitudes);
/**
* Return GEOHASH value of specified members as strings
* Returns members of a geospatial index as standard geohash strings.
* @param key {const char*} Corresponding key value
* the specified key
* @param members {const std::vector<string>&} Member array
* the members array
* @param results {std::vector<string>&} Store result set
* store the result
* @return {bool} Whether operation was successful
* if the operation was successful.
*/
bool geohash(const char* key, const std::vector<string>& members,
std::vector<string>& results);
/**
* Return GEOHASH value of specified member as string
* Returns members of a geospatial index as standard geohash strings.
* @param key {const char*} Corresponding key value
* the specified key
* @param member {const char*} Member name
* the member of a geospatial index
* @param result {std::vector<string>&} Store result
* store the result
* @return {bool} Whether operation was successful
* if the operation was successful.
*/
bool geohash(const char* key, const char* member, string& result);
/**
* Get geospatial coordinates of specified members
* Returns longitude and latitude of members of a geospatial index
* @param key {const char*} Corresponding key value
* the specified key
* @param members {const std::vector<string>&} Member array
* the members array
* @param results {std::vector<std::pair<double, double> >&} Store result set
* store the results
* @return {bool} Whether operation was successful
* if the operation was successful.
*/
bool geopos(const char* key, const std::vector<string>& members,
std::vector<std::pair<double, double> >& results);
/**
* Get geospatial coordinates of a specified member
* Returns longitude and latitude of the one member of
* a geospatial index
* @param key {const char*} Specified key value
* the specifed key
* @param member {const char*} Specified member name
* the specified member
* @param result {std::pair<double, double>&} Store coordinate point result
* store the result of longitude and latitude of the member
* @return {bool} Whether operation was successful
* if the operation was successful.
*/
bool geopos(const char* key, const char* member,
std::pair<double, double>& result);
/**
* Get distance between two geospatial coordinates
* Returns the distance between two members of a geospatial index
* @param key {const char*} Corresponding key value
* the specified key
* @param member1 {const char*} Geospatial coordinate member
* one member of a geospatial index
* @param member2 {const char*} Geospatial coordinate member
* another member of a geospatial index
* @param unit {int} Unit value of returned distance
* @return {double} Length between two coordinates. Return value < 0 indicates
* error
* returns the distance between two members, which was less than 0
* if some error happened.
*/
double geodist(const char* key, const char* member1,
const char* member2, int unit = GEO_UNIT_M);
/**
* Get all coordinate points within given distance range from a specified
* coordinate position
* Query a sorted set representing a geospatial index to fetch
* members matching a given maximum distance from a point
* @param key {const char*} Corresponding key value
* the specified key
* @param longitude {double} Longitude value of specified coordinate point
* the longitude of the specified geospatial coordinate
* @param latitude {double} Latitude value of specified coordinate point
* the latitude of the specified geospatial coordinate
* @param radius {double} Limited distance range size
* the distance from the specified coordinate
* @param unit {int} Unit type of radius distance
* the unit type of the raidus
* @param with {int} Query condition options. See definitions above:
* GEO_WITH_XXX
* the serach operations, defined as GEO_WITH_XXX above
* @param sort {int} Sorting method of query results. See definitions:
* GEO_SORT_XXX
* the sorted type of the results, defined as GEO_SORT_XXX above
* @return {const std::vector<geo_member>&} Result set of coordinate points
* meeting conditions
* Returns the results according the searching conditions.
*/
const std::vector<geo_member>& georadius(const char* key,
double longitude, double latitude, double radius,
int unit = GEO_UNIT_M,
int with = GEO_WITH_COORD | GEO_WITH_DIST,
int sort = GEO_SORT_ASC);
/**
* Get all coordinate points within given distance range from a specified
* coordinate position
* Query a sorted set representing a geospatial index to fetch
* members matching a given maximum distance from a member
* @param key {const char*} Corresponding key value
* the specified key
* @param member {const char*} A specified coordinate point member
* the specified member of a geospatial index
* @param radius {double} Limited distance range size
* the distance from the specified coordinate
* @param unit {int} Unit type of radius distance
* the unit type of the raidus
* @param with {int} Query condition options. See definitions above:
* GEO_WITH_XXX
* the serach operations, defined as GEO_WITH_XXX above
* @param sort {int} Sorting method of query results. See definitions:
* GEO_SORT_XXX
* the sorted type of the results, defined as GEO_SORT_XXX above
* @return {const std::vector<geo_member>&} Result set of coordinate points
* meeting conditions
* Returns the results according the searching conditions.
*/
const std::vector<geo_member>& georadiusbymember(const char* key,
const char* member, double radius,
int unit = GEO_UNIT_M,
int with = GEO_WITH_COORD | GEO_WITH_DIST,
int sort = GEO_SORT_ASC);
private:
std::vector<geo_member> positions_;
void add_one_pos(const redis_result& rr);
static const char* get_unit(int unit);
};
} // namespace acl
#endif // !defined(ACL_CLIENT_ONLY) && !defined(ACL_REDIS_DISABLE)