Created
July 17, 2018 16:55
-
-
Save lachenmayer/8af38f3b137b68a79ffe9820b9965b72 to your computer and use it in GitHub Desktop.
This file contains 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
// Stores a hypercore feed & a hypertrie inside a hypertrie. | |
// The feed is stored under the prefix "feed/" | |
// The trie is stored under the prefix "key-value/" | |
const hypercore = require('hypercore') | |
const hypertrie = require('hypertrie') | |
const rakv = require('random-access-key-value') | |
const ram = require('random-access-memory') | |
function hypertrieStorage(hypertrie, prefix) { | |
const db = { | |
get: function(key, cb) { | |
return hypertrie.get(key, (err, node) => { | |
if (err) return cb(err) | |
return cb(null, node ? node.value : null) | |
}) | |
}, | |
put: hypertrie.put.bind(hypertrie), | |
batch: hypertrie.batch.bind(hypertrie), | |
} | |
return file => rakv(db, prefix + '/' + file) | |
} | |
const db = hypertrie(ram) | |
const feed = hypercore(hypertrieStorage(db, 'feed'), { valueEncoding: 'utf8' }) | |
feed.append('gimme a feed', err => { | |
// WORKS :) | |
feed.get(0, (err, value) => console.log(err, value)) | |
}) | |
const kv = hypertrie(hypertrieStorage(db, 'key-value')) | |
kv.put('gimme a key', 'and a value', err => { | |
// DOES NOT WORK :( | |
kv.get('gimme a key', (err, node) => console.log(err, node)) | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment