-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGeeoDB.go
321 lines (256 loc) · 6.97 KB
/
GeeoDB.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
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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
package main
import (
"errors"
"expvar"
"net/http"
"sync"
"geeo.io/GeeoServer/quad"
set "github.com/deckarep/golang-set"
)
var (
// ErrNotImplemented is returned for actions without an implementation
ErrNotImplemented = errors.New("Not Implemented")
// ErrAgentNotFound is returned when agent can't be found
ErrAgentNotFound = errors.New("Agent doesn't exist")
// ErrViewNotFound is returned when View can't be found
ErrViewNotFound = errors.New("View doesn't exist")
// ErrAgentExistsAlready is returned when agent exists already
//ErrAgentExistsAlready = errors.New("Agent already exists")
// ErrPoiExistsAlready is returned when POI exists already
//ErrPoiExistsAlready = errors.New("POI already exists")
numPOIs, numABs, numViews, numAgents *expvar.Int
)
// Persister is the interface you should implement to provide persistence to GeeoDB
type Persister interface {
readPOIsInto(geeodb *GeeoDB) error
readAirBeaconsInto(geeodb *GeeoDB) error
persistPOI(poi *POI) error
removePOI(poi *POI) error
persistAirBeacon(ab *AirBeacon) error
removeAirBeacon(ab *AirBeacon) error
close()
BackupHandleFunc(w http.ResponseWriter, req *http.Request)
JSONDumpHandleFunc(w http.ResponseWriter, req *http.Request)
// TODO LATER chain of Persisters
}
// JSONMessageAble is the interface for objects which can produce JSON change messages
type JSONMessageAble interface {
enterLeaveMessage(enter bool) JSONChangeMessage
}
// GeeoDB handles all the insert/update/delete operations
type GeeoDB struct {
agents map[string]*Agent
v map[string]*View
ab map[string]*AirBeacon
pois map[string]*POI
persister Persister
tree quad.Quad
sync.RWMutex
}
// NewGeeoDB creates a new GeeoDB
func NewGeeoDB(pers Persister, depth int) *GeeoDB {
// set min depth for quad tree... that's a global actually
quad.MinDepth = depth
newdb := &GeeoDB{
agents: make(map[string]*Agent),
v: make(map[string]*View),
ab: make(map[string]*AirBeacon),
pois: make(map[string]*POI),
persister: pers,
tree: quad.NewQuad(),
}
newdb.persister.readPOIsInto(newdb)
newdb.persister.readAirBeaconsInto(newdb)
numPOIs = expvar.NewInt("num_poi")
numABs = expvar.NewInt("num_airbeacon")
numAgents = expvar.NewInt("num_agent")
numViews = expvar.NewInt("num_view")
numPOIs.Set(int64(len(newdb.pois)))
numABs.Set(int64(len(newdb.ab)))
numAgents.Set(int64(len(newdb.agents)))
numViews.Set(int64(len(newdb.v)))
return newdb
}
func (db *GeeoDB) addAgent(id string, conn *wsConn, pub map[string]interface{}) *Agent {
newagent := &Agent{ID: &id, ws: conn, publicData: pub}
db.Lock()
defer db.Unlock()
if _, exists := db.agents[id]; exists {
log.Error("[BUG] should not attempt to add existing agent ", id)
}
db.agents[id] = newagent
numAgents.Add(1)
return newagent
}
func (db *GeeoDB) removeAgent(id string) {
db.Lock()
defer db.Unlock()
agent, ok := db.agents[id]
if ok {
if agent.GetPoint() != nil {
db.tree.RemovePoint(agent)
}
delete(db.agents, id)
numAgents.Add(-1)
return
}
log.Warn("should not remove inexisting agent ", id)
}
func (db *GeeoDB) updateAgentPosition(id string, pos *quad.Point) *quad.Point {
db.Lock()
defer db.Unlock()
agent, ok := db.agents[id]
var oldPosition *quad.Point
if ok {
oldPosition = agent.GetPoint()
if oldPosition == nil {
agent.SetPoint(pos)
db.tree.AddPoint(agent)
return oldPosition
}
db.tree.RemovePoint(agent)
agent.SetPoint(pos)
db.tree.AddPoint(agent)
return oldPosition
}
log.Error("Agent not found when updating position ", id)
return nil
}
func (db *GeeoDB) getPointLikeIn(pos *quad.Rect) set.Set {
db.RLock()
defer db.RUnlock()
res := db.tree.GetPointsIn(pos)
resultset := set.NewThreadUnsafeSet() // no need for thread safety
for _, each := range res {
resultset.Add(each)
}
return resultset
}
func (db *GeeoDB) addView(id string, conn *wsConn) *View {
v := &View{id: &id, ws: conn}
db.Lock()
defer db.Unlock()
numViews.Add(1)
db.v[id] = v
return v
}
func (db *GeeoDB) removeView(id string) {
db.Lock()
defer db.Unlock()
v, ok := db.v[id]
if ok {
if v.GetRect() == nil {
return
}
db.tree.RemoveRect(v)
delete(db.v, id)
numViews.Add(-1)
}
}
func (db *GeeoDB) updateViewPosition(id string, pos *quad.Rect) *quad.Rect {
db.Lock()
defer db.Unlock()
v, ok := db.v[id]
var oldPosition *quad.Rect
if ok {
oldPosition = v.GetRect()
if oldPosition == nil {
v.SetRect(pos)
db.tree.AddRect(v)
return oldPosition
}
db.tree.MoveRect(v, pos)
}
return oldPosition
}
func (db *GeeoDB) addAirBeacon(id string, pos *quad.Rect, publicData map[string]interface{}, creator *string) *AirBeacon {
db.Lock()
defer db.Unlock()
ab := &AirBeacon{id: &id, rect: pos, publicData: publicData, creator: creator}
db.ab[id] = ab
db.tree.AddRect(ab)
db.persister.persistAirBeacon(ab)
numABs.Add(1)
return ab
}
// _loadPOI is really used to batch load POIs into the db without blocking or checks
func (db *GeeoDB) _loadAirBeacon(id string, pos *quad.Rect, publicData map[string]interface{}, creator *string) *AirBeacon {
airBeacon := &AirBeacon{
id: &id,
publicData: publicData,
creator: creator,
}
airBeacon.SetRect(pos)
db.Lock()
defer db.Unlock()
db.ab[id] = airBeacon
db.tree.AddRect(airBeacon)
return airBeacon
}
func (db *GeeoDB) removeAirBeacon(id string) {
db.Lock()
defer db.Unlock()
ab, ok := db.ab[id]
if ok {
db.tree.RemoveRect(ab)
db.persister.removeAirBeacon(ab)
numABs.Add(-1)
}
}
func (db *GeeoDB) getRectLikeWithPoint(pos *quad.Point) set.Set {
if pos == nil {
log.Error("getRectLikeWithPoint for nil. This shouldn't happen")
}
db.RLock()
defer db.RUnlock()
res := db.tree.GetRectsWithPoint(pos, quad.AcceptAll)
return res
}
func (db *GeeoDB) getViewsWithPoint(pos *quad.Point) set.Set {
if pos == nil {
log.Error("getViewsWithPoint for nil. This shouldn't happen")
}
db.RLock()
defer db.RUnlock()
res := db.tree.GetRectsWithPoint(pos, func(each quad.RectLike) bool {
_, ok := each.(*View)
return ok
})
return res
}
// _loadPOI is really used to batch load POIs into the db without blocking or checks
func (db *GeeoDB) _loadPOI(id string, pos *quad.Point, publicData map[string]interface{}, creator *string) *POI {
poi := &POI{
id: &id,
publicData: publicData,
creator: creator,
}
poi.SetPoint(pos)
db.Lock()
defer db.Unlock()
db.pois[id] = poi
db.tree.AddPoint(poi)
return poi
}
func (db *GeeoDB) addPOI(id string, pos *quad.Point, publicData map[string]interface{}, creator *string) *POI {
if _, exists := db.pois[id]; exists {
log.Error("[BUG] should not attempt to add existing poi ", id)
}
poi := db._loadPOI(id, pos, publicData, creator)
db.persister.persistPOI(poi)
numPOIs.Add(1)
return poi
}
func (db *GeeoDB) removePOI(poi *POI) {
db.Lock()
defer db.Unlock()
_, ok := db.pois[*poi.id]
if ok {
delete(db.pois, *poi.id)
db.tree.RemovePoint(poi)
db.persister.removePOI(poi)
numPOIs.Add(-1)
} else {
log.Warn("should not attempt to remove missing poi ", poi.id)
}
}