Skip to content

Commit

Permalink
[api] Added basic support for Hnet.set
Browse files Browse the repository at this point in the history
  • Loading branch information
Marak committed Jan 1, 2012
1 parent 9e6efc0 commit 8603658
Show file tree
Hide file tree
Showing 4 changed files with 124 additions and 43 deletions.
44 changes: 19 additions & 25 deletions README.md
@@ -1,32 +1,24 @@
# hnet - a distributed, decentralized, and somewhat anonymous database # hnet - an experimental decentralized and anonymous database


## hnet spreads small amounts of data across several engines ## hnet spreads small amounts of data across several non-traditional storage engines such as images, gists, pastebin, twitter streams, irc chat rooms, etc...


It's good for distributing things like ip routing tables ### hnet is ideal for distributing small amounts of state anonymously. By design, it is not reliable, fast, or consistent.


## Usage ### An ideal use-case for hnet would be storing sets of IP addresses and ports for servers.


var hnet = require('hnet'); # How does it work?


// *hnet uses couchiris at top*
// Will start lazy loading sources
//
hnet.load(['http://hnet.iriscouch.com/public/0', 'https://gist.github.com/01889d7e9e8cc35375c8', 'http://hnet.iriscouch.com/public/1']);


hnet.on('load', function(set){ *img of db layout*
console.log('just load data set from ', set);
});


hnet.on('ready', function(){ *another img of client getting data*
console.log('all data sets have been load');
console.log(hnet.get());
});


// *description of data format*
// Get will always work, regardless of how many sets are load.
// If no data sets have been load, get will just not return any data *basic linking*
//
console.log(hnet.get()); *basic circular dep linking*




## hnet protocol ## hnet protocol
Expand Down Expand Up @@ -77,11 +69,13 @@ console.log(hnet.get());


# TODO: # TODO:


Add additional engines for: * Finish pluggable crypto system

* Create basic implementation for `Hnet.set`
- PasteBin * Add additional engines for:
- Image Stenography - Image Stenography
- PasteBin
- Reddit - Reddit
- Imgur - Imgur
- Hacker News deadlink jail - Hacker News deadlink jail

- Twitter
- IRC
25 changes: 25 additions & 0 deletions example/save-pulled-hnet-data.js
@@ -0,0 +1,25 @@
var Hnet = require('../lib/hnet').Hnet;

var hnet = new Hnet();


hnet.on('node', function (node, data, options) {
//console.log('level::' + options.currentDepth + '::' + node.uri);
});

hnet.on('level::*', function(nodes, options){
//console.log('level::' + this.event.split('::')[1] + '::loaded');
//console.log(JSON.stringify(hnet.get(), true, 2));
});

hnet.on('level::0', function(nodes, options){
//console.log(hnet.nodes);
hnet.save(function(err, response, body){

//console.log(err, body);

});
});

hnet.load(['http://hnet.iriscouch.com/public/0']);

86 changes: 75 additions & 11 deletions lib/hnet.js
Expand Up @@ -35,6 +35,13 @@ var Hnet = exports.Hnet = function (options) {
self.circularDelay = options.circularDelay || -1; self.circularDelay = options.circularDelay || -1;




//
// TODO: upgrade EE2 to version that has "**"
//
self.on('*::*', function(data){
console.log('debug:', this.event, data);
});

// //
// Select a default crypto engine // Select a default crypto engine
// //
Expand Down Expand Up @@ -136,9 +143,9 @@ Hnet.prototype.parseDocument = function (node, doc, options) {
} }
}); });
} }
doc.forEach(function(record){

self.store.push(record);
self.store.push(doc); });
} }


// //
Expand All @@ -148,6 +155,8 @@ Hnet.prototype.load = function(nodes, options, cb){


var self = this; var self = this;


console.log('loading data now...', nodes)

// //
// If the incoming node is just a string, // If the incoming node is just a string,
// coherse it into a one element array // coherse it into a one element array
Expand Down Expand Up @@ -249,20 +258,75 @@ Hnet.prototype.get = function(){
}; };




Hnet.prototype.set = function(node, cb){ Hnet.prototype.save = function(cb){

var self = this,
dataSet,
_keys = Object.keys(self.nodes),
backLinks = [],
middle,
last;


var self = this;
/* /*
if(self.encrypt) { if(self.encrypt) {
data = self.encrypt(data); data = self.encrypt(data);
} }
*/ */

//
// Remark: If we have no nodes at all, we'll just create a new one using hnet.iriscouch.com
//
if (_keys.length === 0) {
console.log('no known nodes, using default');
backLinks.push({uri: 'http://hnet.iriscouch.com/public/0', type: 'couchdb'});
} else if (_keys.length === 1) {
//
// Remark: Pick 1 top-level node, and 2 other "random" nodes to link back to.
//
backLinks.push(self.nodes[_keys[0]]);
} else if (_keys.length >= 3) {
middle = self.nodes[_keys[_keys.length / 2]];
last = self.nodes[_keys[_keys.length - 1]];
//
// Remark: Instead of random, let's try the middle and last element instead
//
backLinks.push(_link(middle));
backLinks.push(_link(last));

}


// dataSet = self.store;
// Pick 1 top-level node, and 2 other random nodes to link back to. backLinks.forEach(function(savePoint){
// dataSet.push(savePoint);
if(self.engines[node[0].type].set) { });
self.engines[node[0].type].set(node[0], cb);
}


//
// Pick two "random" engines
//
var _engines = ['couchdb', 'gist'];
_engines.forEach(function(engine){
if (self.engines[engine].save) {
self.engines[engine].save(dataSet, function(err, response, body) {
if (err) {
return cb(err);
}
//
// If we have be given an id back from the Couch
//
if (body.id) {
self.nodes['http://hnet.iriscouch.com/public/' + body.id];
}
self.emit('node::added', 'http://hnet.iriscouch.com/public/' + body.id)
cb(null, body);
});
}
});
}; };


//
// Converts node uris into JSON-RPC link commands
//
function _link(node) {
return { "method": "link", "params": [node] };
}
12 changes: 5 additions & 7 deletions lib/hnet/engines/couchdb.js
Expand Up @@ -22,21 +22,19 @@ couch.load = function (options, cb) {
request({ uri: options.uri, method: "GET" }, cb); request({ uri: options.uri, method: "GET" }, cb);
}; };


couch.set = function (node, cb) { couch.save = function (node, cb) {
var self = this; var self = this;
var parts = node.uri.split('/'); request({ uri: "http://hnet.iriscouch.com/public", json: true, body: JSON.stringify({"h":node}), method: "POST" }, function(err, response, body){
node.data._id = node.data._id || parts[parts.length -1] || 'no-id'; cb(err, response, body);
parts.pop(); /*
node.uri = parts.join('/');
request({ uri: node.uri, json: node.data, method: "POST" }, function(err, response, body){
if(self.nodes[node.uri]) { if(self.nodes[node.uri]) {
self.nodes[node.uri].rev = body.rev; self.nodes[node.uri].rev = body.rev;
} else { } else {
self.nodes[node.uri] = { self.nodes[node.uri] = {
body:rev body:rev
} }

} }
*/
}); });
}; };


Expand Down

0 comments on commit 8603658

Please sign in to comment.