Skip to content

Commit

Permalink
compatible with redis protocol
Browse files Browse the repository at this point in the history
  • Loading branch information
leviathan authored and leviathan committed Feb 19, 2017
1 parent eadd063 commit 095579d
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 17 deletions.
2 changes: 2 additions & 0 deletions include/pika_geo.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
*/
enum Sort
{
Unsort, //default
Asc,
Desc
};
Expand Down Expand Up @@ -117,6 +118,7 @@ class GeoRadiusByMemberCmd : public Cmd {
range_.count = false;
range_.option_num = 0;
range_.count_limit = 0;
range_.sort = Unsort;
}
};

Expand Down
4 changes: 2 additions & 2 deletions src/pika_command.cc
Original file line number Diff line number Diff line change
Expand Up @@ -392,13 +392,13 @@ void InitCmdInfoTable() {
CmdInfo* geoaddptr = new CmdInfo(kCmdNameGeoAdd, -4, kCmdFlagsWrite | kCmdFlagsGeo);
cmd_infos.insert(std::pair<std::string, CmdInfo*>(kCmdNameGeoAdd, geoaddptr));
////GeoPos
CmdInfo* geoposptr = new CmdInfo(kCmdNameGeoPos, -3, kCmdFlagsRead | kCmdFlagsGeo);
CmdInfo* geoposptr = new CmdInfo(kCmdNameGeoPos, -2, kCmdFlagsRead | kCmdFlagsGeo);
cmd_infos.insert(std::pair<std::string, CmdInfo*>(kCmdNameGeoPos, geoposptr));
////GeoDist
CmdInfo* geodistptr = new CmdInfo(kCmdNameGeoDist, -3, kCmdFlagsRead | kCmdFlagsGeo);
cmd_infos.insert(std::pair<std::string, CmdInfo*>(kCmdNameGeoDist, geodistptr));
////GeoHash
CmdInfo* geohashptr = new CmdInfo(kCmdNameGeoHash, -3, kCmdFlagsRead | kCmdFlagsGeo);
CmdInfo* geohashptr = new CmdInfo(kCmdNameGeoHash, -2, kCmdFlagsRead | kCmdFlagsGeo);
cmd_infos.insert(std::pair<std::string, CmdInfo*>(kCmdNameGeoHash, geohashptr));
////GeoRadius
CmdInfo* georadiusptr = new CmdInfo(kCmdNameGeoRadius, -3, kCmdFlagsRead | kCmdFlagsGeo);
Expand Down
46 changes: 31 additions & 15 deletions src/pika_geo.cc
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ void GeoAddCmd::DoInitial(PikaCmdArgsType &argv, const CmdInfo* const ptr_info)
}
size_t argc = argv.size();
if ((argc - 2) % 3 != 0) {
res_.SetRes(CmdRes::kSyntaxErr);
res_.SetRes(CmdRes::kWrongNum, kCmdNameGeoAdd);
return;
}
key_ = argv[1];
Expand Down Expand Up @@ -126,7 +126,7 @@ void GeoPosCmd::Do() {
}
}

