Prajwal Tuladhar’s Blog
 
programming, life and some random thoughts

Dec 03 2009

Node.js: Changing the way we do I/O

Published by Prajwal Tuladhar under JavaScript

Node.js might be the most exciting single piece of software in the current JavaScript universe. Ryan received standing ovations for his talk and he really deserved it!

http://jsconf.eu/2009/video_nodejs_by_ryan_dahl.html

I tried Node.js last week and I was quite impress. Truly, Node.js has the potential to change the way we do I/O. Basically it’s an evented programming concept build on underlying stack of Python and C++ while using Google’s V8 (JavaScript Engine) as an interfacing language capable of performing network programming (using polling) in different way. Here are some of the key design goals of this technology:

  • No function should direct perform I/O (Use Callback to do so)
  • Stream everything, never force the buffering of data (Somewhat like comet)
  • Have built-in support for the most important protocols: HTTP, DNS, TCP
  • Support many HTTP features: Chunked requests, Keep-alive, Hang Requests for comet applications
  • Bridge API somewhere between flexibility of client side JavaScript and old Unix tools
  • Platform independent
Example:
Simple Twitter Client I wrote last week:

var http = require("http"),
	sys = require("sys");
var connection = http.createClient(80, "twitter.com");
var since_id = '';
var interval = 0;
function getTweets()	{
	var url = "/statuses/friends_timeline.json";
	if (since_id != '')	{
		url += "?since_id=" + since_id;
	}
	var request = connection.get(url, {
		"content-type": "application/json",
		"User-Agent": "NodeJS HTTP Client by Infynyxx",
		"host": "twitter.com",
		"Authorization": "Basic " + Base64.encode("infynyxx:*****")
	});
	request.finish(function(response)	{
		var responseBody = "";
		response.setBodyEncoding("utf8");
		response.addListener("body", function(chunk)	{
			responseBody += chunk;
		});
		response.addListener("complete", function()	{
			var tweets = JSON.parse(responseBody);
			if (tweets.error)	{
				sys.puts("Error: " + tweets.error);
			}
			else	{
				var length = tweets.length;
				if (length > 0)	{
					sys.puts("Getting new tweets...\n");
					sys.puts("Number of new tweets: " + length + "\n");
					var str = "";
					tweets.reverse();
					tweets.forEach(function(element, index) {
						str += element.text + "\n";
						str += element.user.name + "\n";;
						str += element.created_at + "\n";;
						str += "*************************\n";
					});
					sys.puts(str);
				}
			}
		});
	});
	setTimeout(getTweets, interval);
	interval = 300000;  //5 minutes
}

getTweets();

It also uses Base64 library. Full Code @ Gitgub


Comments

Nov 19 2009

“Coding isn’t fun if you can do is call things out of a library”

Published by Prajwal Tuladhar under Programming

There’s this overemphasis on reusable software where you never get to open up the box. It’s nice to have these boxes but, almost always, if you look inside the box you can improve it and make work better once you know what’s inside the box. ~ Donald E. Knuth (Excerpt from Coders At Work.)

Second excerpt I posted from the book, Coders At Work that has so deep meaning.

Even though we use so many different libraries and APIs both lower and higher level, if we don’t know what these APIs and libraries do or how they work then, it’s just like using one of those anonymous black boxes with lots of magic.


Comments

Nov 15 2009

MongoDB’s performance as compared to others

Published by Prajwal Tuladhar under MongoDB

Click to view the full size

I haven’t used PostgreSQL and TokyoTyrant so, can’t say much about them. And technically, I really don’t think that one should compare MySQL which is relational database with document based non-relational databases like: CouchDB and MongoDB.

In my opinion, MongoDB out-performs CouchDB in terms of querying, insertion and ease of usage but CouchDB’s support for MVCC and transaction are quite interesting. One of the crons of MongoDB is it’s data size grow at freaking high rate.

Thoough great to see that, NOSQL (NOt Only SQL) is on full swing.

Download OpenSQL comparison PDF (Don’t forget to read the conclusion though) via HackerNews.


Comments

Nov 15 2009

MapReduce API for MongoDB

Published by Prajwal Tuladhar under MongoDB

Currently, I’ve been doing some stuffs using MongoDB. If you don’t know or haven’t use it, it’s a document based key-value database systems, that means it’s fundamentally different from traditional DBMS like MySQL, Oracle.

Systems like MongoDB along with similar technologies like CouchDB make significant use of MapReduce. MapReduce is basically a two step process consisting of Map and Reduce where Map is used for reducing a dataset to smaller sub-sets while Reduce is used for for some specific operations into that mapped or grouped data. You can find more information about it all over the web.

Since, PHP driver MongoDB does not provide any specific MapReduce API, I’ve created mine own using MongoDB::command. You can find it @ Github.

Simple Usage:


<?
$db_name = "test_dbs";
$mongodb = new MongoDB(new Mongo(), $db_name);

$map = <<<MAP
	function()	{
		this.tags.forEach(
			function(x)	{
				emit(x, 1);
			}
		);
	}
MAP;

$reduce = <<<REDUCE
	function(key, values)	{
		return {count: values.length };
	}
REDUCE;

$map_reduce = new MongoMapReduce($map, $reduce);
$collection_name = "animal_tagsaa";
$response = $map_reduce->invoke($mongodb, $collection_name);
print_r($response->getRawResponse());
if ($response->valid())	{
	echo "Total Execution Time: {$response->getTotalExecutionTime()} Milli Seconds\n";
	$count_data = $response->getCountsData();

	echo "Count Data\n";
	foreach ($count_data as $key=>$value)	{
		echo "{$key}: {$value}\n";
	}
	echo "********************\n";
	foreach ($response->getResultSet() as $tag)	{
		echo "{$tag["_id"]}\n";
		echo "Count: {$tag["value"]["count"]}\n";
		echo "****************\n";
	}
}

Usage with Mongo Collections


<?php

function __autoload($class_name) {
    require_once "../lib/".$class_name . '.php';
}

$db_name = "test_dbs";
$mongodb = new MongoDB(new Mongo(), $db_name);

class AnimalTag extends XMongoCollection	{

	const COLLECTION_NAME = "animal_tags";

	public function __construct(MongoDB $mongoDB)	{
		$this->collectionName = self::COLLECTION_NAME;
		parent::__construct($mongoDB, $this->collectionName);
	}
}

$animal_tags = new AnimalTag($mongodb);

$map = <<<MAP
	function()	{
		this.tags.forEach(
			function(x)	{
				emit(x, 1);
			}
		);
	}
MAP;

$reduce = <<<REDUCE
	function(key, values)	{
		return {count: values.length };
	}
REDUCE;

$response = $animal_tags->mapReduce(new MongoMapReduce($map, $reduce));
if ($response->valid())	{
	foreach ($response->getResultSet() as $tag)	{
		echo "{$tag["_id"]}\n";
		echo "Count: {$tag["value"]["count"]}\n";
		echo "****************\n";
	}
}

Enjoy!!!


Comments

Nov 13 2009

Wealth is not about the money you amass but it’s about how many lives you enrich

Published by Prajwal Tuladhar under Miscellaneous

Warren Buffett and Bill Gates: Keeping America Great



Comments

« Prev - Next »

RSS Feed
Subscribe by email
Follow me @ Twitter