1
0
mirror of https://github.com/sqlite/sqlite.git synced 2025-07-29 08:01:23 +03:00

Fiddle: move, rather than copy, data buffers between the threads. Fix the case of an uploaded db failing to install because its filename is the same as the opened db.

FossilOrigin-Name: bcec4f964a7b02f59be05286ff715bac654a32b19f05a743e402f4cdb207cab8
This commit is contained in:
stephan
2022-09-24 11:32:00 +00:00
parent ef11fb915d
commit 395012e58e
4 changed files with 56 additions and 46 deletions

View File

@ -90,18 +90,13 @@
"use strict"; "use strict";
(function(){ (function(){
/** /**
Posts a message in the form {type,data} unless passed more than 2 Posts a message in the form {type,data}. If passed more than 2
args, in which case it posts {type, data:[arg1...argN]}. args, the 3rd must be an array of "transferable" values to pass
*/ as the 2nd argument to postMessage(). */
const wMsg = function(type,data){ const wMsg =
postMessage({ (type,data,transferables)=>{
type, postMessage({type, data}, transferables || []);
data: arguments.length<3 };
? data
: Array.prototype.slice.call(arguments,1)
});
};
const stdout = (...args)=>wMsg('stdout', args); const stdout = (...args)=>wMsg('stdout', args);
const stderr = (...args)=>wMsg('stderr', args); const stderr = (...args)=>wMsg('stderr', args);
@ -242,10 +237,10 @@
const fn2 = fn ? fn.split(/[/\\]/).pop() : null; const fn2 = fn ? fn.split(/[/\\]/).pop() : null;
try{ try{
if(!fn2) throw new Error("DB appears to be closed."); if(!fn2) throw new Error("DB appears to be closed.");
wMsg('db-export',{ const buffer = fiddleModule.FS.readFile(
filename: fn2, fn, {encoding:"binary"}
buffer: fiddleModule.FS.readFile(fn, {encoding:"binary"}) ).buffer;
}); wMsg('db-export',{filename: fn2, buffer}, [buffer]);
}catch(e){ }catch(e){
/* Post a failure message so that UI elements disabled /* Post a failure message so that UI elements disabled
during the export can be re-enabled. */ during the export can be re-enabled. */
@ -258,16 +253,17 @@
} }
case 'open': { case 'open': {
/* Expects: { /* Expects: {
buffer: ArrayBuffer | Uint8Array, buffer: ArrayBuffer | Uint8Array,
filename: for logging/informational purposes only filename: the filename for the db. Any dir part is
} */ stripped.
}
*/
const opt = ev.data; const opt = ev.data;
let buffer = opt.buffer; let buffer = opt.buffer;
stderr('open():',fixmeOPFS); stderr('open():',fixmeOPFS);
if(buffer instanceof Uint8Array){ if(buffer instanceof ArrayBuffer){
}else if(buffer instanceof ArrayBuffer){
buffer = new Uint8Array(buffer); buffer = new Uint8Array(buffer);
}else{ }else if(!(buffer instanceof Uint8Array)){
stderr("'open' expects {buffer:Uint8Array} containing an uploaded db."); stderr("'open' expects {buffer:Uint8Array} containing an uploaded db.");
return; return;
} }
@ -277,18 +273,30 @@
: ("db-"+((Math.random() * 10000000) | 0)+ : ("db-"+((Math.random() * 10000000) | 0)+
"-"+((Math.random() * 10000000) | 0)+".sqlite3") "-"+((Math.random() * 10000000) | 0)+".sqlite3")
); );
/* We cannot delete the existing db file until the new one try {
is installed, which means that we risk overflowing our /* We cannot delete the existing db file until the new one
quota (if any) by having both the previous and current is installed, which means that we risk overflowing our
db briefly installed in the virtual filesystem. */ quota (if any) by having both the previous and current
fiddleModule.FS.createDataFile("/", fn, buffer, true, true); db briefly installed in the virtual filesystem. */
const oldName = Sqlite3Shell.dbFilename(); const fnAbs = '/'+fn;
Sqlite3Shell.exec('.open "/'+fn+'"'); const oldName = Sqlite3Shell.dbFilename();
if(oldName && oldName !== fn){ if(oldName && oldName===fnAbs){
try{fiddleModule.fsUnlink(oldName);} /* We cannot create the replacement file while the current file
catch(e){/*ignored*/} is opened, nor does the shell have a .close command, so we
must temporarily switch to another db... */
Sqlite3Shell.exec('.open :memory:');
fiddleModule.FS.unlink(fnAbs);
}
fiddleModule.FS.createDataFile("/", fn, buffer, true, true);
Sqlite3Shell.exec('.open "'+fnAbs+'"');
if(oldName && oldName!==fnAbs){
try{fiddleModule.fsUnlink(oldName)}
catch(e){/*ignored*/}
}
stdout("Replaced DB with",fn+".");
}catch(e){
stderr("Error installing db",fn+":",e.message);
} }
stdout("Replaced DB with",fn+".");
return; return;
} }
}; };

