Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/set circle support #745

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions core/commands.json
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,23 @@
"type": "string"
}
]
},
{
"name": "CIRCLE",
"arguments": [
{
"name": "lat",
"type": "double"
},
{
"name": "lon",
"type": "double"
},
{
"name": "meters",
"type": "double"
}
]
}
]
}
Expand Down
17 changes: 17 additions & 0 deletions core/commands_gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,23 @@ var commandsJSON = `{
"type": "string"
}
]
},
{
"name": "CIRCLE",
"arguments": [
{
"name": "lat",
"type": "double"
},
{
"name": "lon",
"type": "double"
},
{
"name": "meters",
"type": "double"
}
]
}
]
}
Expand Down
4 changes: 2 additions & 2 deletions internal/collection/collection.go
Original file line number Diff line number Diff line change
Expand Up @@ -353,8 +353,8 @@ func (c *Collection) SearchValuesRange(start, end string, desc bool,
return keepon
}

pstart := object.New("", String(start), 0, field.List{})
pend := object.New("", String(end), 0, field.List{})
pstart := object.New("", object.String(start), 0, field.List{})
pend := object.New("", object.String(end), 0, field.List{})
if desc {
// descend range
c.values.Descend(pstart, func(item *object.Object) bool {
Expand Down
28 changes: 14 additions & 14 deletions internal/collection/collection_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,14 +80,14 @@ func toFields(fNames, fValues []string) field.List {
func TestCollectionSet(t *testing.T) {
t.Run("AddString", func(t *testing.T) {
c := New()
str1 := String("hello")
str1 := object.String("hello")
old := c.Set(object.New("str", str1, 0, field.List{}))
expect(t, old == nil)
})
t.Run("UpdateString", func(t *testing.T) {
c := New()
str1 := String("hello")
str2 := String("world")
str1 := object.String("hello")
str2 := object.String("world")
old := c.Set(object.New("str", str1, 0, field.List{}))
expect(t, old == nil)
old = c.Set(object.New("str", str2, 0, field.List{}))
Expand All @@ -110,15 +110,15 @@ func TestCollectionSet(t *testing.T) {
})
t.Run("Fields", func(t *testing.T) {
c := New()
str1 := String("hello")
str1 := object.String("hello")

fNames := []string{"a", "b", "c"}
fValues := []string{"1", "2", "3"}
fields1 := toFields(fNames, fValues)
old := c.Set(object.New("str", str1, 0, fields1))
expect(t, old == nil)

str2 := String("hello")
str2 := object.String("hello")

fNames = []string{"d", "e", "f"}
fValues = []string{"4", "5", "6"}
Expand All @@ -138,8 +138,8 @@ func TestCollectionSet(t *testing.T) {
t.Run("Delete", func(t *testing.T) {
c := New()

c.Set(object.New("1", String("1"), 0, field.List{}))
c.Set(object.New("2", String("2"), 0, field.List{}))
c.Set(object.New("1", object.String("1"), 0, field.List{}))
c.Set(object.New("2", object.String("2"), 0, field.List{}))
c.Set(object.New("3", PO(1, 2), 0, field.List{}))

expect(t, c.Count() == 3)
Expand Down Expand Up @@ -196,7 +196,7 @@ func TestCollectionScan(t *testing.T) {
c := New()
for _, i := range rand.Perm(N) {
id := fmt.Sprintf("%04d", i)
c.Set(object.New(id, String(id), 0, makeFields(
c.Set(object.New(id, object.String(id), 0, makeFields(
field.Make("ex", id),
)))
}
Expand Down Expand Up @@ -292,7 +292,7 @@ func TestCollectionSearch(t *testing.T) {
for i, j := range rand.Perm(N) {
id := fmt.Sprintf("%04d", j)
ex := fmt.Sprintf("%04d", i)
c.Set(object.New(id, String(ex),
c.Set(object.New(id, object.String(ex),
0, makeFields(
field.Make("i", ex),
field.Make("j", id),
Expand Down Expand Up @@ -351,11 +351,11 @@ func TestCollectionSearch(t *testing.T) {

func TestCollectionWeight(t *testing.T) {
c := New()
c.Set(object.New("1", String("1"), 0, field.List{}))
c.Set(object.New("1", object.String("1"), 0, field.List{}))
expect(t, c.TotalWeight() > 0)
c.Delete("1")
expect(t, c.TotalWeight() == 0)
c.Set(object.New("1", String("1"), 0,
c.Set(object.New("1", object.String("1"), 0,
toFields(
[]string{"a", "b", "c"},
[]string{"1", "2", "3"},
Expand All @@ -364,19 +364,19 @@ func TestCollectionWeight(t *testing.T) {
expect(t, c.TotalWeight() > 0)
c.Delete("1")
expect(t, c.TotalWeight() == 0)
c.Set(object.New("1", String("1"), 0,
c.Set(object.New("1", object.String("1"), 0,
toFields(
[]string{"a", "b", "c"},
[]string{"1", "2", "3"},
),
))
c.Set(object.New("2", String("2"), 0,
c.Set(object.New("2", object.String("2"), 0,
toFields(
[]string{"d", "e", "f"},
[]string{"4", "5", "6"},
),
))
c.Set(object.New("1", String("1"), 0,
c.Set(object.New("1", object.String("1"), 0,
toFields(
[]string{"d", "e", "f"},
[]string{"4", "5", "6"},
Expand Down
4 changes: 2 additions & 2 deletions internal/object/object_binary.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,8 +100,8 @@ func (o *Object) String() string {
}

func (o *Object) IsSpatial() bool {
_, ok := o.geo().(geojson.Spatial)
return ok
_, ok := o.geo().(String)
Copy link
Contributor Author

@undeadcat undeadcat Jun 10, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This change (and consequently, the move of this file to collection package) was due to:

  • Circle does not implement geojson.Spatial interface, yet we do want to store it in the geo tree
  • geojson.Spatial does not seem to be used as an interface to call methods in this project, only as a 'marker interface'

I went with explicitly marking that we are not interested in strings here instead of using Spatial.

Alternatively, could possibly implement geojson.Spatial in Circle, open to other ideas as welll...

return !ok
}

func (o *Object) Weight() int {
Expand Down
88 changes: 0 additions & 88 deletions internal/object/object_struct.go

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package collection
package object

import (
"encoding/json"
Expand Down
21 changes: 20 additions & 1 deletion internal/server/crud.go
Original file line number Diff line number Diff line change
Expand Up @@ -671,7 +671,7 @@ func (s *Server) cmdSET(msg *Message) (resp.Value, commandDetails, error) {
}
str := args[i+1]
i += 1
oobj = collection.String(str)
oobj = object.String(str)
case "point":
if i+2 >= len(args) {
return retwerr(errInvalidNumberOfArguments)
Expand Down Expand Up @@ -739,6 +739,25 @@ func (s *Server) cmdSET(msg *Message) (resp.Value, commandDetails, error) {
if err != nil {
return retwerr(err)
}
case "circle":
if i+3 >= len(args) {
return retwerr(errInvalidNumberOfArguments)
}
slat, slon, smeters := args[i+1], args[i+2], args[i+3]
i += 3
lat, err := strconv.ParseFloat(slat, 64)
if err != nil {
return retwerr(errInvalidArgument(slat))
}
lon, err := strconv.ParseFloat(slon, 64)
if err != nil {
return retwerr(errInvalidArgument(slon))
}
meters, err := strconv.ParseFloat(smeters, 64)
if err != nil || meters < 0 {
return retwerr(errInvalidArgument(smeters))
}
oobj = geojson.NewCircle(geometry.Point{X: lon, Y: lat}, meters, defaultCircleSteps)
default:
return retwerr(errInvalidArgument(args[i]))
}
Expand Down
4 changes: 2 additions & 2 deletions internal/server/fence.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,8 @@ func appendHookDetails(b []byte, hookName string, metas []FenceMeta) []byte {
}

func objIsSpatial(obj geojson.Object) bool {
_, ok := obj.(geojson.Spatial)
return ok
_, ok := obj.(object.String)
return !ok
}

func hookJSONString(hookName string, metas []FenceMeta) string {
Expand Down
4 changes: 2 additions & 2 deletions internal/server/json.go
Original file line number Diff line number Diff line change
Expand Up @@ -299,7 +299,7 @@ func (s *Server) cmdJset(msg *Message) (res resp.Value, d commandDetails, err er
if createcol {
s.cols.Set(key, col)
}
var oobj geojson.Object = collection.String(json)
var oobj geojson.Object = object.String(json)
obj := object.New(id, oobj, 0, fields)
col.Set(obj)

Expand Down Expand Up @@ -368,7 +368,7 @@ func (s *Server) cmdJdel(msg *Message) (res resp.Value, d commandDetails, err er
return s.cmdSET(&nmsg)
}

var oobj geojson.Object = collection.String(json)
var oobj geojson.Object = object.String(json)
obj := object.New(id, oobj, 0, fields)
col.Set(obj)

Expand Down
11 changes: 11 additions & 0 deletions tests/keys_search_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,7 @@ func keys_WITHIN_test(mc *mockServer) error {
{"SET", "mykey", "point6", "POINT", -5, 5}, {"OK"},
{"SET", "mykey", "point7", "POINT", 33, 21}, {"OK"},
{"SET", "mykey", "poly8", "OBJECT", `{"type":"Polygon","coordinates":[[[-122.4408378,37.7341129],[-122.4408378,37.733],[-122.44,37.733],[-122.44,37.7341129],[-122.4408378,37.7341129]],[[-122.44060993194579,37.73345766902749],[-122.44044363498686,37.73345766902749],[-122.44044363498686,37.73355524732416],[-122.44060993194579,37.73355524732416],[-122.44060993194579,37.73345766902749]],[[-122.44060724973677,37.7336888869566],[-122.4402102828026,37.7336888869566],[-122.4402102828026,37.7339752567853],[-122.44060724973677,37.7339752567853],[-122.44060724973677,37.7336888869566]]]}`}, {"OK"},

{"WITHIN", "mykey", "IDS", "OBJECT",
`{
"type": "Polygon",
Expand Down Expand Up @@ -354,6 +355,11 @@ func keys_INTERSECTS_test(mc *mockServer) error {
{"SET", "key7", "multipoly17.1", "OBJECT", `{"type":"MultiPolygon","coordinates":[[[[-122.4407172203064,37.73270249351328],[-122.44049191474916,37.73270249351328],[-122.44049191474916,37.73286371140448],[-122.4407172203064,37.73286371140448],[-122.4407172203064,37.73270249351328]]],[[[-122.44032025337218,37.73267703802467],[-122.44013786315918,37.73267703802467],[-122.44013786315918,37.732838255971316],[-122.44032025337218,37.732838255971316],[-122.44032025337218,37.73267703802467]]]]}`}, {"OK"},
{"INTERSECTS", "key7", "IDS", "GET", "mykey", "multipoly5"}, {
"[0 [multipoly15 multipoly17 multipoly16]]"},

{"SET", "mykey", "circle_small", "CIRCLE", 59.438829, 24.741101, 1000}, {"OK"},
{"SET", "mykey", "circle_large", "CIRCLE", 59.438829, 24.741101, 10000}, {"OK"},
{"INTERSECTS", "mykey", "OBJECTS", "POINT", 59.441398, 24.748075}, {"[0 [[circle_large {\"type\":\"Feature\",\"geometry\":{\"type\":\"Point\",\"coordinates\":[24.741101,59.438829]},\"properties\":{\"type\":\"Circle\",\"radius\":10000,\"radius_units\":\"m\"}}] [circle_small {\"type\":\"Feature\",\"geometry\":{\"type\":\"Point\",\"coordinates\":[24.741101,59.438829]},\"properties\":{\"type\":\"Circle\",\"radius\":1000,\"radius_units\":\"m\"}}]]]"},
{"INTERSECTS", "mykey", "IDS", "POINT", 59.415876, 24.803417}, {"[0 [circle_large]]"},
})
}

