-
-
Notifications
You must be signed in to change notification settings - Fork 36
/
cache.go
87 lines (68 loc) · 2.1 KB
/
cache.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
package main
import (
"github.com/qedus/osmpbf"
"github.com/syndtr/goleveldb/leveldb"
"github.com/syndtr/goleveldb/leveldb/opt"
"log"
"strconv"
)
// queue a leveldb write in a batch
func cacheQueueNode(batch *leveldb.Batch, node *osmpbf.Node) {
id, val := nodeToBytes(node)
batch.Put([]byte(id), []byte(val))
}
// queue a leveldb write in a batch
func cacheQueueWay(batch *leveldb.Batch, way *osmpbf.Way) {
id, val := wayToBytes(way)
batch.Put([]byte(id), []byte(val))
}
// flush a leveldb batch to database and reset batch to 0
func cacheFlush(db *leveldb.DB, batch *leveldb.Batch, sync bool) {
var writeOpts = &opt.WriteOptions{
NoWriteMerge: true,
Sync: sync,
}
err := db.Write(batch, writeOpts)
if err != nil {
log.Fatal(err)
}
batch.Reset()
}
func cacheLookupNodeByID(db *leveldb.DB, id int64) (map[string]string, error) {
stringid := strconv.FormatInt(id, 10)
data, err := db.Get([]byte(stringid), nil)
if err != nil {
log.Println("[warn] fetch failed for node ID:", stringid)
return make(map[string]string, 0), err
}
return bytesToLatLon(data), nil
}
func cacheLookupNodes(db *leveldb.DB, way *osmpbf.Way) ([]map[string]string, error) {
var container []map[string]string
for _, each := range way.NodeIDs {
stringid := strconv.FormatInt(each, 10)
data, err := db.Get([]byte(stringid), nil)
if err != nil {
log.Println("[warn] denormalize failed for way:", way.ID, "node not found:", stringid)
return make([]map[string]string, 0), err
}
container = append(container, bytesToLatLon(data))
}
return container, nil
}
func cacheLookupWayNodes(db *leveldb.DB, wayid int64) ([]map[string]string, error) {
// prefix the key with 'W' to differentiate it from node ids
stringid := "W" + strconv.FormatInt(wayid, 10)
// look up way bytes
reldata, err := db.Get([]byte(stringid), nil)
if err != nil {
log.Println("[warn] lookup failed for way:", wayid, "noderefs not found:", stringid)
return make([]map[string]string, 0), err
}
// generate a way object
var way = &osmpbf.Way{
ID: wayid,
NodeIDs: bytesToIDSlice(reldata),
}
return cacheLookupNodes(db, way)
}