Skip to content

Instantly share code, notes, and snippets.

@wesleytodd
Last active October 25, 2015 20:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wesleytodd/601667c755c63cbe2769 to your computer and use it in GitHub Desktop.
Save wesleytodd/601667c755c63cbe2769 to your computer and use it in GitHub Desktop.
An very basic example usage of k-bucket to route requests on a dht
var KBucket = require('k-bucket');
var crypto = require('crypto');
// Number of nodes
var NUM_NODES = 5;
var NUM_ITEMS = 100;
// Make the routing table
var router = new KBucket();
// Make nodes in the k-bucket
for (var i = 0; i < NUM_NODES; i++) {
router.add(createNode(i));
}
// Make some items and store them
for (var i = 0; i < NUM_ITEMS; i++) {
saveItem('item' + i);
}
// Get some items and describe them
describeItem('item1');
describeItem('item123');
describeItem('item345');
describeItem('item567');
// Desribe a node and the data in it
describeNode('Bucket0');
// A node is a little in memory k/v store
function createNode(i) {
var name = 'Bucket' + i;
return {
id: new Buffer(sha1(name)),
name: name,
_data: {},
length: 0,
put: function(hash, d) {
if (!this._data[hash]) this.length++;
this._data[hash] = d;
},
get: function(hash) {
return this._data[hash];
}
};
}
// To save an item, pick the closest node, and put it there
function saveItem(item) {
var hash = sha1(item);
whichNode(hash).put(hash, item);
}
// Get an item is to find the closes node and get from it
function getItem(item) {
var hash = sha1(item);
return whichNode(hash).get(hash);
}
// Use the k-bucket router to find the closes node to an item
function whichNode(hash) {
return router.closest({
id: new Buffer(hash)
}, 1)[0];
}
// helper to sha1 things
function sha1(d) {
return crypto.createHash('sha1').update(d).digest('hex');
};
function describeItem(item) {
var hash = sha1(item);
var node = whichNode(hash);
console.log('========================');
console.log('Item Conent: ', node.get(hash));
console.log('Node Name: ', node.name);
console.log('Item Hash: ', hash);
}
function describeNode(name) {
var hash = sha1(name);
var node = whichNode(hash);
var items = [];
for (var i in node._data) {
items.push(node._data[i]);
}
console.log('========================');
console.log('Node Name: ', node.name);
console.log('Items Stored In Node: ', node.length);
console.log('Items In Node: \n', items);
}
> $ node index.js
========================
Item Conent: item1
Node Name: Bucket3
Item Hash: 4e702d8dacb758a70499bd7a1cd4259087b743df
========================
Item Conent: undefined
Node Name: Bucket3
Item Hash: 41857183a56ba0402914ad20df39a464879fd484
========================
Item Conent: undefined
Node Name: Bucket0
Item Hash: 7c54e0c538c60d07c52a04f477048e86722e7f09
========================
Item Conent: undefined
Node Name: Bucket1
Item Hash: 10f0bb1c0607d715e313f558769a4bc3b432e0b9
========================
Node Name: Bucket0
Items Stored In Node: 36
Items In Node:
[ 'item2',
'item3',
'item8',
'item11',
'item17',
'item19',
'item20',
'item22',
'item26',
'item27',
'item29',
'item31',
'item33',
'item36',
'item37',
'item38',
'item40',
'item41',
'item42',
'item44',
'item50',
'item52',
'item58',
'item59',
'item61',
'item69',
'item73',
'item75',
'item77',
'item78',
'item79',
'item81',
'item84',
'item89',
'item91',
'item92' ]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment