<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	>

<channel>
	<title>Sameer Parwani</title>
	<atom:link href="http://sameerparwani.com/feed/" rel="self" type="application/rss+xml" />
	<link>http://sameerparwani.com</link>
	<description>Ramblings from a Web Developer</description>
	<pubDate>Mon, 12 Oct 2009 06:09:18 +0000</pubDate>
	<generator>http://wordpress.org/?v=2.6.1</generator>
	<language>en</language>
			<item>
		<title>That&#8217;s My Desi Life (TMDL)</title>
		<link>http://sameerparwani.com/posts/thats-my-desi-life-tmdl</link>
		<comments>http://sameerparwani.com/posts/thats-my-desi-life-tmdl#comments</comments>
		<pubDate>Mon, 12 Oct 2009 06:09:18 +0000</pubDate>
		<dc:creator>Sameer</dc:creator>
		
		<category><![CDATA[Uncategorized]]></category>

		<category><![CDATA[iphone]]></category>

		<guid isPermaLink="false">http://sameerparwani.com/?p=283</guid>
		<description><![CDATA[I recently opened a new website ThatsMyDesiLife.com which parodies the &#8220;funny&#8221; or &#8220;different&#8221; (depending on your perspective) elements of desi culture via user submitted short stories. This is basically a spin off of FML, similar to what others have done by using the fmyscript software.
However, after purchasing that above script I added an API on top and have now [...]]]></description>
			<content:encoded><![CDATA[<p>I recently opened a new website <a href="http://www.thatsmydesilife.com">ThatsMyDesiLife.com</a> which parodies the &#8220;funny&#8221; or &#8220;different&#8221; (depending on your perspective) elements of desi culture via user submitted short stories. This is basically a spin off of FML, similar to what others have done by using the <a href="http://www.fmyscript.com">fmyscript</a> software.</p>
<p>However, after purchasing that above script I added an API on top and have now (in conjuction with another developer) created <a href="http://itunes.apple.com/WebObjects/MZStore.woa/wa/viewSoftware?id=333191433&amp;mt=8">an iPhone application for the website</a>. I&#8217;m still in the process of learning the Cocoa framework and Objective C but its an interesting change from years of PHP. It&#8217;s nice to have a stateful process that can keep track of asynchronous requests and events unlike HTTP. And, the development tools (xcode and interface builder) are very nice to use. It was a bit of pain getting use to using a Mac though.</p>
<p>Anyway, take a look at screenshots from the app:</p>
<p> <a href="http://sameerparwani.com/wp-content/uploads/2009/10/img_0220.jpg"><img class="size-medium wp-image-284 alignnone" title="img_0220" src="http://sameerparwani.com/wp-content/uploads/2009/10/img_0220-208x300.jpg" alt="" width="125" height="180" /></a> <a href="http://sameerparwani.com/wp-content/uploads/2009/10/img_0221.jpg"><img class="alignnone size-medium wp-image-285" title="img_0221" src="http://sameerparwani.com/wp-content/uploads/2009/10/img_0221-208x300.jpg" alt="" width="125" height="180" /></a> <a href="http://sameerparwani.com/wp-content/uploads/2009/10/img_0226.jpg"><img class="alignnone size-medium wp-image-287" title="img_0226" src="http://sameerparwani.com/wp-content/uploads/2009/10/img_0226-208x300.jpg" alt="" width="125" height="180" /></a></p>
]]></content:encoded>
			<wfw:commentRss>http://sameerparwani.com/posts/thats-my-desi-life-tmdl/feed</wfw:commentRss>
		</item>
		<item>
		<title>Xdebug is slow</title>
		<link>http://sameerparwani.com/posts/xdebug-is-slow</link>
		<comments>http://sameerparwani.com/posts/xdebug-is-slow#comments</comments>
		<pubDate>Thu, 21 May 2009 22:50:05 +0000</pubDate>
		<dc:creator>Sameer</dc:creator>
		
		<category><![CDATA[PHP]]></category>

		<category><![CDATA[xdebug]]></category>

		<guid isPermaLink="false">http://sameerparwani.com/?p=272</guid>
		<description><![CDATA[Xdebug is a wonderfully helpful debugger and profiler for PHP. Primarily it serves two main purposes (at least for me). First, it makes error messages so much more helpful because it contains a stack trace that can even be configured to show actual function arguments. Here is an example

Second, it helps profile your code by [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://www.xdebug.org/">Xdebug</a> is a wonderfully helpful debugger and profiler for PHP. Primarily it serves two main purposes (at least for me). First, it makes error messages so much more helpful because it contains a stack trace that can even be configured to show actual function arguments. Here is an example</p>
<p><img src="http://sameerparwani.com/wp-content/uploads/2009/05/xdebug.jpg" alt="" title="xdebug" width="500" height="118" class="alignnone size-full wp-image-274" /></p>
<p>Second, it helps profile your code by creating an output file with a full listing of all functions called and execution time for your script. Using a tool like <a href="http://kcachegrind.sourceforge.net/html/Home.html">kcachegrind</a> or <a href="http://sourceforge.net/projects/wincachegrind/">wincachegrind</a> the output can be visualized in an easy to understand format. I am an avid user of xdebug&#8217;s profiling tool as it&#8217;s the best way to identify performance problems in your php applications. With all that said:</p>
<p><strong>DON&#8217;T RUN XDEBUG ON PRODUCTION</strong>.</p>
<p>I&#8217;m sure you can imagine the profiler&#8217;s output being very large for applications with thousands of function calls. I&#8217;ve seen 100MB+ output files. Imagine if php had to create a 100MB file for every request. By turning on <a href="http://www.xdebug.org/docs/profiler">xdebug.profiler_enable_trigger</a>, you can at least selectively run the profiler.</p>
<p>Yet, even when the profiler is off, the other primary function of xdebug (detailed stack traces on errors) causes your php instance to track lots of extra information slowing down execution. On a recent real world example where I turned on xdebug temporarily (with profiler off) and then shut it off, xdebug caused average response time to go from 400ms to 700ms.</p>
]]></content:encoded>
			<wfw:commentRss>http://sameerparwani.com/posts/xdebug-is-slow/feed</wfw:commentRss>
		</item>
		<item>
		<title>Understanding The Importance of Passing Along Host Names in a Reverse Proxy</title>
		<link>http://sameerparwani.com/posts/understanding-the-importance-of-passing-along-host-names-in-a-reverse-proxy</link>
		<comments>http://sameerparwani.com/posts/understanding-the-importance-of-passing-along-host-names-in-a-reverse-proxy#comments</comments>
		<pubDate>Mon, 18 May 2009 07:33:14 +0000</pubDate>
		<dc:creator>Sameer</dc:creator>
		
		<category><![CDATA[Nginx]]></category>

		<category><![CDATA[Add new tag]]></category>

		<guid isPermaLink="false">http://sameerparwani.com/?p=257</guid>
		<description><![CDATA[In earlier posts I&#8217;ve introduced the benefits of Nginx as a lightweight web server that can be used as a reverse proxy and even to load balance. After using Nginx more extensively, I want to share one lesson that was extremely important for me to get a better understanding of how to configure my reverse [...]]]></description>
			<content:encoded><![CDATA[<p>In earlier posts I&#8217;ve <a href="http://sameerparwani.com/posts/nginx-as-a-front-end-to-apache/">introduced the benefits of Nginx as a lightweight web server that can be used as a reverse proxy</a> and <a href="http://sameerparwani.com/posts/load-balancing-with-nginx/">even to load balance</a>. After using Nginx more extensively, I want to share one lesson that was extremely important for me to get a better understanding of how to configure my reverse proxies to work as intended without wasting hours debugging or needlessly over-configuring. </p>
<p>Let&#8217;s take a look at a very basic reverse proxy configuration:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
</pre></td><td class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">#nginx configuration</span>
server <span style="color: #7a0874; font-weight: bold;">&#123;</span>
   listen       <span style="color: #000000;">80</span>;
   server_name www.mysite.com;
&nbsp;
   <span style="color: #666666; font-style: italic;"># pass along header with reverse proxy requests</span>
   proxy_set_header Host <span style="color: #007800;">$host</span>;
&nbsp;
   <span style="color: #666666; font-style: italic;"># pass along all requests to apache waiting at localhost:8080</span>
   location <span style="color: #000000; font-weight: bold;">/</span> <span style="color: #7a0874; font-weight: bold;">&#123;</span>
      proxy_pass http:<span style="color: #000000; font-weight: bold;">//</span>localhost:<span style="color: #000000;">8080</span>;
   <span style="color: #7a0874; font-weight: bold;">&#125;</span>
<span style="color: #7a0874; font-weight: bold;">&#125;</span></pre></td></tr></table></div>

<p>Apache&#8217;s configuration then would look like</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
</pre></td><td class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">#apache httpd configuration</span>
Listen localhost:<span style="color: #000000;">8080</span>
<span style="color: #000000; font-weight: bold;">&lt;</span>VirtualHost localhost:<span style="color: #000000;">8080</span><span style="color: #000000; font-weight: bold;">&gt;</span>
        DocumentRoot <span style="color: #000000; font-weight: bold;">/</span>path<span style="color: #000000; font-weight: bold;">/</span>to<span style="color: #000000; font-weight: bold;">/</span>webroot
        ServerName www.mysite.com
<span style="color: #000000; font-weight: bold;">&lt;/</span>VirtualHost<span style="color: #000000; font-weight: bold;">&gt;</span></pre></td></tr></table></div>

<p>I want to point along two important lines. First &#8220;proxy_set_header Host $host&#8221; in the Nginx configuration and &#8220;ServerName www.mysite.com&#8221; in the Apache configuration. The first line instructs Nginx to pass along the original host header to Apache in essence tricking Apache into believing the request came directly from the end user. </p>
<p>And because we want Apache to believe it received the request directly, we repeat the server name that was defined in the Nginx configuration. This might seem obvious to some of you, but at first I was tripped up because I did not realize I could pass along the host header request and match it to Apache&#8217;s intended Server Name. </p>
<p>Without doing so, it was very difficult to use the same Apache instance as the backend for multiple reverse proxied websites. That was because Apache had no way to differentiate a request for website A from a request for website B. However, once you pass along the requested host, Apache now has a piece of information that it can use to differentiate requests by matching the host to its defined server name.</p>
]]></content:encoded>
			<wfw:commentRss>http://sameerparwani.com/posts/understanding-the-importance-of-passing-along-host-names-in-a-reverse-proxy/feed</wfw:commentRss>
		</item>
		<item>
		<title>Tamper Data Extension for Firefox</title>
		<link>http://sameerparwani.com/posts/tamper-data-extension-for-firefox</link>
		<comments>http://sameerparwani.com/posts/tamper-data-extension-for-firefox#comments</comments>
		<pubDate>Sun, 17 May 2009 07:27:31 +0000</pubDate>
		<dc:creator>Sameer</dc:creator>
		
		<category><![CDATA[Uncategorized]]></category>

		<category><![CDATA[Web Development]]></category>

		<guid isPermaLink="false">http://sameerparwani.com/?p=223</guid>
		<description><![CDATA[Web Developers use Firefox as their browser of choice for many reasons but maybe most significant are the excellent extensions available to make development quicker, easier, and more effective. The two extensions that pretty much every developer is already aware of are Firebug and the aptly named Web Developer extension. However, an extension I use [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://sameerparwani.com/wp-content/uploads/2009/05/tamperdata2.jpg"><img src="http://sameerparwani.com/wp-content/uploads/2009/05/tamperdata2-150x150.jpg" alt="" title="tamperdata2" width="150" height="150" class="alignright size-thumbnail wp-image-247" /></a>Web Developers use Firefox as their browser of choice for many reasons but maybe most significant are the excellent extensions available to make development quicker, easier, and more effective. The two extensions that pretty much every developer is already aware of are <a href="https://addons.mozilla.org/en-US/firefox/addon/1843">Firebug</a> and the aptly named <a href="https://addons.mozilla.org/en-US/firefox/addon/60">Web Developer</a> extension. However, an extension I use almost as often as those two is <a href="https://addons.mozilla.org/en-US/firefox/addon/966">Tamper Data</a>. </p>
<p>In its most basic form, TamperDdata allows you to view the headers for every request and response your browser handles. With that, you are able to examine the POST requests that your browser sends to a server. </p>
<p>But, the extension being called Tamper Data, it lets you do more than just examine the data being passed. It allows you to trap a request and alter the headers and POST data. Why might that be useful? Heres two of many possible use cases.</p>
<p><strong>Use Cases</strong></p>
<ul class="wordy">
<li><a href="http://sameerparwani.com/wp-content/uploads/2009/05/tamperdata.jpg"><img src="http://sameerparwani.com/wp-content/uploads/2009/05/tamperdata-150x150.jpg" alt="" title="tamperdata" width="150" height="150" class="alignright size-thumbnail wp-image-246" /></a><strong>Form Tampering</strong> - Imagine you have a nifty registration form and you had all this fancy javascript that prevents and notifies a user if he enters invalid information. However, you still need to make sure your backend validates the data without relying on your javascript. One way to do so would be to use Tamper Data.
<p>In your browser, begin by completing the form correctly, but before you hit submit open Tamper Data and press &#8220;Start Tamper&#8221;. Then return to your browser and submit the form. Tamper Data will then popup asking you if you would like to tamper with the request that is being sent. Select tamper and then modify the post values to be invalid, and then hit okay. Tamper Data will submit the modified version of the form with the invalid data. You can then return to your browser window and verify your backend handled the submitted data as intended.</li>
<li><strong>Investigating Session Problems</strong> - Sessions are identified via cookies. A server provides a cookie to a user upon its initial response. The user provides that cookie back to the site on each successive request allowing the site to identify future requests made by that same user. This concept allows a developer to keep a user &#8220;logged in&#8221; between requests.
<p>Several times I&#8217;ve had issues where sessions did not seem to persist. The best first step in identifying the issue is to determine if cookies are being handled properly. Is the server sending a cookie with the proper domain and settings to the user? Is the user sending that cookie in subsequent requests? That&#8217;s where Tamper Data comes in. Use it to verify the cookie data being sent in the headers.</li>
]]></content:encoded>
			<wfw:commentRss>http://sameerparwani.com/posts/tamper-data-extension-for-firefox/feed</wfw:commentRss>
		</item>
		<item>
		<title>Late Static Binding Coming in PHP 5.3</title>
		<link>http://sameerparwani.com/posts/late-static-binding-coming-in-php-53</link>
		<comments>http://sameerparwani.com/posts/late-static-binding-coming-in-php-53#comments</comments>
		<pubDate>Sat, 16 May 2009 02:25:47 +0000</pubDate>
		<dc:creator>Sameer</dc:creator>
		
		<category><![CDATA[PHP]]></category>

		<guid isPermaLink="false">http://sameerparwani.com/?p=249</guid>
		<description><![CDATA[By far the most important change in PHP 5 was its much improved support for object oriented programming. It&#8217;s hard to imagine, but it was not so long ago when PHP did not offer support for basic object oriented necessities such as constructors, destructors, interfaces, abstract classes, public, private, magic methods, and so on.
Yet one [...]]]></description>
			<content:encoded><![CDATA[<p>By far the most important change in PHP 5 was its much improved support for object oriented programming. It&#8217;s hard to imagine, but it was not so long ago when PHP did not offer support for basic object oriented necessities such as constructors, destructors, interfaces, abstract classes, public, private, magic methods, and so on.</p>
<p>Yet one of the most frustrating problems with PHP&#8217;s implementation of object oriented principles is the &#8220;self&#8221; keyword which is intended to reference methods and variables within a class in a static context. <span id="more-249"></span></p>
<pre class="brush: php">
class A {
    public static function who() {
        echo &quot;this is A&quot;;
    }
    public static function test() {
        self::who();
    }
}
</pre>
<p>In the above code, calling A::test() calls A::who() and therefore outputs &#8220;this is A&#8221; because &#8220;self&#8221; resolves to &#8220;A&#8221;. Yet take a look at the code below (taken from PHP.net)</p>
<pre class="brush: php">
class A {
    public static function who() {
        echo &quot;this is A&quot;;
    }
    public static function test() {
        self::who();
    }
}

class B extends A {
    public static function who() {
        echo &quot;this is B&quot;;
    }
}

B::test();
</pre>
<p>The above also outputs &#8220;this is A&#8221;.</p>
<p>For many people that is not the intended behavior. You are directly calling B::test() so because B inherits the test() method from A, you would expect that &#8220;self&#8221; would refer to B. However, PHP resolves &#8220;self&#8221; based on where the method is defined. Since the test() method is defined in A, any calls to &#8220;self&#8221; will reference A.</p>
<p>For the moment, I get around this problem by passing around the name of the child class:</p>
<pre class="brush: php">
class A {
    public static function who() {
        echo &quot;this is A&quot;;
    }
    public static function test($classname) {
        $classname::who();
    }
}

class B extends A {
    public static function who() {
        echo &quot;this is B&quot;;
    }
}

B::test(&quot;B&quot;);
</pre>
<p>The above outputs &#8220;this is B&#8221;.</p>
<p>In PHP 5.3, the problem is resolved by introducing a new keyword &#8220;static&#8221;. Unlike self, static is capable of <strong>late static binding</strong>. In other words, PHP will realize that the method test() was inherited by B and a call to static::who() would resolve to B:who(). </p>
<pre class="brush: php">
class A {
    public static function who() {
        echo &quot;this is A&quot;;
    }
    public static function test() {
        static::who();
    }
}

class B extends A {
    public static function who() {
        echo &quot;this is B&quot;;
    }
}

B::test();
</pre>
<p>The above outputs &#8220;this is B&#8221;.</p>
<p>The functionality of self has not changed, so a call to self::who() would still resolve to A:who(). For more information check the <a href="http://us.php.net/lsb">PHP manual</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://sameerparwani.com/posts/late-static-binding-coming-in-php-53/feed</wfw:commentRss>
		</item>
		<item>
		<title>Forking PHP Processes with pcntl_fork</title>
		<link>http://sameerparwani.com/posts/forking-php-processes-with-pcntl_fork</link>
		<comments>http://sameerparwani.com/posts/forking-php-processes-with-pcntl_fork#comments</comments>
		<pubDate>Fri, 15 May 2009 07:08:47 +0000</pubDate>
		<dc:creator>Sameer</dc:creator>
		
		<category><![CDATA[PHP]]></category>

		<guid isPermaLink="false">http://sameerparwani.com/?p=238</guid>
		<description><![CDATA[For a long time I worked under the assumption that PHP is not capable of running asynchronous tasks because it is single-threaded. However, you can work around the limitations of a single thread by forking a process with pcntl_fork. The new child process receives an exact copy of the process up until that point and [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://sameerparwani.com/wp-content/uploads/2009/05/php.gif"><img src="http://sameerparwani.com/wp-content/uploads/2009/05/php.gif" alt="" title="php" width="120" height="67" class="alignright size-full wp-image-239" /></a>For a long time I worked under the assumption that PHP is not capable of running asynchronous tasks because it is single-threaded. However, you can work around the limitations of a single thread by forking a process with <a href="http://www.php.net/pcntl_fork">pcntl_fork</a>. The new child process receives an exact copy of the process up until that point and continues execution at the same point in the script where it was forked&#8230; with the exception that it is aware that it is a child process.</p>
<p>No longer will you need to create logically linear code in PHP. For example, if you wanted to write a script to collect analytical data from your database and create 10 different reports, a typical PHP script would create one report a time. Imagine if the initial reports were time consuming to generate. But, by chance, some of the later reports only take a few seconds to generate. Yet, you would have to wait for the time consuming reports to finish generating before being able to view the smaller reports (solely because the smaller reports were randomly put at the end of the queue).</p>
<p>However, using pcntl_fork, you could fork your php code and begin generating all 10 reports asynchronously. Within a few seconds, you would have access to the easily generated reports while the more time consuming reports would still be generating.</p>
<p>The basics of how pcntl_fork works is illustrated with the following code taken from the PHP manual:</p>
<pre class="brush: php">
$pid = pcntl_fork();
if ($pid == -1) {
     die(&#039;could not fork&#039;);
} else if ($pid) {
     // we are the parent
     pcntl_wait($status); //Protect against Zombie children
} else {
     // we are the child
}
</pre>
<p>Take a look at this <a href="http://www.van-steenbeek.net/?q=php_pcntl_fork">great introduction</a> to pcntl_fork by Frans-Jan van Steenbeek who explains the concept better than I could.</p>
]]></content:encoded>
			<wfw:commentRss>http://sameerparwani.com/posts/forking-php-processes-with-pcntl_fork/feed</wfw:commentRss>
		</item>
		<item>
		<title>Tokyo Tyrant with PHP</title>
		<link>http://sameerparwani.com/posts/tokyo-tyrant-with-php</link>
		<comments>http://sameerparwani.com/posts/tokyo-tyrant-with-php#comments</comments>
		<pubDate>Thu, 14 May 2009 22:28:53 +0000</pubDate>
		<dc:creator>Sameer</dc:creator>
		
		<category><![CDATA[PHP]]></category>

		<category><![CDATA[Tokyo Cabinet]]></category>

		<category><![CDATA[Tokyo Tyrant]]></category>

		<guid isPermaLink="false">http://sameerparwani.com/?p=231</guid>
		<description><![CDATA[In previous posts I&#8217;ve introduced Tokyo Cabinet and shown how to install both Tokyo Cabinet and Tokyo Tyrant. In this post, I will show how simple it is to interact with Tokyo Cabinet using PHP.
To summarize, Tokyo Cabinet is an exceptionally fast key/value store. Tokyo Tyrant sits on top of Tokyo Cabinet exposing access to [...]]]></description>
			<content:encoded><![CDATA[<p>In previous posts I&#8217;ve <a href="http://sameerparwani.com/posts/tokyo-cabinet-and-tokyo-tyrant/">introduced Tokyo Cabinet</a> and shown how to <a href="http://sameerparwani.com/posts/installing-tokyo-cabinet-and-tokyo-tyrant/">install both Tokyo Cabinet and Tokyo Tyrant</a>. In this post, I will show how simple it is to interact with Tokyo Cabinet using PHP.</p>
<p>To summarize, Tokyo Cabinet is an exceptionally fast key/value store. Tokyo Tyrant sits on top of Tokyo Cabinet exposing access to the underlying data via memcache and http networking proctocols. </p>
<p>First begin by creating your database (we will use a hash) and then starting the Tyrant server on 127.0.0.1:80351.</p>
<pre class="brush: php">
tchmgr create db.tch
ttserver -dmn -host 127.0.0.1 -port 80351 db.tch
</pre>
<p>And because, Tokyo Tyrant implements the memcache protocol, we can use PHP&#8217;s existing support for memcache to interact with our database:</p>
<pre class="brush: php">

&lt;?php

// connect to tokyo tyrant via memcache protocol
$memcache_obj = new Memcache;
$memcache_obj-&gt;connect(&#039;127.0.0.1&#039;, 80351);

// set value
$memcache_obj-&gt;set(&#039;key&#039;,&#039;Sameer Parwani&#039;);

// get
$get = $memcache_obj-&gt;get(&#039;key&#039;);

// it works!
echo &quot;My name is $get&quot;
</pre>
<p>The above outputs</p>
<pre class="brush: php">
My name is Sameer Parwani
</pre>
<p>Keep in mind that Tokyo Tyrant will not support memcache&#8217;s automatic expiration option.</p>
]]></content:encoded>
			<wfw:commentRss>http://sameerparwani.com/posts/tokyo-tyrant-with-php/feed</wfw:commentRss>
		</item>
		<item>
		<title>Installing Tokyo Cabinet and Tokyo Tyrant</title>
		<link>http://sameerparwani.com/posts/installing-tokyo-cabinet-and-tokyo-tyrant</link>
		<comments>http://sameerparwani.com/posts/installing-tokyo-cabinet-and-tokyo-tyrant#comments</comments>
		<pubDate>Thu, 14 May 2009 19:36:50 +0000</pubDate>
		<dc:creator>Sameer</dc:creator>
		
		<category><![CDATA[Elgg]]></category>

		<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://sameerparwani.com/?p=233</guid>
		<description><![CDATA[As mentioned in a recent post, Tokyo Cabinet is a highly performant key/value store. Its speed leaves MySQL and other RDBMS&#8217;s in the dust because it replaces their overhead with highly optimal data structures such as hash tables. Please check the link above for more detail. If you are looking to get more performance out [...]]]></description>
			<content:encoded><![CDATA[<p>As mentioned in a <a href="http://sameerparwani.com/posts/tokyo-cabinet-and-tokyo-tyrant/">recent post</a>, Tokyo Cabinet is a highly performant key/value store. Its speed leaves MySQL and other RDBMS&#8217;s in the dust because it replaces their overhead with highly optimal data structures such as hash tables. Please check the link above for more detail. If you are looking to get more performance out of your system, Tokyo Cabinet is worth trying out.</p>
<p>The process for installing Tokyo Cabinet is very simple. Here is what I did on CentOS 5, starting with a few required libraries:</p>
<pre class="brush: php">
# Tokyo cabinet requires gzip and bzip
yum install gzip bzip2 bzip2-devel
</pre>
<p>I then proceeded to download and install the underlying Tokyo Cabinet</p>
<pre class="brush: php">
wget http://voxel.dl.sourceforge.net/sourceforge/tokyocabinet/tokyocabinet-1.4.20.tar.gz
tar zxf tokyocabinet-1.4.20.tar.gz
cd tokyocabinet-1.4.20
./configure
make
make install
</pre>
<p>On top of Tokyo Cabinet lies Tokyo Tyrant:</p>
<pre class="brush: php">
wget http://voxel.dl.sourceforge.net/sourceforge/tokyocabinet/tokyotyrant-1.1.26.tar.gz
tar zxf tokyotyrant-1.1.26.tar.gz
cd tokyotyrant-1.1.26
./configure
make
make install
</pre>
<p>Tokyo Cabinet supports four types of databases: hash, B+ tree, fixed-length, and table. Each type uses different commands, for example the commands that allows you to create, update, and read from a database are tchmgr, tcbmgr, tcfmgr, and tctmgr respectively.</p>
<p>The following shows how to create a hash database and manipulate it from the command line:</p>
<pre class="brush: php">
[root]# tchmgr create db.tch
[root]# tchmgr put db.tch key1 value1
[root]# tchmgr put db.tch key2 value2
[root]# tchmgr list db.tch
key1
key2
[root]# tchmgr get db2.tch key1
value1
</pre>
<p>Keep in mind Tokyo Cabinet&#8217;s database file extensions must match the type of the database. </p>
<ul>
<li>.tch - Hash</li>
<li>.tcb - B+ tree</li>
<li>.tcf - Fixed-length</li>
<li>.tct - Table</li>
</ul>
<p>In my next post, I will show how to connect to your Tokyo Cabinet database using PHP through Tokyo Tyrant.</p>
]]></content:encoded>
			<wfw:commentRss>http://sameerparwani.com/posts/installing-tokyo-cabinet-and-tokyo-tyrant/feed</wfw:commentRss>
		</item>
		<item>
		<title>Recursive FTP Make Directory (mkdir)</title>
		<link>http://sameerparwani.com/posts/recursive-ftp-make-directory-mkdir</link>
		<comments>http://sameerparwani.com/posts/recursive-ftp-make-directory-mkdir#comments</comments>
		<pubDate>Thu, 14 May 2009 07:06:57 +0000</pubDate>
		<dc:creator>Sameer</dc:creator>
		
		<category><![CDATA[PHP]]></category>

		<guid isPermaLink="false">http://sameerparwani.com/?p=227</guid>
		<description><![CDATA[Managing files over ftp using PHP can be a pain. One of the problems is dealing with long paths that may or may not exist. Fortunately, you can create functions to ease the problems.
For example, using php&#8217;s built-in ftp_mkdir function, to create the directory &#8220;/hello/kitty&#8221; you must first create the directory &#8220;/hello&#8221; and then &#8220;/hello/kitty&#8221;. [...]]]></description>
			<content:encoded><![CDATA[<p>Managing files over ftp using PHP can be a pain. One of the problems is dealing with long paths that may or may not exist. Fortunately, you can create functions to ease the problems.</p>
<p>For example, using php&#8217;s built-in ftp_mkdir function, to create the directory &#8220;/hello/kitty&#8221; you must first create the directory &#8220;/hello&#8221; and then &#8220;/hello/kitty&#8221;. Wouldn&#8217;t it be easier to have a helper function that could be passed the full path of the final directory that you want created (&#8221;/hello/kitty&#8221;) and it would take care of creating each directory in the path by itself? </p>
<p>Here is the code to do it:</p>
<pre class="brush: php">
	// recursive make directory function for ftp
	function make_directory($ftp_stream, $dir)
	{
		// if directory already exists or can be immediately created return true
		if (ftp_is_dir($ftp_stream, $dir) || @ftp_mkdir($ftp_stream, $dir)) return true;
		// otherwise recursively try to make the directory
		if (!make_directory($ftp_stream, dirname($dir))) return false;
		// final step to create the directory
		return ftp_mkdir($ftp_stream, $dir);
	}	

	function ftp_is_dir($ftp_stream, $dir)
	{
	   // get current directory
	   $original_directory = ftp_pwd($ftp_stream);
	   // test if you can change directory to $dir
	   // suppress errors in case $dir is not a file or not a directory
	   if ( @ftp_chdir( $ftp_stream, $dir ) ) {
		   // If it is a directory, then change the directory back to the original directory
		   ftp_chdir( $ftp_stream, $original_directory );
		   return true;
	   } else {
		   return false;
	   }
	}
</pre>
]]></content:encoded>
			<wfw:commentRss>http://sameerparwani.com/posts/recursive-ftp-make-directory-mkdir/feed</wfw:commentRss>
		</item>
		<item>
		<title>Tokyo Cabinet and Tokyo Tyrant</title>
		<link>http://sameerparwani.com/posts/tokyo-cabinet-and-tokyo-tyrant</link>
		<comments>http://sameerparwani.com/posts/tokyo-cabinet-and-tokyo-tyrant#comments</comments>
		<pubDate>Mon, 04 May 2009 06:52:26 +0000</pubDate>
		<dc:creator>Sameer</dc:creator>
		
		<category><![CDATA[MySQL]]></category>

		<category><![CDATA[Scaling]]></category>

		<category><![CDATA[Tokyo Cabinet]]></category>

		<category><![CDATA[Tokyo Tyrant]]></category>

		<guid isPermaLink="false">http://sameerparwani.com/?p=214</guid>
		<description><![CDATA[I recently had a conversation with Lalit Sarna of Oxylabs about scalability and he introduced me to Tokyo Cabinet, a key/value store (database). This category of databases, referred to as DBM, differs from a RDBMS (such as MySQL) in that there are no tables and therefore no concept of rows. Instead you soley provide keys [...]]]></description>
			<content:encoded><![CDATA[<p>I recently had a conversation with <a href="http://www.linkedin.com/pub/dir/lalit/sarna">Lalit Sarna</a> of <a href="http://www.oxylabs.com/">Oxylabs</a> about scalability and he introduced me to <a href="http://tokyocabinet.sourceforge.net/">Tokyo Cabinet</a>, a key/value store (database). This category of databases, referred to as DBM, differs from a RDBMS (such as MySQL) in that there are no tables and therefore no concept of rows. Instead you soley provide keys and get/set/delete values for that particular key.</p>
<p>Your advantage is lightning speed. And, apparently Tokyo Cabinet is the king of the category. We are talking speeds of 10-50x greater than MySQL. Tokyo Cabinet supports multiple underlying database engines with each providing its own advantages.</p>
<ul>
<li><strong>Hash</strong> - Hash tables provide O(1) insert and lookup which can not be beaten so they are your fastest option</li>
<li><strong>B+ Tree</strong> - The underlying data is sorted allowing for prefix and range matching. Speed is not quite as great as Hash since B-trees are O(log<sub>b</sub> n) for insert and lookup. See <a href="http://en.wikipedia.org/wiki/B%2B_tree">here</a> for more details</li>
<li><strong>Fixed Length</strong> - Your values are stored in one large array which is as fast as it gets since its O(1) and the data is concurrent. However your keys have to be natural numbers.</li>
<li><strong>Table</strong> - Attempts to replicate a traditional table database, however no fixed data schema or data types are required. Built on top of the hash db for speed.</li>
</ul>
<p><strong>Tokyo Tyrant</strong></p>
<p><img class="alignright size-full wp-image-215" title="tokyocabinet" src="http://sameerparwani.com/wp-content/uploads/2009/05/tokyocabinet.jpg" alt="" width="285" height="131" />Tokyo Tyrant is the network interface that sits on top of Tokyo Cabinet allowing your software to communicate with Tokyo Cabinet. Tokyo Cabinet is often referred to as the &#8220;database library&#8221; while Tokyo Tyrant is the &#8220;database server&#8221;. Tokyo Tyrant supports the memcached and http protocols.</p>
<p>I love that it supports memcached. That means you can plug and play with Tokyo Cabinet using the many existing memcached clients/libraries. Tokyo Cabinet is not meant to be a replacement for memcached, but you could theoretically use it as such and with minimal setup time. You would get the benefits of persistent data and much cheaper storage with some loss in performance.</p>
<p>In conclusion, targeted and appropriate use of Tokyo Cabinet would allow load to be removed from your traditional RDBMS in cases where the full functionality of a RDBMS is not needed, resulting in major performance improvements.</p>
]]></content:encoded>
			<wfw:commentRss>http://sameerparwani.com/posts/tokyo-cabinet-and-tokyo-tyrant/feed</wfw:commentRss>
		</item>
	</channel>
</rss>
