acl/lib_acl_cpp/include/acl_cpp/redis/redis_geo.hpp
2017-06-02 14:47:24 +08:00

331 lines
10 KiB
C++
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#pragma once
#include "../acl_cpp_define.hpp"
#include <vector>
#include <map>
#include "../stdlib/string.hpp"
#include "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();
/////////////////////////////////////////////////////////////////////
/**
* <20><><EFBFBD><EFBFBD>һ<EFBFBD><D2BB>ָ<EFBFBD><D6B8><EFBFBD>ĵ<EFBFBD><C4B5><EFBFBD>λ<EFBFBD><CEBB><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ָ<EFBFBD><D6B8><EFBFBD><EFBFBD> key <20><>
* Add the specified geospatial item (latitude, logitude, name)
* to the specified key.
* @param key {const char*} <20><>Ӧ<EFBFBD>ļ<EFBFBD>ֵ
* the specified key
* @param member {const char*} <20>õ<EFBFBD><C3B5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ı<EFBFBD>ʶ<EFBFBD><CAB6>
* the geospatial's identifier
* @param loginitude {double} <20><><EFBFBD><EFBFBD>
* the geospatial's loginitude
* @param latitude {double} γ<><CEB3>
* the geospatial's latitude
* @return {int} 1<><31><EFBFBD><EFBFBD><EFBFBD>ӳɹ<D3B3><C9B9><EFBFBD>0<EFBFBD><30><EFBFBD>õ<EFBFBD><C3B5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʶ<EFBFBD><CAB6><EFBFBD>Ѵ<EFBFBD><D1B4>ڣ<EFBFBD><DAA3><EFBFBD>ʹ<EFBFBD><CAB9><EFBFBD><EFBFBD>ֵ<EFBFBD><D6B5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>޸ģ<DEB8>
* Ҳ<><D2B2><EFBFBD><EFBFBD><EFBFBD><EFBFBD> 0<><30>-1<><31><EFBFBD><EFBFBD>ʾ<EFBFBD><CABE><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
* 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);
/**
* <20><>ָ<EFBFBD><D6B8> key <20><><EFBFBD><EFBFBD>һ<EFBFBD><D2BB><EFBFBD><EFBFBD>ַλ<D6B7><CEBB><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
* Add the specified geospatial items (latitude, logitude, name)
* to the specified key.
* @param key {const char*} <20><>Ӧ<EFBFBD>ļ<EFBFBD>ֵ
* the specified key
* @param size {size_t} <20><><EFBFBD><EFBFBD><EFBFBD>ij<EFBFBD><C4B3><EFBFBD>
* the array's size
* @param memebers {const char* []} <20><>Ա<EFBFBD><D4B1><EFBFBD><EFBFBD><EFBFBD><E4B3A4><EFBFBD><EFBFBD> size ָ<><D6B8>
* the members array, which's length was specified by size parameter
* @param longitudes {const double[]} <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><E4B3A4><EFBFBD><EFBFBD> size ָ<><D6B8>
* the logintitudes array, which's length was specifed by size parameter
* @param latitudes {const double[]} γ<><CEB3><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><E4B3A4><EFBFBD><EFBFBD> size ָ<><D6B8>
* the lattitudes array, which's length was specifed by size parameter
* @return {int} <20><><EFBFBD>ӳɹ<D3B3><C9B9>ij<EFBFBD>Ա<EFBFBD><D4B1><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֵ<EFBFBD><D6B5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>£<EFBFBD>
* return the successfully added members's count:
* > 0: <20><>ʾ<EFBFBD>ɹ<EFBFBD><C9B9><EFBFBD><EFBFBD>ӵij<D3B5>Ա<EFBFBD><D4B1><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
* represent the successfully added memebers's count
* 0: <20><>Щ<EFBFBD><D0A9>Ա<EFBFBD><D4B1><EFBFBD>Ѿ<EFBFBD><D1BE><EFBFBD><EFBFBD><EFBFBD>
* the members's belong the key already existing
* -1: <20><>ʾ<EFBFBD><CABE><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ͨ<EFBFBD><CDA8> result_error <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><E9BFB4><EFBFBD><EFBFBD>ԭ<EFBFBD><D4AD>
* 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[]);
/**
* <20><>ָ<EFBFBD><D6B8> key <20><><EFBFBD><EFBFBD>һ<EFBFBD><D2BB><EFBFBD><EFBFBD>ַλ<D6B7><CEBB><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
* Add the specified geospatial items (latitude, logitude, name)
* to the specified key.
* @param key {const char*} <20><>Ӧ<EFBFBD>ļ<EFBFBD>ֵ
* the specified key
* @param memebers {const std::vector<string>&} <20><>Ա<EFBFBD><D4B1><EFBFBD><EFBFBD>
* the members array
* @param longitudes {const std::vector<double>&} <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
* the logintitudes array
* @param latitudes {const std::vector<double>&} γ<><CEB3><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
* the lattitudes array
* @return {int} <20><><EFBFBD>ӳɹ<D3B3><C9B9>ij<EFBFBD>Ա<EFBFBD><D4B1><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֵ<EFBFBD><D6B5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>£<EFBFBD>
* return the successfully added members's count:
* > 0: <20><>ʾ<EFBFBD>ɹ<EFBFBD><C9B9><EFBFBD><EFBFBD>ӵij<D3B5>Ա<EFBFBD><D4B1><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
* represent the successfully added memebers's count
* 0: <20><>Щ<EFBFBD><D0A9>Ա<EFBFBD><D4B1><EFBFBD>Ѿ<EFBFBD><D1BE><EFBFBD><EFBFBD><EFBFBD>
* the members's belong the key already existing
* -1: <20><>ʾ<EFBFBD><CABE><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ͨ<EFBFBD><CDA8> result_error <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><E9BFB4><EFBFBD><EFBFBD>ԭ<EFBFBD><D4AD>
* some error happened, the result_error function can be used
* to find the error's reason
* ע<><EFBFBD><E2A3BA><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>(members, longitudes, latitudes)<29><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ȱ<EFBFBD><C8B1><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
* 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);
/**
* <20><><EFBFBD>ַ<EFBFBD><D6B7><EFBFBD><EFBFBD><EFBFBD>ʽ<EFBFBD><CABD><EFBFBD><EFBFBD>ָ<EFBFBD><D6B8><EFBFBD><EFBFBD>Ա<EFBFBD><D4B1> GEOHASH ֵ
* Returns members of a geospatial index as standard geohash strings.
* @param key {const char*} <20><>Ӧ<EFBFBD>ļ<EFBFBD>ֵ
* the specified key
* @param memebers {const std::vector<string>&} <20><>Ա<EFBFBD><D4B1><EFBFBD><EFBFBD>
* the members array
* @param results {std::vector<string>&} <20><EFBFBD><E6B4A2><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
* store the result
* @return {bool} <20><><EFBFBD><EFBFBD><EFBFBD>Ƿ<EFBFBD><C7B7>ɹ<EFBFBD>
* if the operation was successful.
*/
bool geohash(const char* key, const std::vector<string>& members,
std::vector<string>& results);
/**
* <20><><EFBFBD>ַ<EFBFBD><D6B7><EFBFBD><EFBFBD><EFBFBD>ʽ<EFBFBD><CABD><EFBFBD><EFBFBD>ָ<EFBFBD><D6B8><EFBFBD><EFBFBD>Ա<EFBFBD><D4B1> GEOHASH ֵ
* Returns members of a geospatial index as standard geohash strings.
* @param key {const char*} <20><>Ӧ<EFBFBD>ļ<EFBFBD>ֵ
* the specified key
* @param memeber {const char*} <20><>Ա<EFBFBD><D4B1>
* the member of a geospatial index
* @param result {std::vector<string>&} <20><EFBFBD><E6B4A2><EFBFBD><EFBFBD>
* store the result
* @return {bool} <20><><EFBFBD><EFBFBD><EFBFBD>Ƿ<EFBFBD><C7B7>ɹ<EFBFBD>
* if the operation was successful.
*/
bool geohash(const char* key, const char* member, string& result);
/**
* <20><><EFBFBD><EFBFBD>ָ<EFBFBD><D6B8><EFBFBD><EFBFBD>Ա<EFBFBD>ĵ<EFBFBD><C4B5><EFBFBD>λ<EFBFBD><CEBB><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
* Returns longitude and latitude of members of a geospatial index
* @param key {const char*} <20><>Ӧ<EFBFBD>ļ<EFBFBD>ֵ
* the specified key
* @param memebers {const std::vector<string>&} <20><>Ա<EFBFBD><D4B1><EFBFBD><EFBFBD>
* the members array
* @param results {std::vector<std::pair<double, double> >&} <20><EFBFBD><E6B4A2><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
* store the results
* @return {bool} <20><><EFBFBD><EFBFBD><EFBFBD>Ƿ<EFBFBD><C7B7>ɹ<EFBFBD>
* if the operation was successful.
*/
bool geopos(const char* key, const std::vector<string>& members,
std::vector<std::pair<double, double> >& results);
/**
* <20><><EFBFBD><EFBFBD>ij<EFBFBD><C4B3>ָ<EFBFBD><D6B8><EFBFBD><EFBFBD>Ա<EFBFBD>ĵ<EFBFBD><C4B5><EFBFBD>λ<EFBFBD><CEBB><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
* Returns longitude and latitude of the one member of
* a geospatial index
* @param key {const char*} ָ<><D6B8><EFBFBD><EFBFBD>ֵ
* the specifed key
* @param member {const char*} ָ<><D6B8><EFBFBD><EFBFBD>Ա<EFBFBD><D4B1>
* the specified member
* @param result {std::pair<double, double>&} <20><EFBFBD><E6B4A2><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
* store the result of longitude and latitude of the member
* @return {bool} <20><><EFBFBD><EFBFBD><EFBFBD>Ƿ<EFBFBD><C7B7>ɹ<EFBFBD>
* if the operation was successful.
*/
bool geopos(const char* key, const char* member,
std::pair<double, double>& result);
/**
* <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>λ<EFBFBD><CEBB><EFBFBD><EFBFBD><EFBFBD><EFBFBD>֮<EFBFBD><D6AE><EFBFBD>ľ<EFBFBD><C4BE><EFBFBD>
* Returns the distance between two members of a geospatial index
* @param key {const char*} <20><>Ӧ<EFBFBD>ļ<EFBFBD>ֵ
* the specified key
* @param member1 {const char*} <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ա
* one member of a geospatial index
* @param member2 {const char*} <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ա
* another member of a geospatial index
* @param unit {int} <20><><EFBFBD>صľ<D8B5><C4BE><EFBFBD><EFBFBD>ĵ<EFBFBD>λֵ
* @return {double} <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>֮<EFBFBD><D6AE><EFBFBD>ij<EFBFBD><C4B3>ȣ<EFBFBD><C8A3><EFBFBD><EFBFBD><EFBFBD>ֵ < 0 <20><>ʾ<EFBFBD><CABE><EFBFBD><EFBFBD>
* 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);
/**
* <20><><EFBFBD>þ<EFBFBD><C3BE><EFBFBD>ijָ<C4B3><D6B8><EFBFBD><EFBFBD><EFBFBD><EFBFBD>λ<EFBFBD><CEBB><EFBFBD>ڸ<EFBFBD><DAB8><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Χ<EBB7B6>ڵ<EFBFBD><DAB5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
* Query a sorted set representing a geospatial index to fetch
* members matching a given maximum distance from a point
* @param key {const char*} <20><>Ӧ<EFBFBD>ļ<EFBFBD>ֵ
* the specified key
* @param longitude {double} ָ<><D6B8><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ľ<EFBFBD><C4BE><EFBFBD>ֵ
* the longitude of the specified geospatial coordinate
* @param latitude {double} ָ<><D6B8><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>γ<EFBFBD><CEB3>ֵ
* the latitude of the specified geospatial coordinate
* @param radius {double} <20>޶<EFBFBD><DEB6>ľ<EFBFBD><C4BE>Χ<EBB7B6><CEA7>С
* the distance from the specified coordinate
* @param unit {int} radius <20><><EFBFBD><EFBFBD><EFBFBD>ĵ<EFBFBD>λ<EFBFBD><CEBB><EFBFBD><EFBFBD>
* the unit type of the raidus
* @param with {int} <20><>ѯ<EFBFBD><D1AF><EFBFBD><EFBFBD>ѡ<EFBFBD><EFBFBD>μ<EFBFBD><CEBC><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ķ<EFBFBD><C4B6>壺GEO_WITH_XXX
* the serach operations, defined as GEO_WITH_XXX above
* @param sort {int} <20><>ѯ<EFBFBD><D1AF><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʽ<EFBFBD><CABD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>μ<EFBFBD><CEBC><EFBFBD>GEO_SORT_XXX
* the sorted type of the results, defined as GEO_SORT_XXX above
* @return {const std::vector<geo_member>&} <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ľ<EFBFBD><C4BD><EFBFBD><EFBFBD><EFBFBD>
* 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);
/**
* <20><><EFBFBD>þ<EFBFBD><C3BE><EFBFBD>ijָ<C4B3><D6B8><EFBFBD><EFBFBD><EFBFBD><EFBFBD>λ<EFBFBD><CEBB><EFBFBD>ڸ<EFBFBD><DAB8><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Χ<EBB7B6>ڵ<EFBFBD><DAB5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
* Query a sorted set representing a geospatial index to fetch
* members matching a given maximum distance from a member
* @param key {const char*} <20><>Ӧ<EFBFBD>ļ<EFBFBD>ֵ
* the specified key
* @param member {const char*} ij<><C4B3>ָ<EFBFBD><D6B8><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ա
* the specified member of a geospatial index
* @param radius {double} <20>޶<EFBFBD><DEB6>ľ<EFBFBD><C4BE>Χ<EBB7B6><CEA7>С
* the distance from the specified coordinate
* @param unit {int} radius <20><><EFBFBD><EFBFBD><EFBFBD>ĵ<EFBFBD>λ<EFBFBD><CEBB><EFBFBD><EFBFBD>
* the unit type of the raidus
* @param with {int} <20><>ѯ<EFBFBD><D1AF><EFBFBD><EFBFBD>ѡ<EFBFBD><EFBFBD>μ<EFBFBD><CEBC><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ķ<EFBFBD><C4B6>壺GEO_WITH_XXX
* the serach operations, defined as GEO_WITH_XXX above
* @param sort {int} <20><>ѯ<EFBFBD><D1AF><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʽ<EFBFBD><CABD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>μ<EFBFBD><CEBC><EFBFBD>GEO_SORT_XXX
* the sorted type of the results, defined as GEO_SORT_XXX above
* @return {const std::vector<geo_member>&} <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ľ<EFBFBD><C4BD><EFBFBD><EFBFBD><EFBFBD>
* 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