From: stephan Date: Sun, 23 Nov 2025 12:14:18 +0000 (+0000) Subject: Bump kvvfs's sqlite3_vfs::iVersion to 2 so that its xCurrentTimeInt64() can be used... X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=ea375f1320ffee64acef29e70384c96fda8e5ace;p=thirdparty%2Fsqlite.git Bump kvvfs's sqlite3_vfs::iVersion to 2 so that its xCurrentTimeInt64() can be used. Add JsStorageDb.exportToObject() to serialize a kvvfs to a JSON-friendly form. FossilOrigin-Name: 393c1beee00ec3b3e98eb385cae0abeb1b34475140c0e8c3d57541d349904436 --- diff --git a/ext/wasm/api/sqlite3-vfs-kvvfs.c-pp.js b/ext/wasm/api/sqlite3-vfs-kvvfs.c-pp.js index 1d19cacc79..5365cbc2c2 100644 --- a/ext/wasm/api/sqlite3-vfs-kvvfs.c-pp.js +++ b/ext/wasm/api/sqlite3-vfs-kvvfs.c-pp.js @@ -11,7 +11,9 @@ *********************************************************************** - This file houses the "kvvfs" pieces of the JS API. + This file houses the "kvvfs" pieces of the JS API. Most of kvvfs is + implemented in src/os_kv.c and exposed for use here via + sqlite3-wasm.c. Main project home page: https://sqlite.org @@ -314,8 +316,8 @@ globalThis.sqlite3ApiBootstrap.initializers.push(function(sqlite3){ /** Implementations for members of the object referred to by sqlite3__wasm_kvvfs_methods(). We swap out some native - implementations with these, which use JS Storage for their - backing store. + implementations with these so that we can use JS Storage for + their backing store. */ const methodOverrides = { @@ -557,11 +559,6 @@ globalThis.sqlite3ApiBootstrap.initializers.push(function(sqlite3){ } }, -//#if nope - xFullPathname: function(pVfs, zPath, nOut, zOut){}, - xCurrentTime: function(pVfs,pOutDbl){}, - xCurrentTimeInt64: function(pVfs,pOutI64){}, -//#endif xRandomness: function(pVfs, nOut, pOut){ const heap = wasm.heap8u(); let i = 0; @@ -569,6 +566,20 @@ globalThis.sqlite3ApiBootstrap.initializers.push(function(sqlite3){ for(; i < nOut; ++i) heap[npOut + i] = (Math.random()*255000) & 0xFF; return i; } + +//#if nope + // these impls work but there's currently no pressing need _not_ use + // the native impls. + xCurrentTime: function(pVfs,pOut){ + wasm.poke64f(pOut, 2440587.5 + (new Date().getTime()/86400000)); + return 0; + }, + + xCurrentTimeInt64: function(pVfs,pOut){ + wasm.poke64(pOut, (2440587.5 * 86400000) + new Date().getTime()); + return 0; + } +//#endif }, /** @@ -719,6 +730,13 @@ globalThis.sqlite3ApiBootstrap.initializers.push(function(sqlite3){ ){ const opt = DB.dbCtorHelper.normalizeArgs(...arguments); opt.vfs = 'kvvfs'; + switch( opt.filename ){ + /* sqlite3_open(), in these builds, recognizes the names + below and performs some magic which we want to bypass + here for sanity's sake. */ + case ":sessionStorage:": opt.filename = 'session'; break; + case ":localStorage:": opt.filename = 'local'; break; + } DB.dbCtorHelper.call(this, opt); }; sqlite3.oo1.JsStorageDb.defaultStorageName = 'session'; @@ -744,24 +762,73 @@ globalThis.sqlite3ApiBootstrap.initializers.push(function(sqlite3){ return jdb.storageSize(this.affirmOpen().filename); }; - if( sqlite3.__isUnderTest ){ - jdb.prototype.testDbToObject = function(includeJournal=false){ - const store = cache.jzClassToStorage[this.affirmOpen().filename]; - const rx = includeJournal ? undefined : /^kvvfs(-(local|session))?-jrnl$/; - if( store ){ - const s = store.s; - const rc = Object.create(null); - let i = 0, n = s.length; - for( ; i < n; ++i ){ - const k = s.key(i); - if( !rx || !rx.test(k) ){ - rc[k] = s.getItem(k); - } + /** + Copies the entire contents of this db into a JSON-friendly + form. The returned object is structured as follows... + + - "name": the name of the db. This is 'local' or 'session' for + localStorage resp. sessionStorage, and an arbitrary name for + transient storage. + + - "timestamp": the time this function was called, in Unix + epoch milliseconds. + + - "data": An object holding the raw encoded state. It has the + following properties: + + - "kvvfs[-X]-sz" = the decoded size of the db. Its encoded + size may vary wildly from that in either direction, + depending largely on the ratio of empty space to data. + + - "kvvfs[-X]-jrnl" = if includeJournal is true and the db has + a journal, it is stored in this record. If is has no + journal, or includeJournal is false, this key is not set. + + - "kvvfs[-X]-###" = one encoded page of the db, with ### + corresponding to the page number. + + The [-X] parts are only set for localStorage and sessionStorage + back-ends and the X of each is 'local' or 'session'. That is: + the keys contain the storage back-end's name because of how the + underlying native VFS works (each key goes in its own file so + it must be distinct per storage name). That part is retained + here for backwards compatibility - transient storage objects + elide that part. + + The encoding of the underlying database is not part of this + interface - it is simply passed on as-is. + + Throws if this db is not opened. + + Added in version 3.?? (tenatively 3.52). + */ + jdb.prototype.exportToObject = function(includeJournal=false){ + this.affirmOpen(); + const store = cache.jzClassToStorage[this.affirmOpen().filename]; + const rx = includeJournal ? undefined : /^kvvfs(-(local|session))?-jrnl$/; + if( store ){ + const s = store.s; + const rc = Object.assign(Object.create(null),{ + name: this.filename, + timestamp: (new Date()).valueOf(), + data:Object.create(null) + }); + let i = 0, n = s.length; + for( ; i < n; ++i ){ + const k = s.key(i); + if( !rx || !rx.test(k) ){ + rc.data[k] = s.getItem(k); } - return rc; } + return rc; + } + }; + + if( sqlite3.__isUnderTest ){ + jdb.test = { + kvvfsWhich, + cache } - jdb.testKvvfsWhich = kvvfsWhich; }/* __isUnderTest */ }/*sqlite3.oo1.JsStorageDb*/ diff --git a/ext/wasm/tester1.c-pp.js b/ext/wasm/tester1.c-pp.js index 956690e716..25b014eedd 100644 --- a/ext/wasm/tester1.c-pp.js +++ b/ext/wasm/tester1.c-pp.js @@ -2885,7 +2885,7 @@ globalThis.sqlite3InitModule = sqlite3InitModule; const JDb = sqlite3.oo1.JsStorageDb; const pVfs = capi.sqlite3_vfs_find('kvvfs'); T.assert(looksLikePtr(pVfs)); - let x = JDb.testKvvfsWhich(''); + let x = JDb.test.kvvfsWhich(''); T.assert( 2 === x?.stores?.length ) .assert( x.stores.indexOf(globalThis.sessionStorage)>-1 ) .assert( x.stores.indexOf(globalThis.localStorage)>-1 ) @@ -2936,13 +2936,13 @@ globalThis.sqlite3InitModule = sqlite3InitModule; db = new JDb(filename); db.exec('insert into kvvfs(a) values(4),(5),(6)'); T.assert(6 === db.selectValue('select count(*) from kvvfs')); - console.debug("kvvfs to Object:",db.testDbToObject(true)); + console.debug("kvvfs to Object:",db.exportToObject(true)); close(); db = new JDb('new-storage'); db.exec(sqlSetup); T.assert(3 === db.selectValue('select count(*) from kvvfs')); - console.debug("kvvfs to Object:",db.testDbToObject(true)); + console.debug("kvvfs to Object:",db.exportToObject(true)); const n = db.storageSize(); T.assert( n>0, "Db size count failed" ); diff --git a/manifest b/manifest index 68253d1172..2806839fb2 100644 --- a/manifest +++ b/manifest @@ -1,5 +1,5 @@ -C Replace\san\salloc/free\son\severy\sread\sof\sa\sJS\skvvfs\skey\swith\sa\sone-time\salloc\sand\sa\slong\sparagraph\sexplaining\swhy\swe\shave\sto\sleak\sit. -D 2025-11-23T11:28:27.602 +C Bump\skvvfs's\ssqlite3_vfs::iVersion\sto\s2\sso\sthat\sits\sxCurrentTimeInt64()\scan\sbe\sused.\sAdd\sJsStorageDb.exportToObject()\sto\sserialize\sa\skvvfs\sto\sa\sJSON-friendly\sform. +D 2025-11-23T12:14:18.400 F .fossil-settings/binary-glob 61195414528fb3ea9693577e1980230d78a1f8b0a54c78cf1b9b24d0a409ed6a x F .fossil-settings/empty-dirs dbb81e8fc0401ac46a1491ab34a7f2c7c0452f2f06b54ebb845d024ca8283ef1 F .fossil-settings/ignore-glob 35175cdfcf539b2318cb04a9901442804be81cd677d8b889fcc9149c21f239ea @@ -600,7 +600,7 @@ F ext/wasm/api/sqlite3-api-worker1.c-pp.js 1041dd645e8e821c082b628cd8d9acf70c667 F ext/wasm/api/sqlite3-license-version-header.js 0c807a421f0187e778dc1078f10d2994b915123c1223fe752b60afdcd1263f89 F ext/wasm/api/sqlite3-opfs-async-proxy.js 9654b565b346dc609b75d15337f20acfa7af7d9d558da1afeb9b6d8eaa404966 F ext/wasm/api/sqlite3-vfs-helper.c-pp.js 3f828cc66758acb40e9c5b4dcfd87fd478a14c8fb7f0630264e6c7fa0e57515d -F ext/wasm/api/sqlite3-vfs-kvvfs.c-pp.js 6a7007c26361b9d9b74d0740c61eea5d93253675824422b100be38b9cbe25073 +F ext/wasm/api/sqlite3-vfs-kvvfs.c-pp.js e98bc058013b8fe189e9b38fb9421e6d514b651f4a38c5b6c331aee175cad13d F ext/wasm/api/sqlite3-vfs-opfs-sahpool.c-pp.js 26cb41d5a62f46a106b6371eb00fef02de3cdbfaa51338ba087a45f53028e0d0 F ext/wasm/api/sqlite3-vfs-opfs.c-pp.js 88ce2078267a2d1af57525a32d896295f4a8db7664de0e17e82dc9ff006ed8d3 F ext/wasm/api/sqlite3-vtab-helper.c-pp.js 9097074724172e31e56ce20ccd7482259cf72a76124213cbc9469d757676da86 @@ -647,7 +647,7 @@ F ext/wasm/test-opfs-vfs.html 1f2d672f3f3fce810dfd48a8d56914aba22e45c6834e262555 F ext/wasm/test-opfs-vfs.js 1618670e466f424aa289859fe0ec8ded223e42e9e69b5c851f809baaaca1a00c F ext/wasm/tester1-worker.c-pp.html 0e432ec2c0d99cd470484337066e8d27e7aee4641d97115338f7d962bf7b081a F ext/wasm/tester1.c-pp.html 52d88fe2c6f21a046030a36410b4839b632f4424028197a45a3d5669ea724ddb -F ext/wasm/tester1.c-pp.js ff028364da670936db6b54d393d2c3672341c9285e210677a75f078c3bf0b5aa +F ext/wasm/tester1.c-pp.js a0f3b0c36503440d5488a40c22bfeb633d8011a94db0403a8e22a1a22c801c43 F ext/wasm/tests/opfs/concurrency/index.html 657578a6e9ce1e9b8be951549ed93a6a471f4520a99e5b545928668f4285fb5e F ext/wasm/tests/opfs/concurrency/test.js d08889a5bb6e61937d0b8cbb78c9efbefbf65ad09f510589c779b7cc6a803a88 F ext/wasm/tests/opfs/concurrency/worker.js 0a8c1a3e6ebb38aabbee24f122693f1fb29d599948915c76906681bb7da1d3d2 @@ -717,7 +717,7 @@ F src/notify.c 57c2d1a2805d6dee32acd5d250d928ab94e02d76369ae057dee7d445fd64e878 F src/os.c 509452169d5ea739723e213b8e2481cf0e587f0e88579a912d200db5269f5f6d F src/os.h 1ff5ae51d339d0e30d8a9d814f4b8f8e448169304d83a7ed9db66a65732f3e63 F src/os_common.h 6c0eb8dd40ef3e12fe585a13e709710267a258e2c8dd1c40b1948a1d14582e06 -F src/os_kv.c 653b3825dc63dd6a83b04814df4bda28effde43d5f94f3611648701d04749fc3 +F src/os_kv.c 0101f8f61456d02afc4d0cb26304a8ebcf343a1be639986486184007007aa26c F src/os_setup.h 8efc64eda6a6c2f221387eefc2e7e45fd5a3d5c8337a7a83519ba4fbd2957ae2 F src/os_unix.c 7945ede1e85b2d1b910e1b4af9ba342e964b1e30e79f4176480a60736445cb36 F src/os_win.c a89b501fc195085c7d6c9eec7f5bd782625e94bb2a96b000f4d009703df1083f @@ -2178,8 +2178,8 @@ F tool/version-info.c 33d0390ef484b3b1cb685d59362be891ea162123cea181cb8e6d2cf6dd F tool/warnings-clang.sh bbf6a1e685e534c92ec2bfba5b1745f34fb6f0bc2a362850723a9ee87c1b31a7 F tool/warnings.sh d924598cf2f55a4ecbc2aeb055c10bd5f48114793e7ba25f9585435da29e7e98 F tool/win/sqlite.vsix deb315d026cc8400325c5863eef847784a219a2f -P 69e591d0054218ead789cee199e5258f1c378a89e4b7b0e38fe74e834e23156b -R 59e8541a0d2f7929ff70c46115b7160f +P 66fb9978f0a63887d214e895343adedfa46ee7aefccb8389d6d3af727e0a65ea +R b2359aa56435f6b00b3674071d34a67b U stephan -Z dc6a511f30ab2f711394f8b6dd32e86b +Z 40d9eaa6863aa6db1ffa4d5313d4844e # Remove this line to create a well-formed Fossil manifest. diff --git a/manifest.uuid b/manifest.uuid index 194b2617d1..91cbf558b6 100644 --- a/manifest.uuid +++ b/manifest.uuid @@ -1 +1 @@ -66fb9978f0a63887d214e895343adedfa46ee7aefccb8389d6d3af727e0a65ea +393c1beee00ec3b3e98eb385cae0abeb1b34475140c0e8c3d57541d349904436 diff --git a/src/os_kv.c b/src/os_kv.c index 305962dd09..c3752c7b33 100644 --- a/src/os_kv.c +++ b/src/os_kv.c @@ -96,7 +96,7 @@ static int kvvfsCurrentTime(sqlite3_vfs*, double*); static int kvvfsCurrentTimeInt64(sqlite3_vfs*, sqlite3_int64*); static sqlite3_vfs sqlite3OsKvvfsObject = { - 1, /* iVersion */ + 2, /* iVersion */ sizeof(KVVfsFile), /* szOsFile */ 1024, /* mxPathname */ 0, /* pNext */