diff options
author | stephan <stephan@noemail.net> | 2023-07-14 23:02:58 +0000 |
---|---|---|
committer | stephan <stephan@noemail.net> | 2023-07-14 23:02:58 +0000 |
commit | bee3213145d88f19ea9767a86fef40fd1aa1b557 (patch) | |
tree | bcc83432eeb9863c7df38b7d10bd774736324524 /ext/wasm/api/sqlite3-vfs-opfs-sahpool.js | |
parent | 92bf6fd4f63c8cccf6a0e455fe2b8276f95cddab (diff) | |
download | sqlite-bee3213145d88f19ea9767a86fef40fd1aa1b557.tar.gz sqlite-bee3213145d88f19ea9767a86fef40fd1aa1b557.zip |
Switch opfs-sahpool to use deterministic backing-store file names. Delay VFS registration until after the pool's files are all opened. Fail vfs init if the client's OPFS API has an async FileSystemSyncAccessHandle.close() method (indicating that it's outdated).
FossilOrigin-Name: 7c6697ededee9a64962ac6fd78934c6d6e39258b9558a03c1a6c02bf3be1759e
Diffstat (limited to 'ext/wasm/api/sqlite3-vfs-opfs-sahpool.js')
-rw-r--r-- | ext/wasm/api/sqlite3-vfs-opfs-sahpool.js | 42 |
1 files changed, 25 insertions, 17 deletions
diff --git a/ext/wasm/api/sqlite3-vfs-opfs-sahpool.js b/ext/wasm/api/sqlite3-vfs-opfs-sahpool.js index bb91d3464..410986ff2 100644 --- a/ext/wasm/api/sqlite3-vfs-opfs-sahpool.js +++ b/ext/wasm/api/sqlite3-vfs-opfs-sahpool.js @@ -130,9 +130,13 @@ const installOpfsVfs = async function(sqlite3){ 'cleanup default VFS wrapper', ()=>(dVfs ? dVfs.dispose() : null) ); - const VState = Object.assign(Object.create(null),{ + const getFilename = function(ndx){ + return 'sahpool-'+('00'+ndx).substr(-3); + } + + const SAHPool = Object.assign(Object.create(null),{ /* OPFS dir in which VFS metadata is stored. */ - vfsDir: ".sqlite3-sahpool", + vfsDir: ".sqlite3-opfs-sahpool", dirHandle: undefined, /* Maps OPFS access handles to their opaque file names. */ mapAH2Name: new Map(), @@ -142,10 +146,20 @@ const installOpfsVfs = async function(sqlite3){ getCapacity: function(){return this.mapAH2Name.size}, getFileCount: function(){return this.mapPath2AH.size}, addCapacity: async function(n){ - for(let i = 0; i < n; ++i){ - const name = Math.random().toString(36).replace('0.',''); + const cap = this.getCapacity(); + for(let i = cap; i < cap+n; ++i){ + const name = getFilename(i); const h = await this.dirHandle.getFileHandle(name, {create:true}); - const ah = await h.createSyncAccessHandle(); + let ah = await h.createSyncAccessHandle(); + if(0===i){ + /* Ensure that this client has the "all-synchronous" + OPFS API and fail if they don't. */ + if(undefined !== ah.close()){ + toss("OPFS API is too old for opfs-sahpool:", + "it has an async close() method."); + } + ah = await h.createSyncAccessHandle(); + } this.mapAH2Name.set(ah,name); this.setAssociatedPath(ah, '', 0); } @@ -175,8 +189,8 @@ const installOpfsVfs = async function(sqlite3){ await this.acquireAccessHandles(); } // much more TODO - })/*VState*/; - + })/*SAHPool*/; + sqlite3.SAHPool = SAHPool/*only for testing*/; // Much, much more TODO... /** Impls for the sqlite3_io_methods methods. Maintenance reminder: @@ -287,22 +301,16 @@ const installOpfsVfs = async function(sqlite3){ }; } - try{ + SAHPool.isReady = SAHPool.reset().then(async ()=>{ + if(0===SAHPool.getCapacity()){ + await SAHPool.addCapacity(DEFAULT_CAPACITY); + } log("vfs list:",capi.sqlite3_js_vfs_list()); sqlite3.vfs.installVfs({ io: {struct: opfsIoMethods, methods: ioSyncWrappers}, vfs: {struct: opfsVfs, methods: vfsSyncWrappers} }); log("vfs list:",capi.sqlite3_js_vfs_list()); - }catch(e){ - promiseReject(e); - return; - } - - VState.isReady = VState.reset().then(async ()=>{ - if(0===VState.getCapacity()){ - await VState.addCapacity(DEFAULT_CAPACITY); - } log("opfs-sahpool VFS initialized."); promiseResolve(sqlite3); }).catch(promiseReject); |