-
Notifications
You must be signed in to change notification settings - Fork 5
/
gnparser_stream.go
106 lines (97 loc) · 2.15 KB
/
gnparser_stream.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
package gnparser
import (
"context"
"log/slog"
"sync"
"github.com/gnames/gnparser/ent/nameidx"
"github.com/gnames/gnparser/ent/parsed"
"github.com/gnames/gnparser/ent/parser"
"github.com/gnames/organizer"
)
// ParseNameStream takes an input channel of input.Name and
// returns back a stream of parsed data following the same order as
// the input.
func (gnp gnparser) ParseNameStream(
ctx context.Context,
chIn <-chan nameidx.NameIdx,
chOut chan<- parsed.Parsed,
) {
chUnordered := make(chan organizer.Ordered)
chOrdered := make(chan organizer.Ordered)
var wgWorker, wgOutput sync.WaitGroup
jobs := gnp.cfg.JobsNum
wgWorker.Add(jobs)
wgOutput.Add(1)
for i := jobs; i > 0; i-- {
go gnp.parseStreamWorker(ctx, chIn, chUnordered, &wgWorker)
}
if gnp.cfg.WithNoOrder {
close(chOrdered)
go sendUnordered(ctx, chUnordered, chOut, &wgOutput)
} else {
go organizer.Organize(ctx, chUnordered, chOrdered)
go sendOrdered(ctx, chOrdered, chOut, &wgOutput)
}
wgWorker.Wait()
close(chUnordered)
wgOutput.Wait()
}
func (gnp gnparser) parseStreamWorker(
ctx context.Context,
chIn <-chan nameidx.NameIdx,
chOut chan<- organizer.Ordered,
wg *sync.WaitGroup,
) {
defer wg.Done()
gnp.parser = parser.New()
for v := range chIn {
parseRes := gnp.ParseName(v.NameString)
select {
case <-ctx.Done():
return
case chOut <- parsed.ParsedWithIdx{Parsed: parseRes, Error: nil, Idx: v.Index}:
}
}
}
func sendOrdered(
ctx context.Context,
chOrdered <-chan organizer.Ordered,
chOut chan<- parsed.Parsed,
wg *sync.WaitGroup,
) {
defer wg.Done()
for v := range chOrdered {
var p parsed.Parsed
err := v.Unpack(&p)
if err != nil {
slog.Error("Cannot reorganize data", "error", err)
}
select {
case <-ctx.Done():
return
case chOut <- p:
}
}
close(chOut)
}
func sendUnordered(
ctx context.Context,
chUnordered <-chan organizer.Ordered,
chOut chan<- parsed.Parsed,
wg *sync.WaitGroup,
) {
defer wg.Done()
for v := range chUnordered {
var p parsed.Parsed
err := v.Unpack(&p)
if err != nil {
slog.Error("Cannot reorganize data", "error", err)
}
select {
case <-ctx.Done():
return
case chOut <- p:
}
}
close(chOut)
}