-
Notifications
You must be signed in to change notification settings - Fork 0
/
SSTableBuilder.ts
328 lines (328 loc) · 12.6 KB
/
SSTableBuilder.ts
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
/**
* Copyright (c) 2018-present, heineiuo.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
import { crc32 } from "./Crc32.ts";
import { Buffer } from "./Buffer.ts";
import { assert } from "./DBHelper.ts";
import Slice from "./Slice.ts";
import BloomFilter from "./BloomFilter.ts";
import Footer from "./SSTableFooter.ts";
import { FileHandle } from "./Env.ts";
import { encodeFixed32 } from "./Coding.ts";
import { Options } from "./Options.ts";
import { BlockHandle, CompressionTypes, kBlockTrailerSize, kSizeOfUInt32, } from "./Format.ts";
export default class SSTableBuilder {
constructor(options: Options, file: FileHandle) {
this._file = file;
this._fileSize = 0;
this._metaBlock = new FilterBlockBuilder(); // eslint-disable-line
this._footer = new Footer(Buffer.alloc(48));
this._numberOfEntries = 0;
this._offset = 0;
this._pendingIndexEntry = false;
this._closed = false;
this._options = options;
this._dataBlock = new BlockBuilder(this._options); // eslint-disable-line
this._dataBlock.blockType = "datablock";
this._indexBlock = new BlockBuilder(this._options); // eslint-disable-line
this._indexBlock.blockType = "indexblock";
this._pendingHandle = new BlockHandle();
}
private _closed: boolean;
private _numberOfEntries: number;
private _options: Options;
private _file: FileHandle;
private _fileSize: number;
private _lastKey!: Slice; // internalkey
private _dataBlock: BlockBuilder;
private _metaBlock: FilterBlockBuilder;
private _indexBlock: BlockBuilder;
private _footer: Footer;
private _offset: number;
private _pendingIndexEntry: boolean;
private _pendingHandle: BlockHandle;
// key is internal key
async add(key: Slice, value: Slice): Promise<void> {
assert(!this._closed);
if (this._numberOfEntries > 0) {
assert(this._options.comparator.compare(key, this._lastKey) > 0, `new key must bigger then last key`);
}
if (this._pendingIndexEntry) {
assert(this._dataBlock.isEmpty());
this._options.comparator.findShortestSeparator(this._lastKey, key);
const handleEncoding = this._pendingHandle.buffer;
this._indexBlock.add(this._lastKey, new Slice(handleEncoding));
this._pendingIndexEntry = false;
}
if (!!this._metaBlock) {
this._metaBlock.addkey(key);
}
this._lastKey = new Slice(key);
this._numberOfEntries++;
this._dataBlock.add(key, value);
if (this._dataBlock.currentSizeEstimate() > this._options.blockSize) {
await this.flush();
}
}
get numEntries(): number {
return this._numberOfEntries;
}
get fileSize(): number {
return this._fileSize;
}
async flush(): Promise<void> {
assert(!this._closed);
if (this._dataBlock.isEmpty())
return;
assert(!this._pendingIndexEntry);
await this.writeBlock(this._dataBlock, this._pendingHandle);
this._pendingIndexEntry = true;
if (!!this._metaBlock) {
this._metaBlock.startBlock(this._offset);
}
}
async writeBlock(block: BlockBuilder, handle: BlockHandle): Promise<void> {
// File format contains a sequence of blocks where each block has:
// block_data: uint8[n]
// type: uint8
// crc: uint32
const raw = block.finish();
const type = CompressionTypes.none; // TODO
await this.writeRawBlock(raw, type, handle);
this._dataBlock.reset();
}
async writeRawBlock(blockContent: Slice, type: CompressionTypes, handle: BlockHandle): Promise<void> {
handle.offset = this._offset;
handle.size = blockContent.size;
await this.appendFile(blockContent.buffer);
const trailer = Buffer.alloc(kBlockTrailerSize);
trailer[0] = type;
const crc32buffer = crc32(Buffer.concat([blockContent.buffer, Buffer.fromUnknown([type])]));
trailer.fillBuffer(crc32buffer, 1, 5);
await this.appendFile(trailer);
this._offset += blockContent.size + kBlockTrailerSize;
}
async appendFile(buffer: Buffer): Promise<void> {
await this._file.appendFile(buffer, { encoding: "buffer" });
this._fileSize += buffer.length;
}
public async close(): Promise<void> {
if (!this._closed) {
const file = this._file;
this._closed = true;
delete this._file;
try {
await file.close();
}
catch (e) { }
}
}
public async abandon(): Promise<void> {
await this.close();
}
async finish(): Promise<void> {
await this.flush();
const filterBlockHandle = new BlockHandle();
const metaIndexBlockHandle = new BlockHandle();
const indexBlockHandle = new BlockHandle();
if (!!this._metaBlock) {
await this.writeRawBlock(this._metaBlock.finish(), CompressionTypes.none, filterBlockHandle);
}
// eslint-disable-next-line
const metaIndexBlock = new BlockBuilder(this._options);
metaIndexBlock.blockType = "metaindexblock";
if (!!this._metaBlock) {
let key = "filter.";
key += this._options.filterPolicy.name();
const handleEncoding = filterBlockHandle.buffer;
metaIndexBlock.add(new Slice(key), new Slice(handleEncoding));
// TODO(postrelease): Add stats and other meta blocks
await this.writeBlock(metaIndexBlock, metaIndexBlockHandle);
}
// Write index block
if (this._pendingIndexEntry) {
this._options.comparator.findShortSuccessor(this._lastKey);
const handleEncoding = this._pendingHandle.buffer;
this._indexBlock.add(this._lastKey, new Slice(handleEncoding));
this._pendingIndexEntry = false;
}
await this.writeBlock(this._indexBlock, indexBlockHandle);
// Write footer
this._footer.put({
metaIndexOffset: metaIndexBlockHandle.offset,
metaIndexSize: metaIndexBlockHandle.size,
indexOffset: indexBlockHandle.offset,
indexSize: indexBlockHandle.size,
});
await this.appendFile(this._footer.buffer);
this._offset += this._footer.buffer.length;
await this.close();
}
}
class BlockBuilder {
constructor(options: Options) {
this._options = options;
this._buffer = Buffer.alloc(0);
this._restarts = [0];
this._counter = 0;
this._finished = false;
this._lastKey = new Slice();
}
public blockType!: string;
private _options: Options;
private _buffer: Buffer;
private _restarts: number[]; // First restart point is at offset 0
private _finished: boolean;
private _counter: number;
private _lastKey: Slice;
get buffer(): Buffer {
return this._buffer;
}
public reset(): void {
this._buffer = Buffer.alloc(0);
this._restarts = [0];
this._counter = 0;
this._finished = false;
this._lastKey = new Slice();
}
public add(key: Slice, value: Slice): void {
assert(!this._finished);
assert(this._counter <= this._options.blockRestartInterval);
assert(this._buffer.length === 0 ||
this._options.comparator.compare(key, this._lastKey) > 0);
let shared = 0;
if (this._counter < this._options.blockRestartInterval) {
// See how much sharing to do with previous string
const minLength = Math.min(this._lastKey.size, key.size);
while (shared < minLength &&
this._lastKey.buffer[shared] === key.buffer[shared]) {
shared++;
}
}
else {
// Restart compression
this._restarts.push(this._buffer.length);
this._counter = 0;
}
const nonShared = key.size - shared;
this._buffer = Buffer.concat([
this._buffer,
// Add "<shared><non_shared><value_size>" to buffer_
encodeFixed32(shared),
encodeFixed32(nonShared),
encodeFixed32(value.size),
// Add string delta to buffer_ followed by value
key.buffer.slice(shared),
value.buffer,
]);
this._lastKey = new Slice(Buffer.concat([
this._lastKey.buffer.slice(0, shared),
key.buffer.slice(shared),
]));
assert(Buffer.compare(this._lastKey.buffer, key.buffer) === 0);
}
public isEmpty(): boolean {
return this._buffer.length === 0;
}
// Add <Buffer restarts.start>...<Buffer restarts[restarts.end]><Buffer restarts count> to tail
public finish(): Slice {
// Append restart array
for (let i = 0; i < this._restarts.length; i++) {
this._buffer = Buffer.concat([
this._buffer,
encodeFixed32(this._restarts[i]),
]);
}
this._buffer = Buffer.concat([
this._buffer,
encodeFixed32(this._restarts.length),
]);
this._finished = true;
return new Slice(this._buffer);
}
public currentSizeEstimate(): number {
return (this._buffer.length + // Raw data buffer
this._restarts.length * kSizeOfUInt32 + // sizeof(uint32_t), Restart array
kSizeOfUInt32 // sizeof(uint32_t) // Restart array length
);
}
}
class FilterBlockBuilder {
static kFilterBaseLg = 11;
static kFilterBase = 1 << FilterBlockBuilder.kFilterBaseLg;
constructor() {
this._keys = Buffer.alloc(0);
this._starts = [];
this._result = Buffer.alloc(0);
this._tempKeys = [];
this._filterOffsets = [];
}
private _keys: Buffer; // Flattened key contents
private _starts: number[]; // Offset in _keys of each key
private _result: Buffer; // Filter data computed so far
private _tempKeys: Slice[]; // policy_->CreateFilter() argument
private _filterOffsets: number[]; // Offset in _result of each filter
public startBlock(blockOffset: number): void {
const filterIndex = Math.floor(blockOffset / FilterBlockBuilder.kFilterBase);
assert(filterIndex >= this._filterOffsets.length);
while (filterIndex > this._filterOffsets.length) {
this.generateFilter();
}
}
public generateFilter(): void {
const numKeys = this._starts.length;
if (numKeys === 0) {
// Fast path if there are no keys for this filter
this._filterOffsets.push(this._result.length);
return;
}
// Make list of keys from flattened key structure
this._starts.push(this._keys.length);
for (let i = 0; i < numKeys; i++) {
// c++ original
// const char* base = keys_.data() + start_[i];
// size_t length = start_[i + 1] - start_[i];
// tmp_keys_[i] = Slice(base, length);
// c++ translate directly
// const base = this._starts[i]
// const length = this._starts[i + 1] - this._starts[i]
// this._tempKeys[i] = new Slice(this._keys.slice(base, base + length))
// c++ translate by meaning
this._tempKeys[i] = new Slice(this._keys.slice(this._starts[i], this._starts[i + 1]));
}
// Generate filter for current set of keys and append to result_.
this._filterOffsets.push(this._result.length);
const filter = new BloomFilter();
filter.putKeys(this._tempKeys, numKeys);
this._result = Buffer.concat([this._result, filter.buffer]);
this._keys = Buffer.alloc(0);
this._tempKeys = [];
this._starts = [];
}
public addkey(key: Slice): void {
this._starts.push(this._keys.length);
this._keys = Buffer.concat([this._keys, key.buffer]);
}
public finish(): Slice {
if (this._starts.length > 0) {
this.generateFilter();
}
// Append array of per-filter offsets
const arrayOffset = this._result.length;
for (let i = 0; i < this._filterOffsets.length; i++) {
this._result = Buffer.concat([
this._result,
encodeFixed32(this._filterOffsets[i]),
]);
}
this._result = Buffer.concat([
this._result,
encodeFixed32(arrayOffset),
Buffer.fromUnknown([FilterBlockBuilder.kFilterBaseLg]),
]);
return new Slice(this._result);
}
}