forked from wlredeye/imposm3
/
process.go
164 lines (149 loc) · 3.8 KB
/
process.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
package pbf
import (
"runtime"
"sync"
"github.com/omniscale/imposm3/element"
)
type parser struct {
pbf *Pbf
coords chan []element.Node
nodes chan []element.Node
ways chan []element.Way
relations chan []element.Relation
nParser int
wg sync.WaitGroup
waySync *barrier
relSync *barrier
}
func NewParser(pbf *Pbf, coords chan []element.Node, nodes chan []element.Node, ways chan []element.Way, relations chan []element.Relation) *parser {
return &parser{
pbf: pbf,
coords: coords,
nodes: nodes,
ways: ways,
relations: relations,
nParser: runtime.NumCPU(),
wg: sync.WaitGroup{},
}
}
func (p *parser) Parse() {
blocks := p.pbf.BlockPositions()
for i := 0; i < p.nParser; i++ {
p.wg.Add(1)
go func() {
for block := range blocks {
p.parseBlock(block)
}
if p.waySync != nil {
p.waySync.doneWait()
}
if p.relSync != nil {
p.relSync.doneWait()
}
p.wg.Done()
}()
}
p.wg.Wait()
if p.nodes != nil {
close(p.nodes)
}
if p.coords != nil {
close(p.coords)
}
if p.ways != nil {
close(p.ways)
}
if p.relations != nil {
close(p.relations)
}
}
// FinishedCoords registers a single function that gets called when all
// nodes and coords are parsed. The callback should block until it is
// safe to continue with parsing of all ways.
// This only works when the PBF file is ordered by type (nodes before ways before relations).
func (p *parser) FinishedCoords(cb func()) {
p.waySync = newBarrier(cb)
p.waySync.add(p.nParser)
}
// FinishedWays registers a single function that gets called when all
// nodes and coords are parsed. The callback should block until it is
// safe to continue with parsing of all ways.
// This only works when the PBF file is ordered by type (nodes before ways before relations).
func (p *parser) FinishedWays(cb func()) {
p.relSync = newBarrier(cb)
p.relSync.add(p.nParser)
}
func (p *parser) parseBlock(pos block) {
block := readPrimitiveBlock(pos)
stringtable := newStringTable(block.GetStringtable())
for _, group := range block.Primitivegroup {
dense := group.GetDense()
if dense != nil {
parsedCoords, parsedNodes := readDenseNodes(dense, block, stringtable)
if len(parsedCoords) > 0 && p.coords != nil {
p.coords <- parsedCoords
}
if len(parsedNodes) > 0 && p.nodes != nil {
p.nodes <- parsedNodes
}
}
parsedCoords, parsedNodes := readNodes(group.Nodes, block, stringtable)
if len(parsedCoords) > 0 && p.coords != nil {
p.coords <- parsedCoords
}
if len(parsedNodes) > 0 && p.nodes != nil {
p.nodes <- parsedNodes
}
parsedWays := readWays(group.Ways, block, stringtable)
if len(parsedWays) > 0 && p.ways != nil {
if p.waySync != nil {
p.waySync.doneWait()
}
p.ways <- parsedWays
}
parsedRelations := readRelations(group.Relations, block, stringtable)
if len(parsedRelations) > 0 && p.relations != nil {
if p.waySync != nil {
p.waySync.doneWait()
}
if p.relSync != nil {
p.relSync.doneWait()
}
p.relations <- parsedRelations
}
}
}
// barrier is a struct to synchronize multiple goroutines.
// Works similar to a WaitGroup. Except:
// Calls callback function once all goroutines called doneWait().
// doneWait() blocks until the callback returns. doneWait() does not
// block after all goroutines were blocked once.
type barrier struct {
synced bool
wg sync.WaitGroup
once sync.Once
callbackWg sync.WaitGroup
callback func()
}
func newBarrier(callback func()) *barrier {
s := &barrier{callback: callback}
s.callbackWg.Add(1)
return s
}
func (s *barrier) add(delta int) {
s.wg.Add(delta)
}
func (s *barrier) doneWait() {
if s.synced {
return
}
s.wg.Done()
s.wg.Wait()
s.once.Do(s.call)
s.callbackWg.Wait()
}
func (s *barrier) call() {
s.callback()
s.synced = true
s.callbackWg.Done()
}