From ac0f1704771f501763a0f977555a7d15dacbad4f Mon Sep 17 00:00:00 2001 From: tidwall Date: Wed, 21 Sep 2022 10:42:13 -0700 Subject: [PATCH] Keep struct file in tree --- .../object/{object.go => object_binary.go} | 0 internal/object/object_struct.go | 88 +++++++++++++++++++ 2 files changed, 88 insertions(+) rename internal/object/{object.go => object_binary.go} (100%) create mode 100644 internal/object/object_struct.go diff --git a/internal/object/object.go b/internal/object/object_binary.go similarity index 100% rename from internal/object/object.go rename to internal/object/object_binary.go diff --git a/internal/object/object_struct.go b/internal/object/object_struct.go new file mode 100644 index 00000000..b4fa2ff0 --- /dev/null +++ b/internal/object/object_struct.go @@ -0,0 +1,88 @@ +//go:build exclude + +package object + +import ( + "github.com/tidwall/geojson" + "github.com/tidwall/geojson/geometry" + "github.com/tidwall/tile38/internal/field" +) + +type Object struct { + id string + geo geojson.Object + expires int64 // unix nano expiration + fields field.List +} + +func (o *Object) ID() string { + if o == nil { + return "" + } + return o.id +} + +func (o *Object) Fields() field.List { + if o == nil { + return field.List{} + } + return o.fields +} + +func (o *Object) Expires() int64 { + if o == nil { + return 0 + } + return o.expires +} + +func (o *Object) Rect() geometry.Rect { + if o == nil || o.geo == nil { + return geometry.Rect{} + } + return o.geo.Rect() +} + +func (o *Object) Geo() geojson.Object { + if o == nil || o.geo == nil { + return nil + } + return o.geo +} + +func (o *Object) String() string { + if o == nil || o.geo == nil { + return "" + } + return o.geo.String() +} + +func (o *Object) IsSpatial() bool { + _, ok := o.geo.(geojson.Spatial) + return ok +} + +func (o *Object) Weight() int { + if o == nil { + return 0 + } + var weight int + weight += len(o.ID()) + if o.IsSpatial() { + weight += o.Geo().NumPoints() * 16 + } else { + weight += len(o.Geo().String()) + } + weight += o.Fields().Weight() + return weight +} + +func New(id string, geo geojson.Object, expires int64, fields field.List, +) *Object { + return &Object{ + id: id, + geo: geo, + expires: expires, + fields: fields, + } +}