Instantly share code, notes, and snippets.

Embed
What would you like to do?
var hyperdb = require('hyperdb')
var hypercore = require('hypercore')
var hyperdiscovery = require('hyperdiscovery')
var pages = require('random-access-page-files')
var raf = require('random-access-file')
var ram = require('random-access-memory')
var st = process.argv.indexOf('--ram') > -1 ? ram : storage
var optimized = process.argv.indexOf('--optimized') > -1
var db = hyperdb([
hypercore(st, '48e2619899edb24f4d5031b5e0cf16e6caef0cc20710c8c60783428f4e8d2ef3', {valueEncoding: 'json', sparse: true})
])
var word = process.argv[2] || 'hello'
db.ready(function () {
var sw = hyperdiscovery(db, {live: true})
if (process.argv.indexOf('--sync') > -1) {
db.feeds[0].download({start: 0, end: -1})
return
}
console.log('Joining swarm ...')
db.feeds[0].get(0, function () {
// haxx to make sure we have a connection lol
var now = Date.now()
db.feeds[0].on('download', function (index) {
console.log('Downloaded block', index)
})
db.get(word, function (err, nodes) {
console.log('Query took', Date.now() - now, 'ms')
console.log()
if (nodes && nodes[0] && nodes[0].key === word) {
console.log('"' + word + '" is an English word')
} else {
console.log('"' + word + '" is *not* an English word')
}
console.log()
console.log('Shutting down swarm ...')
sw.destroy()
})
})
})
function storage (name) {
if (name === 'data') return pages('words.db/data')
return raf('words.db/' + name)
}
function noop () {}
{
"name": "hyperdictionary",
"version": "0.0.0",
"main": "hyperdictionary.js",
"dependencies": {
"hypercore": "^6.3.8",
"hyperdb": "github:mafintosh/hyperdb",
"hyperdiscovery": "^6.0.4",
"random-access-file": "^1.7.3",
"random-access-memory": "^2.4.0",
"random-access-page-files": "^1.0.1"
}
}
@mafintosh

This comment has been minimized.

Show comment
Hide comment
@mafintosh

mafintosh Jun 2, 2017

Tested on WebRebels network (--ram forces a cold cache)

> node hyperdictionary.js hello --ram
Joining swarm ...
Downloaded block 354983
Downloaded block 354982
Downloaded block 354963
Downloaded block 354882
Downloaded block 354763
Downloaded block 353355
Downloaded block 344938
Downloaded block 336340
Downloaded block 129848
Query took 377 ms

"hello" is an English word

Shutting down swarm ...
Owner

mafintosh commented Jun 2, 2017

Tested on WebRebels network (--ram forces a cold cache)

> node hyperdictionary.js hello --ram
Joining swarm ...
Downloaded block 354983
Downloaded block 354982
Downloaded block 354963
Downloaded block 354882
Downloaded block 354763
Downloaded block 353355
Downloaded block 344938
Downloaded block 336340
Downloaded block 129848
Query took 377 ms

"hello" is an English word

Shutting down swarm ...
@mafintosh

This comment has been minimized.

Show comment
Hide comment
@mafintosh

mafintosh Jun 2, 2017

> node hyperdictionary.js hello --ram --optimized
Joining swarm ...
Trying optimized query for "hello" (will by dynamic in the future)
Downloaded block 336340
Downloaded block 344938
Downloaded block 353355
Downloaded block 129848
Downloaded block 354983
Downloaded block 354963
Downloaded block 354982
Downloaded block 354882
Downloaded block 354763
Query took 83 ms

"hello" is an English word

Shutting down swarm ...
Owner

mafintosh commented Jun 2, 2017

> node hyperdictionary.js hello --ram --optimized
Joining swarm ...
Trying optimized query for "hello" (will by dynamic in the future)
Downloaded block 336340
Downloaded block 344938
Downloaded block 353355
Downloaded block 129848
Downloaded block 354983
Downloaded block 354963
Downloaded block 354982
Downloaded block 354882
Downloaded block 354763
Query took 83 ms

"hello" is an English word

Shutting down swarm ...
@mafintosh

This comment has been minimized.

Show comment
Hide comment
@mafintosh

mafintosh Jun 4, 2017

hyperdb now supports optimised queries on all keys

Owner

mafintosh commented Jun 4, 2017

hyperdb now supports optimised queries on all keys

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