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 8
/
Copy pathblock.go
347 lines (289 loc) · 7.36 KB
/
block.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
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
/*
Copyright 2020 Google LLC
Use of this source code is governed by a BSD-style
license that can be found in the LICENSE file or at
https://developers.google.com/open-source/licenses/bsd
*/
package reftable
import (
"bytes"
"compress/zlib"
"encoding/binary"
"fmt"
"io"
"sort"
)
func isBlockType(typ byte) bool {
switch typ {
case 'g', 'i', 'r', 'o':
return true
}
return false
}
// blockWriter writes a single block.
type blockWriter struct {
// immutable
buf []byte
blockSize uint32
headerOff uint32
restartInterval int
hashSize int
// mutable
next uint32
restarts []uint32
lastKey string
entries int
}
// newBlockWriter creates a writer for the given block type.
func newBlockWriter(typ byte, buf []byte, headerOff uint32, hashSize int) *blockWriter {
bw := &blockWriter{
buf: buf,
headerOff: headerOff,
blockSize: uint32(len(buf)),
hashSize: hashSize,
}
bw.buf[headerOff] = typ
bw.next = headerOff + 4
// Some sensible default.
bw.restartInterval = 16
return bw
}
func (w *blockWriter) getType() byte {
return w.buf[w.headerOff]
}
// add adds a record, returning true, or if it does not fit, false.
func (w *blockWriter) add(r record) bool {
last := w.lastKey
if w.entries%w.restartInterval == 0 {
last = ""
}
buf := w.buf[w.next:]
start := buf
n, restart, ok := encodeKey(buf, last, r.key(), r.valType())
if !ok {
return false
}
buf = buf[n:]
n, ok = r.encode(buf, w.hashSize)
if !ok {
return false
}
buf = buf[n:]
return w.registerRestart(len(start)-len(buf), restart, r.key())
}
func (w *blockWriter) registerRestart(n int, restart bool, key string) bool {
rlen := len(w.restarts)
if rlen >= maxRestarts {
restart = false
}
if restart {
rlen++
}
if 2+3*rlen+n > len(w.buf[w.next:]) {
return false
}
if restart {
w.restarts = append(w.restarts, w.next)
}
w.next += uint32(n)
w.lastKey = key
w.entries++
return true
}
func putU24(out []byte, i uint32) {
out[0] = byte((i >> 16) & 0xff)
out[1] = byte((i >> 8) & 0xff)
out[2] = byte((i) & 0xff)
}
func getU24(in []byte) uint32 {
return uint32(in[0])<<16 | uint32(in[1])<<8 | uint32(in[2])
}
// finish finalizes the block, and returns the unpadded block.
func (w *blockWriter) finish() (data []byte) {
for _, r := range w.restarts {
putU24(w.buf[w.next:], r)
w.next += 3
}
binary.BigEndian.PutUint16(w.buf[w.next:], uint16(len(w.restarts)))
w.next += 2
putU24(w.buf[w.headerOff+1:], w.next)
data = w.buf[:w.next]
if w.getType() == blockTypeLog {
compressed := bytes.Buffer{}
compressed.Write(data[:w.headerOff+4])
zw, _ := zlib.NewWriterLevel(&compressed, 9)
_, err := zw.Write(data[w.headerOff+4:])
if err != nil {
panic("in mem zlib write")
}
if err := zw.Close(); err != nil {
panic("in mem zlib close")
}
c := compressed.Bytes()
return c
}
return data
}
// blockReader holds data for reading a block. It is immutable, so it
// is safe for concurrent access.
type blockReader struct {
// The offset of the block header, 24 (28 for v2) for the first block
headerOff uint32
// block is the data, including header, file header, but
// excluding restarts and padding.
block []byte
// The bytes holding the restart offsets
restartBytes []byte
// Size of the (compressed) block, including everything. This
// indicates where the next block will be
fullBlockSize uint32
// The size of the hash value in bytes, ie. 20 for SHA1.
hashSize int
restartCount uint16
}
func (br *blockReader) getType() byte {
return br.block[br.headerOff]
}
// newBlockWriter prepares for reading a block.
func newBlockReader(block []byte, headerOff uint32, tableBlockSize uint32, hashSize int) (*blockReader, error) {
fullBlockSize := tableBlockSize
typ := block[headerOff]
if !isBlockType(typ) {
return nil, fmt.Errorf("reftable: unknown block type %c", typ)
}
sz := getU24(block[headerOff+1:])
if typ == blockTypeLog {
decompress := make([]byte, 0, sz)
buf := bytes.NewBuffer(block)
out := bytes.NewBuffer(decompress)
before := buf.Len()
// Consume header
io.CopyN(out, buf, int64(headerOff+4))
r, err := zlib.NewReader(buf)
if err != nil {
return nil, err
}
// Have to use io.Copy. zlib stream has a terminator,
// which we must consume, so go until EOF.
if _, err := io.Copy(out, r); err != nil {
return nil, err
}
r.Close()
if out.Len() != int(sz) {
return nil, fmtError
}
block = out.Bytes()
fullBlockSize = uint32(before - buf.Len())
} else if fullBlockSize == 0 {
// unaligned table.
fullBlockSize = sz
} else if sz < fullBlockSize && int(sz) < len(block) && block[sz] != 0 {
// If the block is smaller than the full block size,
// it is padded (data followed by '\0') or the next
// block is unaligned. It would be better to require
// that the caller sets up the right sizes, but then
// the caller must also handle zlib (de)compression.
fullBlockSize = sz
}
block = block[:sz]
restartCount := binary.BigEndian.Uint16(block[len(block)-2:])
restartStart := len(block) - 2 - 3*int(restartCount)
restartBytes := block[restartStart:]
block = block[:restartStart]
br := &blockReader{
block: block,
fullBlockSize: fullBlockSize,
headerOff: headerOff,
restartCount: restartCount,
restartBytes: restartBytes,
hashSize: hashSize,
}
return br, nil
}
// restart returns the offset within the block of the i-th key
// restart.
func (br *blockReader) restartOffset(i int) uint32 {
return getU24(br.restartBytes[3*i:])
}
// blockIter iterates over the block. A blockIter is a value type, so
// it can be copied.
type blockIter struct {
br *blockReader
lastKey string
nextOffset uint32
}
// seek positions the iter to just before the given key.
func (bi *blockIter) seek(key string) error {
seeked, err := bi.br.seek(key)
if err != nil {
return err
}
*bi = *seeked
return nil
}
// start returns an iterator positioned at the start of the block.
func (br *blockReader) start(bi *blockIter) {
*bi = blockIter{
br: br,
nextOffset: uint32(br.headerOff + 4),
}
}
// seek returns an iterator positioned just before the given key
func (br *blockReader) seek(key string) (*blockIter, error) {
var decodeErr error
// Find the first restart key beyond the wanted key.
j := sort.Search(int(br.restartCount),
func(i int) bool {
rkey, err := decodeRestartKey(br.block, br.restartOffset(i))
if err != nil {
decodeErr = err
}
return key < rkey
})
if decodeErr != nil {
return nil, decodeErr
}
it := &blockIter{
br: br,
}
if j > 0 {
// We have a restart beyond the key, go one back to be before the wanted key
j--
it.nextOffset = br.restartOffset(j)
} else {
it.nextOffset = br.headerOff + 4
}
rec := newRecord(br.getType(), "")
for {
next := *it
ok, err := next.Next(rec)
if err != nil {
return nil, err
}
if !ok || rec.key() >= key {
return it, nil
}
*it = next
}
}
// Next implement the Iterator interface.
func (bi *blockIter) Next(r record) (bool, error) {
if bi.nextOffset >= uint32(len(bi.br.block)) {
return false, nil
}
buf := bi.br.block[bi.nextOffset:]
start := buf
n, key, valType, ok := decodeKey(buf, bi.lastKey)
if !ok {
return false, fmtError
}
buf = buf[n:]
if n, ok := r.decode(buf, key, valType, bi.br.hashSize); !ok {
return false, fmtError
} else {
buf = buf[n:]
}
bi.lastKey = r.key()
bi.nextOffset += uint32(len(start) - len(buf))
return true, nil
}