Skip to content
This repository was archived by the owner on Jun 23, 2025. It is now read-only.

Commit 84d9048

Browse files
committed
Created GEO type
1 parent 3fb8369 commit 84d9048

File tree

7 files changed

+928
-822
lines changed

7 files changed

+928
-822
lines changed

internal/cmd/cmd_geoadd.go

Lines changed: 17 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import (
77
"strconv"
88

99
"github.com/dicedb/dice/internal/errors"
10-
geoUtil "github.com/dicedb/dice/internal/geo"
1110
"github.com/dicedb/dice/internal/object"
1211
"github.com/dicedb/dice/internal/shardmanager"
1312
dsstore "github.com/dicedb/dice/internal/store"
@@ -73,46 +72,44 @@ func evalGEOADD(c *Cmd, s *dsstore.Store) (*CmdRes, error) {
7372
}
7473

7574
key := c.C.Args[0]
76-
geoHashs, members := []int64{}, []string{}
7775
params, nonParams := parseParams(c.C.Args[1:])
7876

7977
if len(nonParams)%3 != 0 {
8078
return GEOADDResNilRes, errors.ErrWrongArgumentCount("GEOADD")
8179
}
8280

81+
var gr *types.GeoRegistry
82+
obj := s.Get(key)
83+
if obj == nil {
84+
gr = types.NewGeoRegistry()
85+
} else {
86+
if obj.Type != object.ObjTypeSortedSet {
87+
return GEOADDResNilRes, errors.ErrWrongTypeOperation
88+
}
89+
gr = obj.Value.(*types.GeoRegistry)
90+
}
91+
92+
GeoCoordinates, members := []*types.GeoCoordinate{}, []string{}
8393
for i := 0; i < len(nonParams); i += 3 {
8494
lon, errLon := strconv.ParseFloat(nonParams[i], 10)
8595
lat, errLat := strconv.ParseFloat(nonParams[i+1], 10)
8696
if errLon != nil || errLat != nil {
8797
return GEOADDResNilRes, errors.ErrInvalidNumberFormat
8898
}
89-
if err := geoUtil.ValidateLonLat(lon, lat); err != nil {
99+
coordinate, err := types.NewGeoCoordinateFromLonLat(lon, lat)
100+
if err != nil {
90101
return GEOADDResNilRes, err
91102
}
92-
geoHash := geoUtil.EncodeHash(lon, lat)
93-
94-
geoHashs = append(geoHashs, int64(geoHash))
103+
GeoCoordinates = append(GeoCoordinates, coordinate)
95104
members = append(members, nonParams[i+2])
96105
}
97106

98-
var ss *types.SortedSet
99-
obj := s.Get(key)
100-
if obj == nil {
101-
ss = types.NewSortedSet()
102-
} else {
103-
if obj.Type != object.ObjTypeSortedSet {
104-
return GEOADDResNilRes, errors.ErrWrongTypeOperation
105-
}
106-
ss = obj.Value.(*types.SortedSet)
107-
}
108-
109-
// Note: Validation of the params is done in the types.SortedSet.ZADD method
110-
count, err := ss.ZADD(geoHashs, members, params)
107+
count, err := gr.Add(GeoCoordinates, members, params)
111108
if err != nil {
112109
return GEOADDResNilRes, err
113110
}
114111

115-
s.Put(key, s.NewObj(ss, -1, object.ObjTypeSortedSet), dsstore.WithPutCmd(dsstore.ZAdd))
112+
s.Put(key, s.NewObj(gr, -1, object.ObjTypeSortedSet), dsstore.WithPutCmd(dsstore.ZAdd))
116113
return newGEOADDRes(count), nil
117114
}
118115

internal/cmd/cmd_geodist.go

Lines changed: 4 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ package cmd
55

66
import (
77
"github.com/dicedb/dice/internal/errors"
8-
geoUtil "github.com/dicedb/dice/internal/geo"
98
"github.com/dicedb/dice/internal/object"
109
"github.com/dicedb/dice/internal/shardmanager"
1110
dsstore "github.com/dicedb/dice/internal/store"
@@ -68,39 +67,24 @@ func evalGEODIST(c *Cmd, s *dsstore.Store) (*CmdRes, error) {
6867
key := c.C.Args[0]
6968
params, nonParams := parseParams(c.C.Args[1:])
7069

71-
unit := getUnitTypeFromParsedParams(params)
70+
unit := types.GetUnitTypeFromParsedParams(params)
7271
if len(c.C.Args) == 4 && len(unit) == 0 {
7372
return GEODISTResNilRes, errors.ErrInvalidUnit(c.C.Args[3])
7473
} else if len(unit) == 0 {
7574
unit = types.M
7675
}
7776

78-
var ss *types.SortedSet
77+
var gr *types.GeoRegistry
7978
obj := s.Get(key)
8079
if obj == nil {
8180
return GEODISTResNilRes, nil
8281
}
83-
8482
if obj.Type != object.ObjTypeSortedSet {
8583
return GEODISTResNilRes, errors.ErrWrongTypeOperation
8684
}
87-
ss = obj.Value.(*types.SortedSet)
88-
89-
node1 := ss.GetByKey(nonParams[0])
90-
node2 := ss.GetByKey(nonParams[1])
91-
92-
// @doubt - Should return error here?
93-
if node1 == nil || node2 == nil {
94-
return GEODISTResNilRes, nil
95-
}
96-
97-
hash1 := node1.Score()
98-
hash2 := node2.Score()
99-
100-
lon1, lat1 := geoUtil.DecodeHash(uint64(hash1))
101-
lon2, lat2 := geoUtil.DecodeHash(uint64(hash2))
85+
gr = obj.Value.(*types.GeoRegistry)
10286

103-
dist, err := geoUtil.ConvertDistance(geoUtil.GetDistance(lon1, lat1, lon2, lat2), unit)
87+
dist, err := gr.GetDistanceBetweenMembers(nonParams[0], nonParams[1], unit)
10488

10589
if err != nil {
10690
return GEODISTResNilRes, err

0 commit comments

Comments
 (0)