|
7 | 7 | "strconv" |
8 | 8 |
|
9 | 9 | "github.com/dicedb/dice/internal/errors" |
10 | | - geoUtil "github.com/dicedb/dice/internal/geo" |
11 | 10 | "github.com/dicedb/dice/internal/object" |
12 | 11 | "github.com/dicedb/dice/internal/shardmanager" |
13 | 12 | dsstore "github.com/dicedb/dice/internal/store" |
@@ -73,46 +72,44 @@ func evalGEOADD(c *Cmd, s *dsstore.Store) (*CmdRes, error) { |
73 | 72 | } |
74 | 73 |
|
75 | 74 | key := c.C.Args[0] |
76 | | - geoHashs, members := []int64{}, []string{} |
77 | 75 | params, nonParams := parseParams(c.C.Args[1:]) |
78 | 76 |
|
79 | 77 | if len(nonParams)%3 != 0 { |
80 | 78 | return GEOADDResNilRes, errors.ErrWrongArgumentCount("GEOADD") |
81 | 79 | } |
82 | 80 |
|
| 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{} |
83 | 93 | for i := 0; i < len(nonParams); i += 3 { |
84 | 94 | lon, errLon := strconv.ParseFloat(nonParams[i], 10) |
85 | 95 | lat, errLat := strconv.ParseFloat(nonParams[i+1], 10) |
86 | 96 | if errLon != nil || errLat != nil { |
87 | 97 | return GEOADDResNilRes, errors.ErrInvalidNumberFormat |
88 | 98 | } |
89 | | - if err := geoUtil.ValidateLonLat(lon, lat); err != nil { |
| 99 | + coordinate, err := types.NewGeoCoordinateFromLonLat(lon, lat) |
| 100 | + if err != nil { |
90 | 101 | return GEOADDResNilRes, err |
91 | 102 | } |
92 | | - geoHash := geoUtil.EncodeHash(lon, lat) |
93 | | - |
94 | | - geoHashs = append(geoHashs, int64(geoHash)) |
| 103 | + GeoCoordinates = append(GeoCoordinates, coordinate) |
95 | 104 | members = append(members, nonParams[i+2]) |
96 | 105 | } |
97 | 106 |
|
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) |
111 | 108 | if err != nil { |
112 | 109 | return GEOADDResNilRes, err |
113 | 110 | } |
114 | 111 |
|
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)) |
116 | 113 | return newGEOADDRes(count), nil |
117 | 114 | } |
118 | 115 |
|
|
0 commit comments