diff --git a/go.mod b/go.mod index aaffb003e..5a922d8d0 100644 --- a/go.mod +++ b/go.mod @@ -6,7 +6,7 @@ require ( github.com/RoaringBitmap/roaring v1.2.3 github.com/bits-and-blooms/bitset v1.2.0 github.com/blevesearch/bleve_index_api v1.0.6 - github.com/blevesearch/geo v0.1.17 + github.com/blevesearch/geo v0.1.18 github.com/blevesearch/go-metrics v0.0.0-20201227073835-cf1acfcdf475 github.com/blevesearch/go-porterstemmer v1.0.3 github.com/blevesearch/goleveldb v1.0.1 diff --git a/go.sum b/go.sum index dd5d422ac..90ebfb29b 100644 --- a/go.sum +++ b/go.sum @@ -4,8 +4,8 @@ github.com/bits-and-blooms/bitset v1.2.0 h1:Kn4yilvwNtMACtf1eYDlG8H77R07mZSPbMjL github.com/bits-and-blooms/bitset v1.2.0/go.mod h1:gIdJ4wp64HaoK2YrL1Q5/N7Y16edYb8uY+O0FJTyyDA= github.com/blevesearch/bleve_index_api v1.0.6 h1:gyUUxdsrvmW3jVhhYdCVL6h9dCjNT/geNU7PxGn37p8= github.com/blevesearch/bleve_index_api v1.0.6/go.mod h1:YXMDwaXFFXwncRS8UobWs7nvo0DmusriM1nztTlj1ms= -github.com/blevesearch/geo v0.1.17 h1:AguzI6/5mHXapzB0gE9IKWo+wWPHZmXZoscHcjFgAFA= -github.com/blevesearch/geo v0.1.17/go.mod h1:uRMGWG0HJYfWfFJpK3zTdnnr1K+ksZTuWKhXeSokfnM= +github.com/blevesearch/geo v0.1.18 h1:Np8jycHTZ5scFe7VEPLrDoHnnb9C4j636ue/CGrhtDw= +github.com/blevesearch/geo v0.1.18/go.mod h1:uRMGWG0HJYfWfFJpK3zTdnnr1K+ksZTuWKhXeSokfnM= github.com/blevesearch/go-metrics v0.0.0-20201227073835-cf1acfcdf475 h1:kDy+zgJFJJoJYBvdfBSiZYBbdsUL0XcjHYWezpQBGPA= github.com/blevesearch/go-metrics v0.0.0-20201227073835-cf1acfcdf475/go.mod h1:9eJDeqxJ3E7WnLebQUlPD7ZjSce7AnDb9vjGmMCbD0A= github.com/blevesearch/go-porterstemmer v1.0.3 h1:GtmsqID0aZdCSNiY8SkuPJ12pD4jI+DdXTAn4YRcHCo= diff --git a/index_impl.go b/index_impl.go index b5d6efb90..592b3c694 100644 --- a/index_impl.go +++ b/index_impl.go @@ -35,6 +35,7 @@ import ( "github.com/blevesearch/bleve/v2/search/facet" "github.com/blevesearch/bleve/v2/search/highlight" index "github.com/blevesearch/bleve_index_api" + "github.com/blevesearch/geo/s2" ) type indexImpl struct { @@ -482,6 +483,18 @@ func (i *indexImpl) SearchInContext(ctx context.Context, req *SearchRequest) (sr ctx = context.WithValue(ctx, search.SearchIOStatsCallbackKey, search.SearchIOStatsCallbackFunc(sendBytesRead)) + var bufPool *s2.GeoBufferPool + getBufferPool := func() *s2.GeoBufferPool { + if bufPool == nil { + bufPool = s2.NewGeoBufferPool(search.MaxGeoBufPoolSize, search.MinGeoBufPoolSize) + } + + return bufPool + } + + ctx = context.WithValue(ctx, search.GeoBufferPoolCallbackKey, + search.GeoBufferPoolCallbackFunc(getBufferPool)) + searcher, err := req.Query.Searcher(ctx, indexReader, i.m, search.SearcherOptions{ Explain: req.Explain, IncludeTermVectors: req.IncludeLocations || req.Highlight != nil, diff --git a/search/searcher/search_geoshape.go b/search/searcher/search_geoshape.go index 1107c9438..ae113107d 100644 --- a/search/searcher/search_geoshape.go +++ b/search/searcher/search_geoshape.go @@ -22,6 +22,7 @@ import ( "github.com/blevesearch/bleve/v2/search" index "github.com/blevesearch/bleve_index_api" "github.com/blevesearch/geo/geojson" + "github.com/blevesearch/geo/s2" ) func NewGeoShapeSearcher(ctx context.Context, indexReader index.IndexReader, shape index.GeoJSON, @@ -70,6 +71,12 @@ func buildRelationFilterOnShapes(ctx context.Context, dvReader index.DocValueRea var dvShapeValue []byte var startReading, finishReading bool var reader *bytes.Reader + + var bufPool *s2.GeoBufferPool + if ctx != nil { + bufPool = ctx.Value(search.GeoBufferPoolCallbackKey).(search.GeoBufferPoolCallbackFunc)() + } + return func(d *search.DocumentMatch) bool { var found bool @@ -104,7 +111,7 @@ func buildRelationFilterOnShapes(ctx context.Context, dvReader index.DocValueRea // apply the filter once the entire docvalue is finished reading. if finishReading { v, err := geojson.FilterGeoShapesOnRelation(shape, - dvShapeValue, relation, &reader) + dvShapeValue, relation, &reader, bufPool) if err == nil && v { found = true } diff --git a/search/util.go b/search/util.go index 82aa21acb..b2cb62a2d 100644 --- a/search/util.go +++ b/search/util.go @@ -14,7 +14,11 @@ package search -import "context" +import ( + "context" + + "github.com/blevesearch/geo/s2" +) func MergeLocations(locations []FieldTermLocationMap) FieldTermLocationMap { rv := locations[0] @@ -119,3 +123,13 @@ func RecordSearchCost(ctx context.Context, } } } + +const GeoBufferPoolCallbackKey = "_geo_buffer_pool_callback_key" + +// Assigning the size of the largest buffer in the pool to 24KB and +// the smallest buffer to 24 bytes. The pools are used to read a +// sequence of vertices which are always 24 bytes each. +const MaxGeoBufPoolSize = 24 * 1024 +const MinGeoBufPoolSize = 24 + +type GeoBufferPoolCallbackFunc func() *s2.GeoBufferPool