-
Notifications
You must be signed in to change notification settings - Fork 98
/
sqlite-file.js
467 lines (382 loc) · 12.1 KB
/
sqlite-file.js
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
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
import * as perf from 'perf-deets';
import { getPageSize, LOCK_TYPES } from './sqlite-util';
function range(start, end, step) {
let r = [];
for (let i = start; i <= end; i += step) {
r.push(i);
}
return r;
}
export function getBoundaryIndexes(blockSize, start, end) {
let startC = start - (start % blockSize);
let endC = end - 1 - ((end - 1) % blockSize);
return range(startC, endC, blockSize);
}
export function readChunks(chunks, start, end) {
let buffer = new ArrayBuffer(end - start);
let bufferView = new Uint8Array(buffer);
let cursor = 0;
for (let i = 0; i < chunks.length; i++) {
let chunk = chunks[i];
// TODO: jest has a bug where we can't do `instanceof ArrayBuffer`
if (chunk.data.constructor.name !== 'ArrayBuffer') {
throw new Error('Chunk data is not an ArrayBuffer');
}
let cstart = 0;
let cend = chunk.data.byteLength;
if (start > chunk.pos) {
cstart = start - chunk.pos;
}
if (end < chunk.pos + chunk.data.byteLength) {
cend = end - chunk.pos;
}
if (cstart > chunk.data.byteLength || cend < 0) {
continue;
}
let len = cend - cstart;
bufferView.set(
new Uint8Array(chunk.data, cstart, len),
chunk.pos - start + cstart
);
cursor += len;
}
return buffer;
}
export function writeChunks(bufferView, blockSize, start, end) {
let indexes = getBoundaryIndexes(blockSize, start, end);
let cursor = 0;
return indexes
.map(index => {
let cstart = 0;
let cend = blockSize;
if (start > index && start < index + blockSize) {
cstart = start - index;
}
if (end > index && end < index + blockSize) {
cend = end - index;
}
let len = cend - cstart;
let chunkBuffer = new ArrayBuffer(blockSize);
if (start > index + blockSize || end <= index) {
return null;
}
let off = bufferView.byteOffset + cursor;
let available = bufferView.buffer.byteLength - off;
if (available <= 0) {
return null;
}
let readLength = Math.min(len, available);
new Uint8Array(chunkBuffer).set(
new Uint8Array(bufferView.buffer, off, readLength),
cstart
);
cursor += readLength;
return {
pos: index,
data: chunkBuffer,
offset: cstart,
length: readLength
};
})
.filter(Boolean);
}
export class File {
constructor(filename, ops, meta = null) {
this.filename = filename;
this.buffer = new Map();
this.ops = ops;
this.meta = meta;
this._metaDirty = false;
this.writeLock = false;
this.openHandles = 0;
}
bufferChunks(chunks) {
for (let i = 0; i < chunks.length; i++) {
let chunk = chunks[i];
this.buffer.set(chunk.pos, chunk);
}
}
open() {
this.openHandles++;
// Don't open the file again if it's already open
if (this.openHandles === 1) {
this.ops.open();
let meta = this.ops.readMeta();
// It's possible that `setattr` has already been called if opening
// the file in a mode that truncates it to 0
if (this.meta == null) {
if (meta == null) {
// New file
meta = { size: 0 };
}
this.meta = meta;
}
}
return this.meta;
}
close() {
this.fsync();
this.openHandles = Math.max(this.openHandles - 1, 0);
// Only close it if there are no existing open handles
if (this.openHandles === 0) {
this.ops.close();
}
}
delete() {
this.ops.delete();
}
load(indexes) {
let status = indexes.reduce(
(acc, b) => {
let inMemory = this.buffer.get(b);
if (inMemory) {
acc.chunks.push(inMemory);
} else {
acc.missing.push(b);
}
return acc;
},
{ chunks: [], missing: [] }
);
let missingChunks = [];
if (status.missing.length > 0) {
perf.record('read-blocks');
missingChunks = this.ops.readBlocks(status.missing, this.meta.blockSize);
perf.endRecording('read-blocks');
}
return status.chunks.concat(missingChunks);
}
read(bufferView, offset, length, position) {
// console.log('reading', this.filename, offset, length, position);
let buffer = bufferView.buffer;
if (length <= 0) {
return 0;
}
if (position < 0) {
// TODO: is this right?
return 0;
}
if (position >= this.meta.size) {
let view = new Uint8Array(buffer, offset);
for (let i = 0; i < length; i++) {
view[i] = 0;
}
return length;
}
position = Math.max(position, 0);
let dataLength = Math.min(length, this.meta.size - position);
let start = position;
let end = position + dataLength;
let indexes = getBoundaryIndexes(this.meta.blockSize, start, end);
let chunks = this.load(indexes);
let readBuffer = readChunks(chunks, start, end);
if (buffer.byteLength - offset < readBuffer.byteLength) {
throw new Error('Buffer given to `read` is too small');
}
let view = new Uint8Array(buffer);
view.set(new Uint8Array(readBuffer), offset);
// TODO: I don't need to do this. `unixRead` does this for us.
for (let i = dataLength; i < length; i++) {
view[offset + i] = 0;
}
return length;
}
write(bufferView, offset, length, position) {
// console.log('writing', this.filename, offset, length, position);
if (this.meta.blockSize == null) {
// We don't have a block size yet (an empty file). The first
// write MUST be the beginning of the file. This is a new file
// and the first block contains the page size which we need.
// sqlite will write this block first, and if you are directly
// writing a db file to disk you can't write random parts of it.
// Just write the whole thing and we'll get the first block
// first.
let pageSize = getPageSize(
new Uint8Array(bufferView.buffer, bufferView.byteOffset + offset)
);
// Page sizes must be a power of 2 between 512 and 65536.
// These was generated by doing `Math.pow(2, N)` where N >= 9
// and N <= 16.
if (
![512, 1024, 2048, 4096, 8192, 16384, 32768, 65536].includes(pageSize)
) {
throw new Error(
'File has invalid page size. (the first block of a new file must be written first)'
);
}
this.setattr({ blockSize: pageSize });
}
let buffer = bufferView.buffer;
if (length <= 0) {
return 0;
}
if (position < 0) {
return 0;
}
if (buffer.byteLength === 0) {
return 0;
}
perf.count('writes');
length = Math.min(length, buffer.byteLength - offset);
let writes = writeChunks(
new Uint8Array(buffer, offset, length),
this.meta.blockSize,
position,
position + length
);
// Find any partial chunks and read them in and merge with
// existing data
let { partialWrites, fullWrites } = writes.reduce(
(state, write) => {
if (write.length !== this.meta.blockSize) {
state.partialWrites.push(write);
} else {
state.fullWrites.push({
pos: write.pos,
data: write.data
});
}
return state;
},
{ fullWrites: [], partialWrites: [] }
);
let reads = [];
if (partialWrites.length > 0) {
reads = this.load(partialWrites.map(w => w.pos));
}
let allWrites = fullWrites.concat(
reads.map(read => {
let write = partialWrites.find(w => w.pos === read.pos);
// MuTatIoN!
new Uint8Array(read.data).set(
new Uint8Array(write.data, write.offset, write.length),
write.offset,
write.length
);
return read;
})
);
this.bufferChunks(allWrites);
if (position + length > this.meta.size) {
this.setattr({ size: position + length });
}
return length;
}
async readIfFallback() {
if (this.ops.readIfFallback) {
// Reset the meta
let meta = await this.ops.readIfFallback();
this.meta = meta || { size: 0 };
}
}
lock(lockType) {
// TODO: Perf APIs need improvement
if (!this._recordingLock) {
perf.record('locked');
this._recordingLock = true;
}
if (this.ops.lock(lockType)) {
if (lockType >= LOCK_TYPES.RESERVED) {
this.writeLock = true;
}
return true;
}
return false;
}
unlock(lockType) {
if (lockType === 0) {
perf.endRecording('locked');
this._recordingLock = false;
}
if (this.writeLock) {
// In certain cases (I saw this while running VACUUM after
// changing page size) sqlite changes the size of the file
// _after_ `fsync` for some reason. In our case, this is
// critical because we are relying on fsync to write everything
// out. If we just did some writes, do another fsync which will
// check the meta and make sure it's persisted if dirty (all
// other writes should already be flushed by now)
this.fsync();
this.writeLock = false;
}
return this.ops.unlock(lockType);
}
fsync() {
if (this.buffer.size > 0) {
// We need to handle page size changes which restructures the
// whole db. We check if the page size is being written and
// handle it
let first = this.buffer.get(0);
if (first) {
let pageSize = getPageSize(new Uint8Array(first.data));
if (pageSize !== this.meta.blockSize) {
// The page size changed! We need to reflect that in our
// storage. We need to restructure all pending writes and
// change our page size so all future writes reflect the new
// size.
let buffer = this.buffer;
this.buffer = new Map();
// We take all pending writes, concat them into a single
// buffer, and rewrite it out with the new size. This would
// be dangerous if the page size could be changed at any
// point in time since we don't handle partial reads here.
// However sqlite only ever actually changes the page size
// in 2 cases:
//
// * The db is empty (no data yet, so nothing to read)
// * A VACUUM command is rewriting the entire db
//
// In both cases, we can assume we have _all_ the needed
// data in the pending buffer, and we don't have to worry
// about overwriting anything.
let writes = [...buffer.values()];
let totalSize = writes.length * this.meta.blockSize;
let buf = new ArrayBuffer(totalSize);
let view = new Uint8Array(buf);
for (let write of writes) {
view.set(new Uint8Array(write.data), write.pos);
}
// Rewrite the buffer with the new page size
this.bufferChunks(writeChunks(view, pageSize, 0, totalSize));
// Change our page size
this.setattr({ blockSize: pageSize });
}
}
this.ops.writeBlocks([...this.buffer.values()], this.meta.blockSize);
}
if (this._metaDirty) {
// We only store the size right now. Block size is already
// stored in the sqlite file and we don't need the rest
//
// TODO: Currently we don't delete any extra blocks after the
// end of the file. This isn't super important, and in fact
// could cause perf regressions (sqlite doesn't compress files
// either!) but what we probably should do is detect a VACUUM
// command (the whole db is being rewritten) and at that point
// delete anything after the end of the file
this.ops.writeMeta({ size: this.meta.size });
this._metaDirty = false;
}
this.buffer = new Map();
}
setattr(attr) {
if (this.meta == null) {
this.meta = {};
}
// Size is the only attribute we actually persist. The rest are
// stored in memory
if (attr.mode !== undefined) {
this.meta.mode = attr.mode;
}
if (attr.blockSize !== undefined) {
this.meta.blockSize = attr.blockSize;
}
if (attr.size !== undefined) {
this.meta.size = attr.size;
this._metaDirty = true;
}
}
getattr() {
return this.meta;
}
}