Cleanup FlatDb branching logic (re-use)

This commit is contained in:
Jaco Greeff
2018-09-03 20:02:14 +02:00
parent 4655726c92
commit 385db9ef25
2 changed files with 26 additions and 21 deletions
+7 -7
View File
@@ -18,11 +18,11 @@ const KEY_B = new Uint8Array([
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
]);
const KEY_C = new Uint8Array([
0x10, 2, 3, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0x10, 2, 6, 6, 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([
0x10, 2, 3, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0x10, 2, 6, 7, 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([
@@ -98,12 +98,12 @@ describe.skip('FileFlatDb (basics)', () => {
console.error('C: expectations');
testGet(KEY_A, VAL_A);
testGet(KEY_B, VAL_B);
// testGet(KEY_A, VAL_A);
// testGet(KEY_B, VAL_B);
testGet(KEY_C, VAL_C);
});
it('writes an entry (expanding the tree, again)', () => {
it.skip('writes an entry (expanding the tree, again)', () => {
console.error('D: execute');
store.put(KEY_D, VAL_D);
@@ -116,7 +116,7 @@ describe.skip('FileFlatDb (basics)', () => {
testGet(KEY_D, VAL_D);
});
it('writes an entry (expanding the tree, yet again)', () => {
it.skip('writes an entry (expanding the tree, yet again)', () => {
console.error('E: execute');
store.put(KEY_E, VAL_E);
@@ -130,7 +130,7 @@ describe.skip('FileFlatDb (basics)', () => {
testGet(KEY_E, VAL_E);
});
it('writes an entry, expanding the top-level', () => {
it.skip('writes an entry, expanding the top-level', () => {
console.error('F: execute');
store.put(KEY_F, VAL_F);
+19 -14
View File
@@ -195,6 +195,14 @@ export default class FileFlatDb implements BaseDb {
return branch;
}
private _getKeyValue (keyAt: number): Buffer {
const keyValue = Buffer.alloc(KEY_TOTAL_SIZE);
fs.readSync(this._fd, keyValue, 0, KEY_TOTAL_SIZE, keyAt);
return keyValue;
}
private _cacheBranch (branchAt: number, branch: Buffer): void {
this._lruBranch.set(branchAt, branch);
}
@@ -225,18 +233,14 @@ export default class FileFlatDb implements BaseDb {
if (entryType === Slot.LEAF) {
const keyAt = branch.readUIntBE(entryIndex + 1, UINT_SIZE);
const keyValue = Buffer.alloc(KEY_TOTAL_SIZE);
fs.readSync(this._fd, keyValue, 0, KEY_TOTAL_SIZE, keyAt);
const prevValue = this._serializeKey(keyValue.subarray(0, KEY_SIZE));
const keyValue = this._getKeyValue(keyAt);
const prevKey = this._serializeKey(keyValue.subarray(0, KEY_SIZE));
let matchIndex = keyIndex;
l.debug(() => ['findKey/isLeaf', { keyAt, branch, branchAt, entryIndex, keyValue }]);
let matchIndex = keyIndex;
while (matchIndex < KEY_SIZE) {
if (prevValue.nibbles[matchIndex] !== key.nibbles[matchIndex]) {
if (prevKey.nibbles[matchIndex] !== key.nibbles[matchIndex]) {
break;
}
@@ -245,7 +249,7 @@ export default class FileFlatDb implements BaseDb {
if (matchIndex !== KEY_SIZE) {
return doCreate
? this.writeNewBranch(branch, branchAt, entryIndex, key, keyAt, prevValue, matchIndex, matchIndex - keyIndex - 1)
? this.writeNewBranch(branch, branchAt, entryIndex, key, keyAt, prevKey, matchIndex, matchIndex - keyIndex - 1)
: null;
}
@@ -350,12 +354,12 @@ export default class FileFlatDb implements BaseDb {
return result;
}
private _writeNewBranch (branch: Buffer, branchAt: number, entryIndex: number, key: NibbleBuffer, prevAt: number, prevValue: NibbleBuffer, matchIndex: number, depth: number): Key {
l.debug(() => ['writeNewBranch', { branch, branchAt, entryIndex, key, prevValue, matchIndex, depth }]);
private _writeNewBranch (branch: Buffer, branchAt: number, entryIndex: number, key: NibbleBuffer, prevAt: number, prevKey: NibbleBuffer, matchIndex: number, depth: number): Key {
l.debug(() => ['writeNewBranch', { branch, branchAt, entryIndex, key, prevAt, prevKey, matchIndex, depth }]);
const { keyAt, keyValue } = this.writeNewKey(key);
const keyIndex = ENTRY_SIZE * key.nibbles[matchIndex];
const prevIndex = ENTRY_SIZE * prevValue.nibbles[matchIndex];
const keyIndex = key.nibbles[matchIndex] * ENTRY_SIZE;
const prevIndex = prevKey.nibbles[matchIndex] * ENTRY_SIZE;
let newBranchAt = fs.fstatSync(this._fd).size;
let newBranch = Buffer.alloc(BRANCH_SIZE);
@@ -364,7 +368,8 @@ export default class FileFlatDb implements BaseDb {
newBranch.set([Slot.LEAF], prevIndex);
newBranch.writeUIntBE(prevAt, prevIndex + 1, UINT_SIZE);
fs.writeSync(this._fd, branch, 0, BRANCH_SIZE, newBranchAt);
fs.writeSync(this._fd, newBranch, 0, BRANCH_SIZE, newBranchAt);
this._cacheBranch(newBranchAt, newBranch);
for (let offset = 1; depth > 0; depth--, offset++) {