Skip to content

Commit

Permalink
initial
Browse files Browse the repository at this point in the history
  • Loading branch information
jlongster committed Jul 5, 2021
1 parent 6510646 commit 4783eb4
Show file tree
Hide file tree
Showing 13 changed files with 4,408 additions and 780 deletions.
16 changes: 16 additions & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
module.exports = {
env: {
browser: true,
node: true,
es2021: true,
jest: true
},
extends: 'eslint:recommended',
parserOptions: {
ecmaVersion: 12,
sourceType: 'module'
},
rules: {
'no-unused-vars': 'off'
}
};
2 changes: 1 addition & 1 deletion babel.config.js
Original file line number Diff line number Diff line change
@@ -1 +1 @@
module.exports = {presets: ['@babel/preset-env']}
module.exports = { plugins: ['@babel/plugin-transform-modules-commonjs'] };
20 changes: 20 additions & 0 deletions backend-indexeddb.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
export class IndexedDBBackend {
readChunks(fileName, positions, chunkSize) {
// TODO:
// * Sort positions and find contiguous chunks
// * Run a query for each contiguous chunk
// * Do all this in another syncify worker!

// return this.syncWorker.readChunks(fileName, positions, chunkSize);

return [];
}

writeChunks(writes) {
// Call out to idb (sync!)

// return this.syncWorker.writeChunks(fileName, positions, chunkSize);

return [];
}
}
80 changes: 80 additions & 0 deletions backend-memory.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
export default class MemoryBackend {
constructor(files, defaultChunkSize) {
this.files = Object.fromEntries(
Object.entries(files).map(([name, data]) => {
return [name, { data, size: data.byteLength }];
})
);
this.defaultChunkSize = defaultChunkSize;
}

getFile(fileName) {
return this.files[fileName];
}

getOrCreateFile(fileName) {
if (this.files[fileName] == null) {
this.files[fileName] = { data: new ArrayBuffer(0), size: 0 };
}
return this.files[fileName];
}

deleteFile(fileName) {
this.files[fileName] = null;
}

readMeta(fileName, defaultMeta) {
let exists = this.getFile(fileName) != null;
let file = this.getOrCreateFile(fileName);
return exists
? { size: file.data.byteLength, chunkSize: this.defaultChunkSize }
: defaultMeta;
}

writeMeta(fileName, meta) {
let file = this.getOrCreateFile(fileName);
file.size = meta.size;
}

readChunks(fileName, positions, chunkSize) {
console.log('_reading', fileName, positions);
// if (positions.length > 0) {
// console.log('reading', positions);
// }
let data = this.files[fileName].data;

return positions.map(pos => {
let buffer = new ArrayBuffer(chunkSize);

if (pos < data.byteLength) {
new Uint8Array(buffer).set(
new Uint8Array(data, pos, Math.min(chunkSize, data.byteLength - pos))
);
}

return { pos, data: buffer };
});
}

writeChunks(fileName, writes) {
console.log('_writing', fileName, writes);
// if (writes.length > 0) {
// console.log('writing', writes.map(w => w.pos));
// }
let file = this.getOrCreateFile(fileName);
let data = file.data;

for (let write of writes) {
let fullLength = write.pos + write.data.byteLength;

if (fullLength > data.byteLength) {
// Resize file
let buffer = new ArrayBuffer(fullLength);
new Uint8Array(buffer).set(new Uint8Array(data));
this.files[fileName].data = data = buffer;
}

new Uint8Array(data).set(new Uint8Array(write.data), write.pos);
}
}
}
Loading

0 comments on commit 4783eb4

Please sign in to comment.