-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.go
156 lines (124 loc) · 3.17 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
package main
import (
"bufio"
"flag"
"fmt"
"io"
"log"
"os"
"runtime"
"sync"
"time"
"github.com/jfcg/sixb"
)
var (
numChars = flag.Int("n", 19, "number of first bytes to use when comparing lines")
)
func main() {
flag.Usage = func() {
fmt.Fprintf(os.Stderr, "Usage: %s [OPTION]... FILE...\n", os.Args[0])
fmt.Fprintln(os.Stderr, "Options:")
flag.PrintDefaults()
}
defaultBufSize := 16 * 1024 * 1024
destFile := flag.String("d", "-", "file to write result to")
writeBufferSize := flag.Int("write-buffer-size", defaultBufSize,
"size of write buffer: determines how often data is flushed to disk")
verbose := flag.Bool("v", false, "verbosity")
skipHeader := flag.Bool("skip-header", false, "skip header of each file")
flag.Parse()
fileNames := flag.Args()
if len(fileNames) < 1 {
flag.Usage()
os.Exit(1)
}
fileMetas := make([]FileMeta, len(fileNames))
maxGoroutines := runtime.NumCPU()
guard := make(chan struct{}, maxGoroutines)
var wg sync.WaitGroup
now := time.Now()
if *verbose {
log.Printf("Creating %v memory-mapped files...\n", len(fileNames))
}
for i, file := range fileNames {
guard <- struct{}{}
src, err := os.Open(file)
if err != nil {
log.Fatalf("couldn't open file %v: %v\n", file, err)
}
wg.Add(1)
go func(src *os.File, i int) {
defer wg.Done()
memoryMapFile(src, fileMetas, i, *skipHeader)
<-guard
}(src, i)
defer func(src *os.File) {
if err := src.Close(); err != nil {
log.Fatalf("couldn't close file %v: %v\n", src.Name(), err)
}
}(src)
}
if *verbose {
log.Printf("Created memory-mapped files in %v. Allocating memory to hold lines...\n", time.Since(now))
now = time.Now()
}
wg.Wait()
var numLines int
for _, fileMeta := range fileMetas {
numLines += fileMeta.NumLines
}
lines := make(Lines, numLines)
if *verbose {
log.Printf("Allocated memory for %v lines in %v. Splitting into lines...\n", numLines, time.Since(now))
now = time.Now()
}
var numLinesBefore int
for _, fileMeta := range fileMetas {
guard <- struct{}{}
wg.Add(1)
go func(content []byte, numLinesBefore int) {
defer wg.Done()
lineSplitter(lines, content, numLinesBefore)
<-guard
}(fileMeta.Content, numLinesBefore)
numLinesBefore += fileMeta.NumLines
}
wg.Wait()
if *verbose {
log.Printf("Splitted files into lines in %v. Sorting...\n", time.Since(now))
now = time.Now()
}
lines.Sort()
if *verbose {
log.Printf("Sorted in %v. Writing...\n", time.Since(now))
now = time.Now()
}
var dst io.Writer = os.Stdout
if *destFile != "-" {
dstFile, err := os.Create(*destFile)
if err != nil {
log.Fatalf("couldn't create file %v: %v\n", *destFile, err)
}
defer func() {
if err := dstFile.Close(); err != nil {
log.Fatalf("couldn't close file %v, %v\n", *destFile, err)
}
}()
dst = dstFile
}
w := bufio.NewWriterSize(dst, *writeBufferSize)
defer func() {
if err := w.Flush(); err != nil {
log.Fatalf("couldn't flush writer: %v\n", err)
}
if *verbose {
log.Printf("Wrote in %v. Closing...\n", time.Since(now))
}
}()
for _, l := range lines {
_, err := w.Write(sixb.StB(l))
if err != nil {
log.Fatalf("couldn't write line to file: %v\n", err)
}
}
}