View File

@ -290,8 +290,8 @@
return this; return this;
}, },
/* Posts a message in the form {type, data} to the db worker. Returns this. */ /* Posts a message in the form {type, data} to the db worker. Returns this. */
wMsg: function(type,data){ wMsg: function(type,data,transferables){
this.worker.postMessage({type, data}); this.worker.postMessage({type, data}, transferables || []);
return this; return this;
}, },
/** /**
@ -558,7 +558,8 @@
SF.echo("Export failed:",ev.error); SF.echo("Export failed:",ev.error);
return; return;
} }
const blob = new Blob([ev.buffer], {type:"application/x-sqlite3"}); const blob = new Blob([ev.buffer],
{type:"application/x-sqlite3"});
const a = document.createElement('a'); const a = document.createElement('a');
document.body.appendChild(a); document.body.appendChild(a);
a.href = window.URL.createObjectURL(blob); a.href = window.URL.createObjectURL(blob);
@ -593,7 +594,7 @@
SF.wMsg('open',{ SF.wMsg('open',{
filename: f.name, filename: f.name,
buffer: this.result buffer: this.result
}); }, [this.result]);
}); });
r.addEventListener('error',function(){ r.addEventListener('error',function(){
enableMutatingElements(true); enableMutatingElements(true);
@ -800,7 +801,8 @@ SELECT group_concat(rtrim(t),x'0a') as Mandelbrot FROM a;`}
'may prove interesting or useful but is not an officially', 'may prove interesting or useful but is not an officially',
'supported deliverable of the sqlite project. It is subject to', 'supported deliverable of the sqlite project. It is subject to',
'any number of changes or outright removal at any time.\n'); 'any number of changes or outright removal at any time.\n');
SF.dbExec(null); const urlParams = new URL(self.location.href).searchParams;
SF.dbExec(urlParams.get('sql') || null);
delete ForceResizeKludge.$disabled; delete ForceResizeKludge.$disabled;
ForceResizeKludge(); ForceResizeKludge();
}/*onSFLoaded()*/; }/*onSFLoaded()*/;

View File

