Last active
July 18, 2017 22:09
-
-
Save mikeal/70daaf34ab39db6f979b8cf36fa5ac56 to your computer and use it in GitHub Desktop.
Potentially a better way to design streaming byte storage.
This file contains hidden or 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
// Byte Storage as Content Addressable Storage | |
class ByteStorage { | |
async set (key, value) async { | |
// Send to temporary storage w/ random UUID | |
let tmpfile = randomFile() | |
let p1 = send(value, tmpfile) | |
// Also stream the value to a hasher | |
let p2 = send(value, /* streaming hashing function */ ) | |
let values = await Promise.all(p1, p2) | |
moveFile(tmpfile, values[1]) | |
return values[1] | |
} | |
async get (key) { | |
return readFileFilesystem(key) | |
} | |
} | |
// Building a higher order store w/ human readable names on top. | |
class FriendlyStore { | |
constructor () { | |
this.byteStorage = new ByteStorage() | |
this.idb = new IDB() // IDB w/ promises | |
} | |
async set (key, value) { | |
let oldkey = await this.idb.get(key) | |
let newkey = await this.byteStorage.set(value) | |
if (oldkey === null || oldkey === newkey) { | |
return await this.idb.set(key, newkey) | |
} else { | |
throw new Error('Update Conflict') | |
} | |
} | |
async get (key) { | |
let hash = await this.idb.get(key) | |
return this.byteStorage.get(hash) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
A complete implementation of a content addressable store on the filesystem is here: https://github.com/mikeal/lucass/blob/master/fs.js although it doesn't use promises :)