Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
const Corestore = require('corestore')
const ram = require('random-access-memory')
const raf = require('random-access-file')
const store1 = new Corestore(ram)
const store2 = new Corestore(ram)
//const store2 = new Corestore(path => raf('store2/' + path))
//const store3 = new Corestore('my-storage-dir')
const MountableHypertrie = require('mountable-hypertrie')
store1.ready(readyStores)
function readyStores(){
console.log("Ready")
const masterTrie = new MountableHypertrie(store1)
const slaveTrie1 = new MountableHypertrie(store1.namespace('slaveTrie1'))
masterTrie.ready(()=>{
console.log('Master key: ', masterTrie.key.toString('hex'))
masterTrie.put('address', '123 Main St', () => {
masterTrie.get('address', (err, node) => {
console.log('Got key: ', node.key)
console.log('Loaded value from trie: ', node.value.toString())
})
})
})
slaveTrie1.ready(()=>{
console.log('Slave key: ', slaveTrie1.key.toString('hex'))
slaveTrie1.mount('/a/', masterTrie.key, ()=>{
// check
slaveTrie1.get('/a/address', (err, node) => {
console.log('Got key: ', node.key)
console.log('Loaded value from trie: ', node.value.toString())
//now change master and check again
masterTrie.put('address', '456 Fake Ave', () => {
slaveTrie1.get('/a/address', (err, node) => {
console.log('Got key: ', node.key)
console.log('Loaded value from trie: ', node.value.toString())
/* It's a too way street, not exactly a slave */
slaveTrie1.put('/a/address', '69 Slave St', () => {
masterTrie.get('address', (err, node) => {
if(err) console.log(err)
console.log('Got key: ', node.key)
console.log('Loaded value from trie: ', node.value.toString())
})
})
/* */
})
})
})
})
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment