Expand FlatDb tests

This commit is contained in:
Jaco Greeff
2018-09-03 16:55:32 +02:00
parent 1f6d7bb118
commit 177824d356
2 changed files with 38 additions and 24 deletions
+25 -5
View File
@@ -10,23 +10,27 @@ import rimraf from 'rimraf';
import FileFlatDb from './FileFlatDb';
const KEY_A = new Uint8Array([
1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0x10, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
]);
const KEY_B = new Uint8Array([
2, 2, 2, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0x20, 2, 2, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
]);
const KEY_C = new Uint8Array([
1, 2, 3, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0x10, 2, 3, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
]);
const KEY_D = new Uint8Array([
1, 2, 3, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0x10, 2, 3, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
]);
const KEY_E = new Uint8Array([
1, 2, 4, 5, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0x10, 2, 4, 5, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
]);
const KEY_F = new Uint8Array([
0x50, 2, 4, 5, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
]);
const VAL_A = new Uint8Array([0x42, 1, 0x69]);
@@ -34,6 +38,7 @@ const VAL_B = new Uint8Array([0x42, 1, 2, 0x69]);
const VAL_C = new Uint8Array([0x42, 1, 2, 3, 0x69]);
const VAL_D = new Uint8Array([0x42, 1, 2, 3, 4, 0x69]);
const VAL_E = new Uint8Array([0x42, 1, 2, 3, 4, 5, 0x69]);
const VAL_F = new Uint8Array([0x42, 1, 2, 3, 4, 5, 6, 0x69]);
describe.skip('FileFlatDb (compacting)', () => {
const combined = new FileFlatDb(process.cwd());
@@ -124,4 +129,19 @@ describe.skip('FileFlatDb (basics)', () => {
testGet(KEY_D, VAL_D);
testGet(KEY_E, VAL_E);
});
it('writes an entry, expanding the top-level', () => {
console.error('F: execute');
store.put(KEY_F, VAL_F);
console.error('F: expectations');
testGet(KEY_A, VAL_A);
testGet(KEY_B, VAL_B);
testGet(KEY_C, VAL_C);
testGet(KEY_D, VAL_D);
testGet(KEY_E, VAL_E);
testGet(KEY_F, VAL_F);
});
});
+13 -19
View File
@@ -302,16 +302,13 @@ export default class FileFlatDb implements BaseDb {
let { valueAt, valueLength } = this._extractValueInfo(keyValue);
if (valueLength < value.length) {
const stats = fs.fstatSync(this._fd);
valueAt = stats.size;
valueAt = fs.fstatSync(this._fd).size;
}
fs.writeSync(this._fd, value, 0, value.length, valueAt);
keyValue.writeUIntBE(value.length, KEY_SIZE, UINT_SIZE);
keyValue.writeUIntBE(valueAt, KEY_SIZE + UINT_SIZE, UINT_SIZE);
fs.writeSync(this._fd, value, 0, value.length, valueAt);
fs.writeSync(this._fd, keyValue, KEY_SIZE, 2 * UINT_SIZE, keyAt + KEY_SIZE);
return {
@@ -331,8 +328,7 @@ export default class FileFlatDb implements BaseDb {
private _writeNewKey (key: NibbleBuffer): Key {
l.debug(() => ['writeNewKey', { key }]);
const stats = fs.fstatSync(this._fd);
const keyAt = stats.size;
const keyAt = fs.fstatSync(this._fd).size;
const keyValue = Buffer.alloc(KEY_TOTAL_SIZE);
keyValue.set(key.buffer, 0);
@@ -361,8 +357,7 @@ export default class FileFlatDb implements BaseDb {
const newBranch = Buffer.alloc(BRANCH_SIZE);
const keyIndex = ENTRY_SIZE * key.nibbles[matchIndex];
const prevIndex = ENTRY_SIZE * prevValue.nibbles[matchIndex];
const stats = fs.fstatSync(this._fd);
const newBranchAt = stats.size;
const newBranchAt = fs.fstatSync(this._fd).size;
newBranch.set([Slot.LEAF], keyIndex);
newBranch.writeUIntBE(keyAt, keyIndex + 1, UINT_SIZE);
@@ -377,11 +372,10 @@ export default class FileFlatDb implements BaseDb {
for (let offset = 1; depth > 0; depth--, offset++) {
const intermediate = Buffer.alloc(BRANCH_SIZE);
const intermediateIndex = key.nibbles[matchIndex - offset] * ENTRY_SIZE;
const stats = fs.fstatSync(this._fd);
intermediate.set([Slot.BRANCH], intermediateIndex);
intermediate.writeUIntBE(intermediateAt, intermediateIndex + 1, UINT_SIZE);
intermediateAt = stats.size;
intermediateAt = fs.fstatSync(this._fd).size;
fs.writeSync(this._fd, intermediate, 0, BRANCH_SIZE, intermediateAt);
this._cacheBranch(intermediateAt, intermediate);
@@ -467,14 +461,15 @@ export default class FileFlatDb implements BaseDb {
}
private _compactWriteKey (fd: number, key: Buffer, value: Buffer): number {
const stats = fs.fstatSync(fd);
const at = stats.size;
const valueAt = fs.fstatSync(fd).size;
const keyAt = valueAt + value.length;
fs.writeSync(fd, value, 0, value.length, at);
key.writeUIntBE(at, KEY_SIZE + UINT_SIZE, UINT_SIZE);
fs.writeSync(fd, key, 0, KEY_TOTAL_SIZE);
key.writeUIntBE(valueAt, KEY_SIZE + UINT_SIZE, UINT_SIZE);
return at + value.length;
fs.writeSync(fd, value, 0, value.length, valueAt);
fs.writeSync(fd, key, 0, KEY_TOTAL_SIZE, keyAt);
return keyAt;
}
private _compactUpdateLink (fd: number, at: number, index: number, pointer: number, type: Slot): void {
@@ -487,8 +482,7 @@ export default class FileFlatDb implements BaseDb {
}
private _compactWriteHeader (fd: number, at: number, index: number): number {
const stats = fs.fstatSync(fd);
const headerAt = stats.size;
const headerAt = fs.fstatSync(fd).size;
const header = Buffer.alloc(BRANCH_SIZE);
fs.writeSync(fd, header, 0, BRANCH_SIZE, headerAt);