/
gnverifier.go
131 lines (120 loc) · 3.44 KB
/
gnverifier.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
package verifio
import (
"context"
"log"
"sync"
"github.com/gnames/bhlindex/internal/ent/name"
vlib "github.com/gnames/gnlib/ent/verifier"
"github.com/gnames/gnverifier/ent/verifier"
)
type verifiedBatch struct {
names []name.VerifiedName
// in case if we get all results instead of only the best, we need to
// know how many unique names were processed.
namesNum int
}
func (vrf verifio) sendToVerify(
ctx context.Context,
vrfREST verifier.Verifier,
chIn <-chan []name.UniqueName,
chOut chan<- verifiedBatch,
wgExt *sync.WaitGroup,
) {
defer wgExt.Done()
var wg sync.WaitGroup
jobs := 4
wg.Add(jobs)
for i := 0; i < jobs; i++ {
go vrf.verifyWorker(ctx, vrfREST, chIn, chOut, &wg)
}
wg.Wait()
}
func (vrf verifio) verifyWorker(
ctx context.Context,
vrfREST verifier.Verifier,
chIn <-chan []name.UniqueName,
chOut chan<- verifiedBatch,
wg *sync.WaitGroup,
) {
defer wg.Done()
for uns := range chIn {
if len(uns) == 0 {
continue
}
names := make([]string, len(uns))
for i := range uns {
names[i] = uns[i].Name
}
input := vlib.Input{NameStrings: names, WithAllMatches: vrf.cfg.VerifAllResults}
output := vrfREST.Verify(ctx, input)
if len(output.Names) < 1 {
log.Fatalf("Did not get results from verifier")
}
outNames, namesNum := prepareNames(output.Names, uns)
select {
case <-ctx.Done():
return
case chOut <- verifiedBatch{names: outNames, namesNum: namesNum}:
}
}
}
func prepareNames(vns []vlib.Name, uns []name.UniqueName) ([]name.VerifiedName, int) {
res := make([]name.VerifiedName, 0, len(vns))
var count int
for i := range vns {
count++
n := name.VerifiedName{
ID: uns[i].ID,
Name: vns[i].Name,
Cardinality: vns[i].Cardinality,
MatchType: vns[i].MatchType.String(),
Curation: vns[i].Curation.String(),
DataSourcesNumber: vns[i].DataSourcesNum,
OddsLog10: uns[i].OddsLog10,
Occurrences: uns[i].Occurrences,
Error: vns[i].Error,
}
if br := vns[i].BestResult; br != nil {
n.RecordID = br.RecordID
n.EditDistance = br.EditDistance
n.StemEditDistance = br.StemEditDistance
n.MatchedName = br.MatchedName
n.MatchedCanonical = br.MatchedCanonicalFull
n.MatchedCardinality = br.MatchedCardinality
n.CurrentName = br.CurrentName
n.CurrentCanonical = br.CurrentCanonicalFull
n.CurrentCardinality = br.CurrentCardinality
n.Classification = br.ClassificationPath
n.ClassificationRanks = br.ClassificationRanks
n.ClassificationIDs = br.ClassificationIDs
n.DataSourceID = br.DataSourceID
n.DataSourceTitle = br.DataSourceTitleShort
res = append(res, n)
} else if rs := vns[i].Results; len(rs) > 0 {
var sortNum int
for _, v := range rs {
n.RecordID = v.RecordID
n.EditDistance = v.EditDistance
n.StemEditDistance = v.StemEditDistance
n.MatchedName = v.MatchedName
n.MatchedCanonical = v.MatchedCanonicalFull
n.MatchedCardinality = v.MatchedCardinality
n.CurrentName = v.CurrentName
n.CurrentCanonical = v.CurrentCanonicalFull
n.CurrentCardinality = v.CurrentCardinality
n.Classification = v.ClassificationPath
n.ClassificationRanks = v.ClassificationRanks
n.ClassificationIDs = v.ClassificationIDs
n.DataSourceID = v.DataSourceID
n.DataSourceTitle = v.DataSourceTitleShort
n.SortOrder = sortNum
nCopy := n
res = append(res, nCopy)
sortNum++
}
} else {
res = append(res, n)
}
}
return res, count
}