/
index.go
556 lines (484 loc) · 14.4 KB
/
index.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
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
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
package main
import (
"encoding/json"
"errors"
//"fmt"
"io"
"io/ioutil"
"log"
"net/url"
"os"
"path/filepath"
"sort"
"strconv"
"strings"
"sync"
"time"
"github.com/fsnotify/fsnotify"
"github.com/golang/geo/r2"
"github.com/golang/geo/s2"
"github.com/paulmach/go.geojson"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promauto"
)
type Index struct {
Collections map[string]*Collection
mutex sync.RWMutex
PublicPath *url.URL
watcher *fsnotify.Watcher
}
type CollectionMetadata struct {
Name string
Path string
LastModified time.Time
}
type Collection struct {
metadata CollectionMetadata
tileCache *TileCache
dataFile *os.File // temporary file, will be deleted
offset []int64 // offset into dataFile
bbox []s2.Rect
webMercator []r2.Point
id []string
byID map[string]int // "W77" -> 3 if Features[3].ID == "W77"
}
func (c *Collection) Close() {
if c.dataFile != nil {
c.dataFile.Close()
os.Remove(c.dataFile.Name())
}
}
var (
lastDataLoad = promauto.NewGauge(prometheus.GaugeOpts{
Name: "miniwfs_data_load_timestamp",
Help: "Timestamp when data was last loaded, in seconds since the Unix epoch.",
})
numDataLoads = promauto.NewCounter(prometheus.CounterOpts{
Name: "miniwfs_data_loads_total",
Help: "Total number of data loads.",
})
numDataLoadErrors = promauto.NewCounter(prometheus.CounterOpts{
Name: "miniwfs_data_load_errors_total",
Help: "Total number of errors when loading data.",
})
numTileCacheHits = promauto.NewCounter(prometheus.CounterOpts{
Name: "miniwfs_tilecache_hits_total",
Help: "Total number of tile cache hits.",
})
numTileCacheMisses = promauto.NewCounter(prometheus.CounterOpts{
Name: "miniwfs_tilecache_misses_total",
Help: "Total number of tile cache misses.",
})
collectionFeaturesCount = promauto.NewGaugeVec(prometheus.GaugeOpts{
Name: "miniwfs_collection_features",
Help: "Number of features per collection.",
},
[]string{"collection"})
collectionTimestamp = promauto.NewGaugeVec(prometheus.GaugeOpts{
Name: "miniwfs_collection_timestamp",
Help: "Timestamp of the collection, in seconds since the Unix epoch.",
},
[]string{"collection", "stage"})
)
func MakeIndex(collections map[string]string, publicPath *url.URL) (*Index, error) {
index := &Index{
Collections: make(map[string]*Collection),
PublicPath: publicPath,
}
if watcher, err := fsnotify.NewWatcher(); err == nil {
index.watcher = watcher
} else {
return nil, err
}
go index.watchFiles()
for name, path := range collections {
var t0 time.Time // The zero value of type Time is January 1, year 1.
coll, err := readCollection(name, path, t0)
if err != nil {
return nil, err
}
index.Collections[name] = coll
}
for _, c := range index.Collections {
dirPath := filepath.Dir(c.metadata.Path)
if err := index.watcher.Add(dirPath); err != nil {
return nil, err
}
}
return index, nil
}
func (index *Index) Close() {
index.mutex.Lock()
defer index.mutex.Unlock()
for _, c := range index.Collections {
c.Close()
index.watcher.Remove(filepath.Dir(c.metadata.Path))
}
index.Collections = make(map[string]*Collection)
}
func (index *Index) GetCollections() []CollectionMetadata {
index.mutex.RLock()
defer index.mutex.RUnlock()
md := make([]CollectionMetadata, 0, len(index.Collections))
for _, coll := range index.Collections {
md = append(md, coll.metadata)
}
sort.Slice(md, func(i, j int) bool { return md[i].Name < md[j].Name })
return md
}
func (index *Index) GetItem(collection string, id string) (*geojson.Feature, error) {
index.mutex.RLock()
defer index.mutex.RUnlock()
coll := index.Collections[collection]
if coll == nil {
return nil, nil
}
i, ok := coll.byID[id]
if !ok {
return nil, nil
}
offset := coll.offset[i]
jsonLen := int(coll.offset[i+1] - offset - 2)
b := make([]byte, jsonLen)
if _, err := coll.dataFile.ReadAt(b, offset); err != nil {
return nil, err
}
var result geojson.Feature
if err := json.Unmarshal(b, &result); err != nil {
return nil, err
}
return &result, nil
}
// We take both startID and startIndex to be more resilient when our
// data changes while a client is iterating over paged results. If
// startID is a known ID, we start the iteration there; otherwise, we
// start the iteration at the feature whose index is startIndex.
//
// If the collection has not been modified since time ifModifiedSince,
// we return error NotModified (unless ifModifiedSince.IsZero() is true).
func (index *Index) GetItems(collection string, startID string, startIndex int, limit int, bbox s2.Rect,
ifModifiedSince time.Time, ifUnmodifiedSince time.Time, includeLinks bool, out io.Writer) (CollectionMetadata, error) {
// We intentionally return CollectionMetadata and not *CollectionMetadata
// so that the metadata gets copied before unlocking the reader mutex.
// Otherwise, the metadata content could change after returning from
// this function. The same problem does not occur with *WFSFeatureCollection
// because that is freshly allocated from scratch, and its members point to
// objects that are not overwritten.
index.mutex.RLock()
defer index.mutex.RUnlock()
coll := index.Collections[collection]
if coll == nil {
return CollectionMetadata{}, NotFound
}
lastModified := coll.metadata.LastModified.Round(time.Second).UTC()
if !ifUnmodifiedSince.IsZero() && lastModified.After(ifUnmodifiedSince.Round(time.Second).UTC()) {
return coll.metadata, Modified
}
if !ifModifiedSince.IsZero() && !lastModified.After(ifModifiedSince.Round(time.Second).UTC()) {
return coll.metadata, NotModified
}
if limit < 1 {
limit = 1
} else if limit > MaxLimit {
limit = MaxLimit
}
if len(startID) > 0 {
if i, ok := coll.byID[startID]; ok {
startIndex = i
}
}
if startIndex < 0 {
startIndex = 0
}
if _, err := out.Write([]byte(`{"type":"FeatureCollection","features":[`)); err != nil {
return CollectionMetadata{}, err
}
// If we had more data, we could compute s2 cell coverages and only
// check the intersection for features inside the coverage area.
// But we operate on a few thousand features, so let's keep things simple
// for the time being.
bounds := s2.EmptyRect()
var nextID string
var nextIndex int
skip := startIndex
numFeatures := 0
buffer := make([]byte, 0, 50*1024)
for i, featureBounds := range coll.bbox {
if !bbox.Intersects(featureBounds) {
continue
}
if numFeatures >= limit {
nextID = coll.id[i]
nextIndex = i
break
}
if skip > 0 {
skip = skip - 1
continue
}
if numFeatures > 0 {
if _, err := out.Write([]byte{','}); err != nil {
return CollectionMetadata{}, err
}
}
b := buffer
jsonLen := int(coll.offset[i+1] - coll.offset[i] - 2)
if jsonLen > cap(b) {
b = make([]byte, 0, jsonLen)
}
if _, err := coll.dataFile.ReadAt(b[0:jsonLen], coll.offset[i]); err != nil {
return CollectionMetadata{}, err
}
if _, err := out.Write(b[0:jsonLen]); err != nil {
return CollectionMetadata{}, err
}
numFeatures += 1
bounds = bounds.Union(featureBounds)
}
if _, err := out.Write([]byte(`],`)); err != nil {
return CollectionMetadata{}, err
}
type Footer struct {
Links []*WFSLink `json:"links,omitempty"`
BoundingBox []float64 `json:"bbox"`
}
var footer Footer
pathPrefix := index.PublicPath.String()
selfLink := &WFSLink{
Rel: "self",
Title: "self",
Type: "application/geo+json",
}
footer.BoundingBox = EncodeBbox(bounds)
if includeLinks {
selfLink.Href = FormatItemsURL(pathPrefix, collection, startID, startIndex, limit, bbox)
footer.Links = append(footer.Links, selfLink)
if nextIndex > 0 {
nextLink := &WFSLink{
Rel: "next",
Title: "next",
Type: "application/geo+json",
}
nextLink.Href = FormatItemsURL(pathPrefix, collection, nextID, nextIndex, limit, bbox)
footer.Links = append(footer.Links, nextLink)
}
}
encodedFooter, err := json.Marshal(footer)
if err != nil {
return CollectionMetadata{}, err
}
if _, err := out.Write(encodedFooter[1:]); err != nil {
return CollectionMetadata{}, err
}
return coll.metadata, nil
}
func (index *Index) watchFiles() {
// We watch the local file system for changes so we quickly catch modifications.
// Additionally, we check once per minute if the files have changed because
// file system watching has not been very reliable in our experience.
ticker := time.NewTicker(time.Minute)
defer ticker.Stop()
for {
select {
case <-ticker.C:
for _, md := range index.GetCollections() {
index.reloadIfChanged(md)
}
case event, ok := <-index.watcher.Events:
log.Printf("Watcher event: %v\n", event)
if !ok {
return
}
if event.Op&fsnotify.Remove == fsnotify.Remove {
return
}
path := event.Name
md := index.getCollectionMetadata(path)
if md != nil {
index.reloadIfChanged(*md)
}
}
}
}
func (index *Index) GetTile(collection string, zoom int, x int, y int) ([]byte, CollectionMetadata, error) {
index.mutex.RLock()
defer index.mutex.RUnlock()
if x < 0 || y < 0 || zoom < 0 || zoom > 30 {
return nil, CollectionMetadata{}, NotFound
}
tileKey := TileKey{X: uint32(x), Y: uint32(y), Zoom: uint8(zoom)}
coll := index.Collections[collection]
if coll == nil {
return nil, CollectionMetadata{}, NotFound
}
if cached := coll.tileCache.Get(tileKey); cached != nil {
numTileCacheHits.Inc()
return cached, coll.metadata, nil
}
scale := 1 << uint8(zoom)
tileBounds := getTileBounds(zoom, x, y)
tileOrigin := r2.Point{X: float64(x) * 256.0 / float64(scale),
Y: float64(y) * 256.0 / float64(scale)}
var tile Tile
for i, featureBounds := range coll.bbox {
if !tileBounds.Intersects(featureBounds) {
continue
}
p := coll.webMercator[i].Sub(tileOrigin).Mul(float64(scale))
tile.DrawPoint(p)
}
png := tile.ToPNG()
coll.tileCache.Put(tileKey, png)
numTileCacheMisses.Inc()
return png, coll.metadata, nil
}
func (index *Index) reloadIfChanged(md CollectionMetadata) {
if coll, err := readCollection(md.Name, md.Path, md.LastModified); err == nil {
log.Printf("success reading collection %s from %s", md.Name, md.Path)
index.replaceCollection(coll)
} else if err == NotModified {
// log.Printf("no change in collection %s at %s",
// md.Name, md.Path)
} else {
log.Printf("error reading collection %s at %s: %v",
md.Name, md.Path, err)
}
}
func (index *Index) getCollectionMetadata(path string) *CollectionMetadata {
index.mutex.Lock()
defer index.mutex.Unlock()
for _, c := range index.Collections {
if path == c.metadata.Path {
return &c.metadata
}
}
return nil
}
func (index *Index) replaceCollection(c *Collection) {
index.mutex.Lock()
defer index.mutex.Unlock()
if old := index.Collections[c.metadata.Name]; old != nil {
old.Close()
}
index.Collections[c.metadata.Name] = c
}
var Modified error = errors.New("FeatureCollection has been modified")
var NotFound error = errors.New("FeatureCollection not found")
var NotModified error = errors.New("FeatureCollection not modified")
// Returns NotModified if the collection has not been modfied since time ifModifiedSince.
func readCollection(name string, path string, ifModifiedSince time.Time) (*Collection, error) {
absPath, err := filepath.Abs(path)
if err != nil {
numDataLoadErrors.Inc()
return nil, err
}
stat, err := os.Stat(absPath)
if err != nil {
numDataLoadErrors.Inc()
return nil, err
}
if !stat.ModTime().After(ifModifiedSince) {
return nil, NotModified
}
data, err := ioutil.ReadFile(absPath)
if err != nil {
numDataLoadErrors.Inc()
return nil, err
}
coll := &Collection{tileCache: NewTileCache(10000)}
coll.metadata.LastModified = stat.ModTime()
coll.metadata.Name = name
coll.metadata.Path = absPath
var features geojson.FeatureCollection
if err := json.Unmarshal(data, &features); err != nil {
numDataLoadErrors.Inc()
return nil, err
}
dataFile, err := ioutil.TempFile("", "miniwfs-*.geojson")
if err != nil {
return nil, err
}
coll.dataFile = dataFile
headerSize, err := dataFile.Write([]byte(`{"type":"FeatureCollection","features":[\n`))
if err != nil {
coll.Close()
return nil, err
}
pos := int64(headerSize)
numFeatures := len(features.Features)
coll.bbox = make([]s2.Rect, numFeatures)
coll.id = make([]string, numFeatures)
coll.webMercator = make([]r2.Point, numFeatures)
coll.offset = make([]int64, numFeatures+1)
coll.byID = make(map[string]int)
for i, f := range features.Features {
if id := getIDString(f.ID); len(id) > 0 {
coll.id[i] = id
coll.byID[id] = i
}
coll.bbox[i] = computeBounds(f.Geometry)
center := coll.bbox[i].Center()
coll.webMercator[i] = projectWebMercator(center)
if i > 0 {
if _, err := dataFile.Write([]byte(",\n")); err == nil {
pos += 2
} else {
coll.Close()
return nil, err
}
}
coll.offset[i] = pos
encoded, err := json.Marshal(f)
if err != nil {
coll.Close()
return nil, err
}
if numBytes, err := dataFile.Write(encoded); err == nil {
pos = pos + int64(numBytes)
} else {
coll.Close()
return nil, err
}
}
coll.offset[len(coll.offset)-1] = pos + 2 // 2 = len(",\n")
if _, err := dataFile.Write([]byte("\n]}\n")); err != nil {
coll.Close()
return nil, err
}
// RFC 7946 does not define a "properties" member on FeatureCollection,
// only on Feature. We still recognize certain collection properties,
// which is is allowed as per RFC 7946 section 6.1 (Foreign Members).
type collectionProperties struct {
Properties map[string]interface{} `json:"properties"`
}
var props collectionProperties
if err := json.Unmarshal(data, &props); err == nil {
for prop, val := range props.Properties {
if strings.HasSuffix(prop, "_timestamp") {
if s, ok := val.(string); ok {
if t, err := time.Parse(time.RFC3339, s); err == nil {
propName := strings.TrimSuffix(prop, "_timestamp")
if len(propName) > 0 {
collectionTimestamp.WithLabelValues(name, propName).Set(float64(t.UTC().Unix()))
}
}
}
}
}
}
lastDataLoad.SetToCurrentTime()
numDataLoads.Inc()
collectionTimestamp.WithLabelValues(name, "last_modified").Set(float64(coll.metadata.LastModified.UTC().Unix()))
collectionTimestamp.WithLabelValues(name, "loaded").Set(float64(time.Now().UTC().Unix()))
collectionFeaturesCount.WithLabelValues(name).Set(float64(numFeatures))
return coll, nil
}
func getIDString(s interface{}) string {
if str, ok := s.(string); ok {
return str
} else if i, ok := s.(int64); ok {
return strconv.FormatInt(i, 10)
} else {
return ""
}
}