/
index.go
216 lines (172 loc) · 4.68 KB
/
index.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
package sstable
import (
"bytes"
"encoding/binary"
"errors"
"io"
"sort"
)
// indexEntry is an entry of index.
type indexEntry struct {
// KeyLength uint32
blockOffset uint64
blockLength uint32
keyBytes []byte
}
// readIndexEntry reads indexEntry from r and returns indexEntry,
// number of bytes read, and error.
func readIndexEntry(r io.Reader) (*indexEntry, int, error) {
lenbuf := make([]byte, 4)
n, err := io.ReadFull(r, lenbuf)
if err != nil {
return nil, n, err
}
length := binary.BigEndian.Uint32(lenbuf)
buf := make([]byte, length+16)
copy(buf[:4], lenbuf)
nn, err := io.ReadFull(r, buf[4:])
n += nn
if err != nil {
return nil, n, err
}
var e indexEntry
return &e, n, e.UnmarshalBinary(buf) //nolint:wsl
}
// readIndexEntryAt reads indexEntry at offset from r and returns
// indexEntry and error.
func readIndexEntryAt(r io.ReaderAt, offset uint64) (*indexEntry, error) {
if int64(offset) < 0 {
panic("unimplemented")
}
lenbuf := make([]byte, 4)
if n, err := r.ReadAt(lenbuf, int64(offset)); n != len(lenbuf) {
return nil, err
}
length := binary.BigEndian.Uint32(lenbuf)
buf := make([]byte, length+16)
if n, err := r.ReadAt(buf, int64(offset)); n != len(buf) {
return nil, err
}
var e indexEntry
return &e, e.UnmarshalBinary(buf) //nolint:wsl
}
// size returns number of bytes of the indexEntry.
func (e *indexEntry) size() int {
return 16 + len(e.keyBytes)
}
// WriteTo implements the io.WriterTo interface.
func (e *indexEntry) WriteTo(w io.Writer) (n int64, err error) {
data, err := e.MarshalBinary()
if err != nil {
return
}
nn, err := w.Write(data)
return int64(nn), err //nolint:wsl
}
// MarshalBinary implements the encoding.BinaryMarshaler interface.
func (e *indexEntry) MarshalBinary() ([]byte, error) {
buf := bytes.NewBuffer([]byte{})
if err := binary.Write(buf, binary.BigEndian, uint32(len(e.keyBytes))); err != nil {
return nil, err
}
if err := binary.Write(buf, binary.BigEndian, e.blockOffset); err != nil {
return nil, err
}
if err := binary.Write(buf, binary.BigEndian, e.blockLength); err != nil {
return nil, err
}
if _, err := buf.Write(e.keyBytes); err != nil {
return nil, err
}
return buf.Bytes(), nil
}
// UnmarshalBinary implements the encoding.BinaryUnmarshaler interface.
func (e *indexEntry) UnmarshalBinary(data []byte) error {
if len(data) < 16 {
return errors.New("indexEntry.UnmarshalBinary: invalid length")
}
length := binary.BigEndian.Uint32(data[:4])
blockOffset := binary.BigEndian.Uint64(data[4:12])
blockLength := binary.BigEndian.Uint32(data[12:16])
if length != uint32(len(data[16:])) {
return errors.New("indexEntry.UnmarshalBinary: invalid length")
}
e.blockOffset = blockOffset
e.blockLength = blockLength
e.keyBytes = make([]byte, length)
copy(e.keyBytes, data[16:])
return nil
}
// index stores index entries and implements a find function.
type index []indexEntry
// entryIndexOf returns the index of index entry that might contain
// the key. It returns -1 if there is no index entry.
func (i index) entryIndexOf(key []byte) int {
return sort.Search(len(i), func(idx int) bool {
return bytes.Compare(i[idx].keyBytes, key) > 0
}) - 1
}
// ReadFrom implements the io.ReaderFrom interface.
func (i *index) ReadFrom(r io.Reader) (n int64, err error) {
for err == nil {
e, nn, err := readIndexEntry(r)
n += int64(nn)
if err == nil || err == io.EOF && nn > 0 {
*i = append(*i, *e)
continue
}
if err == io.EOF {
err = nil
}
return n, err
}
panic("unreachable")
}
// ReadAt reads index from r at offset.
func (i *index) ReadAt(r io.ReaderAt, offset uint64) error {
var err error
for err == nil {
e, err := readIndexEntryAt(r, offset)
if e != nil {
*i = append(*i, *e)
offset += uint64(e.size())
continue
}
if err == io.EOF {
err = nil
}
return err
}
panic("unreachable")
}
// WriteTo implements the io.WriterTo interface.
func (i index) WriteTo(w io.Writer) (n int64, err error) {
var nn int64
for _, entry := range i {
nn, err = entry.WriteTo(w)
n += nn
if err != nil {
return n, err
}
}
return n, nil
}
// indexBuffer implements functions to build a new index.
type indexBuffer struct {
maxBlockLength uint32
offset uint64
index index
}
// Write writes an entry in the buffer to build the index.
func (w *indexBuffer) Write(key []byte, valueSize uint32) {
size := len(w.index)
if size == 0 || int64(w.index[size-1].blockLength)+int64(valueSize) > int64(w.maxBlockLength) {
w.index = append(w.index, indexEntry{
blockOffset: w.offset,
keyBytes: key,
})
size++
}
w.offset += uint64(8) + uint64(len(key)) + uint64(valueSize)
w.index[size-1].blockLength += uint32(8) + uint32(len(key)) + valueSize
}