From cc774f0f24ae458793d8194030f67317a7c3f801 Mon Sep 17 00:00:00 2001 From: c-harish Date: Sat, 23 Nov 2024 14:22:29 +0530 Subject: [PATCH] fix: fix response structure --- internal/eval/geo/geo.go | 9 ++-- internal/eval/store_eval.go | 85 ++++++++++++------------------------- 2 files changed, 31 insertions(+), 63 deletions(-) diff --git a/internal/eval/geo/geo.go b/internal/eval/geo/geo.go index 1affbd950..872b15b6e 100644 --- a/internal/eval/geo/geo.go +++ b/internal/eval/geo/geo.go @@ -3,7 +3,7 @@ package geo import ( "math" - "github.com/dicedb/dice/internal/errors" + diceerrors "github.com/dicedb/dice/internal/errors" "github.com/mmcloughlin/geohash" ) @@ -85,10 +85,7 @@ func DecodeHash(hash float64) (lat, lon float64) { } // ConvertDistance converts a distance from meters to the desired unit -func ConvertDistance( - distance float64, - unit string, -) (converted float64, err []byte) { +func ConvertDistance(distance float64, unit string) (float64, error) { switch Unit(unit) { case Meters: return distance, nil @@ -99,7 +96,7 @@ func ConvertDistance( case Feet: return distance / 0.3048, nil default: - return 0, errors.NewErrWithMessage("ERR unsupported unit provided. please use m, km, ft, mi") + return 0, diceerrors.ErrUnsupportedUnit } } diff --git a/internal/eval/store_eval.go b/internal/eval/store_eval.go index eccfbbbf5..e015943b2 100644 --- a/internal/eval/store_eval.go +++ b/internal/eval/store_eval.go @@ -6318,12 +6318,12 @@ func evalGEODIST(args []string, store *dstore.Store) *EvalResponse { distance := geo.GetDistance(lon1, lat1, lon2, lat2) - result, err := geo.ConvertDistance(distance, unit) + result, conversionErr := geo.ConvertDistance(distance, unit) - if err != nil { + if conversionErr != nil { return &EvalResponse{ Result: nil, - Error: diceerrors.ErrWrongTypeOperation, + Error: conversionErr, } } @@ -7112,6 +7112,8 @@ func evalGEORADIUSBYMEMBER(args []string, store *dstore.Store) *EvalResponse { rangeMembers, rangeHashes := ss.GetMemberScoresInRange(float64(hashMin), float64(hashMax), count, anyMax) members = append(members, rangeMembers...) hashes = append(hashes, rangeHashes...) + count += len(rangeMembers) + lastProcessed = hash } dists := make([]float64, 0, len(members)) @@ -7125,7 +7127,14 @@ func evalGEORADIUSBYMEMBER(args []string, store *dstore.Store) *EvalResponse { if opts.WithDist || opts.IsSorted { dist := geo.GetDistance(centerLon, centerLat, msLon, msLat) - dists = append(dists, dist) + distance, err := geo.ConvertDistance(dist, unit) + if err != nil { + return &EvalResponse{ + Result: nil, + Error: err, + } + } + dists = append(dists, distance) } if opts.WithCoord { @@ -7156,69 +7165,31 @@ func evalGEORADIUSBYMEMBER(args []string, store *dstore.Store) *EvalResponse { } } - optCount := 0 - if opts.WithDist { - optCount++ - } - - if opts.WithHash { - optCount++ - } - - if opts.WithCoord { - optCount++ - } - - max := opts.Count - if max > len(members) { - max = len(members) - } - - if optCount == 0 { - response := make([]string, len(members)) - for i := range members { - response[i] = members[indices[i]] - } - - if max > 0 { - response = response[:max] - } - - return &EvalResponse{ - Result: clientio.Encode(response, false), - } + var countVal int + if opts.Count == 0 { + countVal = len(members) + } else { + countVal = opts.Count } - response := make([][]interface{}, len(members)) - for i := range members { - item := make([]any, optCount+1) - item[0] = members[i] - - itemIdx := 1 - + response := make([][]interface{}, 0, min(len(members), countVal)) + for i := 0; i < cap(response); i++ { + member := []interface{}{} + member = append(member, members[indices[i]]) if opts.WithDist { - item[itemIdx] = dists[i] - itemIdx++ + member = append(member, dists[indices[i]]) } - if opts.WithHash { - item[itemIdx] = hashes[i] - itemIdx++ + member = append(member, hashes[indices[i]]) } - if opts.WithCoord { - item[itemIdx] = coords[i] - itemIdx++ + member = append(member, coords[indices[i]]) } - - response[indices[i]] = item - } - - if max > 0 { - response = response[:max] + response = append(response, member) } return &EvalResponse{ - Result: clientio.Encode(response, false), + Result: response, + Error: nil, } }