Last active
March 23, 2022 20:31
-
-
Save lrowe/70b28f6d0cecf833f0ec5202b3460911 to your computer and use it in GitHub Desktop.
Non-working attempt to enable shared cache in deno-sqlite. See https://github.com/dyedgreen/deno-sqlite/issues/187
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"imports": { | |
"https://deno.land/x/[email protected]/build/sqlite.js": "./sqlite.js" | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// multi.ts | |
// wait for the worker to run first. | |
const worker: Worker = await new Promise((resolve, reject) => { | |
const worker = new Worker(new URL("./multi.worker.ts", import.meta.url), { | |
type: "module", | |
}); | |
worker.addEventListener("error", reject); | |
worker.addEventListener( | |
"message", | |
(msg) => { | |
worker.removeEventListener("error", reject); | |
msg.data === "ready" ? resolve(worker) : reject(msg); | |
}, | |
{ once: true } | |
); | |
}); | |
console.log("main"); | |
import { DB } from "https://deno.land/x/[email protected]/mod.ts"; | |
const db = new DB("file:memdb1?mode=memory&cache=shared", { uri: true }); | |
db.execute(`INSERT INTO kv (key, value) VALUES ('main', 'hello')`); | |
const result = db.query(`select * from kv`); | |
console.log("main", { result }); | |
worker.terminate(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// multi.worker.ts | |
/// <reference lib="deno.worker" /> | |
console.log("worker"); | |
import { DB } from "https://deno.land/x/[email protected]/mod.ts"; | |
const db = new DB("file:memdb1?mode=memory&cache=shared", { uri: true }); | |
console.log("sqlite_version", db.query(`select sqlite_version()`)); | |
db.execute(`CREATE TABLE kv (key TEXT PRIMARY KEY, value TEXT)`); | |
db.execute(`INSERT INTO kv (key, value) VALUES ('worker', 'hello')`); | |
const result = db.query(`select * from kv`); | |
console.log("worker", { result }); | |
self.postMessage("ready"); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
% deno run --no-check --unstable --allow-read --allow-net --import-map import_map.json multi.ts | |
worker | |
sqlite_version [ [ "3.36.0" ] ] | |
worker { result: [ [ "worker", "hello" ] ] } | |
main | |
error: Uncaught (in promise) SqliteError: no such table: kv | |
throw new SqliteError(this._wasm, status); | |
^ | |
at DB.execute (https://deno.land/x/[email protected]/src/db.ts:287:13) | |
at file:///.../multi.ts:20:4 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/// <reference types="https://deno.land/x/[email protected]/build/sqlite.d.ts" /> | |
/* This file is automatically generated. Do not edit directly. */ | |
import env from "https://deno.land/x/[email protected]/build/vfs.js"; | |
const moduleOrInstance = { | |
memory: null, | |
module: null, | |
instances: [], | |
}; | |
export async function compile() { | |
moduleOrInstance.memory = new WebAssembly.Memory({ | |
initial: 10, | |
maximum: 10000, | |
shared: true, | |
}); | |
moduleOrInstance.module = await WebAssembly.compileStreaming( | |
fetch("https://deno.land/x/[email protected]/build/sqlite.wasm") | |
); | |
} | |
export function instantiate() { | |
if (moduleOrInstance.instances.length) { | |
return moduleOrInstance.instances.pop(); | |
} else { | |
const placeholder = { exports: null }; | |
const importObject = env(placeholder); | |
importObject.env.memory = moduleOrInstance.memory; | |
//console.log({ importObject }); | |
const instance = new WebAssembly.Instance( | |
moduleOrInstance.module, | |
importObject | |
); | |
placeholder.exports = instance.exports; | |
instance.exports.seed_rng(Date.now()); | |
return instance; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment