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

fix: geosearch and georadius response format #4420

Merged
merged 1 commit into from
Jan 7, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
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
21 changes: 14 additions & 7 deletions src/server/zset_family.cc
Original file line number Diff line number Diff line change
Expand Up @@ -67,10 +67,10 @@ struct GeoPoint {
double dist;
double score;
std::string member;
GeoPoint() : longitude(0.0), latitude(0.0), dist(0.0), score(0.0) {};
GeoPoint() : longitude(0.0), latitude(0.0), dist(0.0), score(0.0){};
GeoPoint(double _longitude, double _latitude, double _dist, double _score,
const std::string& _member)
: longitude(_longitude), latitude(_latitude), dist(_dist), score(_score), member(_member) {};
: longitude(_longitude), latitude(_latitude), dist(_dist), score(_score), member(_member){};
};
using GeoArray = std::vector<GeoPoint>;

Expand All @@ -86,6 +86,10 @@ struct GeoSearchOpts {
bool withhash = 0;
GeoStoreType store = GeoStoreType::kNoStore;
string_view store_key;

bool HasWithStatement() const {
return withdist || withcoord || withhash;
}
};

inline zrangespec GetZrangeSpec(bool reverse, const ZSetFamily::ScoreInterval& si) {
Expand Down Expand Up @@ -2135,8 +2139,8 @@ bool ValidateZMPopCommand(CmdArgList args, uint32* num_keys, bool* is_max, int*
}

if (!parser.Finalize()) {
builder->SendError(parser.Error()->MakeReply());
return false;
builder->SendError(parser.Error()->MakeReply());
return false;
}

return true;
Expand Down Expand Up @@ -3079,7 +3083,9 @@ void GeoSearchStoreGeneric(Transaction* tx, SinkReplyBuilder* builder, const Geo
rb->StartArray(ga.size());
for (const auto& p : ga) {
// [member, dist, x, y, hash]
rb->StartArray(record_size);
if (geo_ops.HasWithStatement()) {
rb->StartArray(record_size);
}
rb->SendBulkString(p.member);
if (geo_ops.withdist) {
rb->SendDouble(p.dist / geo_ops.conversion);
Expand Down Expand Up @@ -3137,6 +3143,7 @@ void ZSetFamily::GeoSearch(CmdArgList args, const CommandContext& cmd_cntx) {
// BYRADIUS or BYBOX is set
bool by_set = false;
auto* builder = cmd_cntx.rb;

for (size_t i = 1; i < args.size(); ++i) {
string cur_arg = absl::AsciiStrToUpper(ArgS(args, i));

Expand Down Expand Up @@ -3236,9 +3243,9 @@ void ZSetFamily::GeoSearch(CmdArgList args, const CommandContext& cmd_cntx) {
geo_ops.withcoord = true;
} else if (cur_arg == "WITHDIST") {
geo_ops.withdist = true;
} else if (cur_arg == "WITHHASH")
} else if (cur_arg == "WITHHASH") {
geo_ops.withhash = true;
else {
} else {
return builder->SendError(kSyntaxErr);
}
}
Expand Down
3 changes: 3 additions & 0 deletions src/server/zset_family_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1258,6 +1258,9 @@ TEST_F(ZSetFamilyTest, GeoSearch) {
RespArray(ElementsAre(DoubleArg(3.7038), DoubleArg(40.4168))))),
RespArray(ElementsAre("Lisbon", DoubleArg(502.20769462704084),
RespArray(ElementsAre(DoubleArg(9.1427), DoubleArg(38.7369))))))));

resp = Run({"GEOSEARCH", "Europe", "FROMMEMBER", "Madrid", "BYRADIUS", "700", "KM"});
EXPECT_THAT(resp, RespArray(ElementsAre("Madrid", "Lisbon")));
}

TEST_F(ZSetFamilyTest, GeoRadiusByMember) {
Expand Down
Loading