forked from viert/spatial
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.go
152 lines (128 loc) · 3.35 KB
/
server.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
package spatial
import (
"sync"
"time"
"github.com/dhconnelly/rtreego"
)
// Server represents spatial index server
type Server struct {
tree *rtreego.Rtree
idSubs map[string]map[*Listener]*Listener
idIdx map[string]Indexable
lock sync.RWMutex
}
// New creates and initializes a new spatial Server
func New(minBranch int, maxBranch int) *Server {
t := rtreego.NewTree(2, minBranch, maxBranch)
return &Server{
tree: t,
idSubs: make(map[string]map[*Listener]*Listener),
idIdx: make(map[string]Indexable),
}
}
func (s *Server) subscribeID(l *Listener, id string) {
if _, found := s.idSubs[id]; !found {
s.idSubs[id] = make(map[*Listener]*Listener)
}
s.idSubs[id][l] = l
}
func (s *Server) unsubscribeID(l *Listener, id string) {
if idmap, found := s.idSubs[id]; found {
if _, found = idmap[l]; found {
delete(idmap, l)
}
}
}
func (s *Server) findObjectsByIDs(ids map[string]bool) map[string]Indexable {
s.lock.RLock()
defer s.lock.RUnlock()
results := make(map[string]Indexable)
for id := range ids {
if obj, found := s.idIdx[id]; found {
results[obj.ID()] = obj
}
}
return results
}
func (s *Server) findObjectsByBoundingBoxes(boxes []*boundingBox) map[string]Indexable {
s.lock.RLock()
defer s.lock.RUnlock()
results := make(map[string]Indexable)
for _, box := range boxes {
rect := box.bounds
spatials := s.tree.SearchIntersect(rect)
for _, sp := range spatials {
if idxbl, ok := sp.(Indexable); ok {
if idxbl.Type() > 0 {
results[idxbl.ID()] = idxbl
}
}
}
}
return results
}
func (s *Server) findBoundingBoxesByObject(idx Indexable) []boundingBox {
intersections := s.tree.SearchIntersect(idx.Bounds())
boxes := make([]boundingBox, 0)
for _, obj := range intersections {
idxbl, ok := obj.(Indexable)
if ok && idxbl.Type() == itBoundingBox {
if box, ok := idxbl.(*boundingBox); ok {
boxes = append(boxes, *box)
}
}
}
return boxes
}
// Add adds a new object if it doesn't exist (checking by it's ID())
// or modifies existing one, and notifies listeners
func (s *Server) Add(obj Indexable) {
var rmListeners map[*Listener]*Listener
var addListeners map[*Listener]*Listener
s.lock.Lock()
defer s.lock.Unlock()
if curr, found := s.idIdx[obj.ID()]; found {
// collect listeners to remove obj from
boxes := s.findBoundingBoxesByObject(curr)
rmListeners = collectListeners(boxes)
s.tree.Delete(curr)
}
s.idIdx[obj.ID()] = obj
s.tree.Insert(obj)
boxes := s.findBoundingBoxesByObject(obj)
addListeners = collectListeners(boxes)
for l := range rmListeners {
l.setDirty()
}
for l := range addListeners {
l.setDirty()
}
if lmap, found := s.idSubs[obj.ID()]; found {
for l := range lmap {
l.setDirty()
}
}
}
// Remove removes a given object from the index and notifies listeners
func (s *Server) Remove(obj Indexable) {
s.lock.Lock()
defer s.lock.Unlock()
if curr, found := s.idIdx[obj.ID()]; found {
// collect listeners to remove obj from
boxes := s.findBoundingBoxesByObject(curr)
listeners := collectListeners(boxes)
s.tree.Delete(curr)
for l := range listeners {
l.setDirty()
}
if lmap, found := s.idSubs[obj.ID()]; found {
for l := range lmap {
l.setDirty()
}
}
}
}
// NewListener creates and returns a new listener
func (s *Server) NewListener(chSize int, interval time.Duration) *Listener {
return newListener(s, chSize, interval)
}