-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
466 lines (367 loc) · 10.8 KB
/
main.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
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
package main
import (
"bufio"
"errors"
"flag"
"fmt"
"os"
"path/filepath"
"strconv"
"strings"
"time"
)
/*
-f ".\file_delimiter.txt" -d ";" -findd H:2:NAME -findd D:2:NAME -findd T:2:QUANTITY
-f ".\file_position.txt" -findp H:2:8:DATE -findp D:2:20:NAME -findp D:22:2:AGE
-f ".\file_position.txt" -findp H:2:8:DATE -findp D:2:20:NAME -findp D:24:20:COUNTRY -findp D:44:15:TELEPHONE
-f ".\file_position.txt" -findp H:2:8:DATE -findp D:2:20:NAME -findp D:24:20:COUNTRY -findp D:44:15:TELEPHONE -o c:\temp\findp.txt
-f ".\file_delimiter.txt" -d ";" -findd H:2:NAME -findd D:2:NAME -findd D:4:COUNTRY -findd D:5:REGION -o c:\temp\findd.txt
-f "c:\temp\filepos\arq pos*.txt" -findp H:2:8:DATE -findp H:17:6:SEQUENTIAL
-f "c:\temp\filepos\arq pos*.txt" -findp D:2:20:NAME -findp D:22:2:AGE -findp D:64:15:TELEPHONE -findp D:89:6:"ITEM NUMBER"
-f "c:\temp\filepos\arq pos*.txt" -findp D:2:20:NAME -findp D:24:20:COUNTRY -findp D:64:15:TELEPHONE -findp D:89:6:"ITEM NUMBER" -o "c:\temp\arq pos.txt"
-f "C:\temp\filedelim\arq delim*.txt" -d ";" -findd H:2:NAME -findd H:4:"FILE SEQ" -findd D:2:NAME -findd D:4:COUNTRY -findd D:5:REGION -findd D:6:TELEPHONE -findd D:7:"ITEM NUMBER" -o "c:\temp\arq delim.txt"
//with parallelism
-f "c:\temp\filepos\arq pos*.txt" -findp D:2:20:NAME -findp D:24:20:COUNTRY -findp D:64:15:TELEPHONE -findp D:89:6:"ITEM NUMBER" -o "c:\temp\arq pos.txt" -p
-f "C:\temp\filedelim\arq delim*.txt" -d ";" -findd H:2:NAME -findd H:4:"FILE SEQ" -findd D:2:NAME -findd D:4:COUNTRY -findd D:5:REGION -findd D:6:TELEPHONE -findd D:7:"ITEM NUMBER" -o "c:\temp\arq delim.txt" -p
*/
type FindPosition struct {
lineBegin string
pos int
size int
name string
}
type FindDelimiter struct {
lineBegin string
nth int
name string
}
type arrayFindP []FindPosition
type arrayFindD []FindDelimiter
var flagFile string
var flagFindP arrayFindP
var flagFindD arrayFindD
var flagDelimiter string
var flagOutputFile string
var flagParallel bool
var max_parallel int = 3
type fnExtract func(path string) ([]string, error)
func init() {
flag.StringVar(&flagFile, "f", "", `cardinality (1) - file ou dir to search, may contem special char *. ex: C:\temp\file.txt, C:\temp\*.txt`)
flag.StringVar(&flagOutputFile, "o", "", "cardinality (0-1) - output file")
flag.Var(&flagFindP, "findp", "extract from positional text file. cardinality (0*) - format = l:p:s:h = get data at line beginning by #l (l is opcional), position #p, size #s, data name #h. p=>1, s=>1")
flag.Var(&flagFindD, "findd", "extract from delimited text file. cardinality (0*) - format = l:n:h = get data at line beginning by #l (l is opcional), n-th element #n, data name #h. n=>1")
flag.StringVar(&flagDelimiter, "d", "", `cardinality (0-1) - delimiter when extracting from delimited files`)
flag.BoolVar(&flagParallel, "p", false, "activate parallel extractions (files quantity must be greater than 3)")
}
func (i *arrayFindP) String() string {
return fmt.Sprint(*i)
}
func (i *arrayFindP) Set(value string) error {
tok := strings.Split(value, ":")
if len(tok) != 4 {
return errors.New("flag format is l:p:s:h")
}
var novaVal FindPosition
for idx, val := range tok {
if idx == 0 {
novaVal.lineBegin = val
} else if idx == 1 {
novaVal.pos, _ = strconv.Atoi(val)
} else if idx == 2 {
novaVal.size, _ = strconv.Atoi(val)
} else if idx == 3 {
novaVal.name = val
}
}
if novaVal.pos < 1 {
return errors.New("position in flag must be equal ou greater than 1")
}
if novaVal.size < 1 {
return errors.New("size in flag must be equal ou greater than 1")
}
//as posições na linha de comando começam com 1, porem os metodos Go para procurar começam por 0
novaVal.pos--
*i = append(*i, novaVal)
//fmt.Println(novaVal)
return nil
}
func (i *arrayFindD) String() string {
return fmt.Sprint(*i)
}
func (i *arrayFindD) Set(value string) error {
tok := strings.Split(value, ":")
if len(tok) != 3 {
return errors.New("flag format is l:n:h")
}
var novaVal FindDelimiter
for idx, val := range tok {
if idx == 0 {
novaVal.lineBegin = val
} else if idx == 1 {
novaVal.nth, _ = strconv.Atoi(val)
} else if idx == 2 {
novaVal.name = val
}
}
if novaVal.nth < 1 {
return errors.New("element number in flag must be equal ou greater than 1")
}
//indexes in flag begin with 1, but begins with 0 in Go
novaVal.nth--
*i = append(*i, novaVal)
//fmt.Println(novaVal)
return nil
}
func main() {
flag.Parse()
if flagFile == "" {
fmt.Println("flag -f is mandatory")
flag.PrintDefaults()
return
}
if len(flagFindP) == 0 && len(flagFindD) == 0 {
fmt.Println("find flags not found. either use findp or findd")
flag.PrintDefaults()
return
}
if len(flagFindP) > 0 && len(flagFindD) > 0 {
fmt.Println("both find flags found. either use findp or findd")
flag.PrintDefaults()
return
}
if len(flagFindD) > 0 && flagDelimiter == "" {
fmt.Println("flag delimiter is mandatory when using findd")
flag.PrintDefaults()
return
}
Process()
}
func Process() {
start := time.Now()
//choose the apropriate Extract function
var fnExtract = getExtractFunction()
if fnExtract == nil {
panic("No extraction function defined for these flags")
}
FindInFiles(flagFile, fnExtract)
elapsed := time.Since(start)
fmt.Printf("duration\t%s\n", elapsed)
}
func getExtractFunction() fnExtract {
//choose the apropriate Extract function
if len(flagFindP) > 0 {
return ExtactDataPosition
}
if len(flagFindD) > 0 {
return ExtactDataDelimiter
}
return nil
}
var allLines []string
func FindInFiles(path string, fnExtract fnExtract) {
allFiles := getAllFilesInDir(path)
allLines = make([]string, 0)
cLines := make(chan []string)
fmt.Printf("searching %d file(s)\n", len(allFiles))
//let's paralelize, qty >= 3 files, max_parallel go routines max
if flagParallel && len(allFiles) >= 3 {
//let's determine o max parallel bases on the number of files to process, max between 3 and 8
//3 to 20 files: max=3
//21 to 50 files: max=5
//more than 51: max=10
switch {
case len(allFiles) <= 20:
max_parallel = 3
case len(allFiles) <= 50:
max_parallel = 5
default:
max_parallel = 8
}
fmt.Printf("using %d go routines\n", max_parallel)
inc := len(allFiles) / max_parallel
if len(allFiles)%max_parallel != 0 {
inc++
}
fmt.Println("chucks of files", inc)
//process := 0
for i, j := 0, 1; i < len(allFiles); i, j = i+inc, j+1 {
ul := i + inc
if ul > len(allFiles) {
ul = len(allFiles)
}
pFiles := allFiles[i:ul]
//process += len(pFiles)
//fmt.Println("processando ", pFiles)
go processChunkFiles(j, pFiles, fnExtract, cLines)
}
// if process != len(allFiles) {
// log.Fatalf("not all files processed! %d<>%d", process, len(allFiles))
// } else {
// fmt.Println("all files processed")
// }
} else {
go processChunkFiles(0, allFiles, fnExtract, cLines)
}
for i := 0; flagParallel && (i < max_parallel) || i < 1; i++ {
var cl = <-cLines
allLines = append(allLines, cl...)
}
close(cLines)
//write the lines in the output file if informed, or in the terminal
if flagOutputFile != "" {
writeOutputFile(allLines, flagOutputFile)
} else {
for _, l := range allLines {
fmt.Println(l)
}
}
}
func processChunkFiles(idRoutine int, pFiles []string, fnExtract fnExtract, ch chan []string) {
//fmt.Println("go routine #", idRoutine)
pLines := make([]string, 0)
for idx, arq := range pFiles {
pLines = append(pLines, fmt.Sprintf("rout %d, file #%d\t%s", idRoutine, idx+1, arq))
lines, err := fnExtract(arq)
if err != nil {
panic(err)
}
pLines = append(pLines, lines...)
}
//fmt.Println("write line to channel: ", len(pLines))
ch <- pLines
}
func getAllFilesInDir(path string) []string {
allFiles, _ := filepath.Glob(path)
return allFiles
}
func ExtactDataPosition(path string) ([]string, error) {
lines := make([]string, 0)
f, err := os.Open(path)
if err != nil {
return lines, err
}
defer f.Close()
//abre um scan
scanner := bufio.NewScanner(f)
numLinha := 0
tipo := ""
dado := ""
found := false
total := 0
//header
var hdr = "Line"
for _, lps := range flagFindP {
hdr = fmt.Sprintf("%s\t%s:%s", hdr, lps.lineBegin, lps.name)
}
lines = append(lines, hdr)
for scanner.Scan() {
numLinha++
linha := scanner.Text()
var dados []string
found = false
for _, lps := range flagFindP {
//search for the lines beginning with l
if len(lps.lineBegin) > 0 {
tipo = linha[:len(lps.lineBegin)]
if tipo != lps.lineBegin {
continue
}
}
found = true
//dado = strUtf8(linha, lps)
dado = substr(linha, lps)
//fmt.Println(dado)
dados = append(dados, dado)
}
if found {
total++
var out = fmt.Sprintf("%d\t%s", numLinha, strings.Join(dados, "\t"))
lines = append(lines, out)
}
}
if err := scanner.Err(); err != nil {
return []string{}, err
}
return lines, nil
}
// go reads in bytes, not in char. must 'convert' the string
// in Go the indices are byte-indices, not character or rune indices. Go stores the UTF-8 encoded byte sequence of texts in a string
func substr(s string, lps FindPosition) string {
counter, startIdx := 0, 0
for i := range s {
if counter == lps.pos {
startIdx = i
}
if counter == lps.pos+lps.size {
return s[startIdx:i]
}
counter++
}
return s[startIdx:]
}
// func strUtf8(str string, lps FindPosition) string {
// return string([]rune(str)[lps.pos : lps.pos+lps.size])
// }
func ExtactDataDelimiter(path string) ([]string, error) {
lines := make([]string, 0)
f, err := os.Open(path)
if err != nil {
return lines, err
}
defer f.Close()
//abre um scan
scanner := bufio.NewScanner(f)
numLinha := 0
tipo := ""
dado := ""
found := false
total := 0
//header
var hdr = "Line"
for _, lps := range flagFindD {
hdr = fmt.Sprintf("%s\t%s:%s", hdr, lps.lineBegin, lps.name)
}
lines = append(lines, hdr)
for scanner.Scan() {
numLinha++
linha := scanner.Text()
var dados []string
found = false
for _, ln := range flagFindD {
//search for the lines beginning with l
if len(ln.lineBegin) > 0 {
tipo = linha[:len(ln.lineBegin)]
if tipo != ln.lineBegin {
continue
}
}
//check if there is at least n elements in the line
tok := strings.Split(linha, flagDelimiter)
if ln.nth > len(tok) {
//"number of elements in the line is inferior a n-th"
fmt.Printf("number of elements in the line is inferior a n-th. %v\n", ln)
found = false
continue
}
found = true
dado = tok[ln.nth]
dados = append(dados, dado)
}
if found {
total++
var out = fmt.Sprintf("%d\t%s", numLinha, strings.Join(dados, "\t"))
lines = append(lines, out)
}
}
if err := scanner.Err(); err != nil {
return lines, err
}
return lines, nil
}
func writeOutputFile(lines []string, outputFile string) {
f, _ := os.Create(outputFile)
defer f.Close()
for _, l := range lines {
fmt.Fprintln(f, l)
}
}