stephan | 132a87b | 2022-09-17 15:08:22 +0000 | [diff] [blame] | 1 | /* |
| 2 | 2022-09-16 |
| 3 | |
| 4 | The author disclaims copyright to this source code. In place of a |
| 5 | legal notice, here is a blessing: |
| 6 | |
| 7 | * May you do good and not evil. |
| 8 | * May you find forgiveness for yourself and forgive others. |
| 9 | * May you share freely, never taking more than you give. |
| 10 | |
| 11 | *********************************************************************** |
| 12 | |
stephan | f861b36 | 2022-10-25 08:06:17 +0000 | [diff] [blame] | 13 | A Worker which manages asynchronous OPFS handles on behalf of a |
stephan | e72ddfd | 2022-10-14 15:52:29 +0000 | [diff] [blame] | 14 | synchronous API which controls it via a combination of Worker |
| 15 | messages, SharedArrayBuffer, and Atomics. It is the asynchronous |
| 16 | counterpart of the API defined in sqlite3-api-opfs.js. |
stephan | 132a87b | 2022-09-17 15:08:22 +0000 | [diff] [blame] | 17 | |
| 18 | Highly indebted to: |
| 19 | |
| 20 | https://github.com/rhashimoto/wa-sqlite/blob/master/src/examples/OriginPrivateFileSystemVFS.js |
| 21 | |
| 22 | for demonstrating how to use the OPFS APIs. |
stephan | 0731554 | 2022-09-17 20:50:12 +0000 | [diff] [blame] | 23 | |
| 24 | This file is to be loaded as a Worker. It does not have any direct |
| 25 | access to the sqlite3 JS/WASM bits, so any bits which it needs (most |
| 26 | notably SQLITE_xxx integer codes) have to be imported into it via an |
| 27 | initialization process. |
stephan | f861b36 | 2022-10-25 08:06:17 +0000 | [diff] [blame] | 28 | |
| 29 | This file represents an implementation detail of a larger piece of |
| 30 | code, and not a public interface. Its details may change at any time |
| 31 | and are not intended to be used by any client-level code. |
stephan | 132a87b | 2022-09-17 15:08:22 +0000 | [diff] [blame] | 32 | */ |
stephan | f861b36 | 2022-10-25 08:06:17 +0000 | [diff] [blame] | 33 | "use strict"; |
stephan | 0731554 | 2022-09-17 20:50:12 +0000 | [diff] [blame] | 34 | const toss = function(...args){throw new Error(args.join(' '))}; |
| 35 | if(self.window === self){ |
| 36 | toss("This code cannot run from the main thread.", |
| 37 | "Load it as a Worker from a separate Worker."); |
| 38 | }else if(!navigator.storage.getDirectory){ |
| 39 | toss("This API requires navigator.storage.getDirectory."); |
| 40 | } |
stephan | 9a55773 | 2022-10-04 17:06:51 +0000 | [diff] [blame] | 41 | |
stephan | 0731554 | 2022-09-17 20:50:12 +0000 | [diff] [blame] | 42 | /** |
| 43 | Will hold state copied to this object from the syncronous side of |
| 44 | this API. |
| 45 | */ |
| 46 | const state = Object.create(null); |
| 47 | /** |
| 48 | verbose: |
| 49 | |
| 50 | 0 = no logging output |
| 51 | 1 = only errors |
| 52 | 2 = warnings and errors |
| 53 | 3 = debug, warnings, and errors |
| 54 | */ |
| 55 | state.verbose = 2; |
| 56 | |
stephan | 509f405 | 2022-09-19 09:58:01 +0000 | [diff] [blame] | 57 | const loggers = { |
| 58 | 0:console.error.bind(console), |
| 59 | 1:console.warn.bind(console), |
| 60 | 2:console.log.bind(console) |
stephan | 0731554 | 2022-09-17 20:50:12 +0000 | [diff] [blame] | 61 | }; |
stephan | 509f405 | 2022-09-19 09:58:01 +0000 | [diff] [blame] | 62 | const logImpl = (level,...args)=>{ |
| 63 | if(state.verbose>level) loggers[level]("OPFS asyncer:",...args); |
stephan | 0731554 | 2022-09-17 20:50:12 +0000 | [diff] [blame] | 64 | }; |
stephan | 509f405 | 2022-09-19 09:58:01 +0000 | [diff] [blame] | 65 | const log = (...args)=>logImpl(2, ...args); |
| 66 | const warn = (...args)=>logImpl(1, ...args); |
| 67 | const error = (...args)=>logImpl(0, ...args); |
stephan | f815011 | 2022-09-19 17:09:09 +0000 | [diff] [blame] | 68 | const metrics = Object.create(null); |
stephan | aec046a | 2022-09-19 18:22:29 +0000 | [diff] [blame] | 69 | metrics.reset = ()=>{ |
| 70 | let k; |
stephan | 5e8bb0a | 2022-09-20 08:27:57 +0000 | [diff] [blame] | 71 | const r = (m)=>(m.count = m.time = m.wait = 0); |
stephan | aec046a | 2022-09-19 18:22:29 +0000 | [diff] [blame] | 72 | for(k in state.opIds){ |
| 73 | r(metrics[k] = Object.create(null)); |
| 74 | } |
stephan | b8c8d4e | 2022-09-20 13:25:39 +0000 | [diff] [blame] | 75 | let s = metrics.s11n = Object.create(null); |
| 76 | s = s.serialize = Object.create(null); |
| 77 | s.count = s.time = 0; |
| 78 | s = metrics.s11n.deserialize = Object.create(null); |
| 79 | s.count = s.time = 0; |
stephan | aec046a | 2022-09-19 18:22:29 +0000 | [diff] [blame] | 80 | }; |
| 81 | metrics.dump = ()=>{ |
| 82 | let k, n = 0, t = 0, w = 0; |
| 83 | for(k in state.opIds){ |
| 84 | const m = metrics[k]; |
| 85 | n += m.count; |
| 86 | t += m.time; |
stephan | 5e8bb0a | 2022-09-20 08:27:57 +0000 | [diff] [blame] | 87 | w += m.wait; |
stephan | aec046a | 2022-09-19 18:22:29 +0000 | [diff] [blame] | 88 | m.avgTime = (m.count && m.time) ? (m.time / m.count) : 0; |
| 89 | } |
| 90 | console.log(self.location.href, |
stephan | 5e8bb0a | 2022-09-20 08:27:57 +0000 | [diff] [blame] | 91 | "metrics for",self.location.href,":\n", |
stephan | 56fae74 | 2022-09-24 10:12:19 +0000 | [diff] [blame] | 92 | metrics, |
stephan | 5e8bb0a | 2022-09-20 08:27:57 +0000 | [diff] [blame] | 93 | "\nTotal of",n,"op(s) for",t,"ms", |
| 94 | "approx",w,"ms spent waiting on OPFS APIs."); |
stephan | 56fae74 | 2022-09-24 10:12:19 +0000 | [diff] [blame] | 95 | console.log("Serialization metrics:",metrics.s11n); |
stephan | aec046a | 2022-09-19 18:22:29 +0000 | [diff] [blame] | 96 | }; |
stephan | 0731554 | 2022-09-17 20:50:12 +0000 | [diff] [blame] | 97 | |
stephan | 0731554 | 2022-09-17 20:50:12 +0000 | [diff] [blame] | 98 | /** |
| 99 | Map of sqlite3_file pointers (integers) to metadata related to a |
| 100 | given OPFS file handles. The pointers are, in this side of the |
| 101 | interface, opaque file handle IDs provided by the synchronous |
| 102 | part of this constellation. Each value is an object with a structure |
| 103 | demonstrated in the xOpen() impl. |
| 104 | */ |
| 105 | const __openFiles = Object.create(null); |
| 106 | |
| 107 | /** |
stephan | 8200a6d | 2022-09-17 23:29:27 +0000 | [diff] [blame] | 108 | Expects an OPFS file path. It gets resolved, such that ".." |
stephan | e72ddfd | 2022-10-14 15:52:29 +0000 | [diff] [blame] | 109 | components are properly expanded, and returned. If the 2nd arg is |
| 110 | true, the result is returned as an array of path elements, else an |
| 111 | absolute path string is returned. |
stephan | 0731554 | 2022-09-17 20:50:12 +0000 | [diff] [blame] | 112 | */ |
stephan | 8200a6d | 2022-09-17 23:29:27 +0000 | [diff] [blame] | 113 | const getResolvedPath = function(filename,splitIt){ |
| 114 | const p = new URL( |
| 115 | filename, 'file://irrelevant' |
| 116 | ).pathname; |
| 117 | return splitIt ? p.split('/').filter((v)=>!!v) : p; |
stephan | 509f405 | 2022-09-19 09:58:01 +0000 | [diff] [blame] | 118 | }; |
stephan | 0731554 | 2022-09-17 20:50:12 +0000 | [diff] [blame] | 119 | |
| 120 | /** |
| 121 | Takes the absolute path to a filesystem element. Returns an array |
stephan | e72ddfd | 2022-10-14 15:52:29 +0000 | [diff] [blame] | 122 | of [handleOfContainingDir, filename]. If the 2nd argument is truthy |
| 123 | then each directory element leading to the file is created along |
| 124 | the way. Throws if any creation or resolution fails. |
stephan | 0731554 | 2022-09-17 20:50:12 +0000 | [diff] [blame] | 125 | */ |
stephan | 56fae74 | 2022-09-24 10:12:19 +0000 | [diff] [blame] | 126 | const getDirForFilename = async function f(absFilename, createDirs = false){ |
stephan | 8200a6d | 2022-09-17 23:29:27 +0000 | [diff] [blame] | 127 | const path = getResolvedPath(absFilename, true); |
stephan | 0731554 | 2022-09-17 20:50:12 +0000 | [diff] [blame] | 128 | const filename = path.pop(); |
stephan | 8200a6d | 2022-09-17 23:29:27 +0000 | [diff] [blame] | 129 | let dh = state.rootDir; |
| 130 | for(const dirName of path){ |
| 131 | if(dirName){ |
| 132 | dh = await dh.getDirectoryHandle(dirName, {create: !!createDirs}); |
stephan | 0731554 | 2022-09-17 20:50:12 +0000 | [diff] [blame] | 133 | } |
stephan | 132a87b | 2022-09-17 15:08:22 +0000 | [diff] [blame] | 134 | } |
stephan | 0731554 | 2022-09-17 20:50:12 +0000 | [diff] [blame] | 135 | return [dh, filename]; |
| 136 | }; |
stephan | 132a87b | 2022-09-17 15:08:22 +0000 | [diff] [blame] | 137 | |
stephan | 7ff8da8 | 2022-10-03 09:21:37 +0000 | [diff] [blame] | 138 | /** |
stephan | d001060 | 2022-10-30 11:22:45 +0000 | [diff] [blame^] | 139 | An error class specifically for use with getSyncHandle(), the goal |
| 140 | of which is to eventually be able to distinguish unambiguously |
| 141 | between locking-related failures and other types, noting that we |
| 142 | cannot currently do so because createSyncAccessHandle() does not |
| 143 | define its exceptions in the required level of detail. |
| 144 | */ |
| 145 | class GetSyncHandleError extends Error { |
| 146 | constructor(errorObject, ...msg){ |
| 147 | super(); |
| 148 | this.error = errorObject; |
| 149 | this.message = [ |
| 150 | ...msg, ': Original exception ['+errorObject.name+']:', |
| 151 | errorObject.message |
| 152 | ].join(' '); |
| 153 | this.name = 'GetSyncHandleError'; |
| 154 | } |
| 155 | }; |
| 156 | |
| 157 | /** |
stephan | 7ff8da8 | 2022-10-03 09:21:37 +0000 | [diff] [blame] | 158 | Returns the sync access handle associated with the given file |
stephan | f861b36 | 2022-10-25 08:06:17 +0000 | [diff] [blame] | 159 | handle object (which must be a valid handle object, as created by |
| 160 | xOpen()), lazily opening it if needed. |
stephan | c7fb48d | 2022-10-04 09:12:05 +0000 | [diff] [blame] | 161 | |
| 162 | In order to help alleviate cross-tab contention for a dabase, |
| 163 | if an exception is thrown while acquiring the handle, this routine |
| 164 | will wait briefly and try again, up to 3 times. If acquisition |
| 165 | still fails at that point it will give up and propagate the |
| 166 | exception. |
stephan | 7ff8da8 | 2022-10-03 09:21:37 +0000 | [diff] [blame] | 167 | */ |
stephan | 5f0b67c | 2022-10-03 11:33:35 +0000 | [diff] [blame] | 168 | const getSyncHandle = async (fh)=>{ |
| 169 | if(!fh.syncHandle){ |
stephan | c7fb48d | 2022-10-04 09:12:05 +0000 | [diff] [blame] | 170 | const t = performance.now(); |
| 171 | log("Acquiring sync handle for",fh.filenameAbs); |
stephan | 9163ef1 | 2022-10-30 10:24:53 +0000 | [diff] [blame] | 172 | const maxTries = 4, msBase = 300; |
| 173 | let i = 1, ms = msBase; |
| 174 | for(; true; ms = msBase * ++i){ |
stephan | c7fb48d | 2022-10-04 09:12:05 +0000 | [diff] [blame] | 175 | try { |
stephan | d001060 | 2022-10-30 11:22:45 +0000 | [diff] [blame^] | 176 | //if(1 || i<2) toss("Just testing getSyncHandle() exception handling."); |
stephan | c7fb48d | 2022-10-04 09:12:05 +0000 | [diff] [blame] | 177 | //TODO? A config option which tells it to throw here |
stephan | 3e771c0 | 2022-10-14 13:26:18 +0000 | [diff] [blame] | 178 | //randomly every now and then, for testing purposes. |
stephan | c7fb48d | 2022-10-04 09:12:05 +0000 | [diff] [blame] | 179 | fh.syncHandle = await fh.fileHandle.createSyncAccessHandle(); |
| 180 | break; |
| 181 | }catch(e){ |
| 182 | if(i === maxTries){ |
stephan | d001060 | 2022-10-30 11:22:45 +0000 | [diff] [blame^] | 183 | throw new GetSyncHandleError( |
| 184 | e, "Error getting sync handle.",maxTries, |
| 185 | "attempts failed.",fh.filenameAbs |
| 186 | ); |
stephan | c7fb48d | 2022-10-04 09:12:05 +0000 | [diff] [blame] | 187 | } |
stephan | 3e771c0 | 2022-10-14 13:26:18 +0000 | [diff] [blame] | 188 | warn("Error getting sync handle. Waiting",ms, |
stephan | c7fb48d | 2022-10-04 09:12:05 +0000 | [diff] [blame] | 189 | "ms and trying again.",fh.filenameAbs,e); |
stephan | f861b36 | 2022-10-25 08:06:17 +0000 | [diff] [blame] | 190 | Atomics.wait(state.sabOPView, state.opIds.retry, 0, ms); |
stephan | c7fb48d | 2022-10-04 09:12:05 +0000 | [diff] [blame] | 191 | } |
| 192 | } |
| 193 | log("Got sync handle for",fh.filenameAbs,'in',performance.now() - t,'ms'); |
stephan | 5f0b67c | 2022-10-03 11:33:35 +0000 | [diff] [blame] | 194 | } |
stephan | 5f0b67c | 2022-10-03 11:33:35 +0000 | [diff] [blame] | 195 | return fh.syncHandle; |
| 196 | }; |
| 197 | |
stephan | e72ddfd | 2022-10-14 15:52:29 +0000 | [diff] [blame] | 198 | /** |
| 199 | If the given file-holding object has a sync handle attached to it, |
| 200 | that handle is remove and asynchronously closed. Though it may |
| 201 | sound sensible to continue work as soon as the close() returns |
| 202 | (noting that it's asynchronous), doing so can cause operations |
| 203 | performed soon afterwards, e.g. a call to getSyncHandle() to fail |
| 204 | because they may happen out of order from the close(). OPFS does |
| 205 | not guaranty that the actual order of operations is retained in |
| 206 | such cases. i.e. always "await" on the result of this function. |
| 207 | */ |
stephan | 5f0b67c | 2022-10-03 11:33:35 +0000 | [diff] [blame] | 208 | const closeSyncHandle = async (fh)=>{ |
| 209 | if(fh.syncHandle){ |
stephan | 9a55773 | 2022-10-04 17:06:51 +0000 | [diff] [blame] | 210 | log("Closing sync handle for",fh.filenameAbs); |
stephan | 5f0b67c | 2022-10-03 11:33:35 +0000 | [diff] [blame] | 211 | const h = fh.syncHandle; |
| 212 | delete fh.syncHandle; |
| 213 | return h.close(); |
| 214 | } |
| 215 | }; |
stephan | 132a87b | 2022-09-17 15:08:22 +0000 | [diff] [blame] | 216 | |
stephan | 0731554 | 2022-09-17 20:50:12 +0000 | [diff] [blame] | 217 | /** |
stephan | 72ab400 | 2022-09-21 12:27:35 +0000 | [diff] [blame] | 218 | Stores the given value at state.sabOPView[state.opIds.rc] and then |
| 219 | Atomics.notify()'s it. |
stephan | 0731554 | 2022-09-17 20:50:12 +0000 | [diff] [blame] | 220 | */ |
| 221 | const storeAndNotify = (opName, value)=>{ |
stephan | d001060 | 2022-10-30 11:22:45 +0000 | [diff] [blame^] | 222 | log(opName+"() => notify( rc =",value,")"); |
stephan | c9e2602 | 2022-09-20 10:11:52 +0000 | [diff] [blame] | 223 | Atomics.store(state.sabOPView, state.opIds.rc, value); |
| 224 | Atomics.notify(state.sabOPView, state.opIds.rc); |
stephan | 0731554 | 2022-09-17 20:50:12 +0000 | [diff] [blame] | 225 | }; |
stephan | 132a87b | 2022-09-17 15:08:22 +0000 | [diff] [blame] | 226 | |
stephan | 0731554 | 2022-09-17 20:50:12 +0000 | [diff] [blame] | 227 | /** |
| 228 | Throws if fh is a file-holding object which is flagged as read-only. |
| 229 | */ |
| 230 | const affirmNotRO = function(opName,fh){ |
| 231 | if(fh.readOnly) toss(opName+"(): File is read-only: "+fh.filenameAbs); |
| 232 | }; |
| 233 | |
stephan | e72ddfd | 2022-10-14 15:52:29 +0000 | [diff] [blame] | 234 | /** |
| 235 | We track 2 different timers: the "metrics" timer records how much |
| 236 | time we spend performing work. The "wait" timer records how much |
| 237 | time we spend waiting on the underlying OPFS timer. See the calls |
| 238 | to mTimeStart(), mTimeEnd(), wTimeStart(), and wTimeEnd() |
| 239 | throughout this file to see how they're used. |
| 240 | */ |
| 241 | const __mTimer = Object.create(null); |
| 242 | __mTimer.op = undefined; |
| 243 | __mTimer.start = undefined; |
stephan | aec046a | 2022-09-19 18:22:29 +0000 | [diff] [blame] | 244 | const mTimeStart = (op)=>{ |
stephan | e72ddfd | 2022-10-14 15:52:29 +0000 | [diff] [blame] | 245 | __mTimer.start = performance.now(); |
| 246 | __mTimer.op = op; |
stephan | aec046a | 2022-09-19 18:22:29 +0000 | [diff] [blame] | 247 | //metrics[op] || toss("Maintenance required: missing metrics for",op); |
| 248 | ++metrics[op].count; |
| 249 | }; |
| 250 | const mTimeEnd = ()=>( |
stephan | e72ddfd | 2022-10-14 15:52:29 +0000 | [diff] [blame] | 251 | metrics[__mTimer.op].time += performance.now() - __mTimer.start |
stephan | aec046a | 2022-09-19 18:22:29 +0000 | [diff] [blame] | 252 | ); |
stephan | e72ddfd | 2022-10-14 15:52:29 +0000 | [diff] [blame] | 253 | const __wTimer = Object.create(null); |
| 254 | __wTimer.op = undefined; |
| 255 | __wTimer.start = undefined; |
stephan | 5e8bb0a | 2022-09-20 08:27:57 +0000 | [diff] [blame] | 256 | const wTimeStart = (op)=>{ |
stephan | e72ddfd | 2022-10-14 15:52:29 +0000 | [diff] [blame] | 257 | __wTimer.start = performance.now(); |
| 258 | __wTimer.op = op; |
stephan | 5e8bb0a | 2022-09-20 08:27:57 +0000 | [diff] [blame] | 259 | //metrics[op] || toss("Maintenance required: missing metrics for",op); |
| 260 | }; |
| 261 | const wTimeEnd = ()=>( |
stephan | e72ddfd | 2022-10-14 15:52:29 +0000 | [diff] [blame] | 262 | metrics[__wTimer.op].wait += performance.now() - __wTimer.start |
stephan | 5e8bb0a | 2022-09-20 08:27:57 +0000 | [diff] [blame] | 263 | ); |
stephan | aec046a | 2022-09-19 18:22:29 +0000 | [diff] [blame] | 264 | |
stephan | 0731554 | 2022-09-17 20:50:12 +0000 | [diff] [blame] | 265 | /** |
stephan | e72ddfd | 2022-10-14 15:52:29 +0000 | [diff] [blame] | 266 | Gets set to true by the 'opfs-async-shutdown' command to quit the |
| 267 | wait loop. This is only intended for debugging purposes: we cannot |
| 268 | inspect this file's state while the tight waitLoop() is running and |
| 269 | need a way to stop that loop for introspection purposes. |
stephan | 3c272ba | 2022-10-04 00:54:00 +0000 | [diff] [blame] | 270 | */ |
| 271 | let flagAsyncShutdown = false; |
| 272 | |
stephan | 9a55773 | 2022-10-04 17:06:51 +0000 | [diff] [blame] | 273 | |
stephan | 3c272ba | 2022-10-04 00:54:00 +0000 | [diff] [blame] | 274 | /** |
stephan | 0731554 | 2022-09-17 20:50:12 +0000 | [diff] [blame] | 275 | Asynchronous wrappers for sqlite3_vfs and sqlite3_io_methods |
stephan | e72ddfd | 2022-10-14 15:52:29 +0000 | [diff] [blame] | 276 | methods, as well as helpers like mkdir(). Maintenance reminder: |
| 277 | members are in alphabetical order to simplify finding them. |
stephan | 0731554 | 2022-09-17 20:50:12 +0000 | [diff] [blame] | 278 | */ |
| 279 | const vfsAsyncImpls = { |
stephan | 3c272ba | 2022-10-04 00:54:00 +0000 | [diff] [blame] | 280 | 'opfs-async-metrics': async ()=>{ |
| 281 | mTimeStart('opfs-async-metrics'); |
stephan | 56fae74 | 2022-09-24 10:12:19 +0000 | [diff] [blame] | 282 | metrics.dump(); |
stephan | 3c272ba | 2022-10-04 00:54:00 +0000 | [diff] [blame] | 283 | storeAndNotify('opfs-async-metrics', 0); |
stephan | 56fae74 | 2022-09-24 10:12:19 +0000 | [diff] [blame] | 284 | mTimeEnd(); |
| 285 | }, |
stephan | 3c272ba | 2022-10-04 00:54:00 +0000 | [diff] [blame] | 286 | 'opfs-async-shutdown': async ()=>{ |
| 287 | flagAsyncShutdown = true; |
| 288 | storeAndNotify('opfs-async-shutdown', 0); |
| 289 | }, |
stephan | 56fae74 | 2022-09-24 10:12:19 +0000 | [diff] [blame] | 290 | mkdir: async (dirname)=>{ |
stephan | 5e8bb0a | 2022-09-20 08:27:57 +0000 | [diff] [blame] | 291 | mTimeStart('mkdir'); |
stephan | aec046a | 2022-09-19 18:22:29 +0000 | [diff] [blame] | 292 | let rc = 0; |
stephan | 5e8bb0a | 2022-09-20 08:27:57 +0000 | [diff] [blame] | 293 | wTimeStart('mkdir'); |
stephan | aec046a | 2022-09-19 18:22:29 +0000 | [diff] [blame] | 294 | try { |
stephan | 56fae74 | 2022-09-24 10:12:19 +0000 | [diff] [blame] | 295 | await getDirForFilename(dirname+"/filepart", true); |
stephan | aec046a | 2022-09-19 18:22:29 +0000 | [diff] [blame] | 296 | }catch(e){ |
stephan | 56fae74 | 2022-09-24 10:12:19 +0000 | [diff] [blame] | 297 | state.s11n.storeException(2,e); |
stephan | aec046a | 2022-09-19 18:22:29 +0000 | [diff] [blame] | 298 | rc = state.sq3Codes.SQLITE_IOERR; |
stephan | 72ab400 | 2022-09-21 12:27:35 +0000 | [diff] [blame] | 299 | }finally{ |
| 300 | wTimeEnd(); |
stephan | aec046a | 2022-09-19 18:22:29 +0000 | [diff] [blame] | 301 | } |
| 302 | storeAndNotify('mkdir', rc); |
stephan | 5e8bb0a | 2022-09-20 08:27:57 +0000 | [diff] [blame] | 303 | mTimeEnd(); |
stephan | aec046a | 2022-09-19 18:22:29 +0000 | [diff] [blame] | 304 | }, |
stephan | 56fae74 | 2022-09-24 10:12:19 +0000 | [diff] [blame] | 305 | xAccess: async (filename)=>{ |
stephan | aec046a | 2022-09-19 18:22:29 +0000 | [diff] [blame] | 306 | mTimeStart('xAccess'); |
stephan | 8200a6d | 2022-09-17 23:29:27 +0000 | [diff] [blame] | 307 | /* OPFS cannot support the full range of xAccess() queries sqlite3 |
| 308 | calls for. We can essentially just tell if the file is |
| 309 | accessible, but if it is it's automatically writable (unless |
| 310 | it's locked, which we cannot(?) know without trying to open |
| 311 | it). OPFS does not have the notion of read-only. |
| 312 | |
| 313 | The return semantics of this function differ from sqlite3's |
| 314 | xAccess semantics because we are limited in what we can |
| 315 | communicate back to our synchronous communication partner: 0 = |
| 316 | accessible, non-0 means not accessible. |
| 317 | */ |
| 318 | let rc = 0; |
stephan | 5e8bb0a | 2022-09-20 08:27:57 +0000 | [diff] [blame] | 319 | wTimeStart('xAccess'); |
stephan | 8200a6d | 2022-09-17 23:29:27 +0000 | [diff] [blame] | 320 | try{ |
stephan | 56fae74 | 2022-09-24 10:12:19 +0000 | [diff] [blame] | 321 | const [dh, fn] = await getDirForFilename(filename); |
stephan | 8200a6d | 2022-09-17 23:29:27 +0000 | [diff] [blame] | 322 | await dh.getFileHandle(fn); |
| 323 | }catch(e){ |
stephan | 56fae74 | 2022-09-24 10:12:19 +0000 | [diff] [blame] | 324 | state.s11n.storeException(2,e); |
stephan | 8200a6d | 2022-09-17 23:29:27 +0000 | [diff] [blame] | 325 | rc = state.sq3Codes.SQLITE_IOERR; |
stephan | 72ab400 | 2022-09-21 12:27:35 +0000 | [diff] [blame] | 326 | }finally{ |
| 327 | wTimeEnd(); |
stephan | 8200a6d | 2022-09-17 23:29:27 +0000 | [diff] [blame] | 328 | } |
stephan | 0731554 | 2022-09-17 20:50:12 +0000 | [diff] [blame] | 329 | storeAndNotify('xAccess', rc); |
stephan | aec046a | 2022-09-19 18:22:29 +0000 | [diff] [blame] | 330 | mTimeEnd(); |
stephan | 0731554 | 2022-09-17 20:50:12 +0000 | [diff] [blame] | 331 | }, |
stephan | f861b36 | 2022-10-25 08:06:17 +0000 | [diff] [blame] | 332 | xClose: async function(fid/*sqlite3_file pointer*/){ |
stephan | 0731554 | 2022-09-17 20:50:12 +0000 | [diff] [blame] | 333 | const opName = 'xClose'; |
stephan | aec046a | 2022-09-19 18:22:29 +0000 | [diff] [blame] | 334 | mTimeStart(opName); |
stephan | 0731554 | 2022-09-17 20:50:12 +0000 | [diff] [blame] | 335 | const fh = __openFiles[fid]; |
stephan | 5e8bb0a | 2022-09-20 08:27:57 +0000 | [diff] [blame] | 336 | let rc = 0; |
| 337 | wTimeStart('xClose'); |
stephan | 0731554 | 2022-09-17 20:50:12 +0000 | [diff] [blame] | 338 | if(fh){ |
| 339 | delete __openFiles[fid]; |
stephan | 5f0b67c | 2022-10-03 11:33:35 +0000 | [diff] [blame] | 340 | await closeSyncHandle(fh); |
stephan | 0731554 | 2022-09-17 20:50:12 +0000 | [diff] [blame] | 341 | if(fh.deleteOnClose){ |
| 342 | try{ await fh.dirHandle.removeEntry(fh.filenamePart) } |
| 343 | catch(e){ warn("Ignoring dirHandle.removeEntry() failure of",fh,e) } |
stephan | 132a87b | 2022-09-17 15:08:22 +0000 | [diff] [blame] | 344 | } |
stephan | 0731554 | 2022-09-17 20:50:12 +0000 | [diff] [blame] | 345 | }else{ |
stephan | 72ab400 | 2022-09-21 12:27:35 +0000 | [diff] [blame] | 346 | state.s11n.serialize(); |
stephan | 5e8bb0a | 2022-09-20 08:27:57 +0000 | [diff] [blame] | 347 | rc = state.sq3Codes.SQLITE_NOTFOUND; |
stephan | 132a87b | 2022-09-17 15:08:22 +0000 | [diff] [blame] | 348 | } |
stephan | 5e8bb0a | 2022-09-20 08:27:57 +0000 | [diff] [blame] | 349 | wTimeEnd(); |
| 350 | storeAndNotify(opName, rc); |
stephan | aec046a | 2022-09-19 18:22:29 +0000 | [diff] [blame] | 351 | mTimeEnd(); |
stephan | 0731554 | 2022-09-17 20:50:12 +0000 | [diff] [blame] | 352 | }, |
stephan | c4b87be | 2022-09-20 01:28:47 +0000 | [diff] [blame] | 353 | xDelete: async function(...args){ |
| 354 | mTimeStart('xDelete'); |
| 355 | const rc = await vfsAsyncImpls.xDeleteNoWait(...args); |
| 356 | storeAndNotify('xDelete', rc); |
| 357 | mTimeEnd(); |
| 358 | }, |
stephan | 138647a | 2022-09-20 03:31:02 +0000 | [diff] [blame] | 359 | xDeleteNoWait: async function(filename, syncDir = 0, recursive = false){ |
stephan | 8200a6d | 2022-09-17 23:29:27 +0000 | [diff] [blame] | 360 | /* The syncDir flag is, for purposes of the VFS API's semantics, |
| 361 | ignored here. However, if it has the value 0x1234 then: after |
| 362 | deleting the given file, recursively try to delete any empty |
| 363 | directories left behind in its wake (ignoring any errors and |
| 364 | stopping at the first failure). |
| 365 | |
| 366 | That said: we don't know for sure that removeEntry() fails if |
| 367 | the dir is not empty because the API is not documented. It has, |
| 368 | however, a "recursive" flag which defaults to false, so |
| 369 | presumably it will fail if the dir is not empty and that flag |
| 370 | is false. |
| 371 | */ |
stephan | f386012 | 2022-09-18 17:32:35 +0000 | [diff] [blame] | 372 | let rc = 0; |
stephan | 5e8bb0a | 2022-09-20 08:27:57 +0000 | [diff] [blame] | 373 | wTimeStart('xDelete'); |
stephan | 0731554 | 2022-09-17 20:50:12 +0000 | [diff] [blame] | 374 | try { |
stephan | 8200a6d | 2022-09-17 23:29:27 +0000 | [diff] [blame] | 375 | while(filename){ |
stephan | 56fae74 | 2022-09-24 10:12:19 +0000 | [diff] [blame] | 376 | const [hDir, filenamePart] = await getDirForFilename(filename, false); |
stephan | 8200a6d | 2022-09-17 23:29:27 +0000 | [diff] [blame] | 377 | if(!filenamePart) break; |
stephan | f386012 | 2022-09-18 17:32:35 +0000 | [diff] [blame] | 378 | await hDir.removeEntry(filenamePart, {recursive}); |
stephan | 8200a6d | 2022-09-17 23:29:27 +0000 | [diff] [blame] | 379 | if(0x1234 !== syncDir) break; |
| 380 | filename = getResolvedPath(filename, true); |
| 381 | filename.pop(); |
| 382 | filename = filename.join('/'); |
| 383 | } |
stephan | 0731554 | 2022-09-17 20:50:12 +0000 | [diff] [blame] | 384 | }catch(e){ |
stephan | 56fae74 | 2022-09-24 10:12:19 +0000 | [diff] [blame] | 385 | state.s11n.storeException(2,e); |
stephan | f386012 | 2022-09-18 17:32:35 +0000 | [diff] [blame] | 386 | rc = state.sq3Codes.SQLITE_IOERR_DELETE; |
stephan | 132a87b | 2022-09-17 15:08:22 +0000 | [diff] [blame] | 387 | } |
stephan | 5e8bb0a | 2022-09-20 08:27:57 +0000 | [diff] [blame] | 388 | wTimeEnd(); |
stephan | f386012 | 2022-09-18 17:32:35 +0000 | [diff] [blame] | 389 | return rc; |
| 390 | }, |
stephan | f861b36 | 2022-10-25 08:06:17 +0000 | [diff] [blame] | 391 | xFileSize: async function(fid/*sqlite3_file pointer*/){ |
stephan | aec046a | 2022-09-19 18:22:29 +0000 | [diff] [blame] | 392 | mTimeStart('xFileSize'); |
stephan | 0731554 | 2022-09-17 20:50:12 +0000 | [diff] [blame] | 393 | const fh = __openFiles[fid]; |
stephan | d001060 | 2022-10-30 11:22:45 +0000 | [diff] [blame^] | 394 | let rc; |
stephan | 5e8bb0a | 2022-09-20 08:27:57 +0000 | [diff] [blame] | 395 | wTimeStart('xFileSize'); |
stephan | 0731554 | 2022-09-17 20:50:12 +0000 | [diff] [blame] | 396 | try{ |
stephan | d001060 | 2022-10-30 11:22:45 +0000 | [diff] [blame^] | 397 | rc = await (await getSyncHandle(fh)).getSize(); |
| 398 | state.s11n.serialize(Number(rc)); |
| 399 | rc = 0; |
stephan | 0731554 | 2022-09-17 20:50:12 +0000 | [diff] [blame] | 400 | }catch(e){ |
stephan | 56fae74 | 2022-09-24 10:12:19 +0000 | [diff] [blame] | 401 | state.s11n.storeException(2,e); |
stephan | d001060 | 2022-10-30 11:22:45 +0000 | [diff] [blame^] | 402 | rc = (e instanceof GetSyncHandleError) |
| 403 | ? state.sq3Codes.SQLITE_IOERR_LOCK |
| 404 | : state.sq3Codes.SQLITE_IOERR; |
stephan | 132a87b | 2022-09-17 15:08:22 +0000 | [diff] [blame] | 405 | } |
stephan | 5e8bb0a | 2022-09-20 08:27:57 +0000 | [diff] [blame] | 406 | wTimeEnd(); |
stephan | d001060 | 2022-10-30 11:22:45 +0000 | [diff] [blame^] | 407 | storeAndNotify('xFileSize', rc); |
stephan | aec046a | 2022-09-19 18:22:29 +0000 | [diff] [blame] | 408 | mTimeEnd(); |
stephan | 0731554 | 2022-09-17 20:50:12 +0000 | [diff] [blame] | 409 | }, |
stephan | f861b36 | 2022-10-25 08:06:17 +0000 | [diff] [blame] | 410 | xLock: async function(fid/*sqlite3_file pointer*/, |
| 411 | lockType/*SQLITE_LOCK_...*/){ |
stephan | 9a55773 | 2022-10-04 17:06:51 +0000 | [diff] [blame] | 412 | mTimeStart('xLock'); |
| 413 | const fh = __openFiles[fid]; |
| 414 | let rc = 0; |
| 415 | if( !fh.syncHandle ){ |
stephan | e72ddfd | 2022-10-14 15:52:29 +0000 | [diff] [blame] | 416 | wTimeStart('xLock'); |
stephan | 9a55773 | 2022-10-04 17:06:51 +0000 | [diff] [blame] | 417 | try { await getSyncHandle(fh) } |
| 418 | catch(e){ |
| 419 | state.s11n.storeException(1,e); |
stephan | d001060 | 2022-10-30 11:22:45 +0000 | [diff] [blame^] | 420 | rc = (e instanceof GetSyncHandleError) |
| 421 | ? state.sq3Codes.SQLITE_IOERR_LOCK |
| 422 | : state.sq3Codes.SQLITE_IOERR; |
stephan | 9a55773 | 2022-10-04 17:06:51 +0000 | [diff] [blame] | 423 | } |
stephan | e72ddfd | 2022-10-14 15:52:29 +0000 | [diff] [blame] | 424 | wTimeEnd(); |
stephan | 9a55773 | 2022-10-04 17:06:51 +0000 | [diff] [blame] | 425 | } |
| 426 | storeAndNotify('xLock',rc); |
| 427 | mTimeEnd(); |
| 428 | }, |
stephan | f861b36 | 2022-10-25 08:06:17 +0000 | [diff] [blame] | 429 | xOpen: async function(fid/*sqlite3_file pointer*/, filename, |
| 430 | flags/*SQLITE_OPEN_...*/){ |
stephan | 0731554 | 2022-09-17 20:50:12 +0000 | [diff] [blame] | 431 | const opName = 'xOpen'; |
stephan | aec046a | 2022-09-19 18:22:29 +0000 | [diff] [blame] | 432 | mTimeStart(opName); |
stephan | c4b87be | 2022-09-20 01:28:47 +0000 | [diff] [blame] | 433 | const deleteOnClose = (state.sq3Codes.SQLITE_OPEN_DELETEONCLOSE & flags); |
| 434 | const create = (state.sq3Codes.SQLITE_OPEN_CREATE & flags); |
stephan | 5e8bb0a | 2022-09-20 08:27:57 +0000 | [diff] [blame] | 435 | wTimeStart('xOpen'); |
stephan | 0731554 | 2022-09-17 20:50:12 +0000 | [diff] [blame] | 436 | try{ |
stephan | 0731554 | 2022-09-17 20:50:12 +0000 | [diff] [blame] | 437 | let hDir, filenamePart; |
| 438 | try { |
stephan | 56fae74 | 2022-09-24 10:12:19 +0000 | [diff] [blame] | 439 | [hDir, filenamePart] = await getDirForFilename(filename, !!create); |
stephan | 132a87b | 2022-09-17 15:08:22 +0000 | [diff] [blame] | 440 | }catch(e){ |
stephan | 0731554 | 2022-09-17 20:50:12 +0000 | [diff] [blame] | 441 | storeAndNotify(opName, state.sql3Codes.SQLITE_NOTFOUND); |
stephan | aec046a | 2022-09-19 18:22:29 +0000 | [diff] [blame] | 442 | mTimeEnd(); |
stephan | 5e8bb0a | 2022-09-20 08:27:57 +0000 | [diff] [blame] | 443 | wTimeEnd(); |
stephan | 0731554 | 2022-09-17 20:50:12 +0000 | [diff] [blame] | 444 | return; |
stephan | 132a87b | 2022-09-17 15:08:22 +0000 | [diff] [blame] | 445 | } |
stephan | c4b87be | 2022-09-20 01:28:47 +0000 | [diff] [blame] | 446 | const hFile = await hDir.getFileHandle(filenamePart, {create}); |
stephan | 0731554 | 2022-09-17 20:50:12 +0000 | [diff] [blame] | 447 | /** |
| 448 | wa-sqlite, at this point, grabs a SyncAccessHandle and |
stephan | 5f0b67c | 2022-10-03 11:33:35 +0000 | [diff] [blame] | 449 | assigns it to the syncHandle prop of the file state |
stephan | 0731554 | 2022-09-17 20:50:12 +0000 | [diff] [blame] | 450 | object, but only for certain cases and it's unclear why it |
| 451 | places that limitation on it. |
| 452 | */ |
stephan | 5e8bb0a | 2022-09-20 08:27:57 +0000 | [diff] [blame] | 453 | wTimeEnd(); |
stephan | 5f0b67c | 2022-10-03 11:33:35 +0000 | [diff] [blame] | 454 | __openFiles[fid] = Object.assign(Object.create(null),{ |
stephan | 7ff8da8 | 2022-10-03 09:21:37 +0000 | [diff] [blame] | 455 | filenameAbs: filename, |
| 456 | filenamePart: filenamePart, |
| 457 | dirHandle: hDir, |
| 458 | fileHandle: hFile, |
| 459 | sabView: state.sabFileBufView, |
| 460 | readOnly: create |
| 461 | ? false : (state.sq3Codes.SQLITE_OPEN_READONLY & flags), |
| 462 | deleteOnClose: deleteOnClose |
| 463 | }); |
stephan | 0731554 | 2022-09-17 20:50:12 +0000 | [diff] [blame] | 464 | storeAndNotify(opName, 0); |
| 465 | }catch(e){ |
stephan | 5e8bb0a | 2022-09-20 08:27:57 +0000 | [diff] [blame] | 466 | wTimeEnd(); |
stephan | 0731554 | 2022-09-17 20:50:12 +0000 | [diff] [blame] | 467 | error(opName,e); |
stephan | 56fae74 | 2022-09-24 10:12:19 +0000 | [diff] [blame] | 468 | state.s11n.storeException(1,e); |
stephan | 0731554 | 2022-09-17 20:50:12 +0000 | [diff] [blame] | 469 | storeAndNotify(opName, state.sq3Codes.SQLITE_IOERR); |
| 470 | } |
stephan | aec046a | 2022-09-19 18:22:29 +0000 | [diff] [blame] | 471 | mTimeEnd(); |
stephan | 0731554 | 2022-09-17 20:50:12 +0000 | [diff] [blame] | 472 | }, |
stephan | f861b36 | 2022-10-25 08:06:17 +0000 | [diff] [blame] | 473 | xRead: async function(fid/*sqlite3_file pointer*/,n,offset64){ |
stephan | aec046a | 2022-09-19 18:22:29 +0000 | [diff] [blame] | 474 | mTimeStart('xRead'); |
stephan | e72ddfd | 2022-10-14 15:52:29 +0000 | [diff] [blame] | 475 | let rc = 0, nRead; |
stephan | 5f0b67c | 2022-10-03 11:33:35 +0000 | [diff] [blame] | 476 | const fh = __openFiles[fid]; |
stephan | 0731554 | 2022-09-17 20:50:12 +0000 | [diff] [blame] | 477 | try{ |
stephan | 5e8bb0a | 2022-09-20 08:27:57 +0000 | [diff] [blame] | 478 | wTimeStart('xRead'); |
stephan | e72ddfd | 2022-10-14 15:52:29 +0000 | [diff] [blame] | 479 | nRead = (await getSyncHandle(fh)).read( |
stephan | aec046a | 2022-09-19 18:22:29 +0000 | [diff] [blame] | 480 | fh.sabView.subarray(0, n), |
stephan | f861b36 | 2022-10-25 08:06:17 +0000 | [diff] [blame] | 481 | {at: Number(offset64)} |
stephan | aec046a | 2022-09-19 18:22:29 +0000 | [diff] [blame] | 482 | ); |
stephan | 5e8bb0a | 2022-09-20 08:27:57 +0000 | [diff] [blame] | 483 | wTimeEnd(); |
stephan | 0731554 | 2022-09-17 20:50:12 +0000 | [diff] [blame] | 484 | if(nRead < n){/* Zero-fill remaining bytes */ |
stephan | aec046a | 2022-09-19 18:22:29 +0000 | [diff] [blame] | 485 | fh.sabView.fill(0, nRead, n); |
stephan | 0731554 | 2022-09-17 20:50:12 +0000 | [diff] [blame] | 486 | rc = state.sq3Codes.SQLITE_IOERR_SHORT_READ; |
| 487 | } |
| 488 | }catch(e){ |
stephan | e72ddfd | 2022-10-14 15:52:29 +0000 | [diff] [blame] | 489 | if(undefined===nRead) wTimeEnd(); |
stephan | 0731554 | 2022-09-17 20:50:12 +0000 | [diff] [blame] | 490 | error("xRead() failed",e,fh); |
stephan | 56fae74 | 2022-09-24 10:12:19 +0000 | [diff] [blame] | 491 | state.s11n.storeException(1,e); |
stephan | d001060 | 2022-10-30 11:22:45 +0000 | [diff] [blame^] | 492 | rc = (e instanceof GetSyncHandleError) |
| 493 | ? state.sq3Codes.SQLITE_IOERR_LOCK |
| 494 | : state.sq3Codes.SQLITE_IOERR_READ; |
stephan | 0731554 | 2022-09-17 20:50:12 +0000 | [diff] [blame] | 495 | } |
| 496 | storeAndNotify('xRead',rc); |
stephan | aec046a | 2022-09-19 18:22:29 +0000 | [diff] [blame] | 497 | mTimeEnd(); |
stephan | 0731554 | 2022-09-17 20:50:12 +0000 | [diff] [blame] | 498 | }, |
stephan | f861b36 | 2022-10-25 08:06:17 +0000 | [diff] [blame] | 499 | xSync: async function(fid/*sqlite3_file pointer*/,flags/*ignored*/){ |
stephan | aec046a | 2022-09-19 18:22:29 +0000 | [diff] [blame] | 500 | mTimeStart('xSync'); |
stephan | 0731554 | 2022-09-17 20:50:12 +0000 | [diff] [blame] | 501 | const fh = __openFiles[fid]; |
stephan | 72ab400 | 2022-09-21 12:27:35 +0000 | [diff] [blame] | 502 | let rc = 0; |
stephan | 5f0b67c | 2022-10-03 11:33:35 +0000 | [diff] [blame] | 503 | if(!fh.readOnly && fh.syncHandle){ |
stephan | 72ab400 | 2022-09-21 12:27:35 +0000 | [diff] [blame] | 504 | try { |
| 505 | wTimeStart('xSync'); |
stephan | 5f0b67c | 2022-10-03 11:33:35 +0000 | [diff] [blame] | 506 | await fh.syncHandle.flush(); |
stephan | 72ab400 | 2022-09-21 12:27:35 +0000 | [diff] [blame] | 507 | }catch(e){ |
stephan | 56fae74 | 2022-09-24 10:12:19 +0000 | [diff] [blame] | 508 | state.s11n.storeException(2,e); |
stephan | d001060 | 2022-10-30 11:22:45 +0000 | [diff] [blame^] | 509 | rc = state.sq3Codes.SQLITE_IOERR_FSYNC; |
stephan | 72ab400 | 2022-09-21 12:27:35 +0000 | [diff] [blame] | 510 | } |
stephan | e72ddfd | 2022-10-14 15:52:29 +0000 | [diff] [blame] | 511 | wTimeEnd(); |
stephan | 5e8bb0a | 2022-09-20 08:27:57 +0000 | [diff] [blame] | 512 | } |
stephan | 72ab400 | 2022-09-21 12:27:35 +0000 | [diff] [blame] | 513 | storeAndNotify('xSync',rc); |
stephan | aec046a | 2022-09-19 18:22:29 +0000 | [diff] [blame] | 514 | mTimeEnd(); |
stephan | 0731554 | 2022-09-17 20:50:12 +0000 | [diff] [blame] | 515 | }, |
stephan | f861b36 | 2022-10-25 08:06:17 +0000 | [diff] [blame] | 516 | xTruncate: async function(fid/*sqlite3_file pointer*/,size){ |
stephan | aec046a | 2022-09-19 18:22:29 +0000 | [diff] [blame] | 517 | mTimeStart('xTruncate'); |
stephan | 0731554 | 2022-09-17 20:50:12 +0000 | [diff] [blame] | 518 | let rc = 0; |
| 519 | const fh = __openFiles[fid]; |
stephan | 5e8bb0a | 2022-09-20 08:27:57 +0000 | [diff] [blame] | 520 | wTimeStart('xTruncate'); |
stephan | 0731554 | 2022-09-17 20:50:12 +0000 | [diff] [blame] | 521 | try{ |
| 522 | affirmNotRO('xTruncate', fh); |
stephan | 7ff8da8 | 2022-10-03 09:21:37 +0000 | [diff] [blame] | 523 | await (await getSyncHandle(fh)).truncate(size); |
stephan | 0731554 | 2022-09-17 20:50:12 +0000 | [diff] [blame] | 524 | }catch(e){ |
| 525 | error("xTruncate():",e,fh); |
stephan | 56fae74 | 2022-09-24 10:12:19 +0000 | [diff] [blame] | 526 | state.s11n.storeException(2,e); |
stephan | d001060 | 2022-10-30 11:22:45 +0000 | [diff] [blame^] | 527 | rc = (e instanceof GetSyncHandleError) |
| 528 | ? state.sq3Codes.SQLITE_IOERR_LOCK |
| 529 | : state.sq3Codes.SQLITE_IOERR_TRUNCATE; |
stephan | 0731554 | 2022-09-17 20:50:12 +0000 | [diff] [blame] | 530 | } |
stephan | 5e8bb0a | 2022-09-20 08:27:57 +0000 | [diff] [blame] | 531 | wTimeEnd(); |
stephan | 0731554 | 2022-09-17 20:50:12 +0000 | [diff] [blame] | 532 | storeAndNotify('xTruncate',rc); |
stephan | aec046a | 2022-09-19 18:22:29 +0000 | [diff] [blame] | 533 | mTimeEnd(); |
stephan | 0731554 | 2022-09-17 20:50:12 +0000 | [diff] [blame] | 534 | }, |
stephan | f861b36 | 2022-10-25 08:06:17 +0000 | [diff] [blame] | 535 | xUnlock: async function(fid/*sqlite3_file pointer*/, |
| 536 | lockType/*SQLITE_LOCK_...*/){ |
stephan | 9a55773 | 2022-10-04 17:06:51 +0000 | [diff] [blame] | 537 | mTimeStart('xUnlock'); |
| 538 | let rc = 0; |
| 539 | const fh = __openFiles[fid]; |
| 540 | if( state.sq3Codes.SQLITE_LOCK_NONE===lockType |
| 541 | && fh.syncHandle ){ |
stephan | e72ddfd | 2022-10-14 15:52:29 +0000 | [diff] [blame] | 542 | wTimeStart('xUnlock'); |
stephan | 9a55773 | 2022-10-04 17:06:51 +0000 | [diff] [blame] | 543 | try { await closeSyncHandle(fh) } |
| 544 | catch(e){ |
| 545 | state.s11n.storeException(1,e); |
stephan | d001060 | 2022-10-30 11:22:45 +0000 | [diff] [blame^] | 546 | rc = state.sq3Codes.SQLITE_IOERR_UNLOCK; |
stephan | 9a55773 | 2022-10-04 17:06:51 +0000 | [diff] [blame] | 547 | } |
stephan | e72ddfd | 2022-10-14 15:52:29 +0000 | [diff] [blame] | 548 | wTimeEnd(); |
stephan | 9a55773 | 2022-10-04 17:06:51 +0000 | [diff] [blame] | 549 | } |
| 550 | storeAndNotify('xUnlock',rc); |
| 551 | mTimeEnd(); |
| 552 | }, |
stephan | f861b36 | 2022-10-25 08:06:17 +0000 | [diff] [blame] | 553 | xWrite: async function(fid/*sqlite3_file pointer*/,n,offset64){ |
stephan | aec046a | 2022-09-19 18:22:29 +0000 | [diff] [blame] | 554 | mTimeStart('xWrite'); |
stephan | 0731554 | 2022-09-17 20:50:12 +0000 | [diff] [blame] | 555 | let rc; |
stephan | 5e8bb0a | 2022-09-20 08:27:57 +0000 | [diff] [blame] | 556 | wTimeStart('xWrite'); |
stephan | 0731554 | 2022-09-17 20:50:12 +0000 | [diff] [blame] | 557 | try{ |
stephan | aec046a | 2022-09-19 18:22:29 +0000 | [diff] [blame] | 558 | const fh = __openFiles[fid]; |
stephan | 0731554 | 2022-09-17 20:50:12 +0000 | [diff] [blame] | 559 | affirmNotRO('xWrite', fh); |
stephan | aec046a | 2022-09-19 18:22:29 +0000 | [diff] [blame] | 560 | rc = ( |
stephan | 7ff8da8 | 2022-10-03 09:21:37 +0000 | [diff] [blame] | 561 | n === (await getSyncHandle(fh)) |
| 562 | .write(fh.sabView.subarray(0, n), |
stephan | f861b36 | 2022-10-25 08:06:17 +0000 | [diff] [blame] | 563 | {at: Number(offset64)}) |
stephan | aec046a | 2022-09-19 18:22:29 +0000 | [diff] [blame] | 564 | ) ? 0 : state.sq3Codes.SQLITE_IOERR_WRITE; |
stephan | 0731554 | 2022-09-17 20:50:12 +0000 | [diff] [blame] | 565 | }catch(e){ |
| 566 | error("xWrite():",e,fh); |
stephan | 56fae74 | 2022-09-24 10:12:19 +0000 | [diff] [blame] | 567 | state.s11n.storeException(1,e); |
stephan | d001060 | 2022-10-30 11:22:45 +0000 | [diff] [blame^] | 568 | rc = (e instanceof GetSyncHandleError) |
| 569 | ? state.sq3Codes.SQLITE_IOERR_LOCK |
| 570 | : state.sq3Codes.SQLITE_IOERR_WRITE; |
stephan | 0731554 | 2022-09-17 20:50:12 +0000 | [diff] [blame] | 571 | } |
stephan | e72ddfd | 2022-10-14 15:52:29 +0000 | [diff] [blame] | 572 | wTimeEnd(); |
stephan | 0731554 | 2022-09-17 20:50:12 +0000 | [diff] [blame] | 573 | storeAndNotify('xWrite',rc); |
stephan | aec046a | 2022-09-19 18:22:29 +0000 | [diff] [blame] | 574 | mTimeEnd(); |
stephan | 0731554 | 2022-09-17 20:50:12 +0000 | [diff] [blame] | 575 | } |
stephan | 9a55773 | 2022-10-04 17:06:51 +0000 | [diff] [blame] | 576 | }/*vfsAsyncImpls*/; |
stephan | 0731554 | 2022-09-17 20:50:12 +0000 | [diff] [blame] | 577 | |
stephan | 138647a | 2022-09-20 03:31:02 +0000 | [diff] [blame] | 578 | const initS11n = ()=>{ |
stephan | b8c8d4e | 2022-09-20 13:25:39 +0000 | [diff] [blame] | 579 | /** |
stephan | 72ab400 | 2022-09-21 12:27:35 +0000 | [diff] [blame] | 580 | ACHTUNG: this code is 100% duplicated in the other half of this |
| 581 | proxy! The documentation is maintained in the "synchronous half". |
stephan | b8c8d4e | 2022-09-20 13:25:39 +0000 | [diff] [blame] | 582 | */ |
stephan | 138647a | 2022-09-20 03:31:02 +0000 | [diff] [blame] | 583 | if(state.s11n) return state.s11n; |
stephan | b8c8d4e | 2022-09-20 13:25:39 +0000 | [diff] [blame] | 584 | const textDecoder = new TextDecoder(), |
| 585 | textEncoder = new TextEncoder('utf-8'), |
| 586 | viewU8 = new Uint8Array(state.sabIO, state.sabS11nOffset, state.sabS11nSize), |
| 587 | viewDV = new DataView(state.sabIO, state.sabS11nOffset, state.sabS11nSize); |
stephan | 138647a | 2022-09-20 03:31:02 +0000 | [diff] [blame] | 588 | state.s11n = Object.create(null); |
stephan | b8c8d4e | 2022-09-20 13:25:39 +0000 | [diff] [blame] | 589 | const TypeIds = Object.create(null); |
| 590 | TypeIds.number = { id: 1, size: 8, getter: 'getFloat64', setter: 'setFloat64' }; |
| 591 | TypeIds.bigint = { id: 2, size: 8, getter: 'getBigInt64', setter: 'setBigInt64' }; |
| 592 | TypeIds.boolean = { id: 3, size: 4, getter: 'getInt32', setter: 'setInt32' }; |
| 593 | TypeIds.string = { id: 4 }; |
stephan | 72ab400 | 2022-09-21 12:27:35 +0000 | [diff] [blame] | 594 | const getTypeId = (v)=>( |
| 595 | TypeIds[typeof v] |
| 596 | || toss("Maintenance required: this value type cannot be serialized.",v) |
| 597 | ); |
stephan | b8c8d4e | 2022-09-20 13:25:39 +0000 | [diff] [blame] | 598 | const getTypeIdById = (tid)=>{ |
| 599 | switch(tid){ |
stephan | 72ab400 | 2022-09-21 12:27:35 +0000 | [diff] [blame] | 600 | case TypeIds.number.id: return TypeIds.number; |
| 601 | case TypeIds.bigint.id: return TypeIds.bigint; |
| 602 | case TypeIds.boolean.id: return TypeIds.boolean; |
| 603 | case TypeIds.string.id: return TypeIds.string; |
| 604 | default: toss("Invalid type ID:",tid); |
stephan | b8c8d4e | 2022-09-20 13:25:39 +0000 | [diff] [blame] | 605 | } |
| 606 | }; |
stephan | 138647a | 2022-09-20 03:31:02 +0000 | [diff] [blame] | 607 | state.s11n.deserialize = function(){ |
stephan | b8c8d4e | 2022-09-20 13:25:39 +0000 | [diff] [blame] | 608 | ++metrics.s11n.deserialize.count; |
| 609 | const t = performance.now(); |
stephan | b8c8d4e | 2022-09-20 13:25:39 +0000 | [diff] [blame] | 610 | const argc = viewU8[0]; |
stephan | 72ab400 | 2022-09-21 12:27:35 +0000 | [diff] [blame] | 611 | const rc = argc ? [] : null; |
stephan | b8c8d4e | 2022-09-20 13:25:39 +0000 | [diff] [blame] | 612 | if(argc){ |
stephan | 72ab400 | 2022-09-21 12:27:35 +0000 | [diff] [blame] | 613 | const typeIds = []; |
| 614 | let offset = 1, i, n, v; |
stephan | b8c8d4e | 2022-09-20 13:25:39 +0000 | [diff] [blame] | 615 | for(i = 0; i < argc; ++i, ++offset){ |
| 616 | typeIds.push(getTypeIdById(viewU8[offset])); |
| 617 | } |
| 618 | for(i = 0; i < argc; ++i){ |
| 619 | const t = typeIds[i]; |
| 620 | if(t.getter){ |
| 621 | v = viewDV[t.getter](offset, state.littleEndian); |
| 622 | offset += t.size; |
stephan | 72ab400 | 2022-09-21 12:27:35 +0000 | [diff] [blame] | 623 | }else{/*String*/ |
stephan | b8c8d4e | 2022-09-20 13:25:39 +0000 | [diff] [blame] | 624 | n = viewDV.getInt32(offset, state.littleEndian); |
| 625 | offset += 4; |
| 626 | v = textDecoder.decode(viewU8.slice(offset, offset+n)); |
| 627 | offset += n; |
| 628 | } |
| 629 | rc.push(v); |
| 630 | } |
| 631 | } |
| 632 | //log("deserialize:",argc, rc); |
| 633 | metrics.s11n.deserialize.time += performance.now() - t; |
| 634 | return rc; |
| 635 | }; |
stephan | 138647a | 2022-09-20 03:31:02 +0000 | [diff] [blame] | 636 | state.s11n.serialize = function(...args){ |
stephan | b8c8d4e | 2022-09-20 13:25:39 +0000 | [diff] [blame] | 637 | const t = performance.now(); |
stephan | 72ab400 | 2022-09-21 12:27:35 +0000 | [diff] [blame] | 638 | ++metrics.s11n.serialize.count; |
stephan | 5e8bb0a | 2022-09-20 08:27:57 +0000 | [diff] [blame] | 639 | if(args.length){ |
stephan | b8c8d4e | 2022-09-20 13:25:39 +0000 | [diff] [blame] | 640 | //log("serialize():",args); |
stephan | 72ab400 | 2022-09-21 12:27:35 +0000 | [diff] [blame] | 641 | const typeIds = []; |
| 642 | let i = 0, offset = 1; |
| 643 | viewU8[0] = args.length & 0xff /* header = # of args */; |
stephan | b8c8d4e | 2022-09-20 13:25:39 +0000 | [diff] [blame] | 644 | for(; i < args.length; ++i, ++offset){ |
stephan | 72ab400 | 2022-09-21 12:27:35 +0000 | [diff] [blame] | 645 | /* Write the TypeIds.id value into the next args.length |
| 646 | bytes. */ |
stephan | b8c8d4e | 2022-09-20 13:25:39 +0000 | [diff] [blame] | 647 | typeIds.push(getTypeId(args[i])); |
| 648 | viewU8[offset] = typeIds[i].id; |
| 649 | } |
| 650 | for(i = 0; i < args.length; ++i) { |
stephan | 72ab400 | 2022-09-21 12:27:35 +0000 | [diff] [blame] | 651 | /* Deserialize the following bytes based on their |
| 652 | corresponding TypeIds.id from the header. */ |
stephan | b8c8d4e | 2022-09-20 13:25:39 +0000 | [diff] [blame] | 653 | const t = typeIds[i]; |
| 654 | if(t.setter){ |
| 655 | viewDV[t.setter](offset, args[i], state.littleEndian); |
| 656 | offset += t.size; |
stephan | 72ab400 | 2022-09-21 12:27:35 +0000 | [diff] [blame] | 657 | }else{/*String*/ |
stephan | b8c8d4e | 2022-09-20 13:25:39 +0000 | [diff] [blame] | 658 | const s = textEncoder.encode(args[i]); |
| 659 | viewDV.setInt32(offset, s.byteLength, state.littleEndian); |
| 660 | offset += 4; |
| 661 | viewU8.set(s, offset); |
| 662 | offset += s.byteLength; |
| 663 | } |
| 664 | } |
| 665 | //log("serialize() result:",viewU8.slice(0,offset)); |
stephan | 5e8bb0a | 2022-09-20 08:27:57 +0000 | [diff] [blame] | 666 | }else{ |
stephan | b8c8d4e | 2022-09-20 13:25:39 +0000 | [diff] [blame] | 667 | viewU8[0] = 0; |
stephan | 5e8bb0a | 2022-09-20 08:27:57 +0000 | [diff] [blame] | 668 | } |
stephan | b8c8d4e | 2022-09-20 13:25:39 +0000 | [diff] [blame] | 669 | metrics.s11n.serialize.time += performance.now() - t; |
stephan | 138647a | 2022-09-20 03:31:02 +0000 | [diff] [blame] | 670 | }; |
stephan | e8afca3 | 2022-09-21 14:02:47 +0000 | [diff] [blame] | 671 | |
| 672 | state.s11n.storeException = state.asyncS11nExceptions |
stephan | 56fae74 | 2022-09-24 10:12:19 +0000 | [diff] [blame] | 673 | ? ((priority,e)=>{ |
| 674 | if(priority<=state.asyncS11nExceptions){ |
stephan | d001060 | 2022-10-30 11:22:45 +0000 | [diff] [blame^] | 675 | state.s11n.serialize([e.name,': ',e.message].join('')); |
stephan | 56fae74 | 2022-09-24 10:12:19 +0000 | [diff] [blame] | 676 | } |
| 677 | }) |
stephan | e8afca3 | 2022-09-21 14:02:47 +0000 | [diff] [blame] | 678 | : ()=>{}; |
| 679 | |
stephan | 138647a | 2022-09-20 03:31:02 +0000 | [diff] [blame] | 680 | return state.s11n; |
stephan | b8c8d4e | 2022-09-20 13:25:39 +0000 | [diff] [blame] | 681 | }/*initS11n()*/; |
stephan | 138647a | 2022-09-20 03:31:02 +0000 | [diff] [blame] | 682 | |
stephan | 5e8bb0a | 2022-09-20 08:27:57 +0000 | [diff] [blame] | 683 | const waitLoop = async function f(){ |
stephan | 138647a | 2022-09-20 03:31:02 +0000 | [diff] [blame] | 684 | const opHandlers = Object.create(null); |
stephan | c9e2602 | 2022-09-20 10:11:52 +0000 | [diff] [blame] | 685 | for(let k of Object.keys(state.opIds)){ |
| 686 | const vi = vfsAsyncImpls[k]; |
| 687 | if(!vi) continue; |
stephan | 138647a | 2022-09-20 03:31:02 +0000 | [diff] [blame] | 688 | const o = Object.create(null); |
| 689 | opHandlers[state.opIds[k]] = o; |
| 690 | o.key = k; |
stephan | e72ddfd | 2022-10-14 15:52:29 +0000 | [diff] [blame] | 691 | o.f = vi; |
stephan | 138647a | 2022-09-20 03:31:02 +0000 | [diff] [blame] | 692 | } |
stephan | 5f0b67c | 2022-10-03 11:33:35 +0000 | [diff] [blame] | 693 | /** |
| 694 | waitTime is how long (ms) to wait for each Atomics.wait(). |
stephan | c7fb48d | 2022-10-04 09:12:05 +0000 | [diff] [blame] | 695 | We need to wake up periodically to give the thread a chance |
stephan | 5f0b67c | 2022-10-03 11:33:35 +0000 | [diff] [blame] | 696 | to do other things. |
| 697 | */ |
stephan | 9a55773 | 2022-10-04 17:06:51 +0000 | [diff] [blame] | 698 | const waitTime = 1000; |
stephan | 3c272ba | 2022-10-04 00:54:00 +0000 | [diff] [blame] | 699 | while(!flagAsyncShutdown){ |
stephan | 138647a | 2022-09-20 03:31:02 +0000 | [diff] [blame] | 700 | try { |
stephan | 5f0b67c | 2022-10-03 11:33:35 +0000 | [diff] [blame] | 701 | if('timed-out'===Atomics.wait( |
| 702 | state.sabOPView, state.opIds.whichOp, 0, waitTime |
| 703 | )){ |
stephan | 5e8bb0a | 2022-09-20 08:27:57 +0000 | [diff] [blame] | 704 | continue; |
| 705 | } |
| 706 | const opId = Atomics.load(state.sabOPView, state.opIds.whichOp); |
| 707 | Atomics.store(state.sabOPView, state.opIds.whichOp, 0); |
stephan | 138647a | 2022-09-20 03:31:02 +0000 | [diff] [blame] | 708 | const hnd = opHandlers[opId] ?? toss("No waitLoop handler for whichOp #",opId); |
stephan | 56fae74 | 2022-09-24 10:12:19 +0000 | [diff] [blame] | 709 | const args = state.s11n.deserialize() || []; |
stephan | 5f0b67c | 2022-10-03 11:33:35 +0000 | [diff] [blame] | 710 | state.s11n.serialize(/* clear s11n to keep the caller from |
| 711 | confusing this with an exception string |
| 712 | written by the upcoming operation */); |
stephan | 5e8bb0a | 2022-09-20 08:27:57 +0000 | [diff] [blame] | 713 | //warn("waitLoop() whichOp =",opId, hnd, args); |
| 714 | if(hnd.f) await hnd.f(...args); |
| 715 | else error("Missing callback for opId",opId); |
stephan | 138647a | 2022-09-20 03:31:02 +0000 | [diff] [blame] | 716 | }catch(e){ |
stephan | 5f0b67c | 2022-10-03 11:33:35 +0000 | [diff] [blame] | 717 | error('in waitLoop():',e); |
stephan | 138647a | 2022-09-20 03:31:02 +0000 | [diff] [blame] | 718 | } |
stephan | e72ddfd | 2022-10-14 15:52:29 +0000 | [diff] [blame] | 719 | } |
stephan | 138647a | 2022-09-20 03:31:02 +0000 | [diff] [blame] | 720 | }; |
| 721 | |
stephan | 0731554 | 2022-09-17 20:50:12 +0000 | [diff] [blame] | 722 | navigator.storage.getDirectory().then(function(d){ |
| 723 | const wMsg = (type)=>postMessage({type}); |
| 724 | state.rootDir = d; |
stephan | 5e8bb0a | 2022-09-20 08:27:57 +0000 | [diff] [blame] | 725 | self.onmessage = function({data}){ |
stephan | 0731554 | 2022-09-17 20:50:12 +0000 | [diff] [blame] | 726 | switch(data.type){ |
stephan | 138647a | 2022-09-20 03:31:02 +0000 | [diff] [blame] | 727 | case 'opfs-async-init':{ |
stephan | 0731554 | 2022-09-17 20:50:12 +0000 | [diff] [blame] | 728 | /* Receive shared state from synchronous partner */ |
stephan | 138647a | 2022-09-20 03:31:02 +0000 | [diff] [blame] | 729 | const opt = data.args; |
| 730 | state.littleEndian = opt.littleEndian; |
stephan | e8afca3 | 2022-09-21 14:02:47 +0000 | [diff] [blame] | 731 | state.asyncS11nExceptions = opt.asyncS11nExceptions; |
stephan | 0731554 | 2022-09-17 20:50:12 +0000 | [diff] [blame] | 732 | state.verbose = opt.verbose ?? 2; |
| 733 | state.fileBufferSize = opt.fileBufferSize; |
stephan | 138647a | 2022-09-20 03:31:02 +0000 | [diff] [blame] | 734 | state.sabS11nOffset = opt.sabS11nOffset; |
| 735 | state.sabS11nSize = opt.sabS11nSize; |
stephan | c4b87be | 2022-09-20 01:28:47 +0000 | [diff] [blame] | 736 | state.sabOP = opt.sabOP; |
| 737 | state.sabOPView = new Int32Array(state.sabOP); |
| 738 | state.sabIO = opt.sabIO; |
| 739 | state.sabFileBufView = new Uint8Array(state.sabIO, 0, state.fileBufferSize); |
stephan | 138647a | 2022-09-20 03:31:02 +0000 | [diff] [blame] | 740 | state.sabS11nView = new Uint8Array(state.sabIO, state.sabS11nOffset, state.sabS11nSize); |
stephan | 0731554 | 2022-09-17 20:50:12 +0000 | [diff] [blame] | 741 | state.opIds = opt.opIds; |
| 742 | state.sq3Codes = opt.sq3Codes; |
| 743 | Object.keys(vfsAsyncImpls).forEach((k)=>{ |
| 744 | if(!Number.isFinite(state.opIds[k])){ |
| 745 | toss("Maintenance required: missing state.opIds[",k,"]"); |
| 746 | } |
| 747 | }); |
stephan | 138647a | 2022-09-20 03:31:02 +0000 | [diff] [blame] | 748 | initS11n(); |
stephan | aec046a | 2022-09-19 18:22:29 +0000 | [diff] [blame] | 749 | metrics.reset(); |
stephan | 0731554 | 2022-09-17 20:50:12 +0000 | [diff] [blame] | 750 | log("init state",state); |
stephan | 138647a | 2022-09-20 03:31:02 +0000 | [diff] [blame] | 751 | wMsg('opfs-async-inited'); |
stephan | 5e8bb0a | 2022-09-20 08:27:57 +0000 | [diff] [blame] | 752 | waitLoop(); |
stephan | 0731554 | 2022-09-17 20:50:12 +0000 | [diff] [blame] | 753 | break; |
| 754 | } |
stephan | 3c272ba | 2022-10-04 00:54:00 +0000 | [diff] [blame] | 755 | case 'opfs-async-restart': |
| 756 | if(flagAsyncShutdown){ |
| 757 | warn("Restarting after opfs-async-shutdown. Might or might not work."); |
| 758 | flagAsyncShutdown = false; |
| 759 | waitLoop(); |
| 760 | } |
| 761 | break; |
| 762 | case 'opfs-async-metrics': |
| 763 | metrics.dump(); |
| 764 | break; |
stephan | 132a87b | 2022-09-17 15:08:22 +0000 | [diff] [blame] | 765 | } |
| 766 | }; |
stephan | 138647a | 2022-09-20 03:31:02 +0000 | [diff] [blame] | 767 | wMsg('opfs-async-loaded'); |
stephan | 9a55773 | 2022-10-04 17:06:51 +0000 | [diff] [blame] | 768 | }).catch((e)=>error("error initializing OPFS asyncer:",e)); |