From bef7e0ea4706567506ca6cd4afe8e732c00ca1b1 Mon Sep 17 00:00:00 2001 From: KuznetsovRoman Date: Wed, 30 Aug 2023 03:01:10 +0300 Subject: [PATCH 1/3] feat: define db size --- src/api.js | 34 +++++++++++++++++++++++++++++++++- 1 file changed, 33 insertions(+), 1 deletion(-) diff --git a/src/api.js b/src/api.js index 6a7ce924..80e4c228 100644 --- a/src/api.js +++ b/src/api.js @@ -735,6 +735,30 @@ Module["onRuntimeInitialized"] = function onRuntimeInitialized() { this.activeStatement = null; } + function expandFileStorage(node, newCapacity) { + var prevCapacity = node.contents ? node.contents.length : 0; + // No need to expand, the storage was already large enough. + // Don't expand strictly to the given requested limit if it's + // only a very small increase, but instead geometrically grow capacity. + if (prevCapacity >= newCapacity) return; + // For small filesizes (<1MB), perform size*2 geometric increase, but + // for large sizes, do a much more conservative size*1.125 increase to + // avoid overshooting the allocation cap by a very large margin. + var CAPACITY_DOUBLING_MAX = 1024 * 1024; + var capacityCoef = prevCapacity < CAPACITY_DOUBLING_MAX ? 2.0 : 1.125; + var newAutoCapacity = prevCapacity * capacityCoef; + newCapacity = Math.max(newCapacity, newAutoCapacity | 0); + // At minimum allocate 256b for each file when expanding. + if (prevCapacity !== 0) newCapacity = Math.max(newCapacity, 256); + var oldContents = node.contents; + node.contents = new Uint8Array(newCapacity); // Allocate new storage. + + if (node.usedBytes > 0) { + // Copy old data over to the new storage. + node.contents.set(oldContents.subarray(0, node.usedBytes), 0); + } + } + /** * @typedef {{ done:true, value:undefined } | * { done:false, value:Statement}} @@ -822,7 +846,7 @@ Module["onRuntimeInitialized"] = function onRuntimeInitialized() { * @param {number[]} data An array of bytes representing * an SQLite database file */ - function Database(data) { + function Database(data, initialDbSize) { this.filename = "dbfile_" + (0xffffffff * Math.random() >>> 0); if (data != null) { FS.createDataFile("/", this.filename, data, true, true); @@ -830,6 +854,14 @@ Module["onRuntimeInitialized"] = function onRuntimeInitialized() { this.handleError(sqlite3_open(this.filename, apiTemp)); this.db = getValue(apiTemp, "i32"); registerExtensionFunctions(this.db); + + if (initialDbSize) { + this.run("VACUUM"); + var parentNode = FS.lookupPath("/").node; + var dbNode = FS.lookupNode(parentNode, this.filename); + expandFileStorage(dbNode, initialDbSize); + } + // A list of all prepared statements of the database this.statements = {}; // A list of all user function of the database From 58bd257cafaeb9e8deba5682aa7ea179820c195b Mon Sep 17 00:00:00 2001 From: KuznetsovRoman Date: Wed, 30 Aug 2023 13:11:51 +0300 Subject: [PATCH 2/3] chore: rename to @gemini-testing/sql.js --- package-lock.json | 4 ++-- package.json | 10 +++++----- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/package-lock.json b/package-lock.json index 35f65feb..bc6d7bf9 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,6 +1,6 @@ { - "name": "sql.js", - "version": "1.8.0", + "name": "@gemini-testing/sql.js", + "version": "1.8.1", "lockfileVersion": 1, "requires": true, "dependencies": { diff --git a/package.json b/package.json index 52b499ec..8564d226 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { - "name": "sql.js", - "version": "1.8.0", + "name": "@gemini-testing/sql.js", + "version": "1.8.1", "description": "SQLite library with support for opening and writing databases, prepared statements, and more. This SQLite library is in pure javascript (compiled with emscripten).", "keywords": [ "sql", @@ -32,13 +32,13 @@ "test-wasm-debug": "node --unhandled-rejections=strict test/all.js wasm-debug", "doc": "jsdoc -c .jsdoc.config.json" }, - "homepage": "http://github.com/sql-js/sql.js", + "homepage": "https://github.com/gemini-testing/sql.js", "repository": { "type": "git", - "url": "http://github.com/sql-js/sql.js.git" + "url": "http://github.com/gemini-testing/sql.js.git" }, "bugs": { - "url": "https://github.com/sql-js/sql.js/issues" + "url": "https://github.com/gemini-testing/sql.js/issues" }, "devDependencies": { "clean-jsdoc-theme": "^3.3.4", From baf1fe5e87ebfd60fa6f3f523484c123efe19e39 Mon Sep 17 00:00:00 2001 From: KuznetsovRoman Date: Thu, 31 Aug 2023 14:25:11 +0300 Subject: [PATCH 3/3] chore(release): 2.0.0 --- package-lock.json | 2 +- package.json | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/package-lock.json b/package-lock.json index bc6d7bf9..94a4bfde 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,6 +1,6 @@ { "name": "@gemini-testing/sql.js", - "version": "1.8.1", + "version": "2.0.0", "lockfileVersion": 1, "requires": true, "dependencies": { diff --git a/package.json b/package.json index 8564d226..4ff6aebe 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@gemini-testing/sql.js", - "version": "1.8.1", + "version": "2.0.0", "description": "SQLite library with support for opening and writing databases, prepared statements, and more. This SQLite library is in pure javascript (compiled with emscripten).", "keywords": [ "sql",