-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprocessor_export.go
66 lines (59 loc) · 1.25 KB
/
processor_export.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
package thunder
import (
"context"
"errors"
"golang.org/x/sync/errgroup"
"io"
"sync/atomic"
"time"
)
func (p *Processor) StreamDocuments(ctx context.Context, exporter Exporter, w io.Writer, limit uint64) error {
// Preload exporter
if err := exporter.Load(w); err != nil {
return err
}
// Start indexing
eg, egCtx := errgroup.WithContext(ctx)
// Start source
var inChan = make(chan *Document)
eg.Go(func() error {
defer close(inChan)
return p.Source.Driver.GetDocumentsForProcessor(p, inChan, egCtx, limit)
})
// Start exporter broadcasting
eg.Go(func() error {
var position atomic.Uint64
for {
select {
case <-egCtx.Done():
return egCtx.Err()
case <-time.After(time.Second * 10):
return context.DeadlineExceeded
case doc, open := <-inChan:
if !open {
if position.Load() >= 1 {
return exporter.AfterAll()
}
return nil
}
position.Add(1)
if position.Load() == 1 {
if err := exporter.BeforeAll(); err != nil {
return err
}
}
if err := exporter.WriteDocument(doc, position.Load()); err != nil {
return err
}
}
}
})
err := eg.Wait()
if err != nil {
if errors.Is(err, context.Canceled) {
err = context.Cause(egCtx)
}
return err
}
return nil
}