tile38/internal/collection/collection_test.go

771 lines
20 KiB
Go
Raw Normal View History

package collection
import (
"fmt"
"math/rand"
"reflect"
"strconv"
"testing"
"time"
"github.com/tidwall/geojson"
"github.com/tidwall/geojson/geometry"
"github.com/tidwall/gjson"
2022-09-20 03:47:38 +03:00
"github.com/tidwall/tile38/internal/field"
2022-09-21 00:20:53 +03:00
"github.com/tidwall/tile38/internal/object"
)
func PO(x, y float64) *geojson.Point {
return geojson.NewPoint(geometry.Point{X: x, Y: y})
}
func init() {
seed := time.Now().UnixNano()
println(seed)
rand.Seed(seed)
}
func expect(t testing.TB, expect bool) {
t.Helper()
if !expect {
t.Fatal("not what you expected")
}
}
func bounds(c *Collection) geometry.Rect {
minX, minY, maxX, maxY := c.Bounds()
return geometry.Rect{
Min: geometry.Point{X: minX, Y: minY},
Max: geometry.Point{X: maxX, Y: maxY},
}
}
func TestCollectionNewCollection(t *testing.T) {
const numItems = 10000
objs := make(map[string]geojson.Object)
c := New()
for i := 0; i < numItems; i++ {
id := strconv.FormatInt(int64(i), 10)
obj := PO(rand.Float64()*360-180, rand.Float64()*180-90)
objs[id] = obj
2022-09-21 20:03:53 +03:00
c.Set(object.New(id, obj, 0, field.List{}))
}
count := 0
bbox := geometry.Rect{
Min: geometry.Point{X: -180, Y: -90},
Max: geometry.Point{X: 180, Y: 90},
}
2022-09-21 00:20:53 +03:00
c.geoSearch(bbox, func(o *object.Object) bool {
count++
return true
})
if count != len(objs) {
t.Fatalf("count = %d, expect %d", count, len(objs))
}
count = c.Count()
if count != len(objs) {
t.Fatalf("c.Count() = %d, expect %d", count, len(objs))
}
testCollectionVerifyContents(t, c, objs)
}
2022-09-20 03:47:38 +03:00
func toFields(fNames, fValues []string) field.List {
var fields field.List
for i := 0; i < len(fNames); i++ {
fields = fields.Set(field.Make(fNames[i], fValues[i]))
}
return fields
}
func TestCollectionSet(t *testing.T) {
t.Run("AddString", func(t *testing.T) {
c := New()
str1 := String("hello")
2022-09-21 20:03:53 +03:00
old := c.Set(object.New("str", str1, 0, field.List{}))
2022-09-21 00:20:53 +03:00
expect(t, old == nil)
})
t.Run("UpdateString", func(t *testing.T) {
c := New()
str1 := String("hello")
str2 := String("world")
2022-09-21 20:03:53 +03:00
old := c.Set(object.New("str", str1, 0, field.List{}))
2022-09-21 00:20:53 +03:00
expect(t, old == nil)
2022-09-21 20:03:53 +03:00
old = c.Set(object.New("str", str2, 0, field.List{}))
2022-09-21 00:20:53 +03:00
expect(t, old.Geo() == str1)
})
t.Run("AddPoint", func(t *testing.T) {
c := New()
point1 := PO(-112.1, 33.1)
2022-09-21 20:03:53 +03:00
old := c.Set(object.New("point", point1, 0, field.List{}))
2022-09-21 00:20:53 +03:00
expect(t, old == nil)
})
t.Run("UpdatePoint", func(t *testing.T) {
c := New()
point1 := PO(-112.1, 33.1)
point2 := PO(-112.2, 33.2)
2022-09-21 20:03:53 +03:00
old := c.Set(object.New("point", point1, 0, field.List{}))
2022-09-21 00:20:53 +03:00
expect(t, old == nil)
2022-09-21 20:03:53 +03:00
old = c.Set(object.New("point", point2, 0, field.List{}))
2022-09-21 00:20:53 +03:00
expect(t, old.Geo().Center() == point1.Base())
})
t.Run("Fields", func(t *testing.T) {
c := New()
str1 := String("hello")
2022-09-20 03:47:38 +03:00
fNames := []string{"a", "b", "c"}
2022-09-20 03:47:38 +03:00
fValues := []string{"1", "2", "3"}
fields1 := toFields(fNames, fValues)
2022-09-21 20:03:53 +03:00
old := c.Set(object.New("str", str1, 0, fields1))
2022-09-21 00:20:53 +03:00
expect(t, old == nil)
2022-09-20 03:47:38 +03:00
str2 := String("hello")
2022-09-20 03:47:38 +03:00
fNames = []string{"d", "e", "f"}
2022-09-20 03:47:38 +03:00
fValues = []string{"4", "5", "6"}
fields2 := toFields(fNames, fValues)
2022-09-21 20:03:53 +03:00
old = c.Set(object.New("str", str2, 0, fields2))
2022-09-21 00:20:53 +03:00
expect(t, old.Geo() == str1)
expect(t, reflect.DeepEqual(old.Fields(), fields1))
2022-09-20 03:47:38 +03:00
fNames = []string{"a", "b", "c", "d", "e", "f"}
fValues = []string{"7", "8", "9", "10", "11", "12"}
fields3 := toFields(fNames, fValues)
2022-09-21 20:03:53 +03:00
old = c.Set(object.New("str", str1, 0, fields3))
2022-09-21 00:20:53 +03:00
expect(t, old.Geo() == str2)
expect(t, reflect.DeepEqual(old.Fields(), fields2))
})
t.Run("Delete", func(t *testing.T) {
c := New()
2022-09-21 20:03:53 +03:00
c.Set(object.New("1", String("1"), 0, field.List{}))
c.Set(object.New("2", String("2"), 0, field.List{}))
c.Set(object.New("3", PO(1, 2), 0, field.List{}))
expect(t, c.Count() == 3)
expect(t, c.StringCount() == 2)
expect(t, c.PointCount() == 1)
expect(t, bounds(c) == geometry.Rect{
Min: geometry.Point{X: 1, Y: 2},
Max: geometry.Point{X: 1, Y: 2}})
2022-09-21 00:20:53 +03:00
var prev *object.Object
prev = c.Delete("2")
expect(t, prev.Geo().String() == "2")
expect(t, c.Count() == 2)
expect(t, c.StringCount() == 1)
expect(t, c.PointCount() == 1)
2022-09-21 00:20:53 +03:00
prev = c.Delete("1")
expect(t, prev.Geo().String() == "1")
expect(t, c.Count() == 1)
expect(t, c.StringCount() == 0)
expect(t, c.PointCount() == 1)
2022-09-21 00:20:53 +03:00
prev = c.Delete("3")
expect(t, prev.Geo().String() == `{"type":"Point","coordinates":[1,2]}`)
expect(t, c.Count() == 0)
expect(t, c.StringCount() == 0)
expect(t, c.PointCount() == 0)
2022-09-21 00:20:53 +03:00
prev = c.Delete("3")
expect(t, prev == nil)
expect(t, c.Count() == 0)
expect(t, bounds(c) == geometry.Rect{})
2022-09-21 00:20:53 +03:00
expect(t, c.Get("3") == nil)
2022-09-20 03:47:38 +03:00
})
}
func fieldValueAt(fields field.List, index int) string {
if index < 0 || index >= fields.Len() {
panic("out of bounds")
}
var retval string
var i int
fields.Scan(func(f field.Field) bool {
if i == index {
retval = f.Value().Data()
}
i++
return true
})
2022-09-20 03:47:38 +03:00
return retval
}
func TestCollectionScan(t *testing.T) {
N := 256
c := New()
for _, i := range rand.Perm(N) {
id := fmt.Sprintf("%04d", i)
2022-09-21 20:03:53 +03:00
c.Set(object.New(id, String(id), 0, makeFields(
2022-09-20 03:47:38 +03:00
field.Make("ex", id),
2022-09-21 00:20:53 +03:00
)))
}
var n int
var prevID string
2022-09-21 00:20:53 +03:00
c.Scan(false, nil, nil, func(o *object.Object) bool {
if n > 0 {
2022-09-21 00:20:53 +03:00
expect(t, o.ID() > prevID)
}
2022-09-21 00:20:53 +03:00
expect(t, o.ID() == fieldValueAt(o.Fields(), 0))
n++
2022-09-21 00:20:53 +03:00
prevID = o.ID()
return true
})
expect(t, n == c.Count())
n = 0
2022-09-21 00:20:53 +03:00
c.Scan(true, nil, nil, func(o *object.Object) bool {
if n > 0 {
2022-09-21 00:20:53 +03:00
expect(t, o.ID() < prevID)
}
2022-09-21 00:20:53 +03:00
expect(t, o.ID() == fieldValueAt(o.Fields(), 0))
n++
2022-09-21 00:20:53 +03:00
prevID = o.ID()
return true
})
expect(t, n == c.Count())
n = 0
2019-04-24 15:09:41 +03:00
c.ScanRange("0060", "0070", false, nil, nil,
2022-09-21 00:20:53 +03:00
func(o *object.Object) bool {
if n > 0 {
2022-09-21 00:20:53 +03:00
expect(t, o.ID() > prevID)
}
2022-09-21 00:20:53 +03:00
expect(t, o.ID() == fieldValueAt(o.Fields(), 0))
n++
2022-09-21 00:20:53 +03:00
prevID = o.ID()
return true
})
expect(t, n == 10)
n = 0
2019-04-24 15:09:41 +03:00
c.ScanRange("0070", "0060", true, nil, nil,
2022-09-21 00:20:53 +03:00
func(o *object.Object) bool {
if n > 0 {
2022-09-21 00:20:53 +03:00
expect(t, o.ID() < prevID)
}
2022-09-21 00:20:53 +03:00
expect(t, o.ID() == fieldValueAt(o.Fields(), 0))
n++
2022-09-21 00:20:53 +03:00
prevID = o.ID()
return true
})
expect(t, n == 10)
n = 0
2019-04-24 15:09:41 +03:00
c.ScanGreaterOrEqual("0070", true, nil, nil,
2022-09-21 00:20:53 +03:00
func(o *object.Object) bool {
if n > 0 {
2022-09-21 00:20:53 +03:00
expect(t, o.ID() < prevID)
}
2022-09-21 00:20:53 +03:00
expect(t, o.ID() == fieldValueAt(o.Fields(), 0))
n++
2022-09-21 00:20:53 +03:00
prevID = o.ID()
return true
})
expect(t, n == 71)
n = 0
2019-04-24 15:09:41 +03:00
c.ScanGreaterOrEqual("0070", false, nil, nil,
2022-09-21 00:20:53 +03:00
func(o *object.Object) bool {
if n > 0 {
2022-09-21 00:20:53 +03:00
expect(t, o.ID() > prevID)
}
2022-09-21 00:20:53 +03:00
expect(t, o.ID() == fieldValueAt(o.Fields(), 0))
n++
2022-09-21 00:20:53 +03:00
prevID = o.ID()
return true
})
expect(t, n == c.Count()-70)
}
2022-09-20 03:47:38 +03:00
func makeFields(entries ...field.Field) field.List {
var fields field.List
for _, f := range entries {
fields = fields.Set(f)
}
return fields
}
func TestCollectionSearch(t *testing.T) {
N := 256
c := New()
for i, j := range rand.Perm(N) {
id := fmt.Sprintf("%04d", j)
ex := fmt.Sprintf("%04d", i)
2022-09-21 00:20:53 +03:00
c.Set(object.New(id, String(ex),
2022-09-21 20:03:53 +03:00
0, makeFields(
2022-09-20 03:47:38 +03:00
field.Make("i", ex),
field.Make("j", id),
2022-09-21 00:20:53 +03:00
)))
}
var n int
var prevValue string
2022-09-21 00:20:53 +03:00
c.SearchValues(false, nil, nil, func(o *object.Object) bool {
if n > 0 {
2022-09-21 00:20:53 +03:00
expect(t, o.Geo().String() > prevValue)
}
2022-09-21 00:20:53 +03:00
expect(t, o.ID() == fieldValueAt(o.Fields(), 1))
n++
2022-09-21 00:20:53 +03:00
prevValue = o.Geo().String()
return true
})
expect(t, n == c.Count())
n = 0
2022-09-21 00:20:53 +03:00
c.SearchValues(true, nil, nil, func(o *object.Object) bool {
if n > 0 {
2022-09-21 00:20:53 +03:00
expect(t, o.Geo().String() < prevValue)
}
2022-09-21 00:20:53 +03:00
expect(t, o.ID() == fieldValueAt(o.Fields(), 1))
n++
2022-09-21 00:20:53 +03:00
prevValue = o.Geo().String()
return true
})
expect(t, n == c.Count())
n = 0
2019-04-24 15:09:41 +03:00
c.SearchValuesRange("0060", "0070", false, nil, nil,
2022-09-21 00:20:53 +03:00
func(o *object.Object) bool {
if n > 0 {
2022-09-21 00:20:53 +03:00
expect(t, o.Geo().String() > prevValue)
}
2022-09-21 00:20:53 +03:00
expect(t, o.ID() == fieldValueAt(o.Fields(), 1))
n++
2022-09-21 00:20:53 +03:00
prevValue = o.Geo().String()
return true
})
expect(t, n == 10)
n = 0
2019-04-24 15:09:41 +03:00
c.SearchValuesRange("0070", "0060", true, nil, nil,
2022-09-21 00:20:53 +03:00
func(o *object.Object) bool {
if n > 0 {
2022-09-21 00:20:53 +03:00
expect(t, o.Geo().String() < prevValue)
}
2022-09-21 00:20:53 +03:00
expect(t, o.ID() == fieldValueAt(o.Fields(), 1))
n++
2022-09-21 00:20:53 +03:00
prevValue = o.Geo().String()
return true
})
expect(t, n == 10)
}
func TestCollectionWeight(t *testing.T) {
c := New()
2022-09-21 20:03:53 +03:00
c.Set(object.New("1", String("1"), 0, field.List{}))
expect(t, c.TotalWeight() > 0)
c.Delete("1")
expect(t, c.TotalWeight() == 0)
2022-09-21 20:03:53 +03:00
c.Set(object.New("1", String("1"), 0,
2022-09-20 03:47:38 +03:00
toFields(
[]string{"a", "b", "c"},
[]string{"1", "2", "3"},
),
2022-09-21 00:20:53 +03:00
))
expect(t, c.TotalWeight() > 0)
c.Delete("1")
expect(t, c.TotalWeight() == 0)
2022-09-21 20:03:53 +03:00
c.Set(object.New("1", String("1"), 0,
2022-09-20 03:47:38 +03:00
toFields(
[]string{"a", "b", "c"},
[]string{"1", "2", "3"},
),
2022-09-21 00:20:53 +03:00
))
2022-09-21 20:03:53 +03:00
c.Set(object.New("2", String("2"), 0,
2022-09-20 03:47:38 +03:00
toFields(
[]string{"d", "e", "f"},
[]string{"4", "5", "6"},
),
2022-09-21 00:20:53 +03:00
))
2022-09-21 20:03:53 +03:00
c.Set(object.New("1", String("1"), 0,
2022-09-20 03:47:38 +03:00
toFields(
[]string{"d", "e", "f"},
[]string{"4", "5", "6"},
),
2022-09-21 00:20:53 +03:00
))
c.Delete("1")
c.Delete("2")
expect(t, c.TotalWeight() == 0)
}
func TestSpatialSearch(t *testing.T) {
json := `
{"type":"FeatureCollection","features":[
{"type":"Feature","id":"p1","properties":{"marker-color":"#962d28","stroke":"#962d28","fill":"#962d28"},"geometry":{"type":"Point","coordinates":[-71.4743041992187,42.51867517417283]}},
{"type":"Feature","id":"p2","properties":{"marker-color":"#962d28","stroke":"#962d28","fill":"#962d28"},"geometry":{"type":"Point","coordinates":[-71.4056396484375,42.50197174319114]}},
{"type":"Feature","id":"p3","properties":{"marker-color":"#962d28","stroke":"#962d28","fill":"#962d28"},"geometry":{"type":"Point","coordinates":[-71.4619445800781,42.49437779897246]}},
{"type":"Feature","id":"p4","properties":{"marker-color":"#962d28","stroke":"#962d28","fill":"#962d28"},"geometry":{"type":"Point","coordinates":[-71.4337921142578,42.53891577257117]}},
{"type":"Feature","id":"r1","properties":{"marker-color":"#962d28","stroke":"#962d28","fill":"#962d28"},"geometry":{"type":"Polygon","coordinates":[[[-71.4279556274414,42.48804880765346],[-71.37439727783203,42.48804880765346],[-71.37439727783203,42.52322988064187],[-71.4279556274414,42.52322988064187],[-71.4279556274414,42.48804880765346]]]}},
{"type":"Feature","id":"r2","properties":{"marker-color":"#962d28","stroke":"#962d28","fill":"#962d28"},"geometry":{"type":"Polygon","coordinates":[[[-71.4825439453125,42.53588010092859],[-71.45027160644531,42.53588010092859],[-71.45027160644531,42.55839115400447],[-71.4825439453125,42.55839115400447],[-71.4825439453125,42.53588010092859]]]}},
{"type":"Feature","id":"r3","properties":{"marker-color":"#962d28","stroke":"#962d28","fill":"#962d28"},"geometry":{"type":"Polygon","coordinates": [[[-71.4111328125,42.53512115995963],[-71.3833236694336,42.53512115995963],[-71.3833236694336,42.54953946116446],[-71.4111328125,42.54953946116446],[-71.4111328125,42.53512115995963]]]}},
{"type":"Feature","id":"q1","properties":{},"geometry":{"type":"Polygon","coordinates":[[[-71.55258178710938,42.51361399979923],[-71.42074584960938,42.51361399979923],[-71.42074584960938,42.59100512331456],[-71.55258178710938,42.59100512331456],[-71.55258178710938,42.51361399979923]]]}},
{"type":"Feature","id":"q2","properties":{},"geometry":{"type":"Polygon","coordinates":[[[-71.52992248535156,42.48121277771616],[-71.36375427246092,42.48121277771616],[-71.36375427246092,42.57786045892046],[-71.52992248535156,42.57786045892046],[-71.52992248535156,42.48121277771616]]]}},
{"type":"Feature","id":"q3","properties":{},"geometry":{"type":"Polygon","coordinates":[[[-71.49490356445312,42.56673588590953],[-71.52236938476562,42.47462922809497],[-71.42898559570312,42.464499337722344],[-71.43241882324219,42.522217752342236],[-71.37954711914061,42.56420729713456],[-71.49490356445312,42.56673588590953]]]}},
{"type":"Feature","id":"q4","properties":{},"geometry":{"type":"Point","coordinates": [-71.46366119384766,42.54043355305221]}}
]}
`
p1, _ := geojson.Parse(gjson.Get(json, `features.#[id=="p1"]`).Raw, nil)
p2, _ := geojson.Parse(gjson.Get(json, `features.#[id=="p2"]`).Raw, nil)
p3, _ := geojson.Parse(gjson.Get(json, `features.#[id=="p3"]`).Raw, nil)
p4, _ := geojson.Parse(gjson.Get(json, `features.#[id=="p4"]`).Raw, nil)
r1, _ := geojson.Parse(gjson.Get(json, `features.#[id=="r1"]`).Raw, nil)
r2, _ := geojson.Parse(gjson.Get(json, `features.#[id=="r2"]`).Raw, nil)
r3, _ := geojson.Parse(gjson.Get(json, `features.#[id=="r3"]`).Raw, nil)
q1, _ := geojson.Parse(gjson.Get(json, `features.#[id=="q1"]`).Raw, nil)
q2, _ := geojson.Parse(gjson.Get(json, `features.#[id=="q2"]`).Raw, nil)
q3, _ := geojson.Parse(gjson.Get(json, `features.#[id=="q3"]`).Raw, nil)
q4, _ := geojson.Parse(gjson.Get(json, `features.#[id=="q4"]`).Raw, nil)
c := New()
2022-09-21 20:03:53 +03:00
c.Set(object.New("p1", p1, 0, field.List{}))
c.Set(object.New("p2", p2, 0, field.List{}))
c.Set(object.New("p3", p3, 0, field.List{}))
c.Set(object.New("p4", p4, 0, field.List{}))
c.Set(object.New("r1", r1, 0, field.List{}))
c.Set(object.New("r2", r2, 0, field.List{}))
c.Set(object.New("r3", r3, 0, field.List{}))
var n int
n = 0
2022-09-21 00:20:53 +03:00
c.Within(q1, 0, nil, nil, func(o *object.Object) bool {
n++
return true
})
expect(t, n == 3)
n = 0
2022-09-21 00:20:53 +03:00
c.Within(q2, 0, nil, nil, func(o *object.Object) bool {
n++
return true
})
expect(t, n == 7)
n = 0
2022-09-21 00:20:53 +03:00
c.Within(q3, 0, nil, nil, func(o *object.Object) bool {
n++
return true
})
expect(t, n == 4)
n = 0
2022-09-21 00:20:53 +03:00
c.Intersects(q1, 0, nil, nil, func(o *object.Object) bool {
n++
return true
})
expect(t, n == 4)
n = 0
2022-09-21 00:20:53 +03:00
c.Intersects(q2, 0, nil, nil, func(o *object.Object) bool {
n++
return true
})
expect(t, n == 7)
n = 0
2022-09-21 00:20:53 +03:00
c.Intersects(q3, 0, nil, nil, func(o *object.Object) bool {
n++
return true
})
expect(t, n == 5)
n = 0
2022-09-21 00:20:53 +03:00
c.Intersects(q3, 0, nil, nil, func(o *object.Object) bool {
n++
return n <= 1
})
expect(t, n == 2)
var items []geojson.Object
exitems := []geojson.Object{
r2, p4, p1, r1, r3, p3, p2,
}
lastDist := float64(-1)
distsMonotonic := true
2022-09-21 00:20:53 +03:00
c.Nearby(q4, nil, nil, func(o *object.Object, dist float64) bool {
if dist < lastDist {
distsMonotonic = false
}
items = append(items, o.Geo())
return true
})
expect(t, len(items) == 7)
expect(t, distsMonotonic)
expect(t, reflect.DeepEqual(items, exitems))
}
func TestCollectionSparse(t *testing.T) {
rect := geojson.NewRect(geometry.Rect{
Min: geometry.Point{X: -71.598930, Y: 42.4586739},
Max: geometry.Point{X: -71.37302, Y: 42.607937},
})
N := 10000
c := New()
r := rect.Rect()
for i := 0; i < N; i++ {
x := (r.Max.X-r.Min.X)*rand.Float64() + r.Min.X
y := (r.Max.Y-r.Min.Y)*rand.Float64() + r.Min.Y
point := PO(x, y)
2022-09-21 20:03:53 +03:00
c.Set(object.New(fmt.Sprintf("%d", i), point, 0, field.List{}))
}
var n int
n = 0
2022-09-21 00:20:53 +03:00
c.Within(rect, 1, nil, nil, func(o *object.Object) bool {
n++
return true
})
expect(t, n == 4)
n = 0
2022-09-21 00:20:53 +03:00
c.Within(rect, 2, nil, nil, func(o *object.Object) bool {
n++
return true
})
expect(t, n == 16)
n = 0
2022-09-21 00:20:53 +03:00
c.Within(rect, 3, nil, nil, func(o *object.Object) bool {
n++
return true
})
expect(t, n == 64)
n = 0
2022-09-21 00:20:53 +03:00
c.Within(rect, 3, nil, nil, func(o *object.Object) bool {
n++
return n <= 30
})
expect(t, n == 31)
n = 0
2022-09-21 00:20:53 +03:00
c.Intersects(rect, 3, nil, nil, func(o *object.Object) bool {
n++
return true
})
expect(t, n == 64)
n = 0
2022-09-21 00:20:53 +03:00
c.Intersects(rect, 3, nil, nil, func(o *object.Object) bool {
n++
return n <= 30
})
expect(t, n == 31)
}
func testCollectionVerifyContents(t *testing.T, c *Collection, objs map[string]geojson.Object) {
for id, o2 := range objs {
2022-09-21 00:20:53 +03:00
o := c.Get(id)
if o == nil {
t.Fatalf("ok[%s] = false, expect true", id)
}
2022-09-21 00:20:53 +03:00
j1 := string(o.Geo().AppendJSON(nil))
j2 := string(o2.AppendJSON(nil))
if j1 != j2 {
t.Fatalf("j1 == %s, expect %s", j1, j2)
}
}
}
func TestManyCollections(t *testing.T) {
colsM := make(map[string]*Collection)
cols := 100
objs := 1000
k := 0
for i := 0; i < cols; i++ {
key := strconv.FormatInt(int64(i), 10)
for j := 0; j < objs; j++ {
id := strconv.FormatInt(int64(j), 10)
p := geometry.Point{
X: rand.Float64()*360 - 180,
Y: rand.Float64()*180 - 90,
}
obj := geojson.Object(PO(p.X, p.Y))
col, ok := colsM[key]
if !ok {
col = New()
colsM[key] = col
}
2022-09-21 20:03:53 +03:00
col.Set(object.New(id, obj, 0, field.List{}))
k++
}
}
col := colsM["13"]
//println(col.Count())
bbox := geometry.Rect{
Min: geometry.Point{X: -180, Y: 30},
Max: geometry.Point{X: 34, Y: 100},
}
2022-09-21 00:20:53 +03:00
col.geoSearch(bbox, func(o *object.Object) bool {
//println(id)
return true
})
}
type testPointItem struct {
id string
object geojson.Object
2022-09-20 03:47:38 +03:00
fields field.List
}
2022-09-20 03:47:38 +03:00
func makeBenchFields(nFields int) field.List {
var fields field.List
for i := 0; i < nFields; i++ {
key := fmt.Sprintf("%d", i)
val := key
fields = fields.Set(field.Make(key, val))
}
2022-09-20 03:47:38 +03:00
return fields
}
func BenchmarkInsert_Fields(t *testing.B) {
benchmarkInsert(t, 1)
}
func BenchmarkInsert_NoFields(t *testing.B) {
benchmarkInsert(t, 0)
}
func benchmarkInsert(t *testing.B, nFields int) {
rand.Seed(time.Now().UnixNano())
items := make([]testPointItem, t.N)
for i := 0; i < t.N; i++ {
items[i] = testPointItem{
fmt.Sprintf("%d", i),
PO(rand.Float64()*360-180, rand.Float64()*180-90),
makeBenchFields(nFields),
}
}
col := New()
t.ResetTimer()
for i := 0; i < t.N; i++ {
2022-09-21 20:03:53 +03:00
col.Set(object.New(items[i].id, items[i].object, 0, items[i].fields))
}
}
func BenchmarkReplace_Fields(t *testing.B) {
benchmarkReplace(t, 1)
}
func BenchmarkReplace_NoFields(t *testing.B) {
benchmarkReplace(t, 0)
}
func benchmarkReplace(t *testing.B, nFields int) {
rand.Seed(time.Now().UnixNano())
items := make([]testPointItem, t.N)
for i := 0; i < t.N; i++ {
items[i] = testPointItem{
fmt.Sprintf("%d", i),
PO(rand.Float64()*360-180, rand.Float64()*180-90),
makeBenchFields(nFields),
}
}
col := New()
for i := 0; i < t.N; i++ {
2022-09-21 20:03:53 +03:00
col.Set(object.New(items[i].id, items[i].object, 0, items[i].fields))
}
t.ResetTimer()
for _, i := range rand.Perm(t.N) {
2022-09-21 20:03:53 +03:00
o := col.Set(object.New(items[i].id, items[i].object, 0, field.List{}))
2022-09-21 00:20:53 +03:00
if o.Geo() != items[i].object {
t.Fatal("shoot!")
}
}
}
func BenchmarkGet_Fields(t *testing.B) {
benchmarkGet(t, 1)
}
func BenchmarkGet_NoFields(t *testing.B) {
benchmarkGet(t, 0)
}
func benchmarkGet(t *testing.B, nFields int) {
rand.Seed(time.Now().UnixNano())
items := make([]testPointItem, t.N)
for i := 0; i < t.N; i++ {
items[i] = testPointItem{
fmt.Sprintf("%d", i),
PO(rand.Float64()*360-180, rand.Float64()*180-90),
makeBenchFields(nFields),
}
}
col := New()
for i := 0; i < t.N; i++ {
2022-09-21 20:03:53 +03:00
col.Set(object.New(items[i].id, items[i].object, 0, items[i].fields))
}
t.ResetTimer()
for _, i := range rand.Perm(t.N) {
2022-09-21 00:20:53 +03:00
o := col.Get(items[i].id)
if o.Geo() != items[i].object {
t.Fatal("shoot!")
}
}
}
func BenchmarkRemove_Fields(t *testing.B) {
benchmarkRemove(t, 1)
}
func BenchmarkRemove_NoFields(t *testing.B) {
benchmarkRemove(t, 0)
}
func benchmarkRemove(t *testing.B, nFields int) {
rand.Seed(time.Now().UnixNano())
items := make([]testPointItem, t.N)
for i := 0; i < t.N; i++ {
items[i] = testPointItem{
fmt.Sprintf("%d", i),
PO(rand.Float64()*360-180, rand.Float64()*180-90),
makeBenchFields(nFields),
}
}
col := New()
for i := 0; i < t.N; i++ {
2022-09-21 20:03:53 +03:00
col.Set(object.New(items[i].id, items[i].object, 0, items[i].fields))
}
t.ResetTimer()
for _, i := range rand.Perm(t.N) {
2022-09-21 00:20:53 +03:00
prev := col.Delete(items[i].id)
if prev.Geo() != items[i].object {
t.Fatal("shoot!")
}
}
}
func BenchmarkScan_Fields(t *testing.B) {
benchmarkScan(t, 1)
}
func BenchmarkScan_NoFields(t *testing.B) {
benchmarkScan(t, 0)
}
func benchmarkScan(t *testing.B, nFields int) {
rand.Seed(time.Now().UnixNano())
items := make([]testPointItem, t.N)
for i := 0; i < t.N; i++ {
items[i] = testPointItem{
fmt.Sprintf("%d", i),
PO(rand.Float64()*360-180, rand.Float64()*180-90),
makeBenchFields(nFields),
}
}
col := New()
for i := 0; i < t.N; i++ {
2022-09-21 20:03:53 +03:00
col.Set(object.New(items[i].id, items[i].object, 0, items[i].fields))
}
t.ResetTimer()
for i := 0; i < t.N; i++ {
var scanIteration int
2022-09-21 00:20:53 +03:00
col.Scan(true, nil, nil, func(o *object.Object) bool {
scanIteration++
return scanIteration <= 500
})
}
}