-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathserver.go
More file actions
182 lines (158 loc) · 4.08 KB
/
server.go
File metadata and controls
182 lines (158 loc) · 4.08 KB
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
package spatial
import (
"sync"
"time"
"github.com/dhconnelly/rtreego"
"github.com/viert/spatial/rtree"
)
// Server represents spatial index server
type Server struct {
tree *rtree.SafeRtree
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 := rtree.New(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) {
s.lock.Lock()
defer s.lock.Unlock()
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) {
s.lock.Lock()
defer s.lock.Unlock()
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, filters ...rtreego.Filter) 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, filters...)
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(), filterBoundingBoxes)
boxes := make([]boundingBox, 0)
for _, obj := range intersections {
if idxbl, ok := obj.(Indexable); ok {
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.RLock()
curr, found := s.idIdx[obj.ID()]
s.lock.RUnlock()
if found {
// collect listeners to remove obj from
boxes := s.findBoundingBoxesByObject(curr)
rmListeners = collectListeners(boxes)
s.tree.Delete(curr)
}
s.lock.Lock()
s.idIdx[obj.ID()] = obj
s.lock.Unlock()
s.tree.Insert(obj)
boxes := s.findBoundingBoxesByObject(obj)
addListeners = collectListeners(boxes)
for l := range rmListeners {
l.setDirty()
}
for l := range addListeners {
l.setDirty()
}
s.lock.RLock()
lmap, found := s.idSubs[obj.ID()]
s.lock.RUnlock()
if 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.RLock()
curr, found := s.idIdx[obj.ID()]
s.lock.RUnlock()
if found {
// collect listeners to remove obj from
boxes := s.findBoundingBoxesByObject(curr)
listeners := collectListeners(boxes)
s.tree.Delete(curr)
for l := range listeners {
l.setDirty()
}
s.lock.RLock()
lmap, found := s.idSubs[obj.ID()]
s.lock.RUnlock()
if 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)
}
// SearchIntersect syncronously search for intersections
// Use this for quick searches if you don't need to subscribe for updates
func (s *Server) SearchIntersect(bb *rtreego.Rect, filters ...rtreego.Filter) map[string]Indexable {
s.lock.RLock()
defer s.lock.RUnlock()
results := make(map[string]Indexable)
spatials := s.tree.SearchIntersect(bb, filters...)
for _, sp := range spatials {
if idxbl, ok := sp.(Indexable); ok {
if idxbl.Type() > 0 {
results[idxbl.ID()] = idxbl
}
}
}
return results
}