-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathquery.go
More file actions
381 lines (303 loc) · 8.15 KB
/
query.go
File metadata and controls
381 lines (303 loc) · 8.15 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
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
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
package query
import (
"errors"
"log"
"os"
"strconv"
"strings"
"time"
w "github.com/fabricekabongo/loggerhead/world"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promauto"
)
var (
ErrorInvalidQuery = errors.New("invalid query")
version = "1.0"
SaveCounter prometheus.Counter
SaveDuration prometheus.Histogram
DeleteCounter prometheus.Counter
DeleteDuration prometheus.Histogram
PolyCounter prometheus.Counter
PolyDuration prometheus.Histogram
GetCounter prometheus.Counter
GetDuration prometheus.Histogram
)
func init() {
hostname, err := os.Hostname()
if err != nil {
hostname = "unknown"
}
SaveCounter = promauto.NewCounter(prometheus.CounterOpts{
Name: "loggerhead_query_save_total",
Help: "Total number of save queries",
ConstLabels: map[string]string{
"hostname": hostname,
},
})
SaveDuration = promauto.NewHistogram(prometheus.HistogramOpts{
Name: "loggerhead_query_save_duration_nanoseconds",
Help: "Duration of save queries in nanoseconds",
ConstLabels: map[string]string{
"hostname": hostname,
},
})
DeleteCounter = promauto.NewCounter(prometheus.CounterOpts{
Name: "loggerhead_query_delete_total",
Help: "Total number of delete queries",
ConstLabels: map[string]string{
"hostname": hostname,
},
})
DeleteDuration = promauto.NewHistogram(prometheus.HistogramOpts{
Name: "loggerhead_query_delete_duration_nanoseconds",
Help: "Duration of delete queries in nanoseconds",
ConstLabels: map[string]string{
"hostname": hostname,
},
})
PolyCounter = promauto.NewCounter(prometheus.CounterOpts{
Name: "loggerhead_query_query_total",
Help: "Total number of query queries",
ConstLabels: map[string]string{
"hostname": hostname,
},
})
PolyDuration = promauto.NewHistogram(prometheus.HistogramOpts{
Name: "loggerhead_query_query_duration_nanoseconds",
Help: "Duration of query queries in nanoseconds",
ConstLabels: map[string]string{
"hostname": hostname,
},
})
GetCounter = promauto.NewCounter(prometheus.CounterOpts{
Name: "loggerhead_query_get_total",
Help: "Total number of get queries",
ConstLabels: map[string]string{
"hostname": hostname,
},
})
GetDuration = promauto.NewHistogram(prometheus.HistogramOpts{
Name: "loggerhead_query_get_duration_nanoseconds",
Help: "Duration of get queries in nanoseconds",
ConstLabels: map[string]string{
"hostname": hostname,
},
})
}
type EngineInterface interface {
ExecuteQuery(query string) string
}
type Engine struct {
world *w.World
chain []Processor
}
type Processor interface {
Execute(query string) string
CanProcess(query string) bool
}
func NewQueryEngine(world *w.World) EngineInterface {
return &Engine{
world: world,
chain: []Processor{
&GetQueryProcessor{World: world},
&DeleteQueryProcessor{World: world},
&SaveQueryProcessor{World: world},
&PolyQueryProcessor{World: world},
},
}
}
func NewReadQueryEngine(world *w.World) EngineInterface {
return &Engine{
world: world,
chain: []Processor{
&GetQueryProcessor{World: world},
&PolyQueryProcessor{World: world},
},
}
}
func NewWriteQueryEngine(world *w.World) *Engine {
return &Engine{
world: world,
chain: []Processor{
&SaveQueryProcessor{World: world},
&DeleteQueryProcessor{World: world},
},
}
}
func (qp *Engine) World() *w.World {
return qp.world
}
func (qp *Engine) ExecuteQuery(query string) string {
for _, processor := range qp.chain {
if processor.CanProcess(query) {
return processor.Execute(query)
}
}
log.Println(ErrorInvalidQuery.Error(), query)
return version + ",\"" + ErrorInvalidQuery.Error() + "\"\n"
}
type GetQueryProcessor struct {
World *w.World
Processor
}
func (p *GetQueryProcessor) Execute(query string) string {
defer GetCounter.Inc()
start := time.Now()
if p.World == nil {
panic("world is nil")
}
if !p.CanProcess(query) {
panic("call CanProcess before calling me")
}
//GET NamespaceID LocationID
chunks := strings.Split(query, " ")
if chunks[0] != "GET" { //No trust
panic("Invalid DELETE query")
}
namespaceID := chunks[1]
locationID := chunks[2]
location, ok := p.World.GetLocation(namespaceID, locationID)
if !ok {
return version + ",done\n"
}
stringBuilder := strings.Builder{}
stringBuilder.WriteString(version + "," + location.String() + "\n")
stringBuilder.WriteString(version + ",done\n")
elapsed := time.Since(start)
GetDuration.Observe(float64(elapsed.Nanoseconds()))
return stringBuilder.String()
}
func (*GetQueryProcessor) CanProcess(query string) bool {
chunks := strings.Split(query, " ")
if len(chunks) != 3 {
return false
}
return chunks[0] == "GET"
}
type DeleteQueryProcessor struct {
World *w.World
Processor
}
func (p *DeleteQueryProcessor) Execute(query string) string {
defer DeleteCounter.Inc()
start := time.Now()
if p.World == nil {
panic("world is nil")
}
if !p.CanProcess(query) {
panic("call CanProcess before calling me")
}
//DELETE NamespaceID LocationID
chunks := strings.Split(query, " ")
if chunks[0] != "DELETE" { //No trust
panic("Invalid DELETE query")
}
namespaceID := chunks[1]
locationID := chunks[2]
p.World.Delete(namespaceID, locationID)
elapsed := time.Since(start)
DeleteDuration.Observe(float64(elapsed.Nanoseconds()))
return version + ",deleted\n"
}
func (*DeleteQueryProcessor) CanProcess(query string) bool {
chunks := strings.Split(query, " ")
if len(chunks) != 3 {
return false
}
return chunks[0] == "DELETE"
}
type SaveQueryProcessor struct {
World *w.World
}
func (p *SaveQueryProcessor) Execute(query string) string {
defer SaveCounter.Inc()
start := time.Now()
if p.World == nil {
panic("world is nil")
}
if !p.CanProcess(query) {
panic("call CanProcess before calling me")
}
//SAVE NamespaceID LocationID Latitude Longitude
chunks := strings.Split(query, " ")
if chunks[0] != "SAVE" { //No trust
panic("Invalid SAVE query")
}
namespaceID := chunks[1]
locationID := chunks[2]
latitude := chunks[3]
longitude := chunks[4]
latFloat, err := strconv.ParseFloat(latitude, 64)
if err != nil {
return version + "," + "\"Invalid float64 value for latitude\"\n"
}
lonFloat, err := strconv.ParseFloat(longitude, 64)
if err != nil {
return version + "," + "\"Invalid float64 value for longitude\"\n"
}
err = p.World.Save(namespaceID, locationID, latFloat, lonFloat)
if err != nil {
return version + ",\"" + err.Error() + "\"\n"
}
elapsed := time.Since(start)
SaveDuration.Observe(float64(elapsed.Nanoseconds()))
return version + ",saved\n"
}
func (*SaveQueryProcessor) CanProcess(query string) bool {
chunks := strings.Split(query, " ")
if len(chunks) != 5 {
return false
}
return chunks[0] == "SAVE"
}
type PolyQueryProcessor struct {
World *w.World
}
func (p *PolyQueryProcessor) Execute(query string) string {
defer PolyCounter.Inc()
start := time.Now()
if p.World == nil {
panic("world is nil")
}
if !p.CanProcess(query) {
panic("call CanProcess before calling me")
}
//POLY Latitude1 Longitude1 Latitude2 Longitude2
chunks := strings.Split(query, " ")
if chunks[0] != "POLY" { //No trust
panic("Invalid POLY query")
}
ns := chunks[1]
lat1, err := strconv.ParseFloat(chunks[2], 64)
if err != nil {
return version + "," + "\"Invalid float64 value for latitude1\"\n"
}
lon1, err := strconv.ParseFloat(chunks[3], 64)
if err != nil {
return version + "," + "\"Invalid float64 value for longitude1\"\n"
}
lat2, err := strconv.ParseFloat(chunks[4], 64)
if err != nil {
return version + "," + "\"Invalid float64 value for latitude2\"\n"
}
lon2, err := strconv.ParseFloat(chunks[5], 64)
if err != nil {
return version + "," + "\"Invalid float64 value for longitude2\"\n"
}
locations := p.World.QueryRange(ns, lat1, lat2, lon1, lon2)
var result strings.Builder
for _, location := range locations {
result.WriteString(version + "," + location.String() + "\n")
}
result.WriteString(version + ",done\n")
elapsed := time.Since(start)
PolyDuration.Observe(float64(elapsed.Nanoseconds()))
return result.String()
}
func (*PolyQueryProcessor) CanProcess(query string) bool {
chunks := strings.Split(query, " ")
if len(chunks) != 6 {
return false
}
return chunks[0] == "POLY"
}