Skip to content

Commit

Permalink
fix: fix response structure
Browse files Browse the repository at this point in the history
  • Loading branch information
c-harish authored and benbarten committed Nov 27, 2024
1 parent ccabe05 commit cc774f0
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 63 deletions.
9 changes: 3 additions & 6 deletions internal/eval/geo/geo.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down Expand Up @@ -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
Expand All @@ -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
}
}

Expand Down
85 changes: 28 additions & 57 deletions internal/eval/store_eval.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
}

Expand Down Expand Up @@ -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))
Expand All @@ -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 {
Expand Down Expand Up @@ -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,
}
}

0 comments on commit cc774f0

Please sign in to comment.