From 55e05d34692fff5be452f445909f8368d436e1a0 Mon Sep 17 00:00:00 2001 From: Book Pauk Date: Mon, 31 Oct 2022 00:31:54 +0700 Subject: [PATCH] =?UTF-8?q?=D0=9E=D0=BF=D1=82=D0=B8=D0=BC=D0=B8=D0=B7?= =?UTF-8?q?=D0=B0=D1=86=D0=B8=D1=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- server/core/DbCreator.js | 22 ++++++++++++++++++---- 1 file changed, 18 insertions(+), 4 deletions(-) diff --git a/server/core/DbCreator.js b/server/core/DbCreator.js index ef54dc7..f5a9c6a 100644 --- a/server/core/DbCreator.js +++ b/server/core/DbCreator.js @@ -460,7 +460,7 @@ class DbCreator { await db.open({table: from}); await db.create({table: to}); - const bookId2RecId = new Map(); + let bookId2RecId = new Map(); const saveChunk = async(chunk) => { const ids = []; @@ -544,7 +544,7 @@ class DbCreator { await saveChunk(chunk); processed += chunk.length; - callback({progress: processed/fromLength}); + callback({progress: 0.5*processed/fromLength}); } else break; @@ -559,12 +559,26 @@ class DbCreator { await db.close({table: from}); await db.create({table: toId}); - const idRows = []; + + const chunkSize = 50000; + let idRows = []; + let proc = 0; for (const [id, value] of bookId2RecId) { idRows.push({id, value}); + if (idRows.length >= chunkSize) { + await db.insert({table: toId, rows: idRows}); + idRows = []; + + proc += chunkSize; + callback({progress: 0.5 + 0.5*proc/bookId2RecId.size}); + } } - await db.insert({table: toId, rows: idRows}); + if (idRows.length) + await db.insert({table: toId, rows: idRows}); await db.close({table: toId}); + + bookId2RecId = null; + utils.freeMemory(); } async countStats(db, callback, stats) {