Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support GEO #59

Merged
merged 8 commits into from
Feb 21, 2017
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
bugfix
  • Loading branch information
leviathan authored and leviathan committed Feb 20, 2017
commit 1347b5517cb2551b0be2a3ebc4999be64d832d8d
2 changes: 1 addition & 1 deletion include/pika_command.h
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ enum CmdFlagsMask {
kCmdFlagsMaskLocal = 32,
kCmdFlagsMaskSuspend = 64,
kCmdFlagsMaskPrior = 128,
kCmdFlagsMaskAdminRequire = 255
kCmdFlagsMaskAdminRequire = 256
};

enum CmdFlags {
Expand Down
8 changes: 4 additions & 4 deletions src/pika_command.cc
Original file line number Diff line number Diff line change
Expand Up @@ -389,22 +389,22 @@ void InitCmdInfoTable() {

//GEO
////GeoAdd
CmdInfo* geoaddptr = new CmdInfo(kCmdNameGeoAdd, -4, kCmdFlagsWrite | kCmdFlagsGeo);
CmdInfo* geoaddptr = new CmdInfo(kCmdNameGeoAdd, -5, kCmdFlagsWrite | kCmdFlagsGeo);
cmd_infos.insert(std::pair<std::string, CmdInfo*>(kCmdNameGeoAdd, geoaddptr));
////GeoPos
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);
CmdInfo* geodistptr = new CmdInfo(kCmdNameGeoDist, -4, kCmdFlagsRead | kCmdFlagsGeo);
cmd_infos.insert(std::pair<std::string, CmdInfo*>(kCmdNameGeoDist, geodistptr));
////GeoHash
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);
CmdInfo* georadiusptr = new CmdInfo(kCmdNameGeoRadius, -6, kCmdFlagsRead | kCmdFlagsGeo);
cmd_infos.insert(std::pair<std::string, CmdInfo*>(kCmdNameGeoRadius, georadiusptr));
////GeoRadiusByMember
CmdInfo* georadiusbymemberptr = new CmdInfo(kCmdNameGeoRadiusByMember, -3, kCmdFlagsRead | kCmdFlagsGeo);
CmdInfo* georadiusbymemberptr = new CmdInfo(kCmdNameGeoRadiusByMember, -5, kCmdFlagsRead | kCmdFlagsGeo);
cmd_infos.insert(std::pair<std::string, CmdInfo*>(kCmdNameGeoRadiusByMember, georadiusbymemberptr));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这里CmdInfo代表应该有的参数个数,包括命令名在内,负数代表最少参数个数,会在Cmd DoIntial时检查

几个命令的这个值是不是有问题,比如geoadd 应该是-5,geoadd key longitude latitude member; geopos应该是-3; geodist -4; geohash -3; georadius -6; georadiusbymember -5

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

geoadd参数最少个数为-5
geopos参数最少个数为-2

127.0.0.1:6379> geoadd xj 1.0 2.0 kt
(integer) 0
127.0.0.1:6379> geopos xj 
(empty list or set)

geodist参数最少个数为-4
geohash参数最少个数为-2

127.0.0.1:6379> geohash xj
(empty list or set)

georadius参数最少个数为-6
georadiusbymember参数最少个数为-5

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

好的

}

Expand Down
27 changes: 4 additions & 23 deletions src/pika_geo.cc
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@ void GeoAddCmd::DoInitial(PikaCmdArgsType &argv, const CmdInfo* const ptr_info)
void GeoAddCmd::Do() {
nemo::Status s;
int64_t count = 0, ret;
bool exist = false;
const std::shared_ptr<nemo::Nemo> db = g_pika_server->db();
std::vector<GeoPoint>::const_iterator iter = pos_.begin();
for (; iter != pos_.end(); iter++) {
Expand All @@ -58,20 +57,12 @@ void GeoAddCmd::Do() {
geohashEncodeWGS84(iter->longitude, iter->latitude, GEO_STEP_MAX, &hash);
GeoHashFix52Bits bits = geohashAlign52Bits(hash);
// Convert uint64 to double
double score;
std::string str_bits = std::to_string(bits);
double previous_score, score;
slash::string2d(str_bits.data(), str_bits.size(), &score);
s = db->ZScore(key_, iter->member, &previous_score);
if (s.ok()) {
exist = true;
} else if (s.IsNotFound()){
exist = false;
}
s = db->ZAdd(key_, score, iter->member, &ret);
if (s.ok() && !exist) {
count += 1;
} else if (s.ok() && exist) {

if (s.ok()) {
count += ret;
} else {
res_.SetRes(CmdRes::kErrOther, s.ToString());
return;
Expand Down Expand Up @@ -296,7 +287,7 @@ static void GetAllNeighbors(std::string & key, GeoRange & range, CmdRes & res) {
neighbors[7] = georadius.neighbors.south_east;
neighbors[8] = georadius.neighbors.south_west;

// For each neighbor (*and* our own hashbox), get all the matching
// For each neighbor, get all the matching
// members and add them to the potential result list.
std::vector<NeighborPoint> result;
for (size_t i = 0; i < sizeof(neighbors) / sizeof(*neighbors); i++) {
Expand Down Expand Up @@ -393,11 +384,6 @@ void GeoRadiusCmd::DoInitial(PikaCmdArgsType &argv, const CmdInfo* const ptr_inf
res_.SetRes(CmdRes::kWrongNum, kCmdNameGeoRadius);
return;
}
size_t argc = argv.size();
if (argc < 6) {
res_.SetRes(CmdRes::kWrongNum, kCmdNameGeoRadius);
return;
}
key_ = argv[1];
slash::string2d(argv[2].data(), argv[2].size(), &range_.longitude);
slash::string2d(argv[3].data(), argv[3].size(), &range_.latitude);
Expand Down Expand Up @@ -442,11 +428,6 @@ void GeoRadiusByMemberCmd::DoInitial(PikaCmdArgsType &argv, const CmdInfo* const
res_.SetRes(CmdRes::kWrongNum, kCmdNameGeoRadius);
return;
}
size_t argc = argv.size();
if (argc < 5) {
res_.SetRes(CmdRes::kWrongNum, kCmdNameGeoRadius);
return;
}
key_ = argv[1];
range_.member = argv[2];
slash::string2d(argv[3].data(), argv[3].size(), &range_.distance);
Expand Down