-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaptdata.go
247 lines (216 loc) · 5.4 KB
/
aptdata.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
/*
Package aptdata provides an abstracted interface to a database of airport
data.
Currently, support is provided for downloading data from ourairports.com, and
storing it as msgpacked data in a bolt database. The actual storage backend is
only accessed through an interface we provide, so it should be transparent to
the user.
*/
package aptdata
import (
"fmt"
"io"
"net/http"
"os"
"time"
"github.com/coreos/bbolt"
"github.com/pkg/errors"
"github.com/vmihailenco/msgpack"
)
//AptDB provides an opaque wrapper around a boltdb database. This is returned
//to the user from OpenDB().
type AptDB struct {
boltDB *bolt.DB
}
//ErrUnpopulated provides a testable error condition for a database that exists
//but has not been fully populated with data.
type ErrUnpopulated struct {
msg string
}
//Provides the string representation of the ErrUnpopulated error message.
func (e ErrUnpopulated) Error() (msg string) {
return e.msg
}
//Close closes the connection to the airport database.
func (a *AptDB) Close() error {
return a.boltDB.Close()
}
//Populated checks for the presence of an IsPopulated key in the Meta
//database, and if present confirms that it's true.
func (a *AptDB) Populated() bool {
var isPopulated bool
err := a.boltDB.View(func(tx *bolt.Tx) error {
b := tx.Bucket([]byte("Meta"))
if b == nil {
return ErrUnpopulated{"meta bucket does not exist"}
}
err := msgpack.Unmarshal(b.Get([]byte("IsPopulated")), &isPopulated)
if err != nil {
return err
}
if isPopulated {
return nil
}
return ErrUnpopulated{"populated flag false"}
})
if err != nil {
return false
}
return true
}
//Load will process the downloaded airport and runways files and insert
//records for each entry in the database.
func (a *AptDB) Load(dataDir string) error {
err := loadAirports(a.boltDB, dataDir)
if err != nil {
return err
}
err = loadRunways(a.boltDB, dataDir)
if err != nil {
return err
}
err = loadCountries(a.boltDB, dataDir)
if err != nil {
return err
}
err = loadRegions(a.boltDB, dataDir)
if err != nil {
return err
}
err = a.boltDB.Update(func(tx *bolt.Tx) error {
_, err = tx.CreateBucketIfNotExists([]byte("Meta"))
if err != nil {
return err
}
b := tx.Bucket([]byte("Meta"))
m, _ := msgpack.Marshal(true)
err = b.Put([]byte("IsPopulated"), m)
return err
})
return err
}
//Reload deletes existing entries in the database, then loads new records
//via a call to Load.
func (a *AptDB) Reload(dataDir string) error {
err := a.boltDB.Update(func(tx *bolt.Tx) error {
err := tx.DeleteBucket([]byte("Airports"))
if err != nil {
if err.Error() != "bucket not found" {
return errors.Wrap(err, "airports bucket")
}
}
err = tx.DeleteBucket([]byte("Runways"))
if err != nil {
if err.Error() != "bucket not found" {
return errors.Wrap(err, "runways bucket")
}
}
err = tx.DeleteBucket([]byte("Countries"))
if err != nil {
if err.Error() != "bucket not found" {
return errors.Wrap(err, "countries bucket")
}
}
err = tx.DeleteBucket([]byte("Regions"))
if err != nil {
if err.Error() != "bucket not found" {
return errors.Wrap(err, "regions bucket")
}
}
err = tx.DeleteBucket([]byte("Meta"))
if err != nil {
if err.Error() != "bucket not found" {
return errors.Wrap(err, "meta bucket")
}
}
return nil
})
if err != nil {
return err
}
err = a.Load(dataDir)
return err
}
//downloadDataFile is a utility function for downloading a source file and
//saving it to the specified data directory.
func downloadDataFile(dataDir string, filename string, url string, c chan error) {
fullPath := fmt.Sprintf("%s/%s", dataDir, filename)
out, err := os.Create(fullPath)
if err != nil {
c <- err
return
}
defer out.Close()
response, err := http.Get(url)
if err != nil {
c <- err
return
}
if response.StatusCode != 200 {
c <- fmt.Errorf("response code %d for %s", response.StatusCode, url)
//c <- DownloadError{message: fmt.Sprintf("response code %d for %s", response.StatusCode, url)}
err = out.Close()
if err != nil {
c <- err
return
}
err = os.Remove(fullPath)
if err != nil {
c <- err
}
return
}
defer response.Body.Close()
_, err = io.Copy(out, response.Body)
if err != nil {
c <- err
return
}
c <- nil
}
//OpenDB will open the boltdb and return it wrapped in an AptDB.
func OpenDB(path string) (db *AptDB, err error) {
var boltDB *bolt.DB
//populated := false
boltDB, err = bolt.Open(path, 0644, nil)
if err != nil {
return nil, err
}
return &AptDB{boltDB: boltDB}, err
}
//DownloadData iterates over the named source files and calls downloadDataFile
//for each one.
func DownloadData(dataDir string) (err error) {
files := [4]string{"airports.csv", "runways.csv", "countries.csv", "regions.csv"}
channels := make([]chan error, 4)
_, err = os.Stat(dataDir)
if os.IsNotExist(err) {
err = os.Mkdir(dataDir, 0755)
if err != nil {
return err
}
} else if err != nil {
return err
}
for i, file := range files {
c := make(chan error)
channels[i] = c
go downloadDataFile(dataDir, file, fmt.Sprintf("http://ourairports.com/data/%s", file), c)
}
numDownloaded := 0
for numDownloaded < len(files) {
for _, c := range channels {
select {
case err = <-c:
if err != nil {
return err
}
numDownloaded++
// fmt.Println("DID ONE", files[i]) // logging?
default:
time.Sleep(100 * time.Millisecond) // prevent spin-polling
}
}
}
return nil
}