-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
batcher.go
105 lines (86 loc) · 2.31 KB
/
batcher.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
package main
import (
"bufio"
"fmt"
"log"
"os"
"path/filepath"
)
var batchCache = ".batch_cache/"
var batchSize = 10000
func BatchFiles(srcDir, destDir string, filenames []string) error {
for _, filename := range filenames {
err := processBatchfile(srcDir, destDir, filename)
if err != nil {
return fmt.Errorf("error processing file %s: %w", filename, err)
}
}
return nil
}
func processBatchfile(srcDir, destDir, filename string) error {
srcFilePath := filepath.Join(srcDir, filename)
file, err := os.Open(srcFilePath)
if err != nil {
return fmt.Errorf("could not open file %s: %w", srcFilePath, err)
}
defer file.Close()
scanner := bufio.NewScanner(file)
lines := make([]string, 0, batchSize)
batchNum := 1
for scanner.Scan() {
lines = append(lines, scanner.Text())
if len(lines) >= batchSize {
err := writeBatchToFile(destDir, filename, batchNum, lines)
if err != nil {
return err
}
batchNum++
lines = lines[:0] // Reset the slice for the next batch
}
}
if len(lines) > 0 {
err := writeBatchToFile(destDir, filename, batchNum, lines)
if err != nil {
return err
}
}
if err := scanner.Err(); err != nil {
return fmt.Errorf("error reading file %s: %w", filename, err)
}
return nil
}
func writeBatchToFile(destDir, filename string, batchNum int, lines []string) error {
batchFilename := fmt.Sprintf("%s-%d.txt", filename, batchNum)
destFilePath := filepath.Join(destDir, batchFilename)
outFile, err := os.Create(destFilePath)
if err != nil {
return fmt.Errorf("could not create file %s: %w", destFilePath, err)
}
defer outFile.Close()
writer := bufio.NewWriter(outFile)
for _, line := range lines {
_, err := writer.WriteString(line + "\n")
if err != nil {
return fmt.Errorf("error writing to file %s: %w", destFilePath, err)
}
}
err = writer.Flush()
if err != nil {
return fmt.Errorf("error flushing file %s: %w", destFilePath, err)
}
//fmt.Printf("Wrote batch %d to %s\n", batchNum, destFilePath)
return nil
}
func batcher() {
srcDir := wordlistCache
destDir := batchCache
filenames, _ := listFiles(wordlistCache)
err := os.MkdirAll(destDir, 0755)
if err != nil {
log.Fatalf("could not create destination directory: %v", err)
}
err = BatchFiles(srcDir, destDir, filenames)
if err != nil {
log.Fatalf("error batching files: %v", err)
}
}