-
Notifications
You must be signed in to change notification settings - Fork 46
/
buffererwriter.go
173 lines (147 loc) · 3.33 KB
/
buffererwriter.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
package orc
import (
"bytes"
"fmt"
"io"
"sync"
)
type BufferedWriter struct {
uncompressedBuffer *bytes.Buffer
encodedBuffer *bytes.Buffer
codec CompressionCodec
chunkSize int
checkpoint uint64
written uint64
sync.Mutex
}
// NewBufferedWriter returns a new BufferedWriter using the provided
// CompressionCodec.
func NewBufferedWriter(codec CompressionCodec) *BufferedWriter {
chunkSize := 1024
switch codec.(type) {
case CompressionNone:
chunkSize = 1
case CompressionZlib:
chunkSize = int(DefaultCompressionChunkSize)
}
return &BufferedWriter{
codec: codec,
uncompressedBuffer: &bytes.Buffer{},
encodedBuffer: &bytes.Buffer{},
chunkSize: chunkSize,
}
}
// WriteByte writes a byte to the underlying buffer.
// If the desired chunk size is reached, the buffer is compressed
func (b *BufferedWriter) WriteByte(c byte) error {
b.Lock()
defer b.Unlock()
if b.uncompressedBuffer.Len() == b.chunkSize {
err := b.spill()
if err != nil {
return err
}
}
_, err := b.uncompressedBuffer.Write([]byte{c})
return err
}
// Write writes the provided byte slice to the underlying buffer.
// If the desired chunk size is reached, the buffer is compressed
func (b *BufferedWriter) Write(p []byte) (int, error) {
b.Lock()
defer b.Unlock()
pos := 0
var remaining int
l := len(p)
c := b.chunkSize - b.uncompressedBuffer.Len()
if c > l {
remaining = l
} else {
remaining = c
}
n, err := b.uncompressedBuffer.Write(p[pos : pos+remaining])
if err != nil {
return 0, err
}
pos += n
l -= n
for l != 0 {
if err := b.spill(); err != nil {
return 0, err
}
c = b.chunkSize - b.uncompressedBuffer.Len()
if c > l {
remaining = l
} else {
remaining = c
}
n, err = b.uncompressedBuffer.Write(p[pos : pos+remaining])
if err != nil {
return 0, err
}
pos += n
l -= n
}
return pos, nil
}
// spill to the encoder to handle the compression and update the number of
// written bytes to the encoded buffer
func (b *BufferedWriter) spill() error {
encoder := b.codec.Encoder(b.encodedBuffer)
l := b.uncompressedBuffer.Len()
n, err := io.Copy(encoder, b.uncompressedBuffer)
if err != nil {
return err
}
if int(n) != l {
return fmt.Errorf("Expected to write %d bytes, wrote %d", l, n)
}
err = encoder.Close()
if err != nil {
return err
}
b.written += uint64(n)
return nil
}
func (b *BufferedWriter) Positions() []uint64 {
b.Lock()
defer b.Unlock()
//TODO: Do we still need the checkpoint?
switch b.codec.(type) {
case CompressionNone:
checkpoint := b.checkpoint
b.checkpoint = b.written
return []uint64{checkpoint}
default:
//TODO: check if this is correct
checkpoint := b.checkpoint
b.checkpoint = b.written
return []uint64{checkpoint}
// return nil
}
}
func (b *BufferedWriter) Flush() error {
b.Lock()
defer b.Unlock()
return b.spill()
}
func (b *BufferedWriter) Read(p []byte) (int, error) {
b.Lock()
defer b.Unlock()
return b.encodedBuffer.Read(p)
}
func (b *BufferedWriter) Len() int {
b.Lock()
defer b.Unlock()
return b.encodedBuffer.Len()
}
// Close flushes any buffered bytes to the underlying writer.
func (b *BufferedWriter) Close() error {
return b.spill()
}
// Reset resets the underlying encoded buffer
func (b *BufferedWriter) Reset() {
b.Lock()
defer b.Unlock()
b.encodedBuffer.Reset()
}