Fix sporadic kNN results when TTL is used

fixes #326
This commit is contained in:
Josh Baker 2018-06-08 09:52:26 -07:00
parent 47f2e25d27
commit 3a9d3d145a
1 changed files with 68 additions and 27 deletions

View File

@ -87,6 +87,7 @@ func (c *Controller) cmdSearchArgs(cmd string, vs []resp.Value, types []string)
err = errInvalidArgument(typ) err = errInvalidArgument(typ)
return return
} }
s.meters = -1 // this will become non-negative if search is within a circle
switch ltyp { switch ltyp {
case "point": case "point":
var slat, slon, smeters string var slat, slon, smeters string
@ -127,6 +128,41 @@ func (c *Controller) cmdSearchArgs(cmd string, vs []resp.Value, types []string)
err = errInvalidArgument(smeters) err = errInvalidArgument(smeters)
return return
} }
if s.meters < 0 {
err = errInvalidArgument(smeters)
return
}
}
case "circle":
var slat, slon, smeters string
if vs, slat, ok = tokenval(vs); !ok || slat == "" {
err = errInvalidNumberOfArguments
return
}
if vs, slon, ok = tokenval(vs); !ok || slon == "" {
err = errInvalidNumberOfArguments
return
}
if vs, smeters, ok = tokenval(vs); !ok || smeters == "" {
err = errInvalidArgument(slat)
return
}
if s.lat, err = strconv.ParseFloat(slat, 64); err != nil {
err = errInvalidArgument(slat)
return
}
if s.lon, err = strconv.ParseFloat(slon, 64); err != nil {
err = errInvalidArgument(slon)
return
}
if s.meters, err = strconv.ParseFloat(smeters, 64); err != nil {
err = errInvalidArgument(smeters)
return
}
if s.meters < 0 {
err = errInvalidArgument(smeters)
return
} }
case "object": case "object":
var obj string var obj string
@ -292,7 +328,8 @@ func (c *Controller) cmdSearchArgs(cmd string, vs []resp.Value, types []string)
} }
var nearbyTypes = []string{"point"} var nearbyTypes = []string{"point"}
var withinOrIntersectsTypes = []string{"geo", "bounds", "hash", "tile", "quadkey", "get", "object"} var withinOrIntersectsTypes = []string{
"geo", "bounds", "hash", "tile", "quadkey", "get", "object", "circle"}
func (c *Controller) cmdNearby(msg *server.Message) (res resp.Value, err error) { func (c *Controller) cmdNearby(msg *server.Message) (res resp.Value, err error) {
start := time.Now() start := time.Now()
@ -376,32 +413,28 @@ type iterItem struct {
dist float64 dist float64
} }
func (c *Controller) nearestNeighbors( func (c *Controller) nearestNeighbors(s *liveFenceSwitches, sw *scanWriter, lat, lon float64, matched *uint32,
s *liveFenceSwitches, sw *scanWriter, lat, lon float64, matched *uint32, iter func(id string, o geojson.Object, fields []float64, dist *float64) bool) {
iter func(id string, o geojson.Object, fields []float64, dist *float64) bool,
) {
limit := int(sw.cursor + sw.limit) limit := int(sw.cursor + sw.limit)
var items []iterItem var items []iterItem
sw.col.NearestNeighbors(lat, lon, sw.col.NearestNeighbors(lat, lon, func(id string, o geojson.Object, fields []float64) bool {
func(id string, o geojson.Object, fields []float64) bool { if c.hasExpired(s.key, id) {
if c.hasExpired(s.key, id) { return true
return true }
} if _, ok := sw.fieldMatch(fields, o); !ok {
if _, ok := sw.fieldMatch(fields, o); !ok { return true
return true }
} match, keepGoing := sw.globMatch(id, o)
match, keepGoing := sw.globMatch(id, o) if !match {
if !match { return true
return true }
} dist := o.CalculatedPoint().DistanceTo(geojson.Position{X: lon, Y: lat, Z: 0})
dist := o.CalculatedPoint().DistanceTo(geojson.Position{X: lon, Y: lat, Z: 0}) items = append(items, iterItem{id: id, o: o, fields: fields, dist: dist})
items = append(items, iterItem{id: id, o: o, fields: fields, dist: dist}) if !keepGoing {
if !keepGoing { return false
return false }
} return len(items) < limit
return len(items) < limit })
},
)
sort.Slice(items, func(i, j int) bool { sort.Slice(items, func(i, j int) bool {
return items[i].dist < items[j].dist return items[i].dist < items[j].dist
}) })
@ -456,7 +489,11 @@ func (c *Controller) cmdWithinOrIntersects(cmd string, msg *server.Message) (res
if sw.col != nil { if sw.col != nil {
minZ, maxZ := zMinMaxFromWheres(s.wheres) minZ, maxZ := zMinMaxFromWheres(s.wheres)
if cmd == "within" { if cmd == "within" {
sw.col.Within(s.sparse, s.o, s.minLat, s.minLon, s.maxLat, s.maxLon, minZ, maxZ, sw.col.Within(s.sparse,
s.o,
s.minLat, s.minLon, s.maxLat, s.maxLon,
s.lat, s.lon, s.meters,
minZ, maxZ,
func(id string, o geojson.Object, fields []float64) bool { func(id string, o geojson.Object, fields []float64) bool {
if c.hasExpired(s.key, id) { if c.hasExpired(s.key, id) {
return true return true
@ -470,7 +507,11 @@ func (c *Controller) cmdWithinOrIntersects(cmd string, msg *server.Message) (res
}, },
) )
} else if cmd == "intersects" { } else if cmd == "intersects" {
sw.col.Intersects(s.sparse, s.o, s.minLat, s.minLon, s.maxLat, s.maxLon, minZ, maxZ, sw.col.Intersects(s.sparse,
s.o,
s.minLat, s.minLon, s.maxLat, s.maxLon,
s.lat, s.lon, s.meters,
minZ, maxZ,
func(id string, o geojson.Object, fields []float64) bool { func(id string, o geojson.Object, fields []float64) bool {
if c.hasExpired(s.key, id) { if c.hasExpired(s.key, id) {
return true return true