static double length_converter(double meters, std::string unit) {
static double length_converter(double meters, const std::string & unit) {
if (unit == "m") {
return meters;
} else if (unit == "km") {
Expand All @@ -140,21 +140,38 @@ static double length_converter(double meters, std::string unit) {
}
}

static bool check_unit(const std::string & unit) {
if (unit == "m" || unit == "km" || unit == "ft" || unit == "mi") {
return true;
} else {
return false;
}
}

void GeoDistCmd::DoInitial(PikaCmdArgsType &argv, const CmdInfo* const ptr_info) {
if (!ptr_info->CheckArg(argv.size())) {
res_.SetRes(CmdRes::kWrongNum, kCmdNameGeoDist);
return;
}
if (argv.size() < 4) {
res_.SetRes(CmdRes::kWrongNum, kCmdNameGeoDist);
return;
} else if (argv.size() > 5) {
res_.SetRes(CmdRes::kSyntaxErr);
return;
}
key_ = argv[1];
first_pos_ = argv[2];
second_pos_ = argv[3];
if (argv.size() == 5) {
unit_ = argv[4];
} else if (argv.size() > 5) {
res_.SetRes(CmdRes::kSyntaxErr);
} else {
unit_ = "m";
}
if (!check_unit(unit_)) {
res_.SetRes(CmdRes::kErrOther, "unsupported unit provided. please use m, km, ft, mi");
return;
}
}

void GeoDistCmd::Do() {
Expand Down Expand Up @@ -185,10 +202,6 @@ void GeoDistCmd::Do() {

double distance = geohashGetDistance(first_xy[0], first_xy[1], second_xy[0], second_xy[1]);
distance = length_converter(distance, unit_);
if (distance == -1) {
res_.SetRes(CmdRes::kErrOther, "unsupported unit provided. please use m, km, ft, mi");
return;
}
char buf[32];
int64_t len = slash::d2string(buf, sizeof(buf), distance);
res_.AppendStringLen(len);
Expand Down Expand Up @@ -258,7 +271,7 @@ static void GetAllNeighbors(std::string & key, GeoRange & range, CmdRes & res) {
nemo::Status s;
double longitude = range.longitude, latitude = range.latitude, distance = range.distance;
int count_limit = 0;

// Convert other units to meters
if (range.unit == "m") {
distance = distance;
} else if (range.unit == "km") {
Expand All @@ -268,8 +281,7 @@ static void GetAllNeighbors(std::string & key, GeoRange & range, CmdRes & res) {
} else if (range.unit == "mi") {
distance = distance * 1609.34;
} else {
res.SetRes(CmdRes::kErrOther, "unsupported unit provided. please use m, km, ft, mi");
return;
distance = -1;
}
// Search the zset for all matching points
GeoHashRadius georadius = geohashGetAreasByRadiusWGS84(longitude, latitude, distance);
Expand Down Expand Up @@ -344,10 +356,6 @@ static void GetAllNeighbors(std::string & key, GeoRange & range, CmdRes & res) {
geohashDecodeToLongLatWGS84(hash, xy);
double distance = geohashGetDistance(longitude, latitude, xy[0], xy[1]);
distance = length_converter(distance, range.unit);
if (distance == -1) {
res.SetRes(CmdRes::kErrOther, "unsupported unit provided. please use m, km, ft, mi");
return;
}
char buf[32];
int64_t len = slash::d2string(buf, sizeof(buf), distance);
res.AppendStringLen(len);
Expand Down Expand Up @@ -395,6 +403,10 @@ void GeoRadiusCmd::DoInitial(PikaCmdArgsType &argv, const CmdInfo* const ptr_inf
slash::string2d(argv[3].data(), argv[3].size(), &range_.latitude);
slash::string2d(argv[4].data(), argv[4].size(), &range_.distance);
range_.unit = argv[5];
if (!check_unit(range_.unit)) {
res_.SetRes(CmdRes::kErrOther, "unsupported unit provided. please use m, km, ft, mi");
return;
}
size_t pos = 6;
while (pos < argv.size()) {
if (!strcasecmp(argv[pos].c_str(), "withdist")) {
Expand Down Expand Up @@ -439,6 +451,10 @@ void GeoRadiusByMemberCmd::DoInitial(PikaCmdArgsType &argv, const CmdInfo* const
range_.member = argv[2];
slash::string2d(argv[3].data(), argv[3].size(), &range_.distance);
range_.unit = argv[4];
if (!check_unit(range_.unit)) {
res_.SetRes(CmdRes::kErrOther, "unsupported unit provided. please use m, km, ft, mi");
return;
}
size_t pos = 5;
while (pos < argv.size()) {
if (!strcasecmp(argv[pos].c_str(), "withdist")) {
Expand Down

0 comments on commit 095579d

Please sign in to comment.