mirror of
https://gitee.com/acl-dev/acl.git
synced 2024-12-14 08:50:53 +08:00
331 lines
11 KiB
C++
331 lines
11 KiB
C++
#pragma once
|
||
#include "acl_cpp/acl_cpp_define.hpp"
|
||
#include <vector>
|
||
#include <map>
|
||
#include "acl_cpp/stdlib/string.hpp"
|
||
#include "acl_cpp/redis/redis_command.hpp"
|
||
|
||
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);
|
||
|
||
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 redis_client;
|
||
|
||
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*£¬ size_t)
|
||
*/
|
||
redis_geo(redis_client_cluster* cluster, size_t max_conns = 0);
|
||
virtual ~redis_geo();
|
||
|
||
/////////////////////////////////////////////////////////////////////
|
||
|
||
/**
|
||
* Ìí¼ÓÒ»¸öÖ¸¶¨µÄµØÀíλÖÃ×ø±êÖÁÖ¸¶¨µÄ key ÖÐ
|
||
* Add the specified geospatial item (latitude, logitude, name)
|
||
* to the specified key.
|
||
* @param key {const char*} ¶ÔÓ¦µÄ¼üÖµ
|
||
* the specified key
|
||
* @param member {const char*} ¸ÃµØÀí×ø±êµÄ±êʶ·û
|
||
* the geospatial's identifier
|
||
* @param loginitude {double} ¾¶È
|
||
* the geospatial's loginitude
|
||
* @param latitude {double} γ¶È
|
||
* the geospatial's latitude
|
||
* @return {int} 1£ºÌí¼Ó³É¹¦£¬0£º¸ÃµØÀí×ø±ê±êʶ·ûÒÑ´æÔÚ£¬¼´Ê¹¶ÔÆäÖµ½øÐÐÁËÐ޸ģ¬
|
||
* Ò²½«·µ»Ø 0£¬-1£º±íʾ³ö´í¡£
|
||
* 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);
|
||
|
||
/**
|
||
* ¸øÖ¸¶¨ key Ìí¼ÓÒ»×éµØַλÖÃ×ø±êÊý¾Ý
|
||
* Add the specified geospatial items (latitude, logitude, name)
|
||
* to the specified key.
|
||
* @param key {const char*} ¶ÔÓ¦µÄ¼üÖµ
|
||
* the specified key
|
||
* @param size {size_t} Êý×éµÄ³¤¶È
|
||
* the array's size
|
||
* @param memebers {const char* []} ³ÉÔ±Êý×飬Æ䳤¶ÈÓÉ size Ö¸¶¨
|
||
* the members array, which's length was specified by size parameter
|
||
* @param longitudes {const double[]} ¾¶ÈÊý¾ÝÊý×飬Æ䳤¶ÈÓÉ size Ö¸¶¨
|
||
* the logintitudes array, which's length was specifed by size parameter
|
||
* @param latitudes {const double[]} γ¶ÈÊý¾ÝÊý×飬Æ䳤¶ÈÓÉ size Ö¸¶¨
|
||
* the lattitudes array, which's length was specifed by size parameter
|
||
* @return {int} Ìí¼Ó³É¹¦µÄ³ÉÔ±ÊýÁ¿£¬·µ»ØÖµº¬ÒåÈçÏ£º
|
||
* return the successfully added members's count:
|
||
* > 0: ±íʾ³É¹¦Ìí¼ÓµÄ³ÉÔ±ÊýÁ¿£»
|
||
* represent the successfully added memebers's count
|
||
* 0: ÕâЩ³ÉÔ±¶¼ÒѾ´æÔÚ
|
||
* the members's belong the key already existing
|
||
* -1: ±íʾ³ö´í£¬¿ÉÒÔͨ¹ý result_error º¯Êý²é¿´³ö´íÔÒò
|
||
* 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* memebers[],
|
||
const double longitudes[], const double latitudes[]);
|
||
|
||
/**
|
||
* ¸øÖ¸¶¨ key Ìí¼ÓÒ»×éµØַλÖÃ×ø±êÊý¾Ý
|
||
* Add the specified geospatial items (latitude, logitude, name)
|
||
* to the specified key.
|
||
* @param key {const char*} ¶ÔÓ¦µÄ¼üÖµ
|
||
* the specified key
|
||
* @param memebers {const std::vector<string>&} ³ÉÔ±Êý×é
|
||
* the members array
|
||
* @param longitudes {const std::vector<double>&} ¾¶ÈÊý¾ÝÊý×é
|
||
* the logintitudes array
|
||
* @param latitudes {const std::vector<double>&} γ¶ÈÊý¾ÝÊý×é
|
||
* the lattitudes array
|
||
* @return {int} Ìí¼Ó³É¹¦µÄ³ÉÔ±ÊýÁ¿£¬·µ»ØÖµº¬ÒåÈçÏ£º
|
||
* return the successfully added members's count:
|
||
* > 0: ±íʾ³É¹¦Ìí¼ÓµÄ³ÉÔ±ÊýÁ¿£»
|
||
* represent the successfully added memebers's count
|
||
* 0: ÕâЩ³ÉÔ±¶¼ÒѾ´æÔÚ
|
||
* the members's belong the key already existing
|
||
* -1: ±íʾ³ö´í£¬¿ÉÒÔͨ¹ý result_error º¯Êý²é¿´³ö´íÔÒò
|
||
* some error happened, the result_error function can be used
|
||
* to find the error's reason
|
||
* ×¢Ò⣺Èý¸öÊý×é(members, longitudes, latitudes)µÄÊý×鳤¶È±ØÐëÏàµÈ
|
||
* 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);
|
||
|
||
/**
|
||
* ÒÔ×Ö·û´®·½Ê½·µ»ØÖ¸¶¨³ÉÔ±µÄ GEOHASH Öµ
|
||
* Returns members of a geospatial index as standard geohash strings.
|
||
* @param key {const char*} ¶ÔÓ¦µÄ¼üÖµ
|
||
* the specified key
|
||
* @param memebers {const std::vector<string>&} ³ÉÔ±Êý×é
|
||
* the members array
|
||
* @param results {std::vector<string>&} ´æ´¢½á¹û¼¯ºÏ
|
||
* store the result
|
||
* @return {bool} ²Ù×÷ÊÇ·ñ³É¹¦
|
||
* if the operation was successful.
|
||
*/
|
||
bool geohash(const char* key, const std::vector<string>& members,
|
||
std::vector<string>& results);
|
||
|
||
/**
|
||
* ÒÔ×Ö·û´®·½Ê½·µ»ØÖ¸¶¨³ÉÔ±µÄ GEOHASH Öµ
|
||
* Returns members of a geospatial index as standard geohash strings.
|
||
* @param key {const char*} ¶ÔÓ¦µÄ¼üÖµ
|
||
* the specified key
|
||
* @param memeber {const char*} ³ÉÔ±Ãû
|
||
* the member of a geospatial index
|
||
* @param result {std::vector<string>&} ´æ´¢½á¹û
|
||
* store the result
|
||
* @return {bool} ²Ù×÷ÊÇ·ñ³É¹¦
|
||
* if the operation was successful.
|
||
*/
|
||
bool geohash(const char* key, const char* member, string& result);
|
||
|
||
/**
|
||
* »ñµÃÖ¸¶¨³ÉÔ±µÄµØÀíλÖÃ×ø±ê
|
||
* Returns longitude and latitude of members of a geospatial index
|
||
* @param key {const char*} ¶ÔÓ¦µÄ¼üÖµ
|
||
* the specified key
|
||
* @param memebers {const std::vector<string>&} ³ÉÔ±Êý×é
|
||
* the members array
|
||
* @param results {std::vector<std::pair<double, double> >&} ´æ´¢½á¹û¼¯
|
||
* store the results
|
||
* @return {bool} ²Ù×÷ÊÇ·ñ³É¹¦
|
||
* if the operation was successful.
|
||
*/
|
||
bool geopos(const char* key, const std::vector<string>& members,
|
||
std::vector<std::pair<double, double> >& results);
|
||
|
||
/**
|
||
* »ñµÃij¸öÖ¸¶¨³ÉÔ±µÄµØÀíλÖÃ×ø±ê
|
||
* Returns longitude and latitude of the one member of
|
||
* a geospatial index
|
||
* @param key {const char*} Ö¸¶¨¼üÖµ
|
||
* the specifed key
|
||
* @param member {const char*} Ö¸¶¨³ÉÔ±Ãû
|
||
* the specified member
|
||
* @param result {std::pair<double, double>&} ´æ´¢×ø±êµã½á¹û
|
||
* store the result of longitude and latitude of the member
|
||
* @return {bool} ²Ù×÷ÊÇ·ñ³É¹¦
|
||
* if the operation was successful.
|
||
*/
|
||
bool geopos(const char* key, const char* member,
|
||
std::pair<double, double>& result);
|
||
|
||
/**
|
||
* »ñµÃÁ½¸öµØÀíλÖÃ×ø±êÖ®¼äµÄ¾àÀë
|
||
* Returns the distance between two members of a geospatial index
|
||
* @param key {const char*} ¶ÔÓ¦µÄ¼üÖµ
|
||
* the specified key
|
||
* @param member1 {const char*} µØÀí×ø±ê³ÉÔ±
|
||
* one member of a geospatial index
|
||
* @param member2 {const char*} µØÀí×ø±ê³ÉÔ±
|
||
* another member of a geospatial index
|
||
* @param unit {int} ·µ»ØµÄ¾àÀëµÄµ¥Î»Öµ
|
||
* @return {double} Á½¸ö×ø±êÖ®¼äµÄ³¤¶È£¬·µ»ØÖµ < 0 ±íʾ³ö´í
|
||
* 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);
|
||
|
||
/**
|
||
* »ñµÃ¾àÀëijָ¶¨×ø±êλÖÃÔÚ¸ø¶¨¾àÀ뷶ΧÄÚµÄËùÓÐ×ø±êµã
|
||
* Query a sorted set representing a geospatial index to fetch
|
||
* members matching a given maximum distance from a point
|
||
* @param key {const char*} ¶ÔÓ¦µÄ¼üÖµ
|
||
* the specified key
|
||
* @param longitude {double} Ö¸¶¨×ø±êµãµÄ¾¶ÈÖµ
|
||
* the longitude of the specified geospatial coordinate
|
||
* @param latitude {double} Ö¸¶¨×ø±êµãµÄγ¶ÈÖµ
|
||
* the latitude of the specified geospatial coordinate
|
||
* @param radius {double} ÏÞ¶¨µÄ¾àÀ뷶Χ´óС
|
||
* the distance from the specified coordinate
|
||
* @param unit {int} radius ¾àÀëµÄµ¥Î»ÀàÐÍ
|
||
* the unit type of the raidus
|
||
* @param with {int} ²éѯÌõ¼þÑ¡Ï²Î¼ûÉÏÃæµÄ¶¨Ò壺GEO_WITH_XXX
|
||
* the serach operations, defined as GEO_WITH_XXX above
|
||
* @param sort {int} ²éѯ½á¹ûµÄÅÅÐò·½Ê½£¬¶¨Òå²Î¼û£ºGEO_SORT_XXX
|
||
* the sorted type of the results, defined as GEO_SORT_XXX above
|
||
* @return {const std::vector<geo_member>&} ·ûºÏÌõ¼þµÄ×ø±êµãµÄ½á¹û¼¯
|
||
* 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);
|
||
|
||
/**
|
||
* »ñµÃ¾àÀëijָ¶¨×ø±êλÖÃÔÚ¸ø¶¨¾àÀ뷶ΧÄÚµÄËùÓÐ×ø±êµã
|
||
* Query a sorted set representing a geospatial index to fetch
|
||
* members matching a given maximum distance from a member
|
||
* @param key {const char*} ¶ÔÓ¦µÄ¼üÖµ
|
||
* the specified key
|
||
* @param member {const char*} ij¸öÖ¸¶¨µÄ×ø±êµã³ÉÔ±
|
||
* the specified member of a geospatial index
|
||
* @param radius {double} ÏÞ¶¨µÄ¾àÀ뷶Χ´óС
|
||
* the distance from the specified coordinate
|
||
* @param unit {int} radius ¾àÀëµÄµ¥Î»ÀàÐÍ
|
||
* the unit type of the raidus
|
||
* @param with {int} ²éѯÌõ¼þÑ¡Ï²Î¼ûÉÏÃæµÄ¶¨Ò壺GEO_WITH_XXX
|
||
* the serach operations, defined as GEO_WITH_XXX above
|
||
* @param sort {int} ²éѯ½á¹ûµÄÅÅÐò·½Ê½£¬¶¨Òå²Î¼û£ºGEO_SORT_XXX
|
||
* the sorted type of the results, defined as GEO_SORT_XXX above
|
||
* @return {const std::vector<geo_member>&} ·ûºÏÌõ¼þµÄ×ø±êµãµÄ½á¹û¼¯
|
||
* 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
|