-
Notifications
You must be signed in to change notification settings - Fork 0
/
travel.go
310 lines (225 loc) · 6.46 KB
/
travel.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
package travel
import (
"context"
"fmt"
"github.com/whosonfirst/go-reader"
"github.com/whosonfirst/go-whosonfirst-feature/properties"
wof_reader "github.com/whosonfirst/go-whosonfirst-reader"
"log"
"sync"
"time"
)
// TravelFunc is a callback function to be invoked for each `geojson.Feature` encountered during a travel session.
type TravelFunc func(context.Context, []byte, int64) error
// TravelOptions is a struct containing configuration details for a travel session.
type TravelOptions struct {
// TravelFunc is a callback function to be invoked for each `geojson.Feature` encountered during a travel session.
Callback TravelFunc
// A `reader.Reader` instance used to load GeoJSON Feature data.
Reader reader.Reader
// A boolean flag to indicate whether to record timing information.
Timings bool
// A boolean flag to indcate whether or not the same record should be traveled more than once. If true then records will only be traveled once.
Singleton bool
// A boolean flag to indicate whether a travel session should include the records that a feature supersedes .
Supersedes bool
// A boolean flag to indicate whether a travel session should include the records that a feature is superseded by .
SupersededBy bool
// A boolean flag to indicate whether a travel session should include a feature's parent record.
ParentID bool
// A boolean flag to indicate whether a travel session should include the record in a feature's hierarchy.
Hierarchy bool
}
// DefaultTravelFunc returns a TravelFunc callback function that prints the current step, the feature's ID and name as well as its inception and cessation dates.
func DefaultTravelFunc() (TravelFunc, error) {
f := func(ctx context.Context, body []byte, step int64) error {
id, err := properties.Id(body)
if err != nil {
return fmt.Errorf("Failed to derive ID, %w", err)
}
label, err := properties.Name(body)
if err != nil {
return fmt.Errorf("Failed to derive name, %w", err)
}
inception := properties.Inception(body)
cessation := properties.Cessation(body)
is_deprecated := ""
deprecated, err := properties.IsDeprecated(body)
if err != nil {
return err
}
if deprecated.IsKnown() && deprecated.IsTrue() {
is_deprecated = "DEPRECATED"
}
fmt.Printf("[%d] %d %s [%s] [%s] %s\n", step, id, label, inception, cessation, is_deprecated)
return nil
}
return f, nil
}
// DefaultTravelOptions returns a TravelOptions struct configured as a singleton and to use the DefaultTravelFunc callback, a `null://` reader.
func DefaultTravelOptions() (*TravelOptions, error) {
cb, err := DefaultTravelFunc()
if err != nil {
return nil, err
}
ctx := context.Background()
r, err := reader.NewReader(ctx, "null://")
if err != nil {
return nil, err
}
opts := TravelOptions{
Callback: cb,
Reader: r,
Singleton: true,
Supersedes: false,
SupersededBy: false,
ParentID: false,
Hierarchy: false,
}
return &opts, nil
}
// Traveler is a struct for walking the tree of supersedes or superseded_by relations for a Who's On First record.
type Traveler struct {
// Options is a TravelOptions struct containing configuration details for the travel session.
Options *TravelOptions
mu *sync.RWMutex
travelog map[int64]int
Step int64
}
// Create a new Traveler instance.
func NewTraveler(opts *TravelOptions) (*Traveler, error) {
travelog := make(map[int64]int)
mu := new(sync.RWMutex)
t := Traveler{
Options: opts,
mu: mu,
travelog: travelog,
Step: 0,
}
return &t, nil
}
// Travel the relationships for 'f' (a GeoJSON feature).
func (t *Traveler) TravelFeature(ctx context.Context, f []byte) error {
select {
case <-ctx.Done():
return nil
default:
// pass
}
opts := t.Options
t.mu.RLock()
id, err := properties.Id(f)
if err != nil {
return fmt.Errorf("Failed to derive ID for %d, %w", id, err)
}
visits, visited := t.travelog[id]
if opts.Singleton && visited {
t.mu.RUnlock()
return nil
}
t1 := time.Now()
if opts.Timings {
defer func() {
log.Printf("time to travel feature ID %d %v\n", id, time.Since(t1))
}()
}
t.mu.RUnlock()
t.mu.Lock()
t.Step += 1
step := t.Step
t.mu.Unlock()
cb := opts.Callback
err = cb(ctx, f, step)
if err != nil {
return err
}
t.mu.Lock()
if !visited {
visits = 1
} else {
visits += 1
}
t.travelog[id] = visits
t.mu.Unlock()
wg := new(sync.WaitGroup)
if opts.ParentID {
wg.Add(1)
go func() {
defer wg.Done()
t.travelParent(ctx, f)
}()
}
if opts.Supersedes {
wg.Add(1)
go func() {
defer wg.Done()
t.travelSupersedes(ctx, f)
}()
}
if opts.SupersededBy {
wg.Add(1)
go func() {
defer wg.Done()
t.travelSupersededBy(ctx, f)
}()
}
if opts.Hierarchy {
wg.Add(1)
go func() {
defer wg.Done()
t.travelHierarchies(ctx, f)
}()
}
wg.Wait()
return nil
}
// Travel the relationships for a Who's On First ID.
// The ID must be able to be read by the Traveler's `reader.Reader` instance defined in the `TravelOptions`.
func (t *Traveler) TravelID(ctx context.Context, id int64) error {
select {
case <-ctx.Done():
return nil
default:
// pass
}
opts := t.Options
f, err := wof_reader.LoadBytes(ctx, opts.Reader, id)
if err != nil {
return err
}
return t.TravelFeature(ctx, f)
}
// travelParent() will travel the record for the value of the "wof:parent_id" property in 'f'.
func (t *Traveler) travelParent(ctx context.Context, f []byte) error {
parent_id, err := properties.ParentId(f)
if err != nil {
return fmt.Errorf("Failed to derive parent ID, %w", err)
}
return t.TravelID(ctx, parent_id)
}
// travelSupersedes() will travel the record for the values of the "wof:supersedes" property in 'f'.
func (t *Traveler) travelSupersedes(ctx context.Context, f []byte) error {
supersedes := properties.Supersedes(f)
for _, id := range supersedes {
t.TravelID(ctx, id)
}
return nil
}
// travelSupersededBy() will travel the record for the values of the "wof:superseded_by" property in 'f'.
func (t *Traveler) travelSupersededBy(ctx context.Context, f []byte) error {
superseded_by := properties.SupersededBy(f)
for _, id := range superseded_by {
t.TravelID(ctx, id)
}
return nil
}
// travelHierarchies() will travel the record for the values of the "wof:iherarchy" property in 'f'.
func (t *Traveler) travelHierarchies(ctx context.Context, f []byte) error {
hierarchies := properties.Hierarchies(f)
for _, hier := range hierarchies {
for _, id := range hier {
t.TravelID(ctx, id)
}
}
return nil
}