@ -1,5 +1,5 @@
C Resolve\s"already\sconfigured"\swarnings\sfrom\sshell's\smain()\swhen\sstarting\sit\sup\sin\sfiddle\smode. C Fiddle:\smove,\srather\sthan\scopy,\sdata\sbuffers\sbetween\sthe\sthreads.\sFix\sthe\scase\sof\san\suploaded\sdb\sfailing\sto\sinstall\sbecause\sits\sfilename\sis\sthe\ssame\sas\sthe\sopened\sdb.
D 2022-09-24T10:15:08.912 D 2022-09-24T11:32:00.293
F .fossil-settings/empty-dirs dbb81e8fc0401ac46a1491ab34a7f2c7c0452f2f06b54ebb845d024ca8283ef1 F .fossil-settings/empty-dirs dbb81e8fc0401ac46a1491ab34a7f2c7c0452f2f06b54ebb845d024ca8283ef1
F .fossil-settings/ignore-glob 35175cdfcf539b2318cb04a9901442804be81cd677d8b889fcc9149c21f239ea F .fossil-settings/ignore-glob 35175cdfcf539b2318cb04a9901442804be81cd677d8b889fcc9149c21f239ea
F LICENSE.md df5091916dbb40e6e9686186587125e1b2ff51f022cc334e886c19a0e9982724 F LICENSE.md df5091916dbb40e6e9686186587125e1b2ff51f022cc334e886c19a0e9982724
@ -502,9 +502,9 @@ F ext/wasm/demo-kvvfs1.html 7d4f28873de67f51ac18c584b7d920825139866a96049a49c424
F ext/wasm/demo-kvvfs1.js e884ea35022d772c0d1dd884b40011413696438394f605c6cd4808cfb1642a4a F ext/wasm/demo-kvvfs1.js e884ea35022d772c0d1dd884b40011413696438394f605c6cd4808cfb1642a4a
F ext/wasm/fiddle.make fd56fa21bada6ecbf860686a9a789ebda7cc3d9b60835927000fcb00246ea50f F ext/wasm/fiddle.make fd56fa21bada6ecbf860686a9a789ebda7cc3d9b60835927000fcb00246ea50f
F ext/wasm/fiddle/emscripten.css 3d253a6fdb8983a2ac983855bfbdd4b6fa1ff267c28d69513dd6ef1f289ada3f F ext/wasm/fiddle/emscripten.css 3d253a6fdb8983a2ac983855bfbdd4b6fa1ff267c28d69513dd6ef1f289ada3f
F ext/wasm/fiddle/fiddle-worker.js 462dee066849c6cb1a0347e90d3c010ca8abb1640e63b3ed3813e88ae3558d64 F ext/wasm/fiddle/fiddle-worker.js d3e4d1e442a9a86cc34f8bd646059d848cf3345b5220883379268b03b3c3cdfa
F ext/wasm/fiddle/fiddle.html 5daf54e8f3d7777cbb1ca4f93affe28858dbfff25841cb4ab81d694efed28ec2 F ext/wasm/fiddle/fiddle.html 5daf54e8f3d7777cbb1ca4f93affe28858dbfff25841cb4ab81d694efed28ec2
F ext/wasm/fiddle/fiddle.js e7c6dee946818d0e6a10c89b640440fd5d93cbb9bddea490b98cf54e8bb67ae6 F ext/wasm/fiddle/fiddle.js aa44051be6e48c53fd23c829177d43f557dcc6f0998ccfcbae7c473ff405f0c6
F ext/wasm/index.html 8b4b7ea052d558262c8466f94326fb455c21049b2d1d3577ed0a5fce15101ba8 F ext/wasm/index.html 8b4b7ea052d558262c8466f94326fb455c21049b2d1d3577ed0a5fce15101ba8
F ext/wasm/jaccwabyt/jaccwabyt.js 0d7f32817456a0f3937fcfd934afeb32154ca33580ab264dab6c285e6dbbd215 F ext/wasm/jaccwabyt/jaccwabyt.js 0d7f32817456a0f3937fcfd934afeb32154ca33580ab264dab6c285e6dbbd215
F ext/wasm/jaccwabyt/jaccwabyt.md 447cc02b598f7792edaa8ae6853a7847b8178a18ed356afacbdbf312b2588106 F ext/wasm/jaccwabyt/jaccwabyt.md 447cc02b598f7792edaa8ae6853a7847b8178a18ed356afacbdbf312b2588106
@ -2026,8 +2026,8 @@ F vsixtest/vsixtest.tcl 6a9a6ab600c25a91a7acc6293828957a386a8a93
F vsixtest/vsixtest.vcxproj.data 2ed517e100c66dc455b492e1a33350c1b20fbcdc F vsixtest/vsixtest.vcxproj.data 2ed517e100c66dc455b492e1a33350c1b20fbcdc
F vsixtest/vsixtest.vcxproj.filters 37e51ffedcdb064aad6ff33b6148725226cd608e F vsixtest/vsixtest.vcxproj.filters 37e51ffedcdb064aad6ff33b6148725226cd608e
F vsixtest/vsixtest_TemporaryKey.pfx e5b1b036facdb453873e7084e1cae9102ccc67a0 F vsixtest/vsixtest_TemporaryKey.pfx e5b1b036facdb453873e7084e1cae9102ccc67a0
P ef503ced5c2ca842be9aea9ef13719a378ed3020e884032db09afee1b8eba0a1 P 114ef3552af977b272a0baddeb9a2326484b60acfc75284e43c55530f86b413f
R 8ff71a36ad5ceae355c6a421e1d0952c R 2919c32f82c004411030482647bf2402
U stephan U stephan
Z e3192f3e3789b47f0628d3187b22766d Z 37118e27ece07b1ea1fafa2d63837d11
# Remove this line to create a well-formed Fossil manifest. # Remove this line to create a well-formed Fossil manifest.

View File

@ -1 +1 @@
114ef3552af977b272a0baddeb9a2326484b60acfc75284e43c55530f86b413f bcec4f964a7b02f59be05286ff715bac654a32b19f05a743e402f4cdb207cab8