This repository has been archived by the owner on Jul 30, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 146
/
version_edit.go
364 lines (327 loc) · 7.56 KB
/
version_edit.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
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
// Copyright 2012 The LevelDB-Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package leveldb
import (
"bufio"
"bytes"
"encoding/binary"
"errors"
"fmt"
"io"
"sort"
"github.com/golang/leveldb/db"
)
// TODO: describe the MANIFEST file format, independently of the C++ project.
var errCorruptManifest = errors.New("leveldb: corrupt manifest")
type byteReader interface {
io.ByteReader
io.Reader
}
// Tags for the versionEdit disk format.
// Tag 8 is no longer used.
const (
tagComparator = 1
tagLogNumber = 2
tagNextFileNumber = 3
tagLastSequence = 4
tagCompactPointer = 5
tagDeletedFile = 6
tagNewFile = 7
tagPrevLogNumber = 9
)
type compactPointerEntry struct {
level int
key internalKey
}
type deletedFileEntry struct {
level int
fileNum uint64
}
type newFileEntry struct {
level int
meta fileMetadata
}
type versionEdit struct {
comparatorName string
logNumber uint64
prevLogNumber uint64
nextFileNumber uint64
lastSequence uint64
compactPointers []compactPointerEntry
deletedFiles map[deletedFileEntry]bool // A set of deletedFileEntry values.
newFiles []newFileEntry
}
func (v *versionEdit) decode(r io.Reader) error {
br, ok := r.(byteReader)
if !ok {
br = bufio.NewReader(r)
}
d := versionEditDecoder{br}
for {
tag, err := binary.ReadUvarint(br)
if err == io.EOF {
break
}
if err != nil {
return err
}
switch tag {
case tagComparator:
s, err := d.readBytes()
if err != nil {
return err
}
v.comparatorName = string(s)
case tagLogNumber:
n, err := d.readUvarint()
if err != nil {
return err
}
v.logNumber = n
case tagNextFileNumber:
n, err := d.readUvarint()
if err != nil {
return err
}
v.nextFileNumber = n
case tagLastSequence:
n, err := d.readUvarint()
if err != nil {
return err
}
v.lastSequence = n
case tagCompactPointer:
level, err := d.readLevel()
if err != nil {
return err
}
key, err := d.readBytes()
if err != nil {
return err
}
v.compactPointers = append(v.compactPointers, compactPointerEntry{level, key})
case tagDeletedFile:
level, err := d.readLevel()
if err != nil {
return err
}
fileNum, err := d.readUvarint()
if err != nil {
return err
}
if v.deletedFiles == nil {
v.deletedFiles = make(map[deletedFileEntry]bool)
}
v.deletedFiles[deletedFileEntry{level, fileNum}] = true
case tagNewFile:
level, err := d.readLevel()
if err != nil {
return err
}
fileNum, err := d.readUvarint()
if err != nil {
return err
}
size, err := d.readUvarint()
if err != nil {
return err
}
smallest, err := d.readBytes()
if err != nil {
return err
}
largest, err := d.readBytes()
if err != nil {
return err
}
v.newFiles = append(v.newFiles, newFileEntry{
level: level,
meta: fileMetadata{
fileNum: fileNum,
size: size,
smallest: smallest,
largest: largest,
},
})
case tagPrevLogNumber:
n, err := d.readUvarint()
if err != nil {
return err
}
v.prevLogNumber = n
default:
return errCorruptManifest
}
}
return nil
}
func (v *versionEdit) encode(w io.Writer) error {
e := versionEditEncoder{new(bytes.Buffer)}
if v.comparatorName != "" {
e.writeUvarint(tagComparator)
e.writeString(v.comparatorName)
}
if v.logNumber != 0 {
e.writeUvarint(tagLogNumber)
e.writeUvarint(v.logNumber)
}
if v.prevLogNumber != 0 {
e.writeUvarint(tagPrevLogNumber)
e.writeUvarint(v.prevLogNumber)
}
if v.nextFileNumber != 0 {
e.writeUvarint(tagNextFileNumber)
e.writeUvarint(v.nextFileNumber)
}
if v.lastSequence != 0 {
e.writeUvarint(tagLastSequence)
e.writeUvarint(v.lastSequence)
}
for _, x := range v.compactPointers {
e.writeUvarint(tagCompactPointer)
e.writeUvarint(uint64(x.level))
e.writeBytes(x.key)
}
for x := range v.deletedFiles {
e.writeUvarint(tagDeletedFile)
e.writeUvarint(uint64(x.level))
e.writeUvarint(x.fileNum)
}
for _, x := range v.newFiles {
e.writeUvarint(tagNewFile)
e.writeUvarint(uint64(x.level))
e.writeUvarint(x.meta.fileNum)
e.writeUvarint(x.meta.size)
e.writeBytes(x.meta.smallest)
e.writeBytes(x.meta.largest)
}
_, err := w.Write(e.Bytes())
return err
}
type versionEditDecoder struct {
byteReader
}
func (d versionEditDecoder) readBytes() ([]byte, error) {
n, err := d.readUvarint()
if err != nil {
return nil, err
}
s := make([]byte, n)
_, err = io.ReadFull(d, s)
if err != nil {
if err == io.ErrUnexpectedEOF {
return nil, errCorruptManifest
}
return nil, err
}
return s, nil
}
func (d versionEditDecoder) readLevel() (int, error) {
u, err := d.readUvarint()
if err != nil {
return 0, err
}
if u >= numLevels {
return 0, errCorruptManifest
}
return int(u), nil
}
func (d versionEditDecoder) readUvarint() (uint64, error) {
u, err := binary.ReadUvarint(d)
if err != nil {
if err == io.EOF {
return 0, errCorruptManifest
}
return 0, err
}
return u, nil
}
type versionEditEncoder struct {
*bytes.Buffer
}
func (e versionEditEncoder) writeBytes(p []byte) {
e.writeUvarint(uint64(len(p)))
e.Write(p)
}
func (e versionEditEncoder) writeString(s string) {
e.writeUvarint(uint64(len(s)))
e.WriteString(s)
}
func (e versionEditEncoder) writeUvarint(u uint64) {
var buf [binary.MaxVarintLen64]byte
n := binary.PutUvarint(buf[:], u)
e.Write(buf[:n])
}
// bulkVersionEdit summarizes the files added and deleted from a set of version
// edits.
//
// The C++ LevelDB code calls this concept a VersionSet::Builder.
type bulkVersionEdit struct {
added [numLevels][]fileMetadata
deleted [numLevels]map[uint64]bool // map[uint64]bool is a set of fileNums.
}
func (b *bulkVersionEdit) accumulate(ve *versionEdit) {
for _, cp := range ve.compactPointers {
// TODO: handle compaction pointers.
_ = cp
}
for df := range ve.deletedFiles {
dmap := b.deleted[df.level]
if dmap == nil {
dmap = make(map[uint64]bool)
b.deleted[df.level] = dmap
}
dmap[df.fileNum] = true
}
for _, nf := range ve.newFiles {
if dmap := b.deleted[nf.level]; dmap != nil {
delete(dmap, nf.meta.fileNum)
}
// TODO: fiddle with nf.meta.allowedSeeks.
b.added[nf.level] = append(b.added[nf.level], nf.meta)
}
}
// apply applies the delta b to a base version to produce a new version. The
// new version is consistent with respect to the internal key comparer icmp.
//
// base may be nil, which is equivalent to a pointer to a zero version.
func (b *bulkVersionEdit) apply(base *version, icmp db.Comparer) (*version, error) {
v := new(version)
for level := range v.files {
combined := [2][]fileMetadata{
nil,
b.added[level],
}
if base != nil {
combined[0] = base.files[level]
}
n := len(combined[0]) + len(combined[1])
if n == 0 {
continue
}
v.files[level] = make([]fileMetadata, 0, n)
dmap := b.deleted[level]
for _, ff := range combined {
for _, f := range ff {
if dmap != nil && dmap[f.fileNum] {
continue
}
v.files[level] = append(v.files[level], f)
}
}
// TODO: base.files[level] is already sorted. Instead of appending
// b.addFiles[level] to the end and sorting afterwards, it might be more
// efficient to sort b.addFiles[level] and then merge the two sorted slices.
if level == 0 {
sort.Sort(byFileNum(v.files[level]))
} else {
sort.Sort(bySmallest{v.files[level], icmp})
}
}
if err := v.checkOrdering(icmp); err != nil {
return nil, fmt.Errorf("leveldb: internal error: %v", err)
}
v.updateCompactionScore()
return v, nil
}