Expand Down Expand Up @@ -460,6 +466,11 @@ func keys_WITHIN_CIRCLE_test(mc *mockServer) error {
"[0 [2 1 5 4 3]]"},
{"WITHIN", "mykey", "IDS", "CIRCLE", 37.7335, -122.4412, 10}, {
"[0 [2 1]]"},
{"SET", "mykey", "100m_circle", "CIRCLE", 37.7335, -122.4412, 100}, {"OK"},
{"SET", "mykey", "1km_circle", "CIRCLE", 37.7335, -122.4412, 1000}, {"OK"},
// TODO. this fails. should not contain circle with larger radius
{"WITHIN", "mykey", "IDS", "CIRCLE", 37.7335, -122.4412, 200}, {
"[0 [100m_circle 2 1 5 4 3]]"},
})
}

Expand Down
14 changes: 14 additions & 0 deletions tests/keys_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ func subTestKeys(g *testGroup) {
g.regSubTest("KEYS", keys_KEYS_test)
g.regSubTest("PERSIST", keys_PERSIST_test)
g.regSubTest("SET", keys_SET_test)
g.regSubTest("SET CIRCLE", keys_SET_CIRCLE_test)
g.regSubTest("STATS", keys_STATS_test)
g.regSubTest("TTL", keys_TTL_test)
g.regSubTest("SET EX", keys_SET_EX_test)
Expand Down Expand Up @@ -367,6 +368,19 @@ func keys_SET_test(mc *mockServer) error {
)
}

func keys_SET_CIRCLE_test(mc *mockServer) error {
return mc.DoBatch(
Do("SET", "mykey", "myid", "CIRCLE", 33, -115, 100000).OK(),
Do("GET", "mykey", "myid", "POINT").Str("[33 -115]"),
Do("GET", "mykey", "myid", "OBJECT").Str(`{"type":"Feature","geometry":{"type":"Point","coordinates":[-115,33]},"properties":{"type":"Circle","radius":100000,"radius_units":"m"}}`),
Do("GET", "mykey", "myid", "BOUNDS").Str("[[32.10067839408127 -116.072280937208] [33.89932160591873 -113.927719062792]]"),
Do("GET", "mykey", "myid", "HASH", 7).Str("9my5xp7"),
Do("DEL", "mykey", "myid").Str("1"),
Do("GET", "mykey", "myid").Str("<nil>"),
Do("SET", "mykey", "myid", "CIRCLE", 33, -115).Err("wrong number of arguments for 'set' command"),
)
}

func keys_STATS_test(mc *mockServer) error {
return mc.DoBatch(
Do("STATS", "mykey").Str("[nil]"),
Expand Down
Loading