Comparing version 3.0.2 to 3.0.4
{ | ||
"name": "jembadb", | ||
"version": "3.0.2", | ||
"version": "3.0.4", | ||
"description": "Json Embeddable Appendable Database", | ||
@@ -5,0 +5,0 @@ "main": "./src/index.js", |
@@ -28,2 +28,3 @@ 'use strict'; | ||
this.loadedBlocks = []; | ||
this.newBlocks = []; | ||
this.deltas = new Map(); | ||
@@ -137,3 +138,3 @@ | ||
this.blockList.set(this.currentBlockIndex, block); | ||
this.loadedBlocks.push(this.currentBlockIndex); | ||
this.newBlocks.push(this.currentBlockIndex); | ||
this.blocksNotFinalized.set(this.currentBlockIndex, 1); | ||
@@ -172,2 +173,13 @@ | ||
unloadBlocksIfNeeded() { | ||
const nb = []; | ||
for (const index of this.newBlocks) { | ||
if (index < this.lastSavedBlockIndex) { | ||
this.loadedBlocks.push(index); | ||
} else { | ||
nb.push(index); | ||
} | ||
} | ||
this.newBlocks = nb; | ||
if (this.loadedBlocks.length <= this.loadedBlocksCount) | ||
@@ -177,9 +189,9 @@ return; | ||
//check loaded | ||
let missed = new Set(); | ||
while (this.loadedBlocks.length > this.loadedBlocksCount) { | ||
const index = this.loadedBlocks.shift(); | ||
if (index >= this.lastSavedBlockIndex) { | ||
missed.add(index); | ||
//additional check, just in case | ||
if (index >= this.lastSavedBlockIndex) | ||
continue; | ||
} | ||
const block = this.blockList.get(index); | ||
@@ -192,4 +204,2 @@ | ||
} | ||
this.loadedBlocks = this.loadedBlocks.concat(Array.from(missed)); | ||
} | ||
@@ -262,3 +272,3 @@ | ||
this.loadedBlocks.push(block.index); | ||
//console.log(`loaded block ${block.index}`); | ||
//console.log(`loaded block ${block.index}`, this.lastSavedBlockIndex, this.currentBlockIndex); | ||
} | ||
@@ -372,8 +382,2 @@ } finally { | ||
//lastSavedBlockIndex | ||
const len = delta.blockRows.length; | ||
if (len) { | ||
this.lastSavedBlockIndex = delta.blockRows[len - 1][0]; | ||
} | ||
//check all blocks fragmentation & defragment if needed | ||
@@ -488,2 +492,8 @@ if (!this.defragCandidates) | ||
//lastSavedBlockIndex | ||
const len = delta.blockRows.length; | ||
if (len) { | ||
this.lastSavedBlockIndex = delta.blockRows[len - 1][0]; | ||
} | ||
//blocks finalization | ||
@@ -586,4 +596,7 @@ await this.finalizeBlocks(); | ||
const currentBlock = this.blockList.get(this.currentBlockIndex); | ||
if (currentBlock) | ||
if (currentBlock) { | ||
await this.loadBlock(currentBlock); | ||
this.newBlocks.push(this.currentBlockIndex); | ||
this.loadedBlocks = []; | ||
} | ||
@@ -590,0 +603,0 @@ this.blocksNotFinalized = new Map(); |
181290
4769