2019-02-13 22:43:38 +03:00
|
|
|
package item
|
|
|
|
|
|
|
|
import (
|
|
|
|
"reflect"
|
|
|
|
"unsafe"
|
|
|
|
|
|
|
|
"github.com/tidwall/btree"
|
|
|
|
"github.com/tidwall/geojson"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Item is a item for Tile38 collections
|
|
|
|
type Item struct {
|
2019-02-16 00:02:04 +03:00
|
|
|
head [2]uint32 // (1:isPoint,1:isPacked,30:fieldsByteLen),(32:idLen)
|
2019-02-15 22:51:26 +03:00
|
|
|
data unsafe.Pointer // pointer to raw block of bytes, fields+id
|
2019-02-15 01:53:46 +03:00
|
|
|
}
|
|
|
|
type objItem struct {
|
2019-02-15 22:51:26 +03:00
|
|
|
_ [2]uint32
|
2019-02-15 01:53:46 +03:00
|
|
|
_ unsafe.Pointer
|
|
|
|
obj geojson.Object
|
|
|
|
}
|
|
|
|
type pointItem struct {
|
2019-02-15 22:51:26 +03:00
|
|
|
_ [2]uint32
|
2019-02-15 01:53:46 +03:00
|
|
|
_ unsafe.Pointer
|
|
|
|
pt geojson.SimplePoint
|
2019-02-13 22:43:38 +03:00
|
|
|
}
|
|
|
|
|
2019-02-16 00:02:04 +03:00
|
|
|
func setbit(n uint32, pos uint) uint32 {
|
|
|
|
return n | (1 << pos)
|
|
|
|
}
|
|
|
|
func unsetbit(n uint32, pos uint) uint32 {
|
|
|
|
return n & ^(1 << pos)
|
|
|
|
}
|
|
|
|
func hasbit(n uint32, pos uint) bool {
|
|
|
|
return (n & (1 << pos)) != 0
|
|
|
|
}
|
|
|
|
|
2019-02-15 22:51:26 +03:00
|
|
|
func (item *Item) isPoint() bool {
|
2019-02-16 00:02:04 +03:00
|
|
|
return hasbit(item.head[0], 31)
|
2019-02-15 22:51:26 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func (item *Item) setIsPoint(isPoint bool) {
|
|
|
|
if isPoint {
|
2019-02-16 00:02:04 +03:00
|
|
|
item.head[0] = setbit(item.head[0], 31)
|
2019-02-15 22:51:26 +03:00
|
|
|
} else {
|
2019-02-16 00:02:04 +03:00
|
|
|
item.head[0] = unsetbit(item.head[0], 31)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (item *Item) isPacked() bool {
|
|
|
|
return hasbit(item.head[0], 30)
|
|
|
|
}
|
|
|
|
func (item *Item) setIsPacked(isPacked bool) {
|
|
|
|
if isPacked {
|
|
|
|
item.head[0] = setbit(item.head[0], 30)
|
|
|
|
} else {
|
|
|
|
item.head[0] = unsetbit(item.head[0], 30)
|
2019-02-15 22:51:26 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-17 23:10:02 +03:00
|
|
|
func (item *Item) fieldsDataSize() int {
|
2019-02-16 00:02:04 +03:00
|
|
|
return int(item.head[0] & 0x3FFFFFFF)
|
2019-02-15 22:51:26 +03:00
|
|
|
}
|
|
|
|
|
2019-02-17 23:10:02 +03:00
|
|
|
func (item *Item) setFieldsDataSize(len int) {
|
2019-02-16 00:02:04 +03:00
|
|
|
item.head[0] = item.head[0]>>30<<30 | uint32(len)
|
2019-02-15 22:51:26 +03:00
|
|
|
}
|
|
|
|
|
2019-02-17 23:10:02 +03:00
|
|
|
func (item *Item) idDataSize() int {
|
2019-02-15 22:51:26 +03:00
|
|
|
return int(item.head[1])
|
|
|
|
}
|
|
|
|
|
2019-02-17 23:10:02 +03:00
|
|
|
func (item *Item) setIDDataSize(len int) {
|
2019-02-15 22:51:26 +03:00
|
|
|
item.head[1] = uint32(len)
|
|
|
|
}
|
|
|
|
|
2019-02-13 22:43:38 +03:00
|
|
|
// ID returns the items ID as a string
|
|
|
|
func (item *Item) ID() string {
|
|
|
|
return *(*string)((unsafe.Pointer)(&reflect.StringHeader{
|
2019-02-17 23:10:02 +03:00
|
|
|
Data: uintptr(unsafe.Pointer(item.data)) +
|
|
|
|
uintptr(item.fieldsDataSize()),
|
|
|
|
Len: item.idDataSize(),
|
2019-02-13 22:43:38 +03:00
|
|
|
}))
|
|
|
|
}
|
|
|
|
|
|
|
|
// Obj returns the geojson object
|
|
|
|
func (item *Item) Obj() geojson.Object {
|
2019-02-15 22:51:26 +03:00
|
|
|
if item.isPoint() {
|
2019-02-15 01:53:46 +03:00
|
|
|
return &(*pointItem)(unsafe.Pointer(item)).pt
|
|
|
|
}
|
|
|
|
return (*objItem)(unsafe.Pointer(item)).obj
|
2019-02-13 22:43:38 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// New returns a newly allocated Item
|
2019-02-16 00:02:04 +03:00
|
|
|
func New(id string, obj geojson.Object, packed bool) *Item {
|
2019-02-15 01:53:46 +03:00
|
|
|
var item *Item
|
|
|
|
if pt, ok := obj.(*geojson.SimplePoint); ok {
|
|
|
|
pitem := new(pointItem)
|
|
|
|
pitem.pt = *pt
|
|
|
|
item = (*Item)(unsafe.Pointer(pitem))
|
2019-02-15 22:51:26 +03:00
|
|
|
item.setIsPoint(true)
|
2019-02-15 01:53:46 +03:00
|
|
|
} else {
|
|
|
|
oitem := new(objItem)
|
|
|
|
oitem.obj = obj
|
|
|
|
item = (*Item)(unsafe.Pointer(oitem))
|
|
|
|
}
|
2019-02-16 00:02:04 +03:00
|
|
|
item.setIsPacked(packed)
|
2019-02-17 23:10:02 +03:00
|
|
|
item.setIDDataSize(len(id))
|
2019-02-15 21:11:40 +03:00
|
|
|
item.data = unsafe.Pointer((*reflect.SliceHeader)(unsafe.Pointer(&id)).Data)
|
2019-02-13 22:43:38 +03:00
|
|
|
return item
|
|
|
|
}
|
|
|
|
|
|
|
|
// WeightAndPoints returns the memory weight and number of points for Item.
|
|
|
|
func (item *Item) WeightAndPoints() (weight, points int) {
|
2019-02-15 01:53:46 +03:00
|
|
|
_, objIsSpatial := item.Obj().(geojson.Spatial)
|
2019-02-13 22:43:38 +03:00
|
|
|
if objIsSpatial {
|
2019-02-15 01:53:46 +03:00
|
|
|
points = item.Obj().NumPoints()
|
2019-02-13 22:43:38 +03:00
|
|
|
weight = points * 16
|
2019-02-15 01:53:46 +03:00
|
|
|
} else if item.Obj() != nil {
|
|
|
|
weight = len(item.Obj().String())
|
2019-02-13 22:43:38 +03:00
|
|
|
}
|
2019-02-17 23:10:02 +03:00
|
|
|
weight += item.fieldsDataSize() + item.idDataSize()
|
2019-02-13 22:43:38 +03:00
|
|
|
return weight, points
|
|
|
|
}
|
|
|
|
|
|
|
|
// Less is a btree interface that compares if item is less than other item.
|
|
|
|
func (item *Item) Less(other btree.Item, ctx interface{}) bool {
|
2019-02-15 01:53:46 +03:00
|
|
|
value1 := item.Obj().String()
|
|
|
|
value2 := other.(*Item).Obj().String()
|
2019-02-13 22:43:38 +03:00
|
|
|
if value1 < value2 {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
if value1 > value2 {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
// the values match so we'll compare IDs, which are always unique.
|
|
|
|
return item.ID() < other.(*Item).ID()
|
|
|
|
}
|
|
|
|
|
2019-02-17 23:10:02 +03:00
|
|
|
// fieldBytes returns the raw fields data section
|
|
|
|
func (item *Item) fieldsBytes() []byte {
|
|
|
|
return *(*[]byte)((unsafe.Pointer)(&reflect.SliceHeader{
|
|
|
|
Data: uintptr(unsafe.Pointer(item.data)),
|
|
|
|
Len: item.fieldsDataSize(),
|
|
|
|
Cap: item.fieldsDataSize(),
|
|
|
|
}))
|
|
|
|
}
|
|
|
|
|
|
|
|
// Packed returns true when the item's fields are packed
|
|
|
|
func (item *Item) Packed() bool {
|
|
|
|
return item == nil || item.isPacked()
|
|
|
|
}
|
|
|
|
|
2019-02-15 18:26:55 +03:00
|
|
|
// CopyOverFields overwriting previous fields. Accepts an *Item or []float64
|
|
|
|
func (item *Item) CopyOverFields(from interface{}) {
|
2019-02-17 23:10:02 +03:00
|
|
|
if item == nil {
|
|
|
|
return
|
|
|
|
}
|
2019-02-15 18:26:55 +03:00
|
|
|
var values []float64
|
2019-02-17 23:10:02 +03:00
|
|
|
var fieldBytes []byte
|
|
|
|
var directCopy bool
|
2019-02-15 18:26:55 +03:00
|
|
|
switch from := from.(type) {
|
|
|
|
case *Item:
|
2019-02-17 23:10:02 +03:00
|
|
|
if item.Packed() == from.Packed() {
|
|
|
|
// direct copy the bytes
|
|
|
|
fieldBytes = from.fieldsBytes()
|
|
|
|
directCopy = true
|
|
|
|
} else {
|
|
|
|
// get the values through iteration
|
|
|
|
item.ForEachField(-1, func(value float64) bool {
|
|
|
|
values = append(values, value)
|
|
|
|
return true
|
|
|
|
})
|
|
|
|
}
|
2019-02-15 18:26:55 +03:00
|
|
|
case []float64:
|
|
|
|
values = from
|
|
|
|
}
|
2019-02-17 23:10:02 +03:00
|
|
|
if !directCopy {
|
|
|
|
if item.Packed() {
|
|
|
|
fieldBytes = item.packedGenerateFieldBytes(values)
|
|
|
|
} else {
|
|
|
|
fieldBytes = item.unpackedGenerateFieldBytes(values)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
id := item.ID()
|
|
|
|
newData := make([]byte, len(fieldBytes)+len(id))
|
2019-02-13 22:43:38 +03:00
|
|
|
copy(newData, fieldBytes)
|
2019-02-17 23:10:02 +03:00
|
|
|
copy(newData[len(fieldBytes):], id)
|
|
|
|
item.setFieldsDataSize(len(fieldBytes))
|
2019-02-13 22:43:38 +03:00
|
|
|
if len(newData) > 0 {
|
|
|
|
item.data = unsafe.Pointer(&newData[0])
|
|
|
|
} else {
|
|
|
|
item.data = nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetField set a field value at specified index.
|
|
|
|
func (item *Item) SetField(index int, value float64) (updated bool) {
|
2019-02-17 23:10:02 +03:00
|
|
|
if item == nil {
|
|
|
|
return false
|
2019-02-13 22:43:38 +03:00
|
|
|
}
|
2019-02-17 23:10:02 +03:00
|
|
|
if item.Packed() {
|
|
|
|
return item.packedSetField(index, value)
|
|
|
|
}
|
|
|
|
return item.unpackedSetField(index, value)
|
2019-02-13 22:43:38 +03:00
|
|
|
}
|
2019-02-15 18:26:55 +03:00
|
|
|
|
|
|
|
// ForEachField iterates over each field. The count param is the number of
|
|
|
|
// iterations. When count is less than zero, then all fields are returns.
|
|
|
|
func (item *Item) ForEachField(count int, iter func(value float64) bool) {
|
|
|
|
if item == nil {
|
|
|
|
return
|
|
|
|
}
|
2019-02-17 23:10:02 +03:00
|
|
|
if item.Packed() {
|
|
|
|
item.packedForEachField(count, iter)
|
2019-02-15 18:26:55 +03:00
|
|
|
} else {
|
2019-02-17 23:10:02 +03:00
|
|
|
item.unpackedForEachField(count, iter)
|
2019-02-15 18:26:55 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetField returns the value for a field at index.
|
|
|
|
func (item *Item) GetField(index int) float64 {
|
2019-02-16 00:02:04 +03:00
|
|
|
if index < 0 {
|
|
|
|
panic("index out of range")
|
|
|
|
}
|
2019-02-15 18:26:55 +03:00
|
|
|
if item == nil {
|
|
|
|
return 0
|
|
|
|
}
|
2019-02-16 00:02:04 +03:00
|
|
|
if item.Packed() {
|
2019-02-17 23:10:02 +03:00
|
|
|
return item.packedGetField(index)
|
2019-02-15 18:26:55 +03:00
|
|
|
}
|
2019-02-17 23:10:02 +03:00
|
|
|
return item.unpackedGetField(index)
|
2019-02-15 18:26:55 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// HasFields returns true when item has fields
|
|
|
|
func (item *Item) HasFields() bool {
|
2019-02-17 23:10:02 +03:00
|
|
|
return item != nil && item.fieldsDataSize() > 0
|
2019-02-15 18:26:55 +03:00
|
|
|
}
|