-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
249 lines (202 loc) · 4.63 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
package main
import (
"bufio"
"bytes"
"compress/flate"
"encoding/binary"
"flag"
"hash"
"hash/crc32"
"io"
"log"
"os"
"runtime"
"strconv"
"sync"
)
// Declaration of global constants
const (
BLOCK_SIZE = 128 * 1024 // 128 KiB
DICT_SIZE = 32 * 1024 // 32 KiB
TRAILER_SIZE = 8
SUM_SIZE = 8 // 8 bytes
)
// Parsing processes flag
var processes int
func init() {
var (
defaultProcesses = runtime.NumCPU()
usage = "Specify number of goroutines to use for compression"
)
flag.IntVar(&processes, "processes", defaultProcesses, usage)
flag.IntVar(&processes, "p", defaultProcesses, usage)
}
// checksum globals
var checksum hash.Hash32
var checksumChan chan []byte
var nTotalBytes uint32
// This implementation of concurrent compression utilizes the pipelined,
// fan-out, fan-in concurrency pattern as described in
// https://go.dev/blog/pipelines
// There are three stages for a Block to be pipelined through:
// (1) Read stage
// (2) Compress stage
// (3) Write stage
func main() {
flag.Parse()
// Checksum (CRC32-IEEE polynomial)
checksumChan = make(chan []byte)
go func() {
checksum = crc32.NewIEEE()
for data := range checksumChan {
checksum.Write(data)
log.Println("wrote checksum")
}
close(checksumChan)
}()
r := read()
c := compress(r)
//writeHeader()
for output := range c {
write(output)
}
writeTrailer()
/*
compressOutbounds := make([]<-chan *block, processes)
for p := 0; p < processes; p++ {
compressOutbounds[p] = compress(r)
}
for c := range mergeSlice(compressOutbounds) {
write(c)
}
*/
}
// Read stage
func read() <-chan *block {
out := make(chan *block)
go func() {
// Start reading input from Stdin in byte array buffers with BLOCK_SIZE
inputBuffer := make([]byte, BLOCK_SIZE, BLOCK_SIZE)
var numBlocks int
reader := bufio.NewReader(os.Stdin)
numBytes, err := reader.Read(inputBuffer)
for err != io.EOF {
numBytes, err = reader.Read(inputBuffer)
// check if readBuffer is the last block in the buffer
isLastBlock := false
_, err = reader.Peek(1)
if err == bufio.ErrNegativeCount {
isLastBlock = true
}
numBlocks++
b := block{
Index: numBlocks,
LastBlock: isLastBlock,
RawData: inputBuffer,
nRawBytes: numBytes,
}
// checksum
checksumChan <- inputBuffer
nTotalBytes += uint32(numBytes)
log.Println("read block#" + strconv.Itoa(b.Index))
out <- &b
}
close(out)
}()
return out
}
// Compress stage
func compress(in <-chan *block) <-chan *block {
out := make(chan *block)
go func() {
for b := range in {
var buffer bytes.Buffer
flateWriter, err := flate.NewWriter(&buffer, flate.DefaultCompression)
if err != nil {
log.Fatal(err)
}
n, err := io.Copy(flateWriter, bytes.NewReader(b.RawData))
if err != nil {
log.Fatal(err)
}
if !b.LastBlock {
if err := flateWriter.Flush(); err != nil {
log.Fatal(err)
}
}
if err := flateWriter.Close(); err != nil {
log.Fatal(err)
}
b.CompressedData = buffer.Bytes()
b.nCompressedBytes = int(n)
out <- b
log.Println("compressed block#" + strconv.Itoa(b.Index))
}
close(out)
}()
return out
}
func writeHeader() {
w := bufio.NewWriter(os.Stdout)
headerBytes := make([]byte, 10)
headerBytes[0] = 0x1f
headerBytes[1] = 0x8b
headerBytes[2] = 0x08
headerBytes[3] = 0x00
headerBytes[4] = 0x00
headerBytes[5] = 0x00
headerBytes[6] = 0x00
headerBytes[7] = 0x00
headerBytes[8] = 0x00
headerBytes[9] = 0x03
w.Write(headerBytes)
log.Println("wrote header")
}
func writeTrailer() {
w := bufio.NewWriter(os.Stdout)
trailerBuf := make([]byte, TRAILER_SIZE)
le := binary.LittleEndian
le.PutUint32(trailerBuf[:4], checksum.Sum32())
le.PutUint32(trailerBuf[4:8], nTotalBytes)
w.Write(trailerBuf)
log.Println("wrote trailer")
w.Flush()
}
// Write stage
func write(b *block) {
w := bufio.NewWriter(os.Stdout)
w.Write(b.CompressedData)
log.Println("wrote block#" + strconv.Itoa(b.Index))
}
// mergeList fans-in slice of results from the compress goroutines into the write stage
func mergeSlice(compressOutbounds []<-chan *block) <-chan *block {
log.Println("merging")
var wg sync.WaitGroup
out := make(chan *block)
wg.Add(len(compressOutbounds))
for i := 0; i < len(compressOutbounds); i++ {
go func() {
for b := range compressOutbounds[i] {
out <- b
}
wg.Done()
}()
}
go func() {
wg.Wait()
close(out)
}()
return out
}
func merge(cs ...<-chan *block) <-chan *block {
return nil
}
type block struct {
Index int
LastBlock bool
RawData []byte
CompressedData []byte
nRawBytes int
nCompressedBytes int
Err error
}