Skip to content

Instantly share code, notes, and snippets.

@mikeal
Last active Jul 18, 2017
Embed
What would you like to do?
Potentially a better way to design streaming byte storage.
// 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)
}
}
@mikeal
Copy link
Author

mikeal commented Jul 18, 2017

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 :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment