-
Notifications
You must be signed in to change notification settings - Fork 0
/
table_format.go
176 lines (147 loc) · 4.77 KB
/
table_format.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
// Copyright (c) 2020 Bert Young. 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 (
"bytes"
"github.com/golang/snappy"
)
const (
kMaxBlockHandleEncodedLength = 10 + 10 // Maximum encoding length of a BlockHandle
// Encoded length of a Footer. Note that the serialization of a
// Footer will always occupy exactly this many bytes. It consists
// of two block handles and a magic number.
kFooterEncodedLength = 2*kMaxBlockHandleEncodedLength + 8
)
// BlockHandle is a pointer to the extent of a file that stores a data
// block or a meta block.
type BlockHandle struct {
Offset uint64 // The offset of the block in the file.
Size uint64 // The size of the stored block
}
func (bh *BlockHandle) EncodeTo() []byte {
buf := bytes.NewBuffer(nil)
bh.EncodeToBuf(buf)
return buf.Bytes()
}
func (bh *BlockHandle) EncodeToBuf(buf *bytes.Buffer) {
PutVarint64(buf, bh.Offset)
PutVarint64(buf, bh.Size)
}
func (bh *BlockHandle) DecodeFrom(input []byte) Status {
buf := bytes.NewBuffer(input)
return bh.DecodeFromBuf(buf)
}
func (bh *BlockHandle) DecodeFromBuf(buf *bytes.Buffer) Status {
if v, err := GetVarint64(buf); err != nil {
return NewStatus(Corruption, "(Offset) bad block handle")
} else {
bh.Offset = v
}
if v, err := GetVarint64(buf); err != nil {
return NewStatus(Corruption, "(Size) bad block handle")
} else {
bh.Size = v
}
return NewStatus(OK)
}
// Footer encapsulates the fixed information stored at the tail
// end of every table file.
type Footer struct {
MetaIndex BlockHandle // The block handle for the metaindex block of the table
DataIndex BlockHandle // The block handle for the index block of the table
}
func (f *Footer) EncodeTo() []byte {
buf := bytes.NewBuffer(nil)
f.MetaIndex.EncodeToBuf(buf)
f.DataIndex.EncodeToBuf(buf)
padding := 2*kMaxBlockHandleEncodedLength - len(buf.Bytes())
if padding > 0 {
buf.Write(make([]byte, padding))
}
PutFixed32(buf, uint32(kTableMagicNumber&uint64(0x00ffffffff)))
PutFixed32(buf, uint32(kTableMagicNumber>>32))
return buf.Bytes()
}
func (f *Footer) DecodeFrom(input []byte) Status {
buf := bytes.NewBuffer(input)
return f.DecodeFromBuf(buf)
}
func (f *Footer) DecodeFromBuf(input *bytes.Buffer) Status {
oldLen := len(input.Bytes())
if oldLen < kFooterEncodedLength {
return NewStatus(Corruption, "Shorten footer")
}
{
magicBytes := input.Bytes()[kFooterEncodedLength-8 : kFooterEncodedLength]
magicBuf := bytes.NewBuffer(magicBytes)
magicLo, _ := DecodeFixed32(magicBuf)
magicHi, _ := DecodeFixed32(magicBuf)
var magic uint64 = (uint64(magicHi) << 32) | uint64(magicLo)
if magic != kTableMagicNumber {
return NewStatus(Corruption, "not an sstable (bad magic number)")
}
}
if st := f.MetaIndex.DecodeFromBuf(input); !st.IsOK() {
return st
}
if st := f.DataIndex.DecodeFromBuf(input); !st.IsOK() {
return st
}
newLen := len(input.Bytes())
toSkip := kFooterEncodedLength - (newLen - oldLen)
input.Next(toSkip)
return NewStatus(OK)
}
// kTableMagicNumber was picked by running
// echo http://code.google.com/p/leveldb/ | sha1sum
// and taking the leading 64 bits.
const (
kTableMagicNumber uint64 = 0xdb4775248b80fb57
kBlockTrailerSize uint64 = 5 // 1-byte type + 32-bit crc
)
type BlockContents struct {
Data []byte // Actual contents of data
Cachable bool // True iff data can be cached
}
// Read the block identified by "handle" from "file". On failure
// return non-OK. On success return BlockContents and OK.
func ReadBlock(file RandomAccessFile, verifyCrc bool, handle BlockHandle) (BlockContents, Status) {
// Read the block contents as well as the type/crc footer.
// See table_builder.go for the code that built this structure.
n := handle.Size
buf := make([]byte, n+kBlockTrailerSize)
contents, st := file.Read(int64(handle.Offset), len(buf), buf)
if !st.IsOK() {
return BlockContents{}, st
}
if len(contents) != len(buf) {
return BlockContents{}, NewStatus(Corruption, "truncated block read")
}
// Check the crc of the type and the block contents
if verifyCrc {
dst := bytes.NewBuffer(contents[n+1:])
crc, _ := DecodeFixed32(dst)
expectCrc := CrcValue(contents[0 : n+1]).Mask()
if crc != uint32(expectCrc) {
return BlockContents{}, NewStatus(Corruption, "block checksum mismatch")
}
}
switch contents[n] {
case kNoCompression:
bc := BlockContents{}
bc.Data = buf[:n]
return bc, NewStatus(OK)
case kSnappyCompression:
bc := BlockContents{}
if data, err := snappy.Decode(nil, buf[:n]); err != nil {
return bc, NewStatus(Corruption, "corrupted compressed block contents")
} else {
bc.Data = data
}
return bc, NewStatus(OK)
default:
return BlockContents{}, NewStatus(Corruption, "bad block type")
}
return BlockContents{}, NewStatus(OK)
}