Skip to content

Commit

Permalink
support for CIRCLE with geo operations
Browse files Browse the repository at this point in the history
test for containing concentric circles fails, seems bug in geojson library
  • Loading branch information
undeadcat committed Jun 10, 2024
1 parent b341b3a commit 470326c
Show file tree
Hide file tree
Showing 10 changed files with 35 additions and 112 deletions.
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)
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
2 changes: 1 addition & 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
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
10 changes: 10 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,10 @@ 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"},
{"WITHIN", "mykey", "IDS", "CIRCLE", 37.7335, -122.4412, 200}, {
"[0 [100m_circle]]"},
})
}

Expand Down
1 change: 1 addition & 0 deletions tests/keys_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -377,6 +377,7 @@ func keys_SET_CIRCLE_test(mc *mockServer) error {
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"),
)
}

Expand Down

0 comments on commit 470326c

Please sign in to comment.