diff --git a/document.go b/document.go index e3fd488..9b06cca 100644 --- a/document.go +++ b/document.go @@ -89,6 +89,22 @@ type GeoShapeField interface { EncodedShape() []byte } +// GeoShapeV2Field represents an analyzed geo shape field +type GeoShapeV2Field interface { + // InnerCells returns the covering cells fully contained within the shape + InnerCells() []uint64 + // CrossCells returns the covering cells that overlap the shape's boundary + CrossCells() []uint64 + + // EncodedBoundingBox returns the serialized bounding box of the shape + EncodedBoundingBox() []byte + // EncodedShape returns the serialized shape + EncodedShape() []byte + + // Scores returns the shape's inner and cross cell scores + Scores() (inner, cross uint64) +} + type IPField interface { IP() (net.IP, error) } diff --git a/index.go b/index.go index fc12d98..3975fc8 100644 --- a/index.go +++ b/index.go @@ -23,12 +23,15 @@ import ( var reflectStaticSizeTermFieldDoc int var reflectStaticSizeTermFieldVector int +var reflectStaticSizeGeoShapeV2FieldDoc int func init() { var tfd TermFieldDoc reflectStaticSizeTermFieldDoc = int(reflect.TypeOf(tfd).Size()) var tfv TermFieldVector reflectStaticSizeTermFieldVector = int(reflect.TypeOf(tfv).Size()) + var gfd GeoShapeV2FieldDoc + reflectStaticSizeGeoShapeV2FieldDoc = int(reflect.TypeOf(gfd).Size()) } type Index interface { @@ -489,6 +492,62 @@ func (a AncestorID) ToIndexInternalID(prealloc IndexInternalID) IndexInternalID return NewIndexInternalID(prealloc, uint64(a)) } +// ----------------------------------------------------------------------------- +// GeoShapeV2IndexReader is an extended index reader that supports reading and +// querying Geo-Shape V2 data. +type GeoShapeV2IndexReader interface { + IndexReader + + GeoShapeV2FieldReader(ctx context.Context, field string) ( + GeoShapeV2FieldReader, error) +} + +// GeoShapeV2FieldReader iterates over the documents whose shapes satisfy a +// spatial relation with a query shape. Search must be called before Next or Advance. +type GeoShapeV2FieldReader interface { + // Search performs a full search and obtains all of the hits for the + // given shape and relation. + Search(shape GeoJSON, relation string) error + + // Next returns the next document matching the search, or nil when it + // reaches the end of the enumeration. + Next(*GeoShapeV2FieldDoc) (*GeoShapeV2FieldDoc, error) + + // Advance resets the enumeration at specified document. + Advance(ID IndexInternalID, preAlloced *GeoShapeV2FieldDoc) ( + *GeoShapeV2FieldDoc, error) + + // Count returns the number of documents matched by the preceding Search. + Count() uint64 + + // Close releases any resources associated with the reader. + Close() error + + // Size returns the size of the reader in bytes. + Size() int +} + +// GeoShapeV2FieldDoc represents a single hit from a geo shape v2 search. +type GeoShapeV2FieldDoc struct { + ID IndexInternalID +} + +func (g *GeoShapeV2FieldDoc) Size() int { + return reflectStaticSizeGeoShapeV2FieldDoc + sizeOfPtr + len(g.ID) +} + +func (g *GeoShapeV2FieldDoc) Reset() *GeoShapeV2FieldDoc { + // remember the []byte used for the ID + id := g.ID + // idiom to copy over from empty GeoShapeV2FieldDoc (0 allocations) + *g = GeoShapeV2FieldDoc{} + // reuse the []byte already allocated (and reset len to 0) + g.ID = id[:0] + return g +} + +// ----------------------------------------------------------------------------- + // Default no-op implementation. Is called before writing any user data to a file. var WriterHook func(context []byte) (string, func(data []byte) []byte, error) diff --git a/spatial_plugin.go b/spatial_plugin.go index bee0476..1d5812f 100644 --- a/spatial_plugin.go +++ b/spatial_plugin.go @@ -44,4 +44,17 @@ type GeoJSON interface { // Value returns the byte value for the shape. Value() ([]byte, error) + + // ---------------------- geo shape v2 methods ------------------------ + + // IndexCells returns the covering cells computed with the + // index-time coverer. + IndexCells() (inner, cross []uint64) + + // QueryCells returns the covering cells computed with the + // query-time coverer. + QueryCells() (inner, cross []uint64) + + // BoundingBox returns the bounding box of the shape + BoundingBox() GeoJSON }