From 66e5a16543c1bca4099760a6129464fa81227081 Mon Sep 17 00:00:00 2001 From: Leviathan Date: Tue, 28 Mar 2017 12:35:21 +0800 Subject: [PATCH] bugfix GEO (#77) * Update hyperloglog test example * Modify test * bugfix geo && update test --- pikatests/tests/unit/geo.tcl | 26 +++++++++++++------------- src/pika_geo.cc | 21 ++++++++++++++------- 2 files changed, 27 insertions(+), 20 deletions(-) diff --git a/pikatests/tests/unit/geo.tcl b/pikatests/tests/unit/geo.tcl index 44a0ae1b..1e05ead5 100644 --- a/pikatests/tests/unit/geo.tcl +++ b/pikatests/tests/unit/geo.tcl @@ -76,7 +76,7 @@ start_server {tags {"geo"}} { foo bar "luck market" } err set err - } {*valid*} + } {ERR value is not an float} test {GEOADD multi add} { r geoadd nyc -73.9733487 40.7648057 "central park n/q/r" -73.9903085 40.7362513 "union square" -74.0131604 40.7126674 "wtc one" -73.7858139 40.6428986 "jfk" -73.9375699 40.7498929 "q4" -73.9564142 40.7480973 4545 @@ -84,7 +84,7 @@ start_server {tags {"geo"}} { test {Check geoset values} { r zrange nyc 0 -1 withscores - } {{wtc one} 1791873972053020 {union square} 1791875485187452 {central park n/q/r} 1791875761332224 4545 1791875796750882 {lic market} 1791875804419201 q4 1791875830079666 jfk 1791895905559723} + } {{wtc one} 27341826966 {union square} 27341850054 {central park n/q/r} 27341854268 4545 27341854808 {lic market} 27341854925 q4 27341855317 jfk 27342161644} test {GEORADIUS simple (sorted)} { r georadius nyc -73.9798091 40.7598464 3 km asc @@ -92,11 +92,11 @@ start_server {tags {"geo"}} { test {GEORADIUS withdist (sorted)} { r georadius nyc -73.9798091 40.7598464 3 km withdist asc - } {{{central park n/q/r} 0.7750} {4545 2.3651} {{union square} 2.7697}} + } {{{central park n/q/r} 0.7953} {4545 2.3672} {{union square} 2.7844}} test {GEORADIUS with COUNT} { r georadius nyc -73.9798091 40.7598464 10 km COUNT 3 - } {{central park n/q/r} 4545 {union square}} + } {{wtc one} {union square} {central park n/q/r}} test {GEORADIUS with COUNT but missing integer argument} { catch {r georadius nyc -73.9798091 40.7598464 10 km COUNT} e @@ -118,14 +118,14 @@ start_server {tags {"geo"}} { test {GEORADIUSBYMEMBER withdist (sorted)} { r georadiusbymember nyc "wtc one" 7 km withdist - } {{{wtc one} 0.0000} {{union square} 3.2544} {{central park n/q/r} 6.7000} {4545 6.1975} {{lic market} 6.8969}} + } {{{wtc one} 0.0000} {{union square} 3.1908} {{central park n/q/r} 6.6785} {4545 6.1410} {{lic market} 6.8411}} test {GEOHASH is able to return geohash strings} { # Example from Wikipedia. r del points r geoadd points -5.6 42.6 test lindex [r geohash points test] 0 - } {ezs42e44yx0} + } {ezs42e4d0j0} test {GEOPOS simple} { r del points @@ -149,7 +149,7 @@ start_server {tags {"geo"}} { r geoadd points 13.361389 38.115556 "Palermo" \ 15.087269 37.502669 "Catania" set m [r geodist points Palermo Catania] - assert {$m > 166274 && $m < 166275} + assert {$m > 166285 && $m < 166287} set km [r geodist points Palermo Catania km] assert {$km > 166.2 && $km < 166.3} } @@ -209,14 +209,14 @@ start_server {tags {"geo"}} { r del points r geoadd points 13.361389 38.115556 "Palermo" \ 15.087269 37.502669 "Catania" - r georadius points 13.361389 38.115556 500 km storedist points2 asc count 1 - assert {[r zcard points2] == 1} - set res [r zrange points2 0 -1 withscores] + r georadius points 13.361389 38.115556 500 km storedist points3 asc count 1 + assert {[r zcard points3] == 1} + set res [r zrange points3 0 -1 withscores] assert {[lindex $res 0] eq "Palermo"} - r georadius points 13.361389 38.115556 500 km storedist points2 desc count 1 - assert {[r zcard points2] == 1} - set res [r zrange points2 0 -1 withscores] + r georadius points 13.361389 38.115556 500 km storedist points4 desc count 1 + assert {[r zcard points4] == 1} + set res [r zrange points4 0 -1 withscores] assert {[lindex $res 0] eq "Catania"} } diff --git a/src/pika_geo.cc b/src/pika_geo.cc index f26e9301..91945715 100644 --- a/src/pika_geo.cc +++ b/src/pika_geo.cc @@ -194,8 +194,8 @@ void GeoDistCmd::Do() { double distance = geohashGetDistance(first_xy[0], first_xy[1], second_xy[0], second_xy[1]); distance = length_converter(distance, unit_); char buf[32]; - int64_t len = slash::d2string(buf, sizeof(buf), distance); - res_.AppendStringLen(len); + sprintf(buf, "%.4f", distance); + res_.AppendStringLen(strlen(buf)); res_.AppendContent(buf); } @@ -290,6 +290,7 @@ static void GetAllNeighbors(std::string & key, GeoRange & range, CmdRes & res) { // For each neighbor, get all the matching // members and add them to the potential result list. std::vector result; + int last_processed = 0; for (size_t i = 0; i < sizeof(neighbors) / sizeof(*neighbors); i++) { GeoHashFix52Bits min, max; if (HASHISZERO(neighbors[i])) @@ -297,7 +298,11 @@ static void GetAllNeighbors(std::string & key, GeoRange & range, CmdRes & res) { min = geohashAlign52Bits(neighbors[i]); neighbors[i].bits++; max = geohashAlign52Bits(neighbors[i]); - + // When a huge Radius (in the 5000 km range or more) is used, + // adjacent neighbors can be the same, so need to remove duplicated elements + if(last_processed && neighbors[i].bits == neighbors[last_processed].bits && neighbors[i].step == neighbors[last_processed].step) { + continue; + } std::vector sm_v; s = g_pika_server->db()->ZRangebyscore(key, (double)min, (double)max, sm_v, false, false); if (!s.ok()) { @@ -317,10 +322,12 @@ static void GetAllNeighbors(std::string & key, GeoRange & range, CmdRes & res) { result.push_back(item); } } + last_processed = i; } + // If using the count opiton if (range.count) { - count_limit = range.count_limit; + count_limit = result.size() < range.count_limit ? result.size() : range.count_limit; } else { count_limit = result.size(); } @@ -366,9 +373,9 @@ 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); - char buf[32]; - int64_t len = slash::d2string(buf, sizeof(buf), distance); - res.AppendStringLen(len); + char buf[32]; + sprintf(buf, "%.4f", distance); + res.AppendStringLen(strlen(buf)); res.AppendContent(buf); } // If using withcoord option -- GitLab