-
Notifications
You must be signed in to change notification settings - Fork 0
/
mapgraphloader.go
184 lines (154 loc) · 3.34 KB
/
mapgraphloader.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
package wikiassignment
import (
"fmt"
"io"
"github.com/RoaringBitmap/roaring"
"github.com/pkg/errors"
)
type mapGraphLoader struct {
Edges mapGraph
Namespace2IDs map[int]*roaring.Bitmap
nTitle2ID
Err error
}
type pageSourcer interface {
Next() (page, error)
io.Closer
}
type page struct {
ID uint32
Namespace int
Title string
}
func (gl *mapGraphLoader) AddNodes(nss ...pageSourcer) *mapGraphLoader {
for _, ns := range nss {
if gl.Err != nil {
return gl
}
nodes := roaring.NewBitmap()
for _, ids := range gl.Namespace2IDs {
nodes.Or(ids)
}
var p page
var err error
next := ns.Next
for p, err = next(); err == nil; p, err = next() {
if nodes.Contains(p.ID) {
err = errors.Errorf("Error: adding duplicate node with id %v", p.ID)
break
}
nodes.Add(p.ID)
gl.AddPage(p.Namespace, p.Title, p.ID)
nsSet, ok := gl.Namespace2IDs[p.Namespace]
if !ok {
nsSet = roaring.NewBitmap()
gl.Namespace2IDs[p.Namespace] = nsSet
}
nsSet.Add(p.ID)
}
gl.Err = ns.Close()
if err != io.EOF { // err != nil due to "for" condition
gl.Err = err
}
}
return gl
}
type edge struct {
From, To uint32
}
type edgeSourcer interface {
Next() (edge, error)
io.Closer
}
func (gl *mapGraphLoader) AddEdges(ess ...edgeSourcer) *mapGraphLoader {
for _, es := range ess {
if gl.Err != nil {
return gl
}
var e edge
var err error
next := es.Next
for e, err = next(); err == nil; e, err = next() {
gl.Edges.Add(e.From, e.To)
}
gl.Err = es.Close()
if err != io.EOF { // err != nil due to "for" condition
gl.Err = err
}
}
return gl
}
func (gl *mapGraphLoader) SetAliases(ass ...edgeSourcer) *mapGraphLoader {
redirects := redirectMapping{}
for _, as := range ass {
if gl.Err != nil {
return gl
}
var e edge
var err error
next := as.Next
for e, err = next(); err == nil; e, err = next() {
redirects[e.From] = e.To
}
gl.Err = as.Close()
if err != io.EOF { // err != nil due to "for" condition
gl.Err = err
}
}
title2ID := gl.nTitle2ID.m
for title, from := range title2ID {
to, isRedirect := redirects.From(from)
if !isRedirect {
continue
}
title2ID[title] = to
for _, nsSet := range gl.Namespace2IDs {
if nsSet.Contains(from) {
nsSet.Remove(from)
break
}
}
}
return gl
}
type redirectMapping map[uint32]uint32
func (m redirectMapping) From(from uint32) (to uint32, isRedirect bool) {
to, isRedirect = m[from]
if !isRedirect {
to = from
return
}
delete(m, from) //guarantee of no infinite loop
to, _ = m.From(to)
m[from] = to
return
}
func (gl *mapGraphLoader) Filter(ff ...Filter) *mapGraphLoader {
if gl.Err != nil || len(ff) == 0 {
return gl
}
whitelist := gl.Edges.Nodes()
for _, f := range ff {
nodes := gl.Edges.InSubgraph(roaring.BitmapOf(f.Parents...), f.Dept)
if f.IsWhitelist {
whitelist = nodes
} else {
whitelist.AndNot(nodes)
}
gl.Edges.ApplySubgraph(whitelist)
}
return gl
}
type nTitle2ID struct {
m map[string]uint32
}
func (d nTitle2ID) AddPage(namespace int, title string, ID uint32) {
d.m[prefixedTitle(namespace, title)] = ID
}
func (d nTitle2ID) Title2ID(namespace int, title string) (ID uint32, ok bool) {
ID, ok = d.m[prefixedTitle(namespace, title)]
return
}
func prefixedTitle(namespace int, title string) string {
return fmt.Sprintf("%v %v", namespace, title)
}