-
Notifications
You must be signed in to change notification settings - Fork 4
/
sstable.go
248 lines (203 loc) · 5.73 KB
/
sstable.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
package golsm
import (
"io"
"os"
)
// Alias for int64 size
type EntrySize int64
type SSTable struct {
bloomFilter *BloomFilter // Bloom filter for the SSTable
index *Index // Index of the SSTable
file *os.File // File handle for the on-disk SSTable file.
dataOffset EntrySize // Offset from where the actual entries begin
}
type SSTableIterator struct {
s *SSTable // Pointer to the associated SSTable
file *os.File // File handle for the on-disk SSTable file.
Value *LSMEntry // Current entry
}
// Writes a list of MemtableKeyValue to a file in SSTable format.
// Format of the file is:
// 1. Bloom filter size (OffsetSize)
// 2. Bloom filter data (BloomFilter Protobuf)
// 3. Index size (OffsetSize)
// 4. Index data (Index Protobuf)
// 5. Entries data
//
// The entries data is written in as:
// 1. Size of the entry (OffsetSize)
// 2. Entry data (LSMEntry Protobuf)
func SerializeToSSTable(messages []*LSMEntry, filename string) (*SSTable, error) {
bloomFilter, index, entriesBuffer, err := buildMetadataAndEntriesBuffer(messages)
if err != nil {
return nil, err
}
indexData := mustMarshal(index)
bloomFilterData := mustMarshal(bloomFilter)
dataOffset, err := writeSSTable(filename, bloomFilterData, indexData, entriesBuffer)
if err != nil {
return nil, err
}
file, err := os.Open(filename)
if err != nil {
return nil, err
}
return &SSTable{bloomFilter: bloomFilter, index: index, file: file, dataOffset: dataOffset}, nil
}
// Opens an SSTable file for reading and returns a handle to it.
func OpenSSTable(filename string) (*SSTable, error) {
file, err := os.Open(filename)
if err != nil {
return nil, err
}
bloomFilter, index, dataOffset, err := readSSTableMetadata(file)
if err != nil {
return nil, err
}
return &SSTable{bloomFilter: bloomFilter, index: index, file: file, dataOffset: dataOffset}, nil
}
func (s *SSTable) Close() error {
return s.file.Close()
}
// Reads the value for a given key from the SSTable. Returns nil if the key is
// not found.
func (s *SSTable) Get(key string) (*LSMEntry, error) {
// Check if the key is in the bloom filter. If it is not, we can return
// immediately.
if !s.bloomFilter.Test([]byte(key)) {
return nil, nil
}
offset, found := findOffsetForKey(s.index.Entries, key)
if !found {
return nil, nil
}
// Seek to the offset of the entry in the file. The offset is relative to the
// start of the entries data therefore we add the dataOffset to it.
if _, err := s.file.Seek(int64(offset)+int64(s.dataOffset), io.SeekStart); err != nil {
return nil, err
}
size, err := readDataSize(s.file)
if err != nil {
return nil, err
}
data, err := readEntryDataFromFile(s.file, size)
if err != nil {
return nil, err
}
entry := &LSMEntry{}
mustUnmarshal(data, entry)
// We need to include the tombstones in the range scan. The caller will
// need to check the Command field of the LSMEntry to determine if
// the entry is a tombstone.
return entry, nil
}
// RangeScan returns all the values in the SSTable between startKey and endKey
// inclusive.
func (s *SSTable) RangeScan(startKey string, endKey string) ([]*LSMEntry, error) {
startOffset, found := findStartOffsetForRangeScan(s.index.Entries, startKey)
if !found {
return nil, nil
}
if _, err := s.file.Seek(int64(startOffset)+int64(s.dataOffset), io.SeekStart); err != nil {
return nil, err
}
var results []*LSMEntry
for {
size, err := readDataSize(s.file)
if err != nil {
if err == io.EOF {
break
}
return nil, err
}
data, err := readEntryDataFromFile(s.file, size)
if err != nil {
return nil, err
}
entry := &LSMEntry{}
mustUnmarshal(data, entry)
if entry.Key > endKey {
break
}
// We need to include the tombstones in the range scan. The caller will
// need to check the Command field of the LSMEntry to determine if
// the entry is a tombstone.
results = append(results, entry)
}
return results, nil
}
// GetEntries returns all the values in the SSTable.
func (s *SSTable) GetEntries() ([]*LSMEntry, error) {
if _, err := s.file.Seek(int64(s.dataOffset), io.SeekStart); err != nil {
return nil, err
}
var results []*LSMEntry
for {
size, err := readDataSize(s.file)
if err != nil {
if err == io.EOF {
break
}
return nil, err
}
data, err := readEntryDataFromFile(s.file, size)
if err != nil {
return nil, err
}
entry := &LSMEntry{}
mustUnmarshal(data, entry)
// We need to include the tombstones in the range scan. The caller will
// need to check the Command field of the LSMEntry to determine if
// the entry is a tombstone.
results = append(results, entry)
}
return results, nil
}
// Returns an iterator for the SSTable. The iterator is positioned at the
// beginning of the SSTable.
func (s *SSTable) Front() *SSTableIterator {
// Open a new file handle for the iterator.
file, err := os.Open(s.file.Name())
if err != nil {
return nil
}
i := &SSTableIterator{s: s, file: file, Value: &LSMEntry{}}
if _, err := i.file.Seek(int64(i.s.dataOffset), io.SeekStart); err != nil {
panic(err)
}
size, err := readDataSize(i.file)
if err != nil {
if err == io.EOF {
return nil
}
panic(err)
}
data, err := readEntryDataFromFile(i.file, size)
if err != nil {
panic(err)
}
mustUnmarshal(data, i.Value)
return i
}
// Returns the next entry in the SSTable. Returns nil if there are no more
// entries.
func (i *SSTableIterator) Next() *SSTableIterator {
size, err := readDataSize(i.file)
if err != nil {
if err == io.EOF {
return nil
}
panic(err)
}
data, err := readEntryDataFromFile(i.file, size)
if err != nil {
panic(err)
}
i.Value = &LSMEntry{}
mustUnmarshal(data, i.Value)
return i
}
// Closes the iterator.
func (i *SSTableIterator) Close() error {
return i.file.Close()
}