-
Notifications
You must be signed in to change notification settings - Fork 0
/
csv.go
348 lines (323 loc) · 8.75 KB
/
csv.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
package gscp
import (
"context"
"encoding/csv"
"encoding/json"
"io"
"os"
"path/filepath"
"sort"
"strings"
"sync/atomic"
"github.com/skillian/expr/errors"
"github.com/skillian/workers"
)
type csvProcessor struct {
currentRowIndex int64
reader *csv.Reader
reqs chan csvReq
processingRowIndexes []int64
fieldNames []string
workerErrors chan error
todoRowIndexes []int64
}
func localCSVToDest2(ctx context.Context, source, dest *Spec, config *Config) (err error) {
limit := config.WebSessionPoolLimit
if limit < 1 {
limit = 1
}
sourceFile, err := OpenFilenameRead(source.ArchivePath)
if err != nil {
return err
}
defer errors.Catch(&err, sourceFile.Close)
p := &csvProcessor{
currentRowIndex: -1,
reader: csv.NewReader(sourceFile),
reqs: make(chan csvReq, limit*3/2),
processingRowIndexes: make([]int64, limit),
}
if ctx, err = p.initWorkers(ctx, limit, config); err != nil {
return
}
if err = p.loadFieldNames(ctx, source, dest); err != nil {
return
}
if err = p.loadProgress(source, dest); err != nil {
return
}
err = p.processCSV(ctx, dest)
return errors.Aggregate(
err,
p.saveProgress(err, source, dest),
<-p.workerErrors,
)
}
func (p *csvProcessor) initWorkers(ctx context.Context, limit int, config *Config) (ctx2 context.Context, err error) {
ctx2, cancel := context.WithCancel(ctx)
results := workers.Work(ctx, p.reqs, func(ctx context.Context, id int, req csvReq) (struct{}, error) {
p.startRow(id, req.rowIndex)
err := CopyFromSourceToDestSpec(ctx, req.rowSpec, req.rowDestSpec, config)
if err == nil {
p.doneRow(id)
}
return struct{}{}, err
}, workers.WorkerCount(limit))
errs := make([]error, 0, config.WebSessionPoolLimit)
p.workerErrors = make(chan error)
go func() {
defer close(p.workerErrors)
logger.Verbose0("starting results reader...")
defer logger.Verbose0("Results reader stopped.")
for result := range results {
if result.Err != nil {
errs = append(errs, result.Err)
cancel()
}
}
p.workerErrors <- errors.Aggregate(errs...)
}()
return ctx2, nil
}
func (p *csvProcessor) loadFieldNames(ctx context.Context, source, dest *Spec) (err error) {
p.fieldNames, err = getFieldNamesForSourceSpec(ctx, source, dest)
if err != nil {
return errors.Errorf1From(
err, "failed to get field names for spec: %v",
source,
)
}
return nil
}
func (p *csvProcessor) loadProgress(source, dest *Spec) (err error) {
progress, err := readProgressForSpecs(source, dest)
if err != nil {
return err
}
sort.Sort(int64Sort(progress.PendingLineIndexes))
p.todoRowIndexes = progress.PendingLineIndexes
return nil
}
func (p *csvProcessor) startRow(workerID int, rowIndex int64) {
atomic.StoreInt64(&p.processingRowIndexes[workerID], rowIndex)
}
func (p *csvProcessor) doneRow(workerID int) {
atomic.StoreInt64(&p.processingRowIndexes[workerID], 0)
}
func (p *csvProcessor) processCSV(ctx context.Context, dest *Spec) error {
if err := p.processTodoRows(ctx, dest); err != nil {
return err
}
for {
row, err := p.readNextCSVRow()
switch {
case err == io.EOF:
return nil
case err != nil:
return err
}
if err = p.processCSVRow(ctx, row, dest); err != nil {
return err
}
}
}
func (p *csvProcessor) processTodoRows(ctx context.Context, dest *Spec) error {
for {
todoRowIndex, ok := p.getNextTodoRowIndex()
if !ok {
return nil
}
logger.Info1("(re)processing row %d", todoRowIndex)
row, err := p.skipRowsUntilRowIndex(todoRowIndex)
if err != nil {
return errors.Errorf1From(
err, "failed to process todo line %d",
todoRowIndex,
)
}
if err := p.processCSVRow(ctx, row, dest); err != nil {
return err
}
}
}
func (p *csvProcessor) getNextTodoRowIndex() (rowIndex int64, ok bool) {
if len(p.todoRowIndexes) == 0 {
return 0, false
}
rowIndex = p.todoRowIndexes[0]
p.todoRowIndexes = p.todoRowIndexes[1:]
return rowIndex, true
}
func (p *csvProcessor) skipRowsUntilRowIndex(rowIndex int64) (row []string, err error) {
for {
row, err = p.readNextCSVRow()
switch {
case err != nil:
return nil, err
case p.currentRowIndex == rowIndex:
return row, nil
case p.currentRowIndex > rowIndex:
return nil, errors.Errorf1(
"programming error: skipped row %d",
rowIndex,
)
}
}
}
func (p *csvProcessor) readNextCSVRow() ([]string, error) {
row, err := p.reader.Read()
if err == io.EOF {
return nil, err
}
if err != nil {
return nil, errors.Errorf0From(
err, "failed to read row from CSV",
)
}
p.currentRowIndex++
return row, nil
}
func (p *csvProcessor) processCSVRow(ctx context.Context, row []string, dest *Spec) error {
rowSource, err := p.parseSpecFromRow(row)
if err != nil {
return err
}
rowDest := dest.Copy(func(s *Spec) {
for i, fieldName := range p.fieldNames {
if _, ok := s.Fields[fieldName]; !ok {
s.Fields[fieldName] = row[i]
}
}
})
select {
case <-ctx.Done():
return ctx.Err()
case p.reqs <- csvReq{
rowIndex: p.currentRowIndex,
rowSpec: rowSource,
rowDestSpec: rowDest,
}:
}
return nil
}
func (p *csvProcessor) parseSpecFromRow(row []string) (*Spec, error) {
rowSpec, err := ParseSpec(row[len(row)-1])
if err != nil {
return nil, errors.Errorf1From(
err, "failed to parse row filename "+
"%v as spec",
row[len(row)-1],
)
}
return rowSpec, nil
}
func (p *csvProcessor) saveProgress(processCSVErr error, source, dest *Spec) (err error) {
if processCSVErr == nil {
filename := progressFilenameForSpecs(source, dest)
if err = os.Remove(filename); err != nil {
return errors.Errorf1From(
err, "failed to cleanup progress file %v",
filename,
)
}
return nil
}
pendingLineIndexes := make([]int64, len(p.processingRowIndexes))
for i := range p.processingRowIndexes {
pendingLineIndexes[i] = atomic.LoadInt64(&p.processingRowIndexes[i])
}
return writeProgressForSpecs(indexProgress{
PendingLineIndexes: pendingLineIndexes,
}, source, dest)
}
type csvReq struct {
rowIndex int64
rowSpec *Spec
rowDestSpec *Spec
}
type indexProgress struct {
// PendingLineIndexes holds the line numbers (indexed from 0)
// that the worker processes are working on. If there are
// 8 workers, this slice will have a length of 8.
//
// When recovering, all of the lines from 0 to the minimum
// index in this slice can be skipped in the index file.
// The lines for each number in this slice need to be
// reprocessed. Then every line after the maximum index has
// to be reprocessed.
PendingLineIndexes []int64
}
func readProgressForSpecs(source, dest *Spec) (p indexProgress, err error) {
filename := progressFilenameForSpecs(source, dest)
if _, err = os.Stat(filename); err != nil {
if os.IsNotExist(err) {
err = nil
return
}
}
f, err := OpenFilenameRead(filename)
if err != nil {
return p, errors.Errorf0From(
err, "failed to open progress file",
)
}
defer errors.Catch(&err, f.Close)
err = json.NewDecoder(f).Decode(&p)
return
}
func writeProgressForSpecs(p indexProgress, source, dest *Spec) (err error) {
filename := progressFilenameForSpecs(source, dest)
f, err := OpenFilenameCreate(filename, true)
if err != nil {
return errors.Errorf0From(
err, "failed to open progress file",
)
}
defer errors.Catch(&err, f.Close)
temp := make([]int64, 0, len(p.PendingLineIndexes))
for _, i := range p.PendingLineIndexes {
if i == 0 {
continue
}
temp = append(temp, i)
}
p.PendingLineIndexes = temp
return json.NewEncoder(f).Encode(p)
}
func progressFilenameForSpecs(source, dest *Spec) string {
appendFilenameSectionForSpec := func(parts []string, sp *Spec) []string {
appendIf := func(strs *[]string, s string) {
if s == "" {
return
}
*strs = append(*strs, s)
}
appendIf(&parts, sp.Hostname)
if sp.APIPath != defaultAPIPath {
appendIf(&parts, sp.APIPath)
}
appendIf(&parts, sp.Database)
appendIf(&parts, sp.ArchivePath)
appendIf(&parts, sp.Search)
return parts
}
parts := make([]string, 1, 16)
parts[0] = "gscp"
parts = appendFilenameSectionForSpec(parts, source)
parts = appendFilenameSectionForSpec(parts, dest)
filename := strings.Join(parts, "-")
filename = cleanFilename(filename)
return filepath.Join(
os.TempDir(),
filename+".json",
)
}
type int64Sort []int64
var _ sort.Interface = int64Sort{}
func (s int64Sort) Len() int { return len(s) }
func (s int64Sort) Less(i, j int) bool {
return s[i] < s[j]
}
func (s int64Sort) Swap(i, j int) {
s[i], s[j] = s[j], s[i]
}