This repository has been archived by the owner on Jan 10, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
writer.go
199 lines (165 loc) · 4.02 KB
/
writer.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
package sntable
import (
"encoding/binary"
"fmt"
"io"
"github.com/golang/snappy"
)
// WriterOptions define writer specific options.
type WriterOptions struct {
// BlockSize is the minimum uncompressed size in bytes of each table block.
// Default: 4KiB.
BlockSize int
// BlockRestartInterval is the number of keys between restart points
// for delta encoding of keys.
//
// Default: 16.
BlockRestartInterval int
// The compression codec to use.
// Default: SnappyCompression.
Compression Compression
}
func (o *WriterOptions) norm() *WriterOptions {
var oo WriterOptions
if o != nil {
oo = *o
}
if oo.BlockSize < 1 {
oo.BlockSize = 1 << 12
}
if oo.BlockRestartInterval < 1 {
oo.BlockRestartInterval = 16
}
if !oo.Compression.isValid() {
oo.Compression = SnappyCompression
}
return &oo
}
// Writer instances can write a table.
type Writer struct {
w io.Writer
o *WriterOptions
block blockInfo // the current block info
blen int // the number of entries in the current block
soffs []int // section offsets in the current block
buf []byte // plain buffer
snp []byte // snappy buffer
tmp []byte // scratch buffer
index []blockInfo
}
// NewWriter wraps a writer and returns a Writer.
func NewWriter(w io.Writer, o *WriterOptions) *Writer {
return &Writer{
w: w,
o: o.norm(),
tmp: make([]byte, 2*binary.MaxVarintLen64),
}
}
// Append appends a cell to the store.
func (w *Writer) Append(key uint64, value []byte) error {
if w.tmp == nil {
return errClosed
}
if key <= w.block.MaxKey && (w.blen != 0 || len(w.index) != 0) {
return fmt.Errorf("sntable: attempted an out-of-order append, %v must be > %v", key, w.block.MaxKey)
}
if len(w.buf) != 0 && len(w.buf)+len(value)+2*binary.MaxVarintLen64 > w.o.BlockSize {
if err := w.flush(); err != nil {
return err
}
}
skey := key
if w.blen%w.o.BlockRestartInterval == 0 { // new section?
w.soffs = append(w.soffs, len(w.buf))
} else {
skey -= w.block.MaxKey // apply delta-encoding
}
n := binary.PutUvarint(w.tmp[0:], uint64(skey))
n += binary.PutUvarint(w.tmp[n:], uint64(len(value)))
w.buf = append(w.buf, w.tmp[:n]...)
w.buf = append(w.buf, value...)
w.blen++
w.block.MaxKey = key
return nil
}
// Close closes the writer
func (w *Writer) Close() error {
if w.tmp == nil {
return errClosed
}
if err := w.flush(); err != nil {
return err
}
indexOffset := w.block.Offset
if err := w.writeIndex(); err != nil {
return err
}
if err := w.writeFooter(indexOffset); err != nil {
return err
}
w.tmp = nil
return nil
}
func (w *Writer) writeIndex() error {
var prev blockInfo
for i, ent := range w.index {
key := ent.MaxKey
off := ent.Offset
if i != 0 { // delta-encode
key -= prev.MaxKey
off -= prev.Offset
}
prev = ent
n := binary.PutUvarint(w.tmp[0:], uint64(key))
n += binary.PutUvarint(w.tmp[n:], uint64(off))
if err := w.writeRaw(w.tmp[:n]); err != nil {
return err
}
}
return nil
}
func (w *Writer) writeFooter(indexOffset int64) error {
binary.LittleEndian.PutUint64(w.tmp[0:], uint64(indexOffset))
if err := w.writeRaw(w.tmp[:8]); err != nil {
return err
}
if err := w.writeRaw(magic); err != nil {
return err
}
return nil
}
func (w *Writer) writeRaw(p []byte) error {
n, err := w.w.Write(p)
w.block.Offset += int64(n)
return err
}
func (w *Writer) flush() error {
if len(w.buf) == 0 {
return nil
}
for _, o := range w.soffs {
if o > 0 {
binary.LittleEndian.PutUint32(w.tmp, uint32(o))
w.buf = append(w.buf, w.tmp[:4]...)
}
}
binary.LittleEndian.PutUint32(w.tmp, uint32(len(w.soffs)))
w.buf = append(w.buf, w.tmp[:4]...)
var block []byte
switch w.o.Compression {
case SnappyCompression:
w.snp = snappy.Encode(w.snp[:cap(w.snp)], w.buf)
if len(w.snp) < len(w.buf)-len(w.buf)/4 {
block = append(w.snp, blockSnappyCompression)
} else {
block = append(w.buf, blockNoCompression)
}
default:
block = append(w.buf, blockNoCompression)
}
w.index = append(w.index, w.block)
w.buf = w.buf[:0]
w.soffs = w.soffs[:0]
w.blen = 0
return w.writeRaw(block)
}