/
table.go
146 lines (133 loc) · 3.8 KB
/
table.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
package table
import (
"io"
"github.com/kezhuw/leveldb/internal/errors"
"github.com/kezhuw/leveldb/internal/file"
"github.com/kezhuw/leveldb/internal/iterator"
"github.com/kezhuw/leveldb/internal/keys"
"github.com/kezhuw/leveldb/internal/options"
"github.com/kezhuw/leveldb/internal/table/block"
"github.com/kezhuw/leveldb/internal/table/filter"
)
type Table struct {
f file.ReadCloser
fileNumber uint64
blocks *BlockCache
options *options.Options
dataIndex *block.Block
filter *filter.Reader
}
func (t *Table) readMetaBlocks(metaIndexHandle block.Handle) {
if t.options.Filter == nil {
return
}
metaIndex, err := ReadDataBlock(t.f, t.fileNumber, metaIndexHandle, true)
if err != nil {
return
}
cmp := keys.BytewiseComparator
it := metaIndex.NewIterator(cmp)
defer it.Close()
filterName := []byte("filter." + t.options.Filter.Name())
if !it.Seek(filterName) || cmp.Compare(filterName, it.Key()) != 0 {
return
}
h, n := block.DecodeHandle(it.Value())
if n <= 0 {
return
}
buf, err := ReadBlock(t.f, t.fileNumber, h, true)
if err != nil {
return
}
t.filter = filter.NewReader(t.options.Filter, buf)
}
func (t *Table) Get(ikey keys.InternalKey, opts *options.ReadOptions) ([]byte, error, bool) {
indexIt := t.dataIndex.NewIterator(t.options.Comparator)
if !indexIt.Seek(ikey) {
err := indexIt.Close()
return nil, err, err != nil
}
defer indexIt.Close()
h, n := block.DecodeHandle(indexIt.Value())
if n <= 0 {
return nil, errors.NewCorruption(t.fileNumber, "table data index", -1, "invalid block handle"), true
}
if t.filter != nil && !t.filter.Contains(h.Offset, []byte(ikey)) {
return nil, nil, false
}
dataIt := t.readBlockHandleIterator(h, opts)
if !dataIt.Seek(ikey) {
err := dataIt.Close()
return nil, err, err != nil
}
defer dataIt.Close()
ukey, _, kind := keys.InternalKey(dataIt.Key()).Split()
if t.options.Comparator.UserKeyComparator.Compare(ukey, ikey.UserKey()) == 0 {
switch kind {
case keys.Delete:
return nil, errors.ErrNotFound, true
default:
return dataIt.Value(), nil, true
}
}
return nil, nil, false
}
func (t *Table) readBlockHandleIterator(h block.Handle, opts *options.ReadOptions) iterator.Iterator {
b, err := t.blocks.Read(t.f, t.fileNumber, h, opts.VerifyChecksums, opts.DontFillCache)
if err != nil {
return iterator.Error(err)
}
return b.NewIterator(t.options.Comparator)
}
func (t *Table) readBlockIterator(handle []byte, opts *options.ReadOptions) iterator.Iterator {
h, n := block.DecodeHandle(handle)
if n <= 0 {
return iterator.Error(errors.NewCorruption(t.fileNumber, "table data index", -1, "invalid block handle"))
}
return t.readBlockHandleIterator(h, opts)
}
func (t *Table) Close() error {
t.blocks.Evict(t.fileNumber)
return t.f.Close()
}
func (t *Table) NewIterator(opts *options.ReadOptions) iterator.Iterator {
index := t.dataIndex.NewIterator(t.options.Comparator)
blockf := func(value []byte) iterator.Iterator {
return t.readBlockIterator(value, opts)
}
return iterator.NewIndexIterator(index, blockf)
}
func OpenTable(f file.ReadCloser, blocks *BlockCache, opts *options.Options, number, size uint64) (t *Table, err error) {
defer func() {
if err != nil {
f.Close()
}
}()
if size < footerLength {
return nil, errors.NewCorruption(number, "table", 0, "file too short")
}
var scratch [footerLength]byte
_, err = f.ReadAt(scratch[:], int64(size-footerLength))
if err != nil && err != io.EOF {
return nil, err
}
var footer Footer
err = footer.Unmarshal(scratch[:])
if err != nil {
return nil, err
}
dataIndex, err := ReadDataBlock(f, number, footer.DataIndexHandle, true)
if err != nil {
return nil, err
}
t = &Table{
f: f,
fileNumber: number,
blocks: blocks,
options: opts,
dataIndex: dataIndex,
}
t.readMetaBlocks(footer.MetaIndexHandle)
return t, nil
}