-
Notifications
You must be signed in to change notification settings - Fork 6
/
writer.go
215 lines (187 loc) · 4.55 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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
package sst
import (
"bytes"
"encoding/binary"
"fmt"
"hash/crc32"
"io"
)
const (
BlockRestartInterval = 16
BlockSize = 4096
BlockTrailerLen = 5
)
var table = crc32.MakeTable(crc32.Castagnoli)
type indexEntry struct {
bh blockHandle
keyLen int
}
type Writer struct {
writer io.Writer
offset uint64
prevKey []byte
pendingBH blockHandle
indexKeys []byte
indexEntries []indexEntry
buf bytes.Buffer
nEntries int
restarts []uint32
tmp [50]byte
}
func NewWriter(w io.Writer) *Writer {
return &Writer{
writer: w,
prevKey: make([]byte, 0, 256),
restarts: make([]uint32, 0, 256),
}
}
func (w *Writer) Set(key, value []byte) error {
if bytes.Compare(w.prevKey, key) >= 0 {
return fmt.Errorf("Set called with unordered keys: %q, %q", w.prevKey, key)
}
w.flushPendingBH(key)
w.append(key, value, w.nEntries%BlockRestartInterval == 0)
// If the estimated block size is sufficiently large, finish the current block.
if w.buf.Len()+4*(len(w.restarts)+1) >= BlockSize {
bh, err := w.finishBlock()
if err != nil {
return err
}
w.pendingBH = bh
}
return nil
}
func (w *Writer) Close() (err error) {
// Finish the last data block, or force an empty data block if there
// aren't any data blocks at all.
w.flushPendingBH(nil)
if w.nEntries > 0 || len(w.indexEntries) == 0 {
bh, err := w.finishBlock()
if err != nil {
return err
}
w.pendingBH = bh
w.flushPendingBH(nil)
}
// Write the (empty) metaindex block.
metaindexBlockHandle, err := w.finishBlock()
if err != nil {
return err
}
// Write the index block.
// writer.append uses w.tmp[:3*binary.MaxVarintLen64].
i0, tmp := 0, w.tmp[3*binary.MaxVarintLen64:5*binary.MaxVarintLen64]
for _, ie := range w.indexEntries {
n := encodeBlockHandle(tmp, ie.bh)
i1 := i0 + ie.keyLen
w.append(w.indexKeys[i0:i1], tmp[:n], true)
i0 = i1
}
indexBlockHandle, err := w.finishBlock()
if err != nil {
return err
}
// Write the table footer.
footer := w.tmp[:FOOTER_SIZE]
for i := range footer {
footer[i] = 0
}
n := encodeBlockHandle(footer, metaindexBlockHandle)
encodeBlockHandle(footer[n:], indexBlockHandle)
copy(footer[FOOTER_SIZE-len(MAGIC):], MAGIC)
if _, err := w.writer.Write(footer); err != nil {
return err
}
return nil
}
func (w *Writer) flushPendingBH(key []byte) {
if w.pendingBH.length == 0 {
return
}
n0 := len(w.indexKeys)
w.indexKeys = appendSeparator(w.indexKeys, w.prevKey, key)
n1 := len(w.indexKeys)
w.indexEntries = append(w.indexEntries, indexEntry{w.pendingBH, n1 - n0})
w.pendingBH = blockHandle{}
}
// append appends a key/value pair, which may also be a restart point.
func (w *Writer) append(key, value []byte, restart bool) {
nShared := 0
if restart {
w.restarts = append(w.restarts, uint32(w.buf.Len()))
} else {
nShared = sharedPrefixLen(w.prevKey, key)
}
w.prevKey = append(w.prevKey[:0], key...)
w.nEntries++
n := binary.PutUvarint(w.tmp[0:], uint64(nShared))
n += binary.PutUvarint(w.tmp[n:], uint64(len(key)-nShared))
n += binary.PutUvarint(w.tmp[n:], uint64(len(value)))
w.buf.Write(w.tmp[:n])
w.buf.Write(key[nShared:])
w.buf.Write(value)
}
func (w *Writer) finishBlock() (blockHandle, error) {
if w.nEntries == 0 {
w.restarts = w.restarts[:1]
w.restarts[0] = 0
}
tmp4 := w.tmp[:4]
for _, x := range w.restarts {
binary.LittleEndian.PutUint32(tmp4, x)
w.buf.Write(tmp4)
}
binary.LittleEndian.PutUint32(tmp4, uint32(len(w.restarts)))
w.buf.Write(tmp4)
b := w.buf.Bytes()
w.tmp[0] = 0
checksum := crc32.Update(0, table, b)
checksum = crc32.Update(checksum, table, w.tmp[:1])
checksum = uint32(checksum>>15|checksum<<17) + 0xa282ead8
binary.LittleEndian.PutUint32(w.tmp[1:5], checksum)
if _, err := w.writer.Write(b); err != nil {
return blockHandle{}, err
}
if _, err := w.writer.Write(w.tmp[:5]); err != nil {
return blockHandle{}, err
}
bh := blockHandle{int64(w.offset), int64(len(b))}
w.offset += uint64(len(b)) + BlockTrailerLen
w.buf.Reset()
w.nEntries = 0
w.restarts = w.restarts[:0]
return bh, nil
}
func appendSeparator(dst, a, b []byte) []byte {
i, n := sharedPrefixLen(a, b), len(dst)
dst = append(dst, a...)
if len(b) > 0 {
if i == len(a) {
return dst
}
if i == len(b) {
panic("a < b is a precondition, but b is a prefix of a")
}
if a[i] == 0xff || a[i]+1 >= b[i] {
return dst
}
}
i += n
for ; i < len(dst); i++ {
if dst[i] != 0xff {
dst[i]++
return dst[:i+1]
}
}
return dst
}
func sharedPrefixLen(a, b []byte) int {
i, n := 0, len(a)
if n > len(b) {
n = len(b)
}
for i < n && a[i] == b[i] {
i++
}
return i
}