-
Notifications
You must be signed in to change notification settings - Fork 3
/
waysdb.go
161 lines (142 loc) · 3.02 KB
/
waysdb.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
package main
import (
"encoding/binary"
"encoding/json"
"github.com/boltdb/bolt"
)
var (
waysBucket = []byte("ways")
relationsBucket = []byte("relations")
locationsBucket = []byte("locations")
centroidsBucket = []byte("centroids")
)
type WaysDb struct {
db *bolt.DB
}
func OpenWaysDb(path string) (*WaysDb, error) {
db, err := bolt.Open(path, 0666, nil)
if err != nil {
return nil, err
}
defer func() {
if db != nil {
db.Close()
}
}()
err = db.Update(func(tx *bolt.Tx) error {
names := [][]byte{
waysBucket,
relationsBucket,
locationsBucket,
centroidsBucket,
}
for _, name := range names {
_, err := tx.CreateBucketIfNotExists(name)
if err != nil {
return err
}
}
return nil
})
if err != nil {
return nil, err
}
waysDb := &WaysDb{
db: db,
}
db = nil
return waysDb, nil
}
func (db *WaysDb) Close() error {
if db.db != nil {
return db.db.Close()
}
return nil
}
func makeByteKey(id int64) []byte {
buf := make([]byte, 9)
n := binary.PutVarint(buf, id)
return buf[:n]
}
func (db *WaysDb) putJson(bucket []byte, id int64, o interface{}) error {
data, err := json.Marshal(o)
if err != nil {
return err
}
key := makeByteKey(id)
return db.db.Update(func(tx *bolt.Tx) error {
return tx.Bucket(bucket).Put(key, data)
})
}
func (db *WaysDb) getJson(bucket []byte, id int64, o interface{}) (bool, error) {
key := makeByteKey(id)
found := false
err := db.db.View(func(tx *bolt.Tx) error {
data := tx.Bucket(bucket).Get(key)
if data == nil {
return nil
}
found = true
return json.Unmarshal(data, o)
})
return found, err
}
func (db *WaysDb) Put(w *Linestring) error {
return db.putJson(waysBucket, w.Id, w)
}
func (db *WaysDb) Get(id int64) (*Linestring, error) {
w := &Linestring{}
ok, err := db.getJson(waysBucket, id, w)
if !ok {
w = nil
}
return w, err
}
func (db *WaysDb) PutRelation(r *Relation) error {
return db.putJson(relationsBucket, r.Id, r)
}
func (db *WaysDb) GetRelation(id int64) (*Relation, error) {
r := &Relation{}
ok, err := db.getJson(relationsBucket, id, r)
if !ok {
r = nil
}
return r, err
}
func (db *WaysDb) PutLocation(id int64, doc *Location) error {
return db.putJson(locationsBucket, id, doc)
}
func (db *WaysDb) GetLocation(id int64) (*Location, error) {
doc := &Location{}
ok, err := db.getJson(locationsBucket, id, doc)
if !ok {
doc = nil
}
return doc, err
}
func (db *WaysDb) HasLocation(id int64) (bool, error) {
ok := false
key := makeByteKey(id)
err := db.db.View(func(tx *bolt.Tx) error {
data := tx.Bucket(locationsBucket).Get(key)
ok = data != nil
return nil
})
return ok, err
}
func (db *WaysDb) PutCentroid(id int64, doc *Centroid) error {
return db.putJson(centroidsBucket, id, doc)
}
func (db *WaysDb) GetCentroid(id int64) (*Centroid, error) {
doc := &Centroid{}
ok, err := db.getJson(centroidsBucket, id, doc)
if !ok {
doc = nil
}
return doc, err
}
func (db *WaysDb) DeleteBucket(name string) error {
return db.db.Update(func(tx *bolt.Tx) error {
return tx.DeleteBucket([]byte(name))
})
}