Fix sporadic kNN results when TTL is used

fixes #326
This commit is contained in:
Josh Baker 2018-06-08 09:45:18 -07:00
parent fd1d2f0dd7
commit 47f2e25d27
1 changed files with 31 additions and 69 deletions

View File

@ -87,7 +87,6 @@ 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
@ -128,41 +127,6 @@ 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
@ -328,8 +292,7 @@ func (c *Controller) cmdSearchArgs(cmd string, vs []resp.Value, types []string)
} }
var nearbyTypes = []string{"point"} var nearbyTypes = []string{"point"}
var withinOrIntersectsTypes = []string{ var withinOrIntersectsTypes = []string{"geo", "bounds", "hash", "tile", "quadkey", "get", "object"}
"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()
@ -367,9 +330,6 @@ func (c *Controller) cmdNearby(msg *server.Message) (res resp.Value, err error)
if sw.col != nil { if sw.col != nil {
var matched uint32 var 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 {
if c.hasExpired(s.key, id) {
return true
}
// Calculate distance if we need to // Calculate distance if we need to
distance := 0.0 distance := 0.0
if s.distance { if s.distance {
@ -389,10 +349,13 @@ func (c *Controller) cmdNearby(msg *server.Message) (res resp.Value, err error)
}) })
} }
if s.knn { if s.knn {
nearestNeighbors(sw, s.lat, s.lon, &matched, iter) c.nearestNeighbors(&s, sw, s.lat, s.lon, &matched, iter)
} else { } else {
sw.col.Nearby(s.sparse, s.lat, s.lon, s.meters, minZ, maxZ, sw.col.Nearby(s.sparse, 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) {
return true
}
return iter(id, o, fields, nil) return iter(id, o, fields, nil)
}, },
) )
@ -413,25 +376,32 @@ type iterItem struct {
dist float64 dist float64
} }
func nearestNeighbors(sw *scanWriter, lat, lon float64, matched *uint32, func (c *Controller) nearestNeighbors(
iter func(id string, o geojson.Object, fields []float64, dist *float64) bool) { s *liveFenceSwitches, sw *scanWriter, lat, lon float64, matched *uint32,
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, func(id string, o geojson.Object, fields []float64) bool { sw.col.NearestNeighbors(lat, lon,
if _, ok := sw.fieldMatch(fields, o); !ok { func(id string, o geojson.Object, fields []float64) bool {
return true if c.hasExpired(s.key, id) {
} return true
match, keepGoing := sw.globMatch(id, o) }
if !match { if _, ok := sw.fieldMatch(fields, o); !ok {
return true return true
} }
dist := o.CalculatedPoint().DistanceTo(geojson.Position{X: lon, Y: lat, Z: 0}) match, keepGoing := sw.globMatch(id, o)
items = append(items, iterItem{id: id, o: o, fields: fields, dist: dist}) if !match {
if !keepGoing { return true
return false }
} dist := o.CalculatedPoint().DistanceTo(geojson.Position{X: lon, Y: lat, Z: 0})
return len(items) < limit items = append(items, iterItem{id: id, o: o, fields: fields, dist: dist})
}) if !keepGoing {
return false
}
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
}) })
@ -486,11 +456,7 @@ 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, sw.col.Within(s.sparse, s.o, s.minLat, s.minLon, s.maxLat, s.maxLon, minZ, maxZ,
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
@ -504,11 +470,7 @@ func (c *Controller) cmdWithinOrIntersects(cmd string, msg *server.Message) (res
}, },
) )
} else if cmd == "intersects" { } else if cmd == "intersects" {
sw.col.Intersects(s.sparse, sw.col.Intersects(s.sparse, s.o, s.minLat, s.minLon, s.maxLat, s.maxLon, minZ, maxZ,
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