forked from bluesky-social/indigo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbulk.go
371 lines (292 loc) · 7.66 KB
/
bulk.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
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
package search
import (
"bufio"
"context"
"encoding/hex"
"encoding/json"
"fmt"
"os"
"strconv"
"strings"
"sync"
appbsky "github.com/bluesky-social/indigo/api/bsky"
"github.com/bluesky-social/indigo/atproto/identity"
"github.com/bluesky-social/indigo/atproto/syntax"
"github.com/ipfs/go-cid"
)
type pagerankJob struct {
did syntax.DID
rank float64
}
// BulkIndexPageranks updates the pageranks for the DIDs in the Search Index from a CSV file.
func (idx *Indexer) BulkIndexPageranks(ctx context.Context, pagerankFile string) error {
f, err := os.Open(pagerankFile)
if err != nil {
return fmt.Errorf("failed to open csv file: %w", err)
}
defer f.Close()
// Run 5 pagerank indexers in parallel
for i := 0; i < 5; i++ {
go idx.runPagerankIndexer(ctx)
}
logger := idx.logger.With("source", "bulk_index_pageranks")
queue := make(chan string, 20_000)
wg := &sync.WaitGroup{}
workerCount := 20
for i := 0; i < workerCount; i++ {
wg.Add(1)
go func() {
defer wg.Done()
for line := range queue {
if err := idx.processPagerankCSVLine(line); err != nil {
logger.Error("failed to process line", "err", err)
}
}
}()
}
// Create a scanner to read the file line by line
scanner := bufio.NewScanner(f)
buf := make([]byte, 0, 64*1024)
scanner.Buffer(buf, 1024*1024)
linesRead := 0
// Iterate over each line in the file
for scanner.Scan() {
line := scanner.Text()
queue <- line
linesRead++
if linesRead%100_000 == 0 {
idx.logger.Info("processed csv lines", "lines", linesRead)
}
}
close(queue)
// Check for any scanner errors
if err := scanner.Err(); err != nil {
return fmt.Errorf("error reading csv file: %w", err)
}
wg.Wait()
idx.logger.Info("finished processing csv file", "lines", linesRead)
return nil
}
// BulkIndexPosts indexes posts from a CSV file.
func (idx *Indexer) BulkIndexPosts(ctx context.Context, postsFile string) error {
f, err := os.Open(postsFile)
if err != nil {
return fmt.Errorf("failed to open csv file: %w", err)
}
defer f.Close()
// Run 5 post indexers in parallel
for i := 0; i < 5; i++ {
go idx.runPostIndexer(ctx)
}
logger := idx.logger.With("source", "bulk_index_posts")
queue := make(chan string, 20_000)
wg := &sync.WaitGroup{}
workerCount := 20
for i := 0; i < workerCount; i++ {
wg.Add(1)
go func() {
defer wg.Done()
for line := range queue {
if err := idx.processPostCSVLine(line); err != nil {
logger.Error("failed to process line", "err", err)
}
}
}()
}
// Create a scanner to read the file line by line
scanner := bufio.NewScanner(f)
buf := make([]byte, 0, 64*1024)
scanner.Buffer(buf, 1024*1024)
linesRead := 0
// Iterate over each line in the file
for scanner.Scan() {
line := scanner.Text()
queue <- line
linesRead++
if linesRead%100_000 == 0 {
idx.logger.Info("processed csv lines", "lines", linesRead)
}
}
close(queue)
// Check for any scanner errors
if err := scanner.Err(); err != nil {
return fmt.Errorf("error reading csv file: %w", err)
}
wg.Wait()
idx.logger.Info("finished processing csv file", "lines", linesRead)
return nil
}
// BulkIndexProfiles indexes profiles from a CSV file.
func (idx *Indexer) BulkIndexProfiles(ctx context.Context, profilesFile string) error {
f, err := os.Open(profilesFile)
if err != nil {
return fmt.Errorf("failed to open csv file: %w", err)
}
defer f.Close()
for i := 0; i < 5; i++ {
go idx.runProfileIndexer(ctx)
}
logger := idx.logger.With("source", "bulk_index_profiles")
queue := make(chan string, 20_000)
wg := &sync.WaitGroup{}
workerCount := 20
for i := 0; i < workerCount; i++ {
wg.Add(1)
go func() {
defer wg.Done()
for line := range queue {
if err := idx.processProfileCSVLine(line); err != nil {
logger.Error("failed to process line", "err", err)
}
}
}()
}
// Create a scanner to read the file line by line
scanner := bufio.NewScanner(f)
buf := make([]byte, 0, 64*1024)
scanner.Buffer(buf, 1024*1024)
linesRead := 0
// Iterate over each line in the file
for scanner.Scan() {
line := scanner.Text()
queue <- line
linesRead++
if linesRead%100_000 == 0 {
idx.logger.Info("processed csv lines", "lines", linesRead)
}
}
close(queue)
// Check for any scanner errors
if err := scanner.Err(); err != nil {
return fmt.Errorf("error reading csv file: %w", err)
}
wg.Wait()
idx.logger.Info("finished processing csv file", "lines", linesRead)
return nil
}
func (idx *Indexer) processPagerankCSVLine(line string) error {
// Split the line into DID and rank
parts := strings.Split(line, ",")
if len(parts) != 2 {
return fmt.Errorf("invalid pagerank line: %s", line)
}
did, err := syntax.ParseDID(parts[0])
if err != nil {
return fmt.Errorf("invalid DID: %s", parts[0])
}
rank, err := strconv.ParseFloat(parts[1], 64)
if err != nil {
return fmt.Errorf("invalid pagerank value: %s", parts[1])
}
job := PagerankIndexJob{
did: did,
rank: rank,
}
// Send the job to the pagerank queue
idx.pagerankQueue <- &job
return nil
}
func (idx *Indexer) processPostCSVLine(line string) error {
// CSV is formatted as
// actor_did,rkey,taken_down(time or null),violates_threadgate(False or null),cid,raw(post JSON as hex)
parts := strings.Split(line, ",")
if len(parts) != 6 {
return fmt.Errorf("invalid csv line: %s", line)
}
did, err := syntax.ParseDID(parts[0])
if err != nil {
return fmt.Errorf("invalid DID: %s", parts[0])
}
rkey, err := syntax.ParseRecordKey(parts[1])
if err != nil {
return fmt.Errorf("invalid record key: %s", parts[1])
}
isTakenDown := false
if parts[2] != "" && parts[2] != "null" {
isTakenDown = true
}
violatesThreadgate := false
if parts[3] != "" && parts[3] != "False" {
violatesThreadgate = true
}
if isTakenDown || violatesThreadgate {
return nil
}
cid, err := cid.Parse(parts[4])
if err != nil {
return fmt.Errorf("invalid CID: %s", parts[4])
}
if len(parts[5]) <= 2 {
return nil
}
raw, err := hex.DecodeString(parts[5][2:])
if err != nil {
return fmt.Errorf("invalid raw record (%s/%s): %s", did, rkey, parts[5][2:])
}
post := appbsky.FeedPost{}
if err := json.Unmarshal(raw, &post); err != nil {
return fmt.Errorf("failed to unmarshal post: %w", err)
}
job := PostIndexJob{
did: did,
rkey: rkey.String(),
rcid: cid,
record: &post,
}
// Send the job to the post queue
idx.postQueue <- &job
return nil
}
func (idx *Indexer) processProfileCSVLine(line string) error {
// CSV is formatted as
// actor_did,taken_down(time or null),cid,handle,raw(profile JSON as hex)
parts := strings.Split(line, ",")
if len(parts) != 5 {
return fmt.Errorf("invalid csv line: %s", line)
}
did, err := syntax.ParseDID(parts[0])
if err != nil {
return fmt.Errorf("invalid DID: %s", parts[0])
}
isTakenDown := false
if parts[1] != "" && parts[1] != "null" {
isTakenDown = true
}
if isTakenDown {
return nil
}
// Skip actors without profile records
if parts[2] == "" {
return nil
}
cid, err := cid.Parse(parts[2])
if err != nil {
return fmt.Errorf("invalid CID: %s", parts[2])
}
if len(parts[3]) <= 2 {
return nil
}
raw, err := hex.DecodeString(parts[4][2:])
if err != nil {
return fmt.Errorf("invalid raw record (%s): %s", did, parts[4][2:])
}
profile := appbsky.ActorProfile{}
if err := json.Unmarshal(raw, &profile); err != nil {
return fmt.Errorf("failed to unmarshal profile: %w", err)
}
ident := identity.Identity{DID: did}
handle, err := syntax.ParseHandle(parts[3])
if err != nil {
ident.Handle = syntax.HandleInvalid
} else {
ident.Handle = handle
}
job := ProfileIndexJob{
ident: &ident,
rcid: cid,
record: &profile,
}
// Send the job to the profile queue
idx.profileQueue <- &job
return nil
}