forked from paulmach/osm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinterval.go
354 lines (292 loc) · 9.78 KB
/
interval.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
package replication
import (
"bytes"
"compress/gzip"
"context"
"encoding/xml"
"fmt"
"io/ioutil"
"net/http"
"strconv"
"time"
"github.com/paulmach/osm"
)
var _ SeqNum = MinuteSeqNum(0)
var _ SeqNum = HourSeqNum(0)
var _ SeqNum = DaySeqNum(0)
// MinuteSeqStart is the beginning of valid minutely sequence data.
// The few before look to be way more than a minute.
// A quick looks says about 75, 57, 17 for 1, 2, 3 respectively.
const MinuteSeqStart = MinuteSeqNum(4)
// HourSeqStart is the beginning of valid hour sequence data.
// Without deep inspection it looks like 1-10 are from July 2013.
const HourSeqStart = HourSeqNum(11)
// DaySeqStart is the beginning of valid day sequence data.
const DaySeqStart = DaySeqNum(1)
// State returns information about the current replication state.
type State struct {
SeqNum uint64 `json:"seq_num"`
Timestamp time.Time `json:"timestamp"`
TxnMax int `json:"txn_max,omitempty"`
TxnMaxQueried int `json:"txn_max_queries,omitempty"`
}
// SeqNum is an interface type that includes MinuteSeqNum,
// HourSeqNum and DaySeqNum. This is an experiment to implement
// a sum type, a type that can be one of several things only.
type SeqNum interface {
fmt.Stringer
Dir() string
Uint64() uint64
private()
}
func (n MinuteSeqNum) private() {}
func (n HourSeqNum) private() {}
func (n DaySeqNum) private() {}
var _ = SeqNum(MinuteSeqNum(0)).private // for the linters
// MinuteSeqNum indicates the sequence of the minutely diff replication found here:
// http://planet.osm.org/replication/minute
type MinuteSeqNum uint64
// String returns 'minute/%d'.
func (n MinuteSeqNum) String() string {
return fmt.Sprintf("minute/%d", n)
}
// Dir returns the directory of this data on planet osm.
func (n MinuteSeqNum) Dir() string {
return "minute"
}
// Uint64 returns the seq num as a uint64 type.
func (n MinuteSeqNum) Uint64() uint64 {
return uint64(n)
}
// HourSeqNum indicates the sequence of the hourly diff replication found here:
// http://planet.osm.org/replication/hour
type HourSeqNum uint64
// String returns 'hour/%d'.
func (n HourSeqNum) String() string {
return fmt.Sprintf("hour/%d", n)
}
// Dir returns the directory of this data on planet osm.
func (n HourSeqNum) Dir() string {
return "hour"
}
// Uint64 returns the seq num as a uint64 type.
func (n HourSeqNum) Uint64() uint64 {
return uint64(n)
}
// DaySeqNum indicates the sequence of the daily diff replication found here:
// http://planet.osm.org/replication/day
type DaySeqNum uint64
// String returns 'day/%d'.
func (n DaySeqNum) String() string {
return fmt.Sprintf("day/%d", n)
}
// Dir returns the directory of this data on planet osm.
func (n DaySeqNum) Dir() string {
return "day"
}
// Uint64 returns the seq num as a uint64 type.
func (n DaySeqNum) Uint64() uint64 {
return uint64(n)
}
// CurrentMinuteState returns the current state of the minutely replication.
// Delegates to the DefaultDatasource and uses its http.Client to make the request.
func CurrentMinuteState(ctx context.Context) (MinuteSeqNum, *State, error) {
return DefaultDatasource.CurrentMinuteState(ctx)
}
// CurrentMinuteState returns the current state of the minutely replication.
func (ds *Datasource) CurrentMinuteState(ctx context.Context) (MinuteSeqNum, *State, error) {
s, err := ds.MinuteState(ctx, 0)
if err != nil {
return 0, nil, err
}
return MinuteSeqNum(s.SeqNum), s, err
}
// MinuteState returns the state of the given minutely replication.
// Delegates to the DefaultDatasource and uses its http.Client to make the request.
func MinuteState(ctx context.Context, n MinuteSeqNum) (*State, error) {
return DefaultDatasource.MinuteState(ctx, n)
}
// MinuteState returns the state of the given minutely replication.
func (ds *Datasource) MinuteState(ctx context.Context, n MinuteSeqNum) (*State, error) {
return ds.fetchState(ctx, n)
}
// CurrentHourState returns the current state of the hourly replication.
// Delegates to the DefaultDatasource and uses its http.Client to make the request.
func CurrentHourState(ctx context.Context) (HourSeqNum, *State, error) {
return DefaultDatasource.CurrentHourState(ctx)
}
// CurrentHourState returns the current state of the hourly replication.
func (ds *Datasource) CurrentHourState(ctx context.Context) (HourSeqNum, *State, error) {
s, err := ds.HourState(ctx, 0)
if err != nil {
return 0, nil, err
}
return HourSeqNum(s.SeqNum), s, err
}
// HourState returns the state of the given hourly replication.
// Delegates to the DefaultDatasource and uses its http.Client to make the request.
func HourState(ctx context.Context, n HourSeqNum) (*State, error) {
return DefaultDatasource.HourState(ctx, n)
}
// HourState returns the state of the given hourly replication.
func (ds *Datasource) HourState(ctx context.Context, n HourSeqNum) (*State, error) {
return ds.fetchState(ctx, n)
}
// CurrentDayState returns the current state of the daily replication.
// Delegates to the DefaultDatasource and uses its http.Client to make the request.
func CurrentDayState(ctx context.Context) (DaySeqNum, *State, error) {
return DefaultDatasource.CurrentDayState(ctx)
}
// CurrentDayState returns the current state of the daily replication.
func (ds *Datasource) CurrentDayState(ctx context.Context) (DaySeqNum, *State, error) {
s, err := ds.DayState(ctx, 0)
if err != nil {
return 0, nil, err
}
return DaySeqNum(s.SeqNum), s, err
}
// DayState returns the state of the given daily replication.
// Delegates to the DefaultDatasource and uses its http.Client to make the request.
func DayState(ctx context.Context, n DaySeqNum) (*State, error) {
return DefaultDatasource.DayState(ctx, n)
}
// DayState returns the state of the given daily replication.
func (ds *Datasource) DayState(ctx context.Context, n DaySeqNum) (*State, error) {
return ds.fetchState(ctx, n)
}
func (ds *Datasource) fetchState(ctx context.Context, n SeqNum) (*State, error) {
var url string
if n.Uint64() != 0 {
url = ds.baseSeqURL(n) + ".state.txt"
} else {
url = fmt.Sprintf("%s/replication/%s/state.txt", ds.baseURL(), n.Dir())
}
req, err := http.NewRequest(http.MethodGet, url, nil)
if err != nil {
return nil, err
}
resp, err := ds.client().Do(req.WithContext(ctx))
if err != nil {
return nil, err
}
defer resp.Body.Close()
if resp.StatusCode != 200 {
resp.Body.Close()
return nil, &UnexpectedStatusCodeError{
Code: resp.StatusCode,
URL: url,
}
}
data, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err
}
return decodeIntervalState(data)
}
func decodeIntervalState(data []byte) (*State, error) {
// example
// ---
// #Sat Jul 16 06:14:03 UTC 2016
// txnMaxQueried=836439235
// sequenceNumber=2010580
// timestamp=2016-07-16T06\:14\:02Z
// txnReadyList=
// txnMax=836439235
// txnActiveList=836439008
var (
n int
err error
)
state := &State{}
for _, l := range bytes.Split(data, []byte("\n")) {
parts := bytes.Split(l, []byte("="))
if bytes.Equal(parts[0], []byte("sequenceNumber")) {
n, err = strconv.Atoi(string(bytes.TrimSpace(parts[1])))
if err != nil {
return nil, err
}
state.SeqNum = uint64(n)
} else if bytes.Equal(parts[0], []byte("txnMax")) {
state.TxnMax, err = strconv.Atoi(string(bytes.TrimSpace(parts[1])))
if err != nil {
return nil, err
}
} else if bytes.Equal(parts[0], []byte("txnMaxQueried")) {
state.TxnMaxQueried, err = strconv.Atoi(string(bytes.TrimSpace(parts[1])))
if err != nil {
return nil, err
}
} else if bytes.Equal(parts[0], []byte("timestamp")) {
timeString := string(bytes.TrimSpace(parts[1]))
state.Timestamp, err = decodeTime(timeString)
if err != nil {
return nil, err
}
}
}
return state, nil
}
// Minute returns the change diff for a given minute.
// Delegates to the DefaultDatasource and uses its http.Client to make the request.
func Minute(ctx context.Context, n MinuteSeqNum) (*osm.Change, error) {
return DefaultDatasource.Minute(ctx, n)
}
// Minute returns the change diff for a given minute.
func (ds *Datasource) Minute(ctx context.Context, n MinuteSeqNum) (*osm.Change, error) {
return ds.fetchIntervalData(ctx, ds.changeURL(n))
}
// Hour returns the change diff for a given hour.
// Delegates to the DefaultDatasource and uses its http.Client to make the request.
func Hour(ctx context.Context, n HourSeqNum) (*osm.Change, error) {
return DefaultDatasource.Hour(ctx, n)
}
// Hour returns the change diff for a given hour.
func (ds *Datasource) Hour(ctx context.Context, n HourSeqNum) (*osm.Change, error) {
return ds.fetchIntervalData(ctx, ds.changeURL(n))
}
// Day returns the change diff for a given day.
// Delegates to the DefaultDatasource and uses its http.Client to make the request.
func Day(ctx context.Context, n DaySeqNum) (*osm.Change, error) {
return DefaultDatasource.Day(ctx, n)
}
// Day returns the change diff for a given day.
func (ds *Datasource) Day(ctx context.Context, n DaySeqNum) (*osm.Change, error) {
return ds.fetchIntervalData(ctx, ds.changeURL(n))
}
func (ds *Datasource) fetchIntervalData(ctx context.Context, url string) (*osm.Change, error) {
req, err := http.NewRequestWithContext(ctx, "GET", url, nil)
if err != nil {
return nil, err
}
resp, err := ds.Client.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
if resp.StatusCode != 200 {
return nil, &UnexpectedStatusCodeError{
Code: resp.StatusCode,
URL: url,
}
}
gzReader, err := gzip.NewReader(resp.Body)
if err != nil {
return nil, err
}
defer gzReader.Close()
change := &osm.Change{}
err = xml.NewDecoder(gzReader).Decode(change)
return change, err
}
func (ds *Datasource) changeURL(n SeqNum) string {
return ds.baseSeqURL(n) + ".osc.gz"
}
func (ds *Datasource) baseSeqURL(sn SeqNum) string {
n := sn.Uint64()
return fmt.Sprintf("%s/replication/%s/%03d/%03d/%03d",
ds.baseURL(),
sn.Dir(),
n/1000000,
(n%1000000)/1000,
n%1000)
}