Skip to content

Commit

Permalink
snap
Browse files Browse the repository at this point in the history
  • Loading branch information
jlongster committed Jul 19, 2021
1 parent c2a98ba commit 1325cdb
Show file tree
Hide file tree
Showing 11 changed files with 564 additions and 498 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
"serve-demo": "cd src/demo && ../../node_modules/.bin/webpack serve "
},
"dependencies": {
"detect-browser": "^5.2.0",
"sql.js": "file:/Users/james/projects/sql.js",
"uuid": "^8.3.2"
},
Expand Down
53 changes: 35 additions & 18 deletions src/backend-indexeddb.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,23 @@ function positionToKey(pos, blockSize) {
}

function invokeWorker(method, args) {
// console.log('invoking', method, args);
switch (method) {
case 'stats-start': {
writer.string('stats-start');
writer.finalize();
reader.int32();
reader.done();
break;
}

case 'stats': {
writer.string('stats');
writer.finalize();
reader.int32();
reader.done();
break;
}

case 'readBlocks': {
let { name, positions, blockSize } = args;

Expand All @@ -29,7 +44,12 @@ function invokeWorker(method, args) {

let data = reader.bytes();
reader.done();
res.push({ pos, data });
res.push({
pos,
// If th length is 0, the block didn't exist. We return a
// blank block in that case
data: data.byteLength === 0 ? new ArrayBuffer(blockSize) : data
});
}

return res;
Expand Down Expand Up @@ -89,6 +109,7 @@ function invokeWorker(method, args) {
case 'lockFile': {
writer.string('lockFile');
writer.string(args.name);
writer.int32(args.lockType);
writer.finalize();

let res = reader.int32();
Expand All @@ -99,11 +120,12 @@ function invokeWorker(method, args) {
case 'unlockFile': {
writer.string('unlockFile');
writer.string(args.name);
writer.int32(args.lockType);
writer.finalize();

let res = reader.int32();
reader.done();
return res;
return res === 0;
}
}
}
Expand All @@ -114,29 +136,24 @@ class FileOps {
}

startStats() {
this.stats = {
read: 0,
write: 0
};
return invokeWorker('stats-start');
}

endStats() {
let stats = this.stats;
this.stats = {};
return stats;
stats() {
return invokeWorker('stats');
}

getStoreName() {
// TODO: better sanitization
return this.filename.replace(/\//g, '-');
}

lock() {
return invokeWorker('lockFile', { name: this.getStoreName() });
lock(lockType) {
return invokeWorker('lockFile', { name: this.getStoreName(), lockType });
}

unlock() {
return invokeWorker('unlockFile', { name: this.getStoreName() });
unlock(lockType) {
return invokeWorker('unlockFile', { name: this.getStoreName(), lockType });
}

delete() {
Expand All @@ -152,9 +169,9 @@ class FileOps {
}

readBlocks(positions, blockSize) {
if (Math.random() < 0.005) {
console.log('reading', positions);
}
// if (Math.random() < 0.005) {
// console.log('reading', positions);
// }

if (this.stats) {
this.stats.read += positions.length;
Expand Down
29 changes: 13 additions & 16 deletions src/blockedfs.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
const SQLITE_NOTFOUND = 12;

const ERRNO_CODES = {
EPERM: 63,
ENOENT: 44,
Expand Down Expand Up @@ -166,12 +164,7 @@ export default class BlockedFS {
},
mknod: (parent, name, mode, dev) => {
if (name.endsWith('.lock')) {
let file = this.FS.lookupNode(parent, name.replace(/\.lock$/, ''));

if (!file.contents.lock()) {
// File exists (can't lock)
throw new this.FS.ErrnoError(20);
}
throw new Error('Locking via lockfiles is not supported')
}

return this.createNode(parent, name, mode, dev);
Expand All @@ -183,12 +176,6 @@ export default class BlockedFS {
let node = this.FS.lookupNode(parent, name);
node.contents.delete(name);
},
rmdir: (parent, name) => {
if (name.endsWith('.lock')) {
let file = this.FS.lookupNode(parent, name.replace(/\.lock$/, ''));
file.contents.unlock();
}
},
readdir: node => {
// We could list all the available databases here if `node` is
// the root directory. However Firefox does not implemented
Expand Down Expand Up @@ -220,7 +207,7 @@ export default class BlockedFS {
},

read: (stream, buffer, offset, length, position) => {
// console.log('read', offset, length, position);
console.log('read', offset, length, position);
return stream.node.contents.read(buffer, offset, length, position);
},

Expand Down Expand Up @@ -267,6 +254,17 @@ export default class BlockedFS {
return this.createNode(null, '/', 16384 /* dir */ | 511 /* 0777 */, 0);
}

lock(path, lockType) {
console.log('locking', path, lockType)
let { node } = this.FS.lookupPath(path)
return node.contents.lock(lockType)
}

unlock(path, lockType) {
let { node } = this.FS.lookupPath(path)
return node.contents.unlock(lockType)
}

// TODO: implement lookup for existing files (maybe)

createNode(parent, name, mode, dev) {
Expand All @@ -282,7 +280,6 @@ export default class BlockedFS {
lookup: this.node_ops.lookup,
unlink: this.node_ops.unlink,
setattr: this.node_ops.setattr,
rmdir: this.node_ops.rmdir
};
node.stream_ops = {};
node.contents = {};
Expand Down
Loading

0 comments on commit 1325cdb

Please sign in to comment.