<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" media="screen" href="/~d/styles/atom10full.xsl"?><?xml-stylesheet type="text/css" media="screen" href="http://feeds.feedburner.com/~d/styles/itemcontent.css"?><feed xmlns="http://www.w3.org/2005/Atom" xmlns:openSearch="http://a9.com/-/spec/opensearch/1.1/" xmlns:georss="http://www.georss.org/georss" xmlns:gd="http://schemas.google.com/g/2005" xmlns:thr="http://purl.org/syndication/thread/1.0" xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0" gd:etag="W/&quot;CEAGR3Y8eCp7ImA9WhdTEUw.&quot;"><id>tag:blogger.com,1999:blog-5201868627988726097</id><updated>2011-07-08T01:12:06.870-07:00</updated><category term="Python" /><category term="gists github" /><category term="programming algorithm" /><category term="objects design" /><category term="closure perl &quot;functional programming&quot;" /><category term="mysql" /><category term="java" /><category term="c programming" /><category term="svn" /><category term="google &quot;realtime search&quot;" /><category term="privacy gpg" /><category term="perl &quot;linux utilities&quot; sed grep awk" /><title>pragmatism</title><subtitle type="html">I blog about some random technical stuff. Some of it is useful, others are mostly rants or trivial stuff.</subtitle><link rel="http://schemas.google.com/g/2005#feed" type="application/atom+xml" href="http://prag-matism.blogspot.com/feeds/posts/default" /><link rel="alternate" type="text/html" href="http://prag-matism.blogspot.com/" /><author><name>rahulsinner</name><uri>http://www.blogger.com/profile/10060216590711230463</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="16" height="16" src="http://img2.blogblog.com/img/b16-rounded.gif" /></author><generator version="7.00" uri="http://www.blogger.com">Blogger</generator><openSearch:totalResults>20</openSearch:totalResults><openSearch:startIndex>1</openSearch:startIndex><openSearch:itemsPerPage>25</openSearch:itemsPerPage><atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="self" type="application/atom+xml" href="http://feeds.feedburner.com/Pragmatism" /><feedburner:info uri="pragmatism" /><atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="hub" href="http://pubsubhubbub.appspot.com/" /><entry gd:etag="W/&quot;Dk4ASHs5cCp7ImA9WxBbGUo.&quot;"><id>tag:blogger.com,1999:blog-5201868627988726097.post-8898923125880069119</id><published>2010-03-18T21:58:00.000-07:00</published><updated>2010-03-18T22:02:29.528-07:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2010-03-18T22:02:29.528-07:00</app:edited><category scheme="http://www.blogger.com/atom/ns#" term="Python" /><title>Passwordless scp with Python and pexpect</title><content type="html">I have to deal with a lot of Unix/Linux/BSD boxes at work. The problem with such large number of boxes is copying my config files and .ssh directory first time I use them. Many a times, for some new work, I need to configure multiple boxes. It's quite a pain to enter passwords and pass-phrases that many times.&lt;br /&gt;
&lt;br /&gt;
So, I decided to automate this copying with python and pexpect.&lt;br /&gt;
&lt;br /&gt;
&lt;script src="http://gist.github.com/337245.js"&gt;
&lt;/script&gt;&lt;br /&gt;
&lt;br /&gt;
The script is really simple. You invoke it as follows:&lt;br /&gt;
&lt;br /&gt;
&lt;code&gt;&lt;br /&gt;
&lt;pre&gt;$ pyscp.py -s .ssh,.vimrc,.zshrc,.screenrc,.inputrc -t node1,node2,node3
&lt;/pre&gt;&lt;/code&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
It will copy .vimrc, .zshrc .. to mentioned nodes. The automation looks for prompts asking for passphrases or passwords. You just have to enter your passwords once. On all subsequent attempts, the cached passwords is reused.&lt;br /&gt;
&lt;br /&gt;
Password-less access can be configured using ssh-agents as well but this approach is generalized for all the cases - ssh not configured for public key authentication, ssh configured for keys with/without passphrase, with/without agent.The code can be cleaned further and made more elegant but it does the job.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/5201868627988726097-8898923125880069119?l=prag-matism.blogspot.com' alt='' /&gt;&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/Pragmatism/~4/X82RhQV1aME" height="1" width="1"/&gt;</content><link rel="replies" type="application/atom+xml" href="http://prag-matism.blogspot.com/feeds/8898923125880069119/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://prag-matism.blogspot.com/2010/03/passwordless-scp-with-python-and.html#comment-form" title="0 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/5201868627988726097/posts/default/8898923125880069119?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/5201868627988726097/posts/default/8898923125880069119?v=2" /><link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/Pragmatism/~3/X82RhQV1aME/passwordless-scp-with-python-and.html" title="Passwordless scp with Python and pexpect" /><author><name>rahulsinner</name><uri>http://www.blogger.com/profile/10060216590711230463</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="16" height="16" src="http://img2.blogblog.com/img/b16-rounded.gif" /></author><thr:total>0</thr:total><feedburner:origLink>http://prag-matism.blogspot.com/2010/03/passwordless-scp-with-python-and.html</feedburner:origLink></entry><entry gd:etag="W/&quot;Ak8ERngzcCp7ImA9WxBWF0U.&quot;"><id>tag:blogger.com,1999:blog-5201868627988726097.post-8760631197966025746</id><published>2010-02-09T23:54:00.000-08:00</published><updated>2010-02-10T00:00:07.688-08:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2010-02-10T00:00:07.688-08:00</app:edited><title>The beauty of node.js (Follow-up)</title><content type="html">In the &lt;a href="http://prag-matism.blogspot.com/2010/02/beauty-of-nodejs.html"&gt;last post&lt;/a&gt;, I presented a simple port forwarding solution using node.js. The solution was simple, elegant and scalable. I got a follow up from &lt;a href="http://github.com/jashkenas"&gt;Jeremy Ashkenas&lt;/a&gt; presenting a solution in &lt;a href="http://jashkenas.github.com/coffee-script/"&gt;Coffee Script.&lt;/a&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;script src="http://gist.github.com/299376.js"&gt;
&lt;/script&gt;&lt;br /&gt;
&lt;br /&gt;
I have a personal preference for terse syntax, one of my reasons for liking Python and Clojure.&lt;br /&gt;
&lt;br /&gt;
For a little background, the coffee script compiler translates the given program to javascript and then it would run on any platform where Javascript runs. I queried him about how long and how extensively he intends to support Coffee Script. I am copying his response verbatim.&lt;br /&gt;
&lt;br /&gt;
&lt;i&gt;"&lt;/i&gt;&lt;span class="Apple-style-span" style="font-family: helvetica, arial, freesans, clean, sans-serif; font-size: 13px; line-height: 19px;"&gt;&lt;i&gt;I would say that it's not very mature yet. Aspects of the language syntax have changed as recently as last week. That said, CoffeeScript compiles into vanilla JavaScript -- it's not a runtime, and it's not a binary compiler, so even if the language were to go away tomorrow, you'd still be left with a bunch of completely usable JavaScript.&lt;span class="Apple-style-span" style="font-style: normal;"&gt;&lt;i&gt;So while I'm not using it for any real-world projects, other people are, and I'm not too worried about their future viability. In the absolute worst case, they can always used the output JS, which is pretty-printed, with comments inline."&lt;/i&gt;&lt;/span&gt;&lt;/i&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span class="Apple-style-span" style="font-family: helvetica, arial, freesans, clean, sans-serif; font-size: 13px; line-height: 18px;"&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span class="Apple-style-span" style="font-family: helvetica, arial, freesans, clean, sans-serif; font-size: 13px; line-height: 18px;"&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span class="Apple-style-span" style="font-family: helvetica, arial, freesans, clean, sans-serif; font-size: 13px; line-height: 18px;"&gt;&lt;div style="line-height: 1.5em !important; margin-bottom: 1em !important; margin-left: 0px !important; margin-right: 0px !important; margin-top: 1em !important; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;&lt;span class="Apple-style-span" style="font-family: 'Times New Roman'; font-size: medium; line-height: normal;"&gt;Though it would be bad to start using Coffee Script only to find it gone some day, the risks are small. It isn't a mature project yet but it shows promise and I, for one, would start using it in personal projects.&amp;nbsp;&lt;/span&gt;&lt;/div&gt;&lt;/span&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/5201868627988726097-8760631197966025746?l=prag-matism.blogspot.com' alt='' /&gt;&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/Pragmatism/~4/I2h4BuWIK8k" height="1" width="1"/&gt;</content><link rel="replies" type="application/atom+xml" href="http://prag-matism.blogspot.com/feeds/8760631197966025746/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://prag-matism.blogspot.com/2010/02/beauty-of-nodejs-follow-up.html#comment-form" title="0 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/5201868627988726097/posts/default/8760631197966025746?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/5201868627988726097/posts/default/8760631197966025746?v=2" /><link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/Pragmatism/~3/I2h4BuWIK8k/beauty-of-nodejs-follow-up.html" title="The beauty of node.js (Follow-up)" /><author><name>rahulsinner</name><uri>http://www.blogger.com/profile/10060216590711230463</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="16" height="16" src="http://img2.blogblog.com/img/b16-rounded.gif" /></author><thr:total>0</thr:total><feedburner:origLink>http://prag-matism.blogspot.com/2010/02/beauty-of-nodejs-follow-up.html</feedburner:origLink></entry><entry gd:etag="W/&quot;Dk8AQH47fyp7ImA9WxBWGUo.&quot;"><id>tag:blogger.com,1999:blog-5201868627988726097.post-7671107631275983385</id><published>2010-02-09T07:06:00.000-08:00</published><updated>2010-02-12T03:40:41.007-08:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2010-02-12T03:40:41.007-08:00</app:edited><title>The beauty of node.js</title><content type="html">At work I came across a problem which needed ack remote file copying complete event. Most of the data comes on ftp and when the data arrives, some processing has to be done on it. The problem is to when to kick in the processing. If we do a cronjob approach, the cronjob processor can't know for sure if the new file has been pushed completely. inotify posix system call only tell you the status of the file viz. created, modified, read etc. &lt;br /&gt;
&lt;br /&gt;
EDIT: inotify does allow you to listen for the write complete events. The flag to look for is IN_CLOSE_WRITE. I kinda missed this.&lt;br /&gt;
&lt;br /&gt;
The fix we have at work is to change the ftpd itself. The ftpd writes to a unix named pipe when the transfer completer. The listener daemon is watching the named pipe for changes and kicks in the callback when a new entry is made.&lt;br /&gt;
&lt;br /&gt;
This works but has a couple of issues.&lt;br /&gt;
&lt;br /&gt;
&lt;ul&gt;&lt;li&gt;Changing the ftp daemon itself means keeping up with the ftpd changes. If a new release of ftpd is to be deployed, the patch has to be applied.&lt;/li&gt;
&lt;li&gt;The same patching needs to be done if we decide to support another service. If we decide to allow rsync, similar fix has to be done for rsync as well.&lt;/li&gt;
&lt;/ul&gt;&lt;br /&gt;
How we do at work isn't going to change because our push data is solely ftp. But given a chance to re-design it, I would simply write a port forwarding service. The port forwarding server would listen at a dummy ftp port &lt;i&gt;incoming&lt;/i&gt;. When a connection is made to port &lt;i&gt;incoming, &lt;/i&gt;it would open a client connection to real ftp server. It would forward the whole traffic and it's only job would be to trigger the callback when the ftp transfer completes.&lt;br /&gt;
&lt;br /&gt;
This solution required writing some code. Out of box port forwarding solutions don't have the customization for listening to end of transfer. So, faced with a problem, I did what everyone does - googled it. I found two acceptable solutions in Python. I prefer to code much of my stuff in Python for some obvious and not-so-obvious reasons(coming soon - topic for some other inflammatory post).&lt;br /&gt;
&lt;br /&gt;
One of the solutions was spawning threads:&lt;br /&gt;
&lt;a href="http://code.activestate.com/recipes/483730/"&gt;Port forwarding using threads&lt;/a&gt;&lt;br /&gt;
&lt;br /&gt;
And the other one was using asyncore module.&lt;br /&gt;
&lt;a href="http://code.activestate.com/recipes/483732/"&gt;Port forwarding using asyncore&lt;/a&gt;&lt;br /&gt;
&lt;br /&gt;
The solutions worked and looked good for my taste. But something seemed amiss. It looked like just too much work when all I want to do is to know when the communication completes. I looked around a while and coded this in &lt;a href="http://nodejs.org/"&gt;node.js&lt;/a&gt;.&lt;br /&gt;
&lt;br /&gt;
&lt;script src="http://gist.github.com/299258.js"&gt;
&lt;/script&gt;&lt;br /&gt;
&lt;br /&gt;
The solution works and is terse, concise and highly scalable(look at the node.js website for how and why). And above all, it's as simple as it gets.&lt;br /&gt;
&lt;br /&gt;
The code itself is pretty simple. I create a server and install callbacks for it. On connect event, I create the client for real service. Client and servers listen for &lt;i&gt;receive, &lt;/i&gt;and the callbacks just sends data from one socket to other. The server socket triggers the callback on receiving the eof. &lt;br /&gt;
&lt;br /&gt;
For the example, I am forwarding port 7000 to port 22. When we run the code and do a ssh on port 7000, the traffic is correctly forwarded to sshd running on port 22.&lt;br /&gt;
&lt;br /&gt;
&lt;code&gt;&lt;br /&gt;
&lt;pre&gt;node portfwd.js
ssh -p 7000 localhost
&lt;/pre&gt;&lt;/code&gt;&lt;br /&gt;
&lt;br /&gt;
This solution can be extended(must be extended) to function as a fully fledged port forwarder. The configurations(which port to forward where) can be passed as JSON. And the services to which we are forwarding should be made inaccessible to the outer world. For example, sshd can be configured to listen only to loopback address. This is important otherwise we run the risk of circumventing port forwarding and not running the callback.&lt;br /&gt;
&lt;br /&gt;
There is much that can be done in node.js. This is just the tip of the iceberg. Looks like Javascript is really on the rise.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/5201868627988726097-7671107631275983385?l=prag-matism.blogspot.com' alt='' /&gt;&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/Pragmatism/~4/IT_C-XNGwvM" height="1" width="1"/&gt;</content><link rel="replies" type="application/atom+xml" href="http://prag-matism.blogspot.com/feeds/7671107631275983385/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://prag-matism.blogspot.com/2010/02/beauty-of-nodejs.html#comment-form" title="5 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/5201868627988726097/posts/default/7671107631275983385?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/5201868627988726097/posts/default/7671107631275983385?v=2" /><link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/Pragmatism/~3/IT_C-XNGwvM/beauty-of-nodejs.html" title="The beauty of node.js" /><author><name>rahulsinner</name><uri>http://www.blogger.com/profile/10060216590711230463</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="16" height="16" src="http://img2.blogblog.com/img/b16-rounded.gif" /></author><thr:total>5</thr:total><feedburner:origLink>http://prag-matism.blogspot.com/2010/02/beauty-of-nodejs.html</feedburner:origLink></entry><entry gd:etag="W/&quot;AkcFRnk7eip7ImA9WxBWFUk.&quot;"><id>tag:blogger.com,1999:blog-5201868627988726097.post-1674999977645043367</id><published>2010-02-06T22:38:00.000-08:00</published><updated>2010-02-07T05:06:57.702-08:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2010-02-07T05:06:57.702-08:00</app:edited><category scheme="http://www.blogger.com/atom/ns#" term="gists github" /><title>Sharing code with github</title><content type="html">I have been using pastebin to share code for a long time. While reading through a couple of blogs, I saw that github gists allow to share code in a prettier manner. &lt;br /&gt;
&lt;br /&gt;
&lt;div&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div&gt;So, I decided to solve a small problem to test the github gists. The problem I am solving is to find the equidex. Equidex can be best explained by an example. Consider the given array:&lt;/div&gt;&lt;div&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div&gt;&lt;pre&gt;&lt;code&gt;{-7, 1, 5, 2, -4, 3, 0}&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div&gt;Here, index 3 is an equidex as the sum of the elements before it (-7 + 1 + 5) is the same as sum of the elements after it (-4 + 3 + 0). &lt;/div&gt;&lt;div&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div&gt;Here is the code to solve it.&lt;/div&gt;&lt;br /&gt;
&lt;script src="http://gist.github.com/297257.js"&gt;&lt;/script&gt;&lt;br /&gt;
&lt;div&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/5201868627988726097-1674999977645043367?l=prag-matism.blogspot.com' alt='' /&gt;&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/Pragmatism/~4/j3VDqZwbxlY" height="1" width="1"/&gt;</content><link rel="replies" type="application/atom+xml" href="http://prag-matism.blogspot.com/feeds/1674999977645043367/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://prag-matism.blogspot.com/2010/02/sharing-code-with-github.html#comment-form" title="0 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/5201868627988726097/posts/default/1674999977645043367?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/5201868627988726097/posts/default/1674999977645043367?v=2" /><link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/Pragmatism/~3/j3VDqZwbxlY/sharing-code-with-github.html" title="Sharing code with github" /><author><name>rahulsinner</name><uri>http://www.blogger.com/profile/10060216590711230463</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="16" height="16" src="http://img2.blogblog.com/img/b16-rounded.gif" /></author><thr:total>0</thr:total><feedburner:origLink>http://prag-matism.blogspot.com/2010/02/sharing-code-with-github.html</feedburner:origLink></entry><entry gd:etag="W/&quot;AkcDQn0zeip7ImA9WxBWFUk.&quot;"><id>tag:blogger.com,1999:blog-5201868627988726097.post-8443407376000058662</id><published>2009-12-07T12:05:00.000-08:00</published><updated>2010-02-07T05:07:53.382-08:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2010-02-07T05:07:53.382-08:00</app:edited><category scheme="http://www.blogger.com/atom/ns#" term="closure perl &quot;functional programming&quot;" /><title>Objects: Functional programming style</title><content type="html">Functional languages which don't have build in objects support can implement object oriented programming using closures. Closures help implement encapsulation and the inheritance can be achieved using composition. The members can be maintained as members in hash ref. The hash ref will be closed by a closure so that external modification can't be done directly on it. Here is a sample code implementing encapsulation, inheritance and polymorphism.&lt;br /&gt;
&lt;script src="http://gist.github.com/297278.js"&gt;&lt;/script&gt;&lt;br /&gt;
Employee class extends person. It uses composition to implement inheritance. &lt;br /&gt;
&lt;br /&gt;
The dispatch subroutine is the helper for implementing inheritance and polymorphism. You can see that this trivial code implements many important OOP concepts.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/5201868627988726097-8443407376000058662?l=prag-matism.blogspot.com' alt='' /&gt;&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/Pragmatism/~4/Te9cxxkiSxs" height="1" width="1"/&gt;</content><link rel="replies" type="application/atom+xml" href="http://prag-matism.blogspot.com/feeds/8443407376000058662/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://prag-matism.blogspot.com/2009/12/objects-functional-programming-style.html#comment-form" title="0 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/5201868627988726097/posts/default/8443407376000058662?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/5201868627988726097/posts/default/8443407376000058662?v=2" /><link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/Pragmatism/~3/Te9cxxkiSxs/objects-functional-programming-style.html" title="Objects: Functional programming style" /><author><name>rahulsinner</name><uri>http://www.blogger.com/profile/10060216590711230463</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="16" height="16" src="http://img2.blogblog.com/img/b16-rounded.gif" /></author><thr:total>0</thr:total><feedburner:origLink>http://prag-matism.blogspot.com/2009/12/objects-functional-programming-style.html</feedburner:origLink></entry><entry gd:etag="W/&quot;AkYESXs-fip7ImA9WxBWFUk.&quot;"><id>tag:blogger.com,1999:blog-5201868627988726097.post-2304169919851417707</id><published>2009-11-17T00:29:00.000-08:00</published><updated>2010-02-07T05:08:28.556-08:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2010-02-07T05:08:28.556-08:00</app:edited><category scheme="http://www.blogger.com/atom/ns#" term="c programming" /><title>K&amp;R - timeless classic</title><content type="html">There is a recent(well, may be not too recent) trend geared towards becoming overnight programmers. Peter Norvig has &lt;a href="http://norvig.com/21-days.html"&gt;commented&lt;/a&gt; about that. &lt;br /&gt;
&lt;br /&gt;
Viewing a couple of discussions on the web, I see people saying that K&amp;R is hard to follow and they would prefer something with more examples and elaborations. I would drop any iota of diplomacy here and would say if you can't follow K&amp;R, you are hopeless and either continue to be a monkey coder or find an alternate career.&lt;br /&gt;
&lt;br /&gt;
K&amp;R is the ultimate example of concise, complete, practical technical writing. I believe it is one of the primary reasons behind C's popularity. Everytime I re-read the book, I learn something new. And I am not the only one who feels that way. The coverage is comprehensive and explanations are lucid. The specs at the back used to be the standard C specs for the first 10 years or so. The examples are real world complete programs. Exercises force you to stretch your mental abilities. Compared to it, the other C texts look like a joke(with a couple of exceptions). &lt;br /&gt;
&lt;br /&gt;
I am seeing a rise in head-first series now-a-days. I have read a couple of them and I did find "Head First Java" enjoyable. But again, it's suited only to the beginners and someone with some programming experience won't be too happy with it.  &lt;br /&gt;
&lt;br /&gt;
K&amp;R is something I am religious about and would protect it to death. If you believe I am being over zealous here, read the book cover to cover and try the exercises. If it doesn't give you a better understanding of C and programming as a whole, you really don't have the aptitude for programming.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/5201868627988726097-2304169919851417707?l=prag-matism.blogspot.com' alt='' /&gt;&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/Pragmatism/~4/PW5s5K--Jcs" height="1" width="1"/&gt;</content><link rel="replies" type="application/atom+xml" href="http://prag-matism.blogspot.com/feeds/2304169919851417707/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://prag-matism.blogspot.com/2009/11/k-timeless-classic.html#comment-form" title="0 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/5201868627988726097/posts/default/2304169919851417707?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/5201868627988726097/posts/default/2304169919851417707?v=2" /><link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/Pragmatism/~3/PW5s5K--Jcs/k-timeless-classic.html" title="K&amp;R - timeless classic" /><author><name>rahulsinner</name><uri>http://www.blogger.com/profile/10060216590711230463</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="16" height="16" src="http://img2.blogblog.com/img/b16-rounded.gif" /></author><thr:total>0</thr:total><feedburner:origLink>http://prag-matism.blogspot.com/2009/11/k-timeless-classic.html</feedburner:origLink></entry><entry gd:etag="W/&quot;AkYGR3Y-eip7ImA9WxBWFUk.&quot;"><id>tag:blogger.com,1999:blog-5201868627988726097.post-6046525585741245277</id><published>2009-08-09T08:17:00.000-07:00</published><updated>2010-02-07T05:08:46.852-08:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2010-02-07T05:08:46.852-08:00</app:edited><category scheme="http://www.blogger.com/atom/ns#" term="perl &quot;linux utilities&quot; sed grep awk" /><title>Standard *nix utilities using Perl</title><content type="html">Lately, I have been working on Perl and though I am not a big fan of the language, it does come handy for a variety of tasks. One of the reasons I like to keep in touch with Perl is it can very easily be used as substitutes for various *nix utilities viz. grep, sed, awk, find etc.&lt;br /&gt;
&lt;br /&gt;
But why would someone want to do that when you already have the standard utilities? Well, you might be used to do everything in Perl. Or you might be on windows where the shell utilities are either non-existing or leave much to be desired. MS does claim to change that with PowerShell but I haven't looked at it yet.&lt;br /&gt;
&lt;br /&gt;
So, enough talk. Here goes my short list of handy Perl one liners.&lt;br /&gt;
&lt;br /&gt;
1) grep&lt;br /&gt;
grep is one handy little utility which does a full text search on the specified files. &lt;br /&gt;
&lt;br /&gt;
$ grep -n 'main' *.c&lt;br /&gt;
formatting.c:3:int main(void)&lt;br /&gt;
hello.c:4:int main(void)&lt;br /&gt;
&lt;br /&gt;
It can be done with a Perl one liner by using regex. &lt;br /&gt;
&lt;br /&gt;
$ perl -ne 'print "$ARGV: $.: $_" if /main/; close ARGV if eof;' *.c&lt;br /&gt;
formatting.c: 3: int main(void)&lt;br /&gt;
hello.c: 4: int main(void)&lt;br /&gt;
&lt;br /&gt;
2) sed&lt;br /&gt;
'sed', the venerated stream editor, is mainly used for substitutions.&lt;br /&gt;
$ echo "Hello world" | sed -e 's/Hello/Hi/'&lt;br /&gt;
Hi world&lt;br /&gt;
&lt;br /&gt;
The perl equivalent for the above would be:&lt;br /&gt;
$ echo "Hello world" | perl -pe 's#Hello#Hi#'&lt;br /&gt;
Hi world&lt;br /&gt;
&lt;br /&gt;
Neat, huh! &lt;br /&gt;
&lt;br /&gt;
3) awk&lt;br /&gt;
awk is generally used for parsing delimited input. This is what we would do to get the username field from /etc/passwd. &lt;br /&gt;
awk -F: '{print $1;}' /etc/passwd&lt;br /&gt;
&lt;br /&gt;
The perl one liner for this one would be:&lt;br /&gt;
perl -F: -lane 'print $F[0];' /etc/passwd&lt;br /&gt;
&lt;br /&gt;
4) find&lt;br /&gt;
find is trickiest of them all. There isn't a shortcut available for it. Though the standard module File::Find comes  handy.&lt;br /&gt;
&lt;br /&gt;
To find and print all "*.c" files under directory "/usr/src", the find command would be:&lt;br /&gt;
find /usr/src -name '*.c' -print&lt;br /&gt;
&lt;br /&gt;
The Perl equivalent is:&lt;br /&gt;
$ perl -MFile::Find -e 'find(sub {print "$File::Find::name\n" if /\.c$/;}, "/usr/src")'&lt;br /&gt;
&lt;br /&gt;
There are other utilities which can be done using Perl viz. sort, cut etc. But these 4 are the ones that I miss the most when I am not on *nix.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/5201868627988726097-6046525585741245277?l=prag-matism.blogspot.com' alt='' /&gt;&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/Pragmatism/~4/E6uaA1DRz7I" height="1" width="1"/&gt;</content><link rel="replies" type="application/atom+xml" href="http://prag-matism.blogspot.com/feeds/6046525585741245277/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://prag-matism.blogspot.com/2009/08/lately-i-have-been-working-on-perl-and.html#comment-form" title="1 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/5201868627988726097/posts/default/6046525585741245277?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/5201868627988726097/posts/default/6046525585741245277?v=2" /><link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/Pragmatism/~3/E6uaA1DRz7I/lately-i-have-been-working-on-perl-and.html" title="Standard *nix utilities using Perl" /><author><name>rahulsinner</name><uri>http://www.blogger.com/profile/10060216590711230463</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="16" height="16" src="http://img2.blogblog.com/img/b16-rounded.gif" /></author><thr:total>1</thr:total><feedburner:origLink>http://prag-matism.blogspot.com/2009/08/lately-i-have-been-working-on-perl-and.html</feedburner:origLink></entry><entry gd:etag="W/&quot;D0UGRHYzcCp7ImA9WxBbGUo.&quot;"><id>tag:blogger.com,1999:blog-5201868627988726097.post-1474908608601766824</id><published>2009-04-20T22:57:00.000-07:00</published><updated>2010-03-18T22:07:05.888-07:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2010-03-18T22:07:05.888-07:00</app:edited><category scheme="http://www.blogger.com/atom/ns#" term="svn" /><title>svn - diff between working copy and checked out copy</title><content type="html">&lt;code&gt;&lt;pre&gt;svn diff fileName &lt;/pre&gt;&lt;/code&gt;&lt;br /&gt;
&lt;br /&gt;
Pretty trivial. But the problem is the unified diff output is not very readable. And if the differences are large, it makes it practically impossible to follow through(which actually won't happen if you check-in often and keep your working copy updated). &lt;br /&gt;
&lt;br /&gt;
EDIT: A simple way to do it would to do:&lt;br /&gt;
&lt;code&gt;&lt;pre&gt;vimdiff &lt;(svn cat "$1") $1
&lt;/pre&gt;
&lt;/code&gt;

where $1 is the name of the file.

Back to the point. I would prefer the diff between the working copy and checked out copy presented in a visually comparable manner. I am not much into UI oriented programs. For me, most of the times, it isn't a feasible option as I work on remote machines. 

So, here goes a trivial script without error checking which allows me to visually compare working copy and checked out copy.

&lt;code&gt;&lt;pre&gt;#!/bin/bash
name="$@"
svn="/path/to/svn"
# Backup working copy, revert to checked out copy, 
# do the diff and restore working copy
mv "${name}" "${name}.bak"
${svn} revert "${name}"
mv "${name}" "${name}.orig"
mv "${name}.bak" "${name}"
vim -d "${name}" "${name}.orig"
rm "${name}.orig"
exit 0
&lt;/pre&gt;&lt;/code&gt;
&lt;br /&gt;
&lt;br /&gt;
The script is trivial and self-explanatory. "vim -d" might be unfamiliar. It opens vim vertically split in two panes showing the two files highlighting the differences. Additions, deletions and modifications are color-coded making it easy for analysis. I find the diff mode in vim very useful for manual merging. I use it all the time to resolve conflicts during check-in. &lt;br /&gt;
&lt;br /&gt;
Here is a quick refresher on vidiff:&lt;br /&gt;
]c - next difference&lt;br /&gt;
[c - previous difference&lt;br /&gt;
dp - put differences to other file&lt;br /&gt;
dg - get differences from other file&lt;br /&gt;
&lt;br /&gt;
You will be surprised that how much is accomplished from these 4 commands.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/5201868627988726097-1474908608601766824?l=prag-matism.blogspot.com' alt='' /&gt;&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/Pragmatism/~4/QsG4SBGanUQ" height="1" width="1"/&gt;</content><link rel="replies" type="application/atom+xml" href="http://prag-matism.blogspot.com/feeds/1474908608601766824/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://prag-matism.blogspot.com/2009/04/svn-diff-between-working-copy-and.html#comment-form" title="1 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/5201868627988726097/posts/default/1474908608601766824?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/5201868627988726097/posts/default/1474908608601766824?v=2" /><link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/Pragmatism/~3/QsG4SBGanUQ/svn-diff-between-working-copy-and.html" title="svn - diff between working copy and checked out copy" /><author><name>rahulsinner</name><uri>http://www.blogger.com/profile/10060216590711230463</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="16" height="16" src="http://img2.blogblog.com/img/b16-rounded.gif" /></author><thr:total>1</thr:total><feedburner:origLink>http://prag-matism.blogspot.com/2009/04/svn-diff-between-working-copy-and.html</feedburner:origLink></entry><entry gd:etag="W/&quot;AkUMSHo4fip7ImA9WxBWFUk.&quot;"><id>tag:blogger.com,1999:blog-5201868627988726097.post-236912383716123990</id><published>2009-04-20T04:22:00.000-07:00</published><updated>2010-02-07T05:11:29.436-08:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2010-02-07T05:11:29.436-08:00</app:edited><category scheme="http://www.blogger.com/atom/ns#" term="privacy gpg" /><title>Privacy using public key cryptography</title><content type="html">Incompetent governments around the world are coming out with oppressive measures to curb privacy of citizens in the name of controlling terror. Let's face it.  How is being terrorized by the government different from terrorized by some other motherfucking moron? Secrecy is different from privacy. I won't enjoy even my daily joke forwards being forcefully read by government agencies.&lt;br /&gt;
&lt;br /&gt;
The recent regulations require email hosting services to have their servers in India to bring them under Indian jurisdiction. God knows what purpose it can possibly server. At best, the details that were being submitted while creating the a/c can be extracted. And though the terrorists are a bunch of idiots, still, going by the best practices, they don't fill in their correct details.  The mails are encrypted and government agencies neither have the know-how nor the resources to attempt a decryption. Theoretically every encryption algorithm is breakable and most of them depend on no efficient algorithm for factoring a large number into prime factors. Practically, anything beyond 64 bit would take like eternity to brute force. No known deterministic polynomial time algorithm exists. &lt;br /&gt;
&lt;br /&gt;
Being lawful citizens, there isn't much we can do about mail servers moved to India and forced to divulge details available with them but we sure can encrypt our mails.&lt;br /&gt;
&lt;br /&gt;
&lt;span style="font-weight: bold;"&gt;Some background&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;span style="font-weight: bold;"&gt;Secret/Private/Symmetric key cryptography&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
The same key (a kind of password which isn't limited to numbers, alphabets and special characters but can consist of any arbitrary byte sequence) is used for encryption and decryption. &lt;br /&gt;
&lt;br /&gt;
It is kinda weak as you need to share your key with the other end. Further, if anyone gets hold of the key, he not only can decrypt the files but can send encrypted files which would appear to come from the other end(based on the belief that only the other end has got the key).&lt;br /&gt;
&lt;br /&gt;
&lt;span style="font-weight: bold;"&gt;Public key cryptography&lt;/span&gt;&lt;br /&gt;
Everyone has a public and private key. Public key is published on a known forum and private key is just what it says - private. If user A wants to send an encrypted message to user B, he will encrypt it with B's public key which is accessible to anyone. A document encrypted with B's public key can only be decrypted by B's private key and vice-versa. This "vice-versa" has an interesting application - digital signatures. Not getting too involved, digital signatures are a way of testifying your identity. And unlike actual signatures, they can't be forged unless you loose your private key.&lt;br /&gt;
&lt;br /&gt;
Enough theory. Let's see how this can be done using GnuPG(GNU Privacy Guard).&lt;br /&gt;
&lt;br /&gt;
&lt;span style="font-weight: bold;"&gt;* Symmetric encryption&lt;/span&gt;&lt;br /&gt;
&lt;code&gt;&lt;br /&gt;
&lt;pre&gt;1 gpg -c filename
2 gpg -c -a filename
&lt;/pre&gt;&lt;br /&gt;
&lt;/code&gt;&lt;br /&gt;
&lt;br /&gt;
Both 1 and 2 will prompt for a password and then go on with encrypting the file.  1 produces binary output while 2 produces base-64 encoded output.&lt;br /&gt;
&lt;br /&gt;
Decryption is simple&lt;br /&gt;
&lt;code&gt;&lt;br /&gt;
&lt;pre&gt;gpg encryptedFile
&lt;/pre&gt;&lt;br /&gt;
&lt;/code&gt;&lt;br /&gt;
This will de-crypt the file in-place after prompting for and verifying the password.&lt;br /&gt;
&lt;br /&gt;
&lt;span style="font-weight:bold;"&gt;Key generation for public key encryption&lt;/span&gt;&lt;br /&gt;
The keys are generated using:&lt;br /&gt;
&lt;code&gt;&lt;br /&gt;
&lt;pre&gt;gpg --gen-key
&lt;/pre&gt;&lt;br /&gt;
&lt;/code&gt;&lt;br /&gt;
It will ask you a couple of questions. Defaults are OK if you don't understand them. A public/private key will be created after successful execution of this command.&lt;br /&gt;
&lt;span style="font-weight:bold;"&gt;&lt;br /&gt;
Sharing public key&lt;/span&gt;&lt;br /&gt;
After generation of keys, you need to share it with others. For that, either you can export your keys and physically pass it along or you can upload it to any of the key servers. I have mine on pgp.mit.edu and it propagates it to a number of other keyservers. Here is how you export a key and upload it to a key server:&lt;br /&gt;
&lt;code&gt;&lt;br /&gt;
&lt;pre&gt;Exporting key:
gpg --export key_name
Sending keys to server:
gpg --keyserver serverName --send-keys keyID
Key id is obtained by seeing the listing:
gpg --list-keys
&lt;/pre&gt;&lt;br /&gt;
&lt;/code&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;span style="font-weight:bold;"&gt;&lt;br /&gt;
Importing keys&lt;/span&gt;&lt;br /&gt;
That is about giving your keys to others. When you receive keys from someone else, you need to import the keys in your keyring.&lt;br /&gt;
&lt;code&gt;&lt;br /&gt;
&lt;pre&gt;Receiving keys from a key-server:
gpg --keyserver serverName --recv-keys keyID
Importing physically received keys:
A "gpg keyfile" would import the keyfile into your keyring. But that's not it. Further steps are required.

First verify the key fingerprint:
gpg --fingerprint keyID
The fingerprint is the unique identifier for the key. People post it on the web, pass it on disks, verify over phone etc. 
Once the fingerprint has been verified, you can set the key as trusted.
gpg --edit-key keyID
command&gt;trust
&lt;/pre&gt;&lt;br /&gt;
&lt;/code&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;span style="font-weight:bold;"&gt;Grand finale&lt;/span&gt;&lt;br /&gt;
Phew! That sure was a lot of work and a lot of pain. But now you are done. Say now you need to send a file to A, all you need to do is &lt;br /&gt;
&lt;code&gt;&lt;br /&gt;
&lt;pre&gt;gpg -s -e -r keyIdForA file
&lt;/pre&gt;&lt;br /&gt;
&lt;/code&gt;&lt;br /&gt;
This will:&lt;br /&gt;
1) encrypt your file in such a way that only A can decrypt it&lt;br /&gt;
2) Sign your file so that when it reaches A, A know that you have sent it. It also verifies that the file hasn't changed in the transit.&lt;br /&gt;
&lt;br /&gt;
When A receives the file, all he has to do is:&lt;br /&gt;
&lt;code&gt;&lt;br /&gt;
&lt;pre&gt;gpg encryptedFile
&lt;/pre&gt;&lt;br /&gt;
&lt;/code&gt;&lt;br /&gt;
Since the file was encrypted for him and assuming he has a default private key configured and your public keys in his keyring, this command will verify and decrypt the file.&lt;br /&gt;
&lt;br /&gt;
Now go and enjoy your new-found privacy. Don't forget to spread word about importance of privacy. &lt;br /&gt;
&lt;br /&gt;
What? You use M$ Windows? Get a life. Just kidding :-). Google for a windows version of GnuPG. Basically, any pgp or openssl or equivalent implementation will do the trick.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/5201868627988726097-236912383716123990?l=prag-matism.blogspot.com' alt='' /&gt;&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/Pragmatism/~4/s8078BiQlSw" height="1" width="1"/&gt;</content><link rel="replies" type="application/atom+xml" href="http://prag-matism.blogspot.com/feeds/236912383716123990/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://prag-matism.blogspot.com/2009/04/privacy-using-public-key-cryptography.html#comment-form" title="0 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/5201868627988726097/posts/default/236912383716123990?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/5201868627988726097/posts/default/236912383716123990?v=2" /><link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/Pragmatism/~3/s8078BiQlSw/privacy-using-public-key-cryptography.html" title="Privacy using public key cryptography" /><author><name>rahulsinner</name><uri>http://www.blogger.com/profile/10060216590711230463</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="16" height="16" src="http://img2.blogblog.com/img/b16-rounded.gif" /></author><thr:total>0</thr:total><feedburner:origLink>http://prag-matism.blogspot.com/2009/04/privacy-using-public-key-cryptography.html</feedburner:origLink></entry><entry gd:etag="W/&quot;AkUNRHs-fip7ImA9WxBWFUk.&quot;"><id>tag:blogger.com,1999:blog-5201868627988726097.post-338819210051594051</id><published>2009-02-06T21:03:00.000-08:00</published><updated>2010-02-07T05:11:35.556-08:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2010-02-07T05:11:35.556-08:00</app:edited><category scheme="http://www.blogger.com/atom/ns#" term="java" /><title>"private" in Java</title><content type="html">Some people take the concept of "private", "public" in OOP a little too seriously. That is meant to be a logical concept and that most certainly does not mean that private members can't be accessed. The debugger does that all the time. If "private" members were so inaccessible, then even the debugger shoudn't have  been able to dump their values since the debugger is external to class being debugged. Reflection can be used to access the private members of a class. It depends on SecurityManager installed. The default SecurityManger allows accessing private members using reflection.&lt;br /&gt;
&lt;br /&gt;
&lt;script src="http://gist.github.com/297280.js"&gt;&lt;/script&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The reflection API does have methods for invoking private methods as well. These techniques are internally used by the debugger and can be used for debugging purposes.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/5201868627988726097-338819210051594051?l=prag-matism.blogspot.com' alt='' /&gt;&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/Pragmatism/~4/Vvl3VolVWvo" height="1" width="1"/&gt;</content><link rel="replies" type="application/atom+xml" href="http://prag-matism.blogspot.com/feeds/338819210051594051/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://prag-matism.blogspot.com/2009/02/private-in-java.html#comment-form" title="0 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/5201868627988726097/posts/default/338819210051594051?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/5201868627988726097/posts/default/338819210051594051?v=2" /><link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/Pragmatism/~3/Vvl3VolVWvo/private-in-java.html" title="&quot;private&quot; in Java" /><author><name>rahulsinner</name><uri>http://www.blogger.com/profile/10060216590711230463</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="16" height="16" src="http://img2.blogblog.com/img/b16-rounded.gif" /></author><thr:total>0</thr:total><feedburner:origLink>http://prag-matism.blogspot.com/2009/02/private-in-java.html</feedburner:origLink></entry><entry gd:etag="W/&quot;AkQEQHwzcSp7ImA9WxBWFUk.&quot;"><id>tag:blogger.com,1999:blog-5201868627988726097.post-8156488441490184360</id><published>2009-02-06T20:51:00.000-08:00</published><updated>2010-02-07T05:11:41.289-08:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2010-02-07T05:11:41.289-08:00</app:edited><category scheme="http://www.blogger.com/atom/ns#" term="java" /><title>Running a Java program without a main</title><content type="html">Java allows static initialization blocks which run before the class is loaded. Since the main method is contained in a class in Java, the static initialization block runs before main. Though running code through static initialization block would output a message about "main not found". This message can be avoided by ending the static initialization block with a System.exit(0).&lt;br /&gt;
&lt;br /&gt;
&lt;script src="http://gist.github.com/297281.js"&gt;&lt;/script&gt;&lt;br /&gt;
&lt;br /&gt;
The first Hello.java is the traditional "Hello World" program. Hello2.java prints "Hello World" without a main but the spits an error message about "main not found". Hello3.java print "Hello World" and then exits thus avoiding the error messsage.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/5201868627988726097-8156488441490184360?l=prag-matism.blogspot.com' alt='' /&gt;&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/Pragmatism/~4/N9ygoE2KhCc" height="1" width="1"/&gt;</content><link rel="replies" type="application/atom+xml" href="http://prag-matism.blogspot.com/feeds/8156488441490184360/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://prag-matism.blogspot.com/2009/02/running-java-program-without-main.html#comment-form" title="0 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/5201868627988726097/posts/default/8156488441490184360?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/5201868627988726097/posts/default/8156488441490184360?v=2" /><link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/Pragmatism/~3/N9ygoE2KhCc/running-java-program-without-main.html" title="Running a Java program without a main" /><author><name>rahulsinner</name><uri>http://www.blogger.com/profile/10060216590711230463</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="16" height="16" src="http://img2.blogblog.com/img/b16-rounded.gif" /></author><thr:total>0</thr:total><feedburner:origLink>http://prag-matism.blogspot.com/2009/02/running-java-program-without-main.html</feedburner:origLink></entry><entry gd:etag="W/&quot;AkQFQXY_cSp7ImA9WxBWFUk.&quot;"><id>tag:blogger.com,1999:blog-5201868627988726097.post-8210241749189719942</id><published>2009-02-06T09:40:00.000-08:00</published><updated>2010-02-07T05:11:50.849-08:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2010-02-07T05:11:50.849-08:00</app:edited><category scheme="http://www.blogger.com/atom/ns#" term="programming algorithm" /><title>Detecting cycle in a linked list</title><content type="html">Finding cycle in a linked list in a single pass is an often asked question. The first  approach that people tend to do is to store the seen node pointers into an array. And everytime a new element is encountered, it is searched in the seen array. But that's very inefficient. An auxiliary array of size n is required. Further, for each element, a search into the array is to be made followed by the insertion.&lt;br /&gt;
&lt;br /&gt;
This isn't actually required. A cycle in a link list can be detected by taking a fast and slow pointer. Everytime a slow pointer advances by one, fast advances by two. If at any point of time, fast becomes equal to slow, that indicates a cycle in the list.&lt;br /&gt;
&lt;br /&gt;
&lt;script src="http://gist.github.com/297283.js"&gt;&lt;/script&gt;&lt;br /&gt;
Feel free to experiment that this work for all sort of cycles. This code returns 1 when passed a single element list.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/5201868627988726097-8210241749189719942?l=prag-matism.blogspot.com' alt='' /&gt;&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/Pragmatism/~4/-P1KRsa-E8Y" height="1" width="1"/&gt;</content><link rel="replies" type="application/atom+xml" href="http://prag-matism.blogspot.com/feeds/8210241749189719942/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://prag-matism.blogspot.com/2009/02/detecting-cycle-in-linked-list.html#comment-form" title="0 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/5201868627988726097/posts/default/8210241749189719942?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/5201868627988726097/posts/default/8210241749189719942?v=2" /><link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/Pragmatism/~3/-P1KRsa-E8Y/detecting-cycle-in-linked-list.html" title="Detecting cycle in a linked list" /><author><name>rahulsinner</name><uri>http://www.blogger.com/profile/10060216590711230463</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="16" height="16" src="http://img2.blogblog.com/img/b16-rounded.gif" /></author><thr:total>0</thr:total><feedburner:origLink>http://prag-matism.blogspot.com/2009/02/detecting-cycle-in-linked-list.html</feedburner:origLink></entry><entry gd:etag="W/&quot;AkQBRX89fyp7ImA9WxBWFUk.&quot;"><id>tag:blogger.com,1999:blog-5201868627988726097.post-1965057266071511446</id><published>2009-02-05T08:42:00.000-08:00</published><updated>2010-02-07T05:12:34.167-08:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2010-02-07T05:12:34.167-08:00</app:edited><category scheme="http://www.blogger.com/atom/ns#" term="programming algorithm" /><title>Finding common ancestor of two given nodes in a binary tree</title><content type="html">Given two nodes n1 and n2, the problem is to find the common ancestor. The common ancestor depends on the relation between two nodes and the root.&lt;br /&gt;
&lt;br /&gt;
1) If n1 is less than root and n2 is greater than root, then root is the common ancestor for the nodes. &lt;br /&gt;
&lt;br /&gt;
2) If both nodes are smaller(or greater) than root, then call the algorithm recursively.&lt;br /&gt;
&lt;br /&gt;
Recursively applying this algorithm to the tree will give the common ancestor.&lt;br /&gt;
&lt;br /&gt;
&lt;script src="http://gist.github.com/297285.js"&gt;&lt;/script&gt;&lt;br /&gt;
If the two nodes lie on two sides of the ancestor, then the algorithm is straight forward. The special if conditional is required when the two nodes fall in the same linear path. In that case, the parent of the smaller (or larger) of two will be the common ancestor. Since there is no way to get to a node's parent, we pass the parent as a function parameter.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/5201868627988726097-1965057266071511446?l=prag-matism.blogspot.com' alt='' /&gt;&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/Pragmatism/~4/N6R569apezk" height="1" width="1"/&gt;</content><link rel="replies" type="application/atom+xml" href="http://prag-matism.blogspot.com/feeds/1965057266071511446/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://prag-matism.blogspot.com/2009/02/finding-common-ancestor-of-two-given.html#comment-form" title="0 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/5201868627988726097/posts/default/1965057266071511446?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/5201868627988726097/posts/default/1965057266071511446?v=2" /><link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/Pragmatism/~3/N6R569apezk/finding-common-ancestor-of-two-given.html" title="Finding common ancestor of two given nodes in a binary tree" /><author><name>rahulsinner</name><uri>http://www.blogger.com/profile/10060216590711230463</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="16" height="16" src="http://img2.blogblog.com/img/b16-rounded.gif" /></author><thr:total>0</thr:total><feedburner:origLink>http://prag-matism.blogspot.com/2009/02/finding-common-ancestor-of-two-given.html</feedburner:origLink></entry><entry gd:etag="W/&quot;AkQCRn06fyp7ImA9WxBWFUk.&quot;"><id>tag:blogger.com,1999:blog-5201868627988726097.post-9056406825485603440</id><published>2009-02-04T20:09:00.000-08:00</published><updated>2010-02-07T05:12:47.317-08:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2010-02-07T05:12:47.317-08:00</app:edited><category scheme="http://www.blogger.com/atom/ns#" term="google &quot;realtime search&quot;" /><title>Google's real time search</title><content type="html">I am posting this on 9:43 AM IST. The sole purpose of this post is to test how quickly it starts appearing in search results. I have heard that it takes less than 10 seconds. Lets put the theory to test. I have got another google search page open with "rahulsinner google's real time search" as the search query.&lt;br /&gt;
&lt;br /&gt;
/* Checked the search results */&lt;br /&gt;
So it turned out to be one of the urban legends. I didn't get anything related. But it was obvious from the beginning itself. Google's search to results latency is very small. What I mean is the lapse when Google's crawler hits a page and it starts appearing in results is very small. Google must be having some real time clusters which don't use a static db but are fed data on the run.&lt;br /&gt;
&lt;br /&gt;
/* Checking back after 2 hours */&lt;br /&gt;
I don't know when it appeared on the search. Its 11.51 am. That is about 2 hours(or lesser - I checked the results just now).&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/5201868627988726097-9056406825485603440?l=prag-matism.blogspot.com' alt='' /&gt;&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/Pragmatism/~4/LYHdYhi-4ro" height="1" width="1"/&gt;</content><link rel="replies" type="application/atom+xml" href="http://prag-matism.blogspot.com/feeds/9056406825485603440/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://prag-matism.blogspot.com/2009/02/googles-real-time-search.html#comment-form" title="0 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/5201868627988726097/posts/default/9056406825485603440?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/5201868627988726097/posts/default/9056406825485603440?v=2" /><link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/Pragmatism/~3/LYHdYhi-4ro/googles-real-time-search.html" title="Google's real time search" /><author><name>rahulsinner</name><uri>http://www.blogger.com/profile/10060216590711230463</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="16" height="16" src="http://img2.blogblog.com/img/b16-rounded.gif" /></author><thr:total>0</thr:total><feedburner:origLink>http://prag-matism.blogspot.com/2009/02/googles-real-time-search.html</feedburner:origLink></entry><entry gd:etag="W/&quot;AkQDSH09eyp7ImA9WxBWFUk.&quot;"><id>tag:blogger.com,1999:blog-5201868627988726097.post-2681936410953425987</id><published>2009-02-03T05:47:00.000-08:00</published><updated>2010-02-07T05:12:59.363-08:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2010-02-07T05:12:59.363-08:00</app:edited><category scheme="http://www.blogger.com/atom/ns#" term="objects design" /><title>Object oriented desing principles</title><content type="html">All complex talks and terms regarding object oriented analysis and design notwithstanding, there are only a couple of commonly used principles that if used wisely, can lead to extensible code. &lt;br /&gt;
&lt;br /&gt;
&lt;span style="font-weight:bold;"&gt;Open/Closed principle&lt;/span&gt;&lt;br /&gt;
This principle says that the classes should be designed such that they are open for extension and closed for modification. Various other principles viz. access controOpen/Closed principlel - public, private, protected and programming to interface are a direct  derivative of this principle. &lt;br /&gt;
&lt;br /&gt;
&lt;script src="http://gist.github.com/297286.js"&gt;&lt;/script&gt;&lt;br /&gt;
The Connection class is open to extension but closed to modification.&lt;br /&gt;
&lt;br /&gt;
&lt;span style="font-weight:bold;"&gt;Liskov Substitution Principle&lt;/span&gt;&lt;br /&gt;
This helps avoids simple design mistakes. Consider the following:&lt;br /&gt;
&lt;br /&gt;
&lt;script src="http://gist.github.com/297288.js"&gt;&lt;/script&gt;&lt;br /&gt;
This design looks good as a Square is juat a rectangle which has length = breadth. But it fails the LSP. LSP says that a super class should be able to stand for the subclass(yes, you are reading it right). What possibly can getLength() and getBreadth() methods mean for the Square class? Since the superclass isn't sharing much behaviour with sub-class, this inheritance is flawed. The proper way to implement would be :&lt;br /&gt;
&lt;br /&gt;
&lt;script src="http://gist.github.com/297289.js"&gt;&lt;/script&gt;&lt;br /&gt;
&lt;br /&gt;
All the generic code should go into Shape and all the methods should be applicable to both Rectangle and Square.&lt;br /&gt;
&lt;br /&gt;
Java, does a violation of LSP as the methods defined in Object don't do anything useful in their default implementation. As a rule, they need to be overridden. The proper design should have made Object an interface rather than a class.&lt;br /&gt;
&lt;br /&gt;
&lt;span style="font-weight:bold;"&gt;Single Responsibility Principle - SRP&lt;/span&gt;&lt;br /&gt;
A class should be doing one thing. Cosider:&lt;br /&gt;
&lt;br /&gt;
&lt;script src="http://gist.github.com/297290.js"&gt;&lt;/script&gt;&lt;br /&gt;
Well, a Person class shouldn't be connecting to DB. A helper class should be written. That way, you only change the helper class when you change the back-end db. &lt;br /&gt;
&lt;br /&gt;
&lt;script src="http://gist.github.com/297291.js"&gt;&lt;/script&gt;&lt;br /&gt;
&lt;span style="font-weight:bold;"&gt;Don't Repeat Yourself(DRY)&lt;/span&gt;&lt;br /&gt;
If you find code being repeated, abstract the code into a separate module. That directly enhances usability. At the same time, the changes are less painful. Consider having a Person, Employee, Engineer classes. Each one of them have methods to store themselves to the DB. If you decide to change the backend db, you will have to change the code at 3 locations. In real life scenarios, the number is far, far larger than 3. &lt;br /&gt;
&lt;br /&gt;
&lt;span style="font-weight:bold;"&gt;Watch your classes&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;script src="http://gist.github.com/297292.js"&gt;&lt;/script&gt;&lt;br /&gt;
Do you see something wrong with this class? This isn't a class at all. A class is both data and behaviour and the Person class above is just data. If you ever find yourself defining classes in this manner, re-think your desing. Either your class design is flawed or you don't need the class at all. You should be using a Hashmap if all that you  are going to do is store name value pairs.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/5201868627988726097-2681936410953425987?l=prag-matism.blogspot.com' alt='' /&gt;&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/Pragmatism/~4/QnggBfqLAAo" height="1" width="1"/&gt;</content><link rel="replies" type="application/atom+xml" href="http://prag-matism.blogspot.com/feeds/2681936410953425987/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://prag-matism.blogspot.com/2009/02/object-oriented-desing-principles.html#comment-form" title="1 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/5201868627988726097/posts/default/2681936410953425987?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/5201868627988726097/posts/default/2681936410953425987?v=2" /><link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/Pragmatism/~3/QnggBfqLAAo/object-oriented-desing-principles.html" title="Object oriented desing principles" /><author><name>rahulsinner</name><uri>http://www.blogger.com/profile/10060216590711230463</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="16" height="16" src="http://img2.blogblog.com/img/b16-rounded.gif" /></author><thr:total>1</thr:total><feedburner:origLink>http://prag-matism.blogspot.com/2009/02/object-oriented-desing-principles.html</feedburner:origLink></entry><entry gd:etag="W/&quot;AkQMRHY7fip7ImA9WxBWFUk.&quot;"><id>tag:blogger.com,1999:blog-5201868627988726097.post-1764108386913546909</id><published>2009-02-02T20:58:00.000-08:00</published><updated>2010-02-07T05:13:05.806-08:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2010-02-07T05:13:05.806-08:00</app:edited><category scheme="http://www.blogger.com/atom/ns#" term="mysql" /><title>MySQL Replication Part 2</title><content type="html">This is a follow up of the previous post about MySQL replication. &lt;br /&gt;
&lt;br /&gt;
&lt;span style="font-weight:bold;"&gt;Overview&lt;/span&gt;&lt;br /&gt;
The replication model involves one server as Master and one or more servers as Slaves. The slaves periodically update themselves with server. The keep track of the changes by following the binary log maintained by the Master. &lt;br /&gt;
&lt;br /&gt;
&lt;span style="font-weight:bold;"&gt;Creating user&lt;/span&gt;&lt;br /&gt;
For security reasons, its always better to have a dedicated user for replication. This user account is used by slave to connect to master and get the data. So, we need to create a user on master.&lt;br /&gt;
&lt;br /&gt;
First create the user on slave:&lt;br /&gt;
&lt;code&gt;&lt;br /&gt;
create user 'replica' identified by 'replica';&lt;br /&gt;
&lt;/code&gt;&lt;br /&gt;
&lt;br /&gt;
Then grant access on master.&lt;br /&gt;
&lt;code&gt;&lt;br /&gt;
grant replication slave, replication client&lt;br /&gt;
on *.*&lt;br /&gt;
to 'replica'@'slave_node'&lt;br /&gt;
identified by 'replica';&lt;br /&gt;
&lt;/code&gt; &lt;br /&gt;
&lt;br /&gt;
&lt;span style="font-weight:bold;"&gt;Changing my.cnf&lt;/span&gt;&lt;br /&gt;
Under linux, my.cnf is the file that needs to be changed to configure a node as a master/slave. The following goes under [mysqld] section:&lt;br /&gt;
&lt;code&gt; &lt;br /&gt;
server-id = 1&lt;br /&gt;
log-bin = /var/log/mysql/backup.log&lt;br /&gt;
&lt;/code&gt;&lt;br /&gt;
&lt;br /&gt;
The server id should be unique among the masters and slave. Other than that, there aren't any other constraints and it can be anything. &lt;br /&gt;
&lt;br /&gt;
For the slave, the configs are more elaborate.&lt;br /&gt;
&lt;code&gt;&lt;br /&gt;
server-id = 2&lt;br /&gt;
&lt;br /&gt;
master-host = mastersite.com&lt;br /&gt;
master-user = replica&lt;br /&gt;
master-password = replica&lt;br /&gt;
&lt;br /&gt;
log-bin = /var/log/mysql/backup.log&lt;br /&gt;
log-bin-index = /var/log/mysql/log.index&lt;br /&gt;
log-error = /var/log/mysql/error.log&lt;br /&gt;
&lt;br /&gt;
relay-log = /var/log/mysql/relay.log&lt;br /&gt;
relay-log-info-file = /var/log/mysql/log.info&lt;br /&gt;
relay-log-index = /var/log/mysql/relay-log.index&lt;br /&gt;
&lt;/code&gt;&lt;br /&gt;
&lt;br /&gt;
The first section defines the connection to the master. The second section starts binary logging on slave. The relay section makes a copy of master's binary log on the slave.&lt;br /&gt;
&lt;br /&gt;
&lt;span style="font-weight:bold;"&gt;Initializing&lt;/span&gt;&lt;br /&gt;
Now we need to take a dump of existing data, dump it on slave, start the slave and we are done.&lt;br /&gt;
&lt;br /&gt;
The following needs to be run on master node.&lt;br /&gt;
&lt;code&gt;&lt;br /&gt;
mysqldump --user=root --password=foo&lt;br /&gt;
--extended-insert --all-databases &lt;br /&gt;
--master-data  &gt; backup.sql&lt;br /&gt;
&lt;/code&gt;  &lt;br /&gt;
&lt;br /&gt;
Then this dump is to be executed on slave node.&lt;br /&gt;
&lt;code&gt;&lt;br /&gt;
mysql --user=root --password=foo &lt; backup.sql
&lt;/code&gt;&lt;br /&gt;
&lt;br /&gt;
That completes the configuration. We just need to start the slave and it will periodically sync itself with the master. In case of master, all it takes is switching the nodes based on some config or dns results.&lt;br /&gt;
&lt;code&gt;&lt;br /&gt;
mysql --user=root --password=foo&lt;br /&gt;
mysql&gt; start slave;&lt;br /&gt;
&lt;/code&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/5201868627988726097-1764108386913546909?l=prag-matism.blogspot.com' alt='' /&gt;&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/Pragmatism/~4/YD2O6GVaZKA" height="1" width="1"/&gt;</content><link rel="replies" type="application/atom+xml" href="http://prag-matism.blogspot.com/feeds/1764108386913546909/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://prag-matism.blogspot.com/2009/02/mysql-replication-part-2.html#comment-form" title="0 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/5201868627988726097/posts/default/1764108386913546909?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/5201868627988726097/posts/default/1764108386913546909?v=2" /><link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/Pragmatism/~3/YD2O6GVaZKA/mysql-replication-part-2.html" title="MySQL Replication Part 2" /><author><name>rahulsinner</name><uri>http://www.blogger.com/profile/10060216590711230463</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="16" height="16" src="http://img2.blogblog.com/img/b16-rounded.gif" /></author><thr:total>0</thr:total><feedburner:origLink>http://prag-matism.blogspot.com/2009/02/mysql-replication-part-2.html</feedburner:origLink></entry><entry gd:etag="W/&quot;AkQNQHY5fyp7ImA9WxBWFUk.&quot;"><id>tag:blogger.com,1999:blog-5201868627988726097.post-2194924785168997746</id><published>2009-02-02T20:39:00.000-08:00</published><updated>2010-02-07T05:13:11.827-08:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2010-02-07T05:13:11.827-08:00</app:edited><category scheme="http://www.blogger.com/atom/ns#" term="mysql" /><title>MySQL replication</title><content type="html">MySQL is a robust, reliable and mature database server. Nevertheless, you can't expect it not to crash ever. That is why prouduction databases always have replicas. Off late, mysql has been supporting replication out of the box. You can choose either to use mysql's replication or implement a trivial replication mechanism.&lt;br /&gt;
&lt;br /&gt;
&lt;span style="font-weight:bold;"&gt;Replication by hand&lt;/span&gt;&lt;br /&gt;
The strategy is simple. Write a cronjob which takes a snapshot of the database and rsyncs the snapshot to a fixed location. The rsyncing part can be replaced with commiting to svn(or other repository) as well.&lt;br /&gt;
&lt;br /&gt;
A simple shell script can do this stuff.&lt;br /&gt;
&lt;code&gt;&lt;br /&gt;
&lt;br /&gt;
#!/bin/bash&lt;br /&gt;
&lt;br /&gt;
mysqldump --user=foo --password=bar --all-databases &gt; /replica/backup-`date`.sql&lt;br /&gt;
&lt;br /&gt;
# tables can be locked to avoid any inconsistencies. the downtime, if any, will depend on the #    size of the database.&lt;br /&gt;
# mysqldump --user=foo --password=bar --all-databases --lock-all-tables --extended-insert &gt;     &lt;br /&gt;
# /replica/backup-`date`.sql&lt;br /&gt;
&lt;br /&gt;
&lt;/code&gt;&lt;br /&gt;
&lt;br /&gt;
Having it on the same node won't do any good in case of disk failures. In that case, a better idea would be to rsync it to some other node or check it in the repository.&lt;br /&gt;
&lt;code&gt;&lt;br /&gt;
&lt;br /&gt;
#!/bin/bash&lt;br /&gt;
# tables can be locked to avoid any inconsistencies. the downtime, if any, will depend on the #    size of the database.&lt;br /&gt;
file_name=backup-`date`.sql&lt;br /&gt;
mysqldump --user=foo --password=bar --all-databases --lock-all-tables --extended-insert &gt;     &lt;br /&gt;
/replica/${file_name}&lt;br /&gt;
rsync /replica/${file_name} backup_node::backup&lt;br /&gt;
&lt;br /&gt;
&lt;/code&gt;&lt;br /&gt;
&lt;br /&gt;
Extending the script to check it in the repository is straight forward.&lt;br /&gt;
&lt;br /&gt;
&lt;code&gt;&lt;br /&gt;
&lt;br /&gt;
#!/bin/bash&lt;br /&gt;
# tables can be locked to avoid any inconsistencies. the downtime, if any, will depend on the #    size of the database.&lt;br /&gt;
file_name=backup-`date`.sql&lt;br /&gt;
mysqldump --user=foo --password=bar --all-databases --lock-all-tables --extended-insert &gt;     &lt;br /&gt;
/replica/${file_name}&lt;br /&gt;
cmd="cd /replica &amp;&amp; svn commit -m 'automated db backup'"&lt;br /&gt;
${cmd}&lt;br /&gt;
if [ $? -ne 0 ]&lt;br /&gt;
then&lt;br /&gt;
echo "backup failed" 2&gt;&amp;1&lt;br /&gt;
fi&lt;br /&gt;
cmd="cd /replica &amp;&amp; svn update"&lt;br /&gt;
${cmd}&lt;br /&gt;
if [ $? -ne 0 ]&lt;br /&gt;
then&lt;br /&gt;
echo "backup done and disk copy updated"&lt;br /&gt;
fi &lt;br /&gt;
&lt;br /&gt;
&lt;/code&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/5201868627988726097-2194924785168997746?l=prag-matism.blogspot.com' alt='' /&gt;&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/Pragmatism/~4/aTHp9MglwJ0" height="1" width="1"/&gt;</content><link rel="replies" type="application/atom+xml" href="http://prag-matism.blogspot.com/feeds/2194924785168997746/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://prag-matism.blogspot.com/2009/02/mysql-replication.html#comment-form" title="0 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/5201868627988726097/posts/default/2194924785168997746?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/5201868627988726097/posts/default/2194924785168997746?v=2" /><link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/Pragmatism/~3/aTHp9MglwJ0/mysql-replication.html" title="MySQL replication" /><author><name>rahulsinner</name><uri>http://www.blogger.com/profile/10060216590711230463</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="16" height="16" src="http://img2.blogblog.com/img/b16-rounded.gif" /></author><thr:total>0</thr:total><feedburner:origLink>http://prag-matism.blogspot.com/2009/02/mysql-replication.html</feedburner:origLink></entry><entry gd:etag="W/&quot;AkMEQH4_eCp7ImA9WxBWFUk.&quot;"><id>tag:blogger.com,1999:blog-5201868627988726097.post-8981952980458041185</id><published>2009-02-02T06:16:00.001-08:00</published><updated>2010-02-07T05:13:21.040-08:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2010-02-07T05:13:21.040-08:00</app:edited><category scheme="http://www.blogger.com/atom/ns#" term="programming algorithm" /><title>Fiding duplicated element in an array</title><content type="html">A simple variation on the theme on finding the missing element is finding the duplicated element. Given a n sized array, 1 to n - 1 are there and one element is duplicated. The problem is to find the duplicated element in a single pass. Again, this problem breaks down to simply calculating the sum.&lt;br /&gt;
&lt;br /&gt;
The sum of array = sum of n - 1 number + x (duplicated number)&lt;br /&gt;
&lt;br /&gt;
The duplicated number is obtained by subtracting the sum of n - 1 numbers from the actual sum.  &lt;br /&gt;
&lt;br /&gt;
&lt;script src="http://gist.github.com/297293.js"&gt;&lt;/script&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/5201868627988726097-8981952980458041185?l=prag-matism.blogspot.com' alt='' /&gt;&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/Pragmatism/~4/zWtj4wELAo4" height="1" width="1"/&gt;</content><link rel="replies" type="application/atom+xml" href="http://prag-matism.blogspot.com/feeds/8981952980458041185/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://prag-matism.blogspot.com/2009/02/fiding-duplicated-element-in-array.html#comment-form" title="0 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/5201868627988726097/posts/default/8981952980458041185?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/5201868627988726097/posts/default/8981952980458041185?v=2" /><link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/Pragmatism/~3/zWtj4wELAo4/fiding-duplicated-element-in-array.html" title="Fiding duplicated element in an array" /><author><name>rahulsinner</name><uri>http://www.blogger.com/profile/10060216590711230463</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="16" height="16" src="http://img2.blogblog.com/img/b16-rounded.gif" /></author><thr:total>0</thr:total><feedburner:origLink>http://prag-matism.blogspot.com/2009/02/fiding-duplicated-element-in-array.html</feedburner:origLink></entry><entry gd:etag="W/&quot;AkMFQXg7eSp7ImA9WxBWFUk.&quot;"><id>tag:blogger.com,1999:blog-5201868627988726097.post-7133014762374794986</id><published>2009-02-02T06:03:00.000-08:00</published><updated>2010-02-07T05:13:30.601-08:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2010-02-07T05:13:30.601-08:00</app:edited><category scheme="http://www.blogger.com/atom/ns#" term="programming algorithm" /><title>Finding missing element in an array</title><content type="html">This is a damn simple one if you remember your basic mathematics. The problem statement give an array of size n which consists of numbers from 1 to n + 1. The question is to find the missing number. Like given an array of size 5:&lt;br /&gt;
&lt;code&gt;&lt;br /&gt;
int a[5] = {1, 2, 5, 3, 4}&lt;br /&gt;
&lt;/code&gt;&lt;br /&gt;
This 5 sized array has numbers from 1 to 6 and one number is missing. That number is to be calculated in a single pass. The sum of numbers from 1 to n is (n * (n + 1)) / 2. The sum of the given array is sum of n - 1 numbers plus one number that is repeated. So  this is what we have.&lt;br /&gt;
&lt;br /&gt;
Sum of n numbers = (n * (n + 1) ) / 2&lt;br /&gt;
&lt;br /&gt;
Sum of the array = sum of n  numbers - missing number&lt;br /&gt;
&lt;br /&gt;
Its pretty straight forward to see that 1 -2 gives the missing number.&lt;br /&gt;
&lt;br /&gt;
&lt;script src="http://gist.github.com/297294.js"&gt;&lt;/script&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/5201868627988726097-7133014762374794986?l=prag-matism.blogspot.com' alt='' /&gt;&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/Pragmatism/~4/hOxcmg42mu4" height="1" width="1"/&gt;</content><link rel="replies" type="application/atom+xml" href="http://prag-matism.blogspot.com/feeds/7133014762374794986/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://prag-matism.blogspot.com/2009/02/finding-duplicate-element-in-array.html#comment-form" title="0 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/5201868627988726097/posts/default/7133014762374794986?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/5201868627988726097/posts/default/7133014762374794986?v=2" /><link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/Pragmatism/~3/hOxcmg42mu4/finding-duplicate-element-in-array.html" title="Finding missing element in an array" /><author><name>rahulsinner</name><uri>http://www.blogger.com/profile/10060216590711230463</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="16" height="16" src="http://img2.blogblog.com/img/b16-rounded.gif" /></author><thr:total>0</thr:total><feedburner:origLink>http://prag-matism.blogspot.com/2009/02/finding-duplicate-element-in-array.html</feedburner:origLink></entry><entry gd:etag="W/&quot;AkMFSHk9eCp7ImA9WxBWFUk.&quot;"><id>tag:blogger.com,1999:blog-5201868627988726097.post-1136365490365318562</id><published>2009-02-02T02:06:00.000-08:00</published><updated>2010-02-07T05:13:39.760-08:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2010-02-07T05:13:39.760-08:00</app:edited><category scheme="http://www.blogger.com/atom/ns#" term="programming algorithm" /><title>Calculating maximum sum ranges</title><content type="html">Someone asked me how would I calculate the maximum sum range in a given array. Like given an array&lt;br /&gt;
int a[] = {1, -30, 50, 10, -80, 8, 5, 15}&lt;br /&gt;
&lt;br /&gt;
I was asked to calculate the start and end index which will have the highest sum. This really caught me off guard. I went as ahead as thinking that this isn't solvable in polynomial time. But in fact, its quite a straight forward question.&lt;br /&gt;
&lt;br /&gt;
This is just about generating the ranges. Given an array of size n, the different ranges possible would be:&lt;br /&gt;
0-1, 0-2, 0-3 ... 0- (n-1)&lt;br /&gt;
1-2, 1-3, 1-4 ... 1 - (n-1)&lt;br /&gt;
...&lt;br /&gt;
(n-1) - n&lt;br /&gt;
&lt;br /&gt;
Once you figure out this, its quite a straight forward question. We just have to calculate the sum for all the ranges and figure out which one is the highest. The question reduces to finding all the ranges, their sums and the maximum sum.&lt;br /&gt;
&lt;br /&gt;
&lt;script src="http://gist.github.com/297296.js"&gt;&lt;/script&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/5201868627988726097-1136365490365318562?l=prag-matism.blogspot.com' alt='' /&gt;&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/Pragmatism/~4/UZ-_7WOyT8Y" height="1" width="1"/&gt;</content><link rel="replies" type="application/atom+xml" href="http://prag-matism.blogspot.com/feeds/1136365490365318562/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://prag-matism.blogspot.com/2009/02/calculating-maximum-sum-ranges.html#comment-form" title="0 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/5201868627988726097/posts/default/1136365490365318562?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/5201868627988726097/posts/default/1136365490365318562?v=2" /><link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/Pragmatism/~3/UZ-_7WOyT8Y/calculating-maximum-sum-ranges.html" title="Calculating maximum sum ranges" /><author><name>rahulsinner</name><uri>http://www.blogger.com/profile/10060216590711230463</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="16" height="16" src="http://img2.blogblog.com/img/b16-rounded.gif" /></author><thr:total>0</thr:total><feedburner:origLink>http://prag-matism.blogspot.com/2009/02/calculating-maximum-sum-ranges.html</feedburner:origLink></entry></feed>

