Caching AJAX Requests

Hello Readers,

This post will outline the steps needed to cache AJAX requests. Caching response of AJAX requests leads to better user experience and reduces the load on server side.

What is AJAX request?
In an AJAX request, the web page refreshes 1 or more components without refreshing the entire page. This creates a better user experience. AJAX request will typically be fired on click of a button or some other Javascript event. You would map the AJAX request to a server side URL which will get the dynamic content (from database/rest/webservice). This dynamic content will be passed as a callback to success method in AJAX (Error callback can be used to handle errors).


Most likely AJAX request will pass few parameters to get dynamic content. Corresponding AJAX response can be cached in browser so that requests with same parameters do not go to server side. This will result in super fast response and reduce the load on database server (or whatever is the engine to get dynamic content).

To tell browser to cache response, you would need to indicate it by setting below response header.

response.setHeader("Cache-Control", "max-age=14400");

Above response is HttpServletResponse object in Java (set this header on response object of your language used). This setting will cache the response in browser for 4 hours (14400 seconds). Make sure you are not sending timestamp or some unique identifier in the request. Browser will only read from cache if all parameters match.
Max cache age need not be hard-coded to 4 hours. It can be passed as a parameter in AJAX request and read in the server side code processing the request.

Also ensure request method is GET. Caching works only with GET requests.

That’s it. You can verify that response is read from browser cache. Press F12 in Firefox to launch Firebug. Status columns should have a suffix of BFCache indicating the response was read from browser cache. See below screenshot:


Hope this post is helpful. Please feel free to leave your comments & feedback.

Your’s Truly!

Posted in Web Development | Tagged , , , , | Leave a comment

Hadoop – Word Count

Hello Readers,

In the last few months I am getting my feet wet with Hadoop & Map Reduce. Hadoop infrastructure provides a beautiful solution for processing massive amounts of data. It uses distributed storage and computing. I won’t get into why/what/how Hadoop works. This post assumes you have some background about Hadoop and are thinking to write some MR code.

Word Count program is the Hello World of Hadoop.

Problem: Given a text file; write a Map Reduce program to output the frequency of each word.

Major components:

  1. Mapper: map() will be executed for each input line. It tokenizes the line into words and output the word as key and 1 as value. Ex: If word “Tendulkar” appears in the line twice, then mapper will output Tendulkar,1.
  2. Reducer: The output of mapper is shuffled and partitioned before giving it as input to reducer. reduce() will receive unique keys from mapper and iterable of all values per key. Ex: If word “Tendulkar” appears in the file 6 times, the input to reducer will be: key – Tendulkar, value – <1,1,1,1,1,1>. The reduce() will simply add up of the values in iterable and output the result with the word.

Let’s take a look at Mapper class:

public static class WordCountMapper extends Mapper<Object, Text, Text, LongWritable>
        Text         outKey   = new Text();
        LongWritable outValue = new LongWritable(1);

        protected void map(Object key, Text value, Mapper<Object, Text, Text, LongWritable>.Context context)
                throws IOException, InterruptedException
            String[] values = value.toString().split(" ");

            for (String v : values)

                context.write(outKey, outValue);



Reducer class:

public static class WordCountReducer extends Reducer<Text, LongWritable, Text, LongWritable>
        LongWritable result = new LongWritable();

        protected void reduce(Text key, Iterable<LongWritable> values,
                Reducer<Text, LongWritable, Text, LongWritable>.Context context) throws IOException,
            long count = 0;
            for (LongWritable val : values)


            context.write(key, result);


You need to stitch the Mapper and Reducer classes in the main method of the Driver class. Complete example can be found at:

Input File: Received as first argument args[0]

Output File: Received as second argument args[1]

Hope this is helpful.

Thank you!

Posted in Hadoop, Map reduce | Tagged , , , | 1 Comment

Generating XSD from XML

Hello Readers,

You’d think it should be straightforward to create XSD from XML using Eclipse but it ain’t.  I was faced with this task few months back and got stumped for couple of hours.

There is this nice utility trang that does a pretty good job. See usage below:

java -jar <path_of_trang.jar> <path_of_xml> <path_of_xsd>

See below example:


All you have to do is run above command and you should have the XSD 90% ready. I say 90% because you’d have to open up generated XSD and verify the datatypes. I had to replace occurrences of type=”xs:NCName” with type=”xs:string”.

You can download the files used in this post at: trang_files.

You’d have to rename the downloaded file from .pdf to .zip. It contains below files:

  1. trang.jar
  2. cd_catalog.xml
  3. cd_catalog.xsd (generated using trang.jar)


Let me know any questions/feedback.

Your’s Truly.




Posted in XML | Tagged , , | Leave a comment

Archiving Oracle Partitions

Hello Readers,

Let’s do some data archiving in Oracle. We often have a scenario when we have to archive the data stored in tables. The reasons could be several: you need to speed up data loading in the table, faster querying, you no longer need the data or may be you are done with the data ;).

This post will focus on archiving (or moving) Oracle partitions. This post assumes you have knowledge working with Oracle partitions.

Scenario: You have a SALES table that is partitioned by month. The partition nomenclature is SALES_YYMM. So SALES_1501 will hold data for Jan 2015, SALES_1603 will hold data for March 2016.

Requirement: Archive all 2015 data in SALES_ARCHIVE table.

Solution: The most optimal solution is to take all 12 partitions of 2015 from SALES table and move them into SALES_ARCHIVE table. Oracle does not provide an “archive partition” operation that will do the trick for us. But we can leverage the “exchange partition” operation. Which basically says exchange partition in SALES table with SALES_ARCHIVE table.

alter table SALES exchange partition SALES_1501 with table SALES_ARCHIVE

Above DDL will move data stored in Jan 2015 partition from SALES table into SALES_ARCHIVE. Similarly you can archive data for remaining 2015 months.

Job done? Well, not quite. Here is the caveat. Exchange partition operation only works with a partitioned and non-partitioned table. So SALES_ARCHIVE will have to be a non-partitioned table. So after you archive data for remaining months using above DDL, all of 2015 Sales data will be sitting in a non-partitioned table. Now if you want to restore data for Dec 2015, then you would have to do a DML (slow) operation instead of a DDL. If you are alright with this state, then you are job is done.

But chances are you would like to archive the data into a partitioned table, so that you can manage partitions in an efficient way. For doing that, you would have to move data from SALES (partitioned) table into an interim (non-partitioned) table and then from interim table into SALES_ARCHIVE (partitioned) table.

Below are the steps:

  1. Create an interim non-partitioned table, say SALES_INTERIM. An empty interim table can be created quickly using below. This is a one-time operation and will only hold data in transit from SALES table to SALES_ARCHIVE table.

create table SALES_INTERIM as select * from SALES where 1=2;

2. Move Jan 2015 data from SALES table to SALES_INTERM table.

alter table SALES exchange partition SALES_1501 with table SALES_INTERIM;

3. Create partition in SALES_ARCHIVE table to hold the data. Before you do this, you would have to create a partitioned SALES_ARCHIVE table with a dummy partition. Ideally the partition key in SALES_ARCHIVE should be same as SALES.

alter table SALES_ARCHIVE add partition SALES_1501 values ('partition_key');

4. Move data from SALES_INTERIM table to SALES_ARCHIVE.

alter table SALES_ARCHIVE exchange partition SALES_1501 with table SALES_INTERIM;


That’s it. These steps would archive data from SALES table to SALES_ARCHIVE.

Optionally, you could drop this partition from SALES table.

alter table SALES dorp partition SALES_1501;

Now if you wanted to restore data from SALES_ARCHIVE to SALES; you could just follow above steps by swapping table names. So steps would be:

  1. Move data from SALES_ARCHIVE to SALES_INTERIM.
  2. Create partition, if it does not exist, in SALES.
  3. Move data from SALES_INTERIM to SALES.

If you want to archive/restore a bunch of partitions, then do it sequentially. If you do in parallel, your SALES_INTERIM table could be in bad state.

If you want to read more about different partition operations, click here.

Hope this helps. Looking forward for any comments you may have.


Your’s Truly

Posted in Oracle | Tagged , , , , | Leave a comment

Debug Java classes in Eclipse without their source file

Hello Readers,

This post will show how to debug Java classes in Eclipse when you don’t have their source. It can be used to see how various libraries/frameworks are written and give you better understanding about it. I had written a post on Jadclipse earlier that uses JAD, but JD-Eclipse is more advanced. JAD supports decompiling from command line which can be handy if you don’t have the project set up in Eclipse.

You’d need to download JD-Eclipse plugin for Eclipse (download link at the end) and copy it in Eclipse’s plugins directory. Then start Eclipse in clean mode to load the plugin.

Verify that decompiler plugin is installed. Go to Window –> Preferences –> Java –> Decompiler. You should see something like below:

Eclipse Decompiler

Then go into File Associations and make sure *.class files are associated to JD Class File Viewer.


I am going to use Apache’s commons-lang3-3.4.jar as an example. I have added the jar in classpath but not added its sources. So we will let JD-Eclipse decompile the classes and allow us to step through the source.

I wrote a basic HelloWorld class. It’s main method initializes 2 String objects and then uses StringUtils.equals() to check if they are equal. Using JD-Eclipse plugin, we can step through StringUtils.equals() method:

  1. Put a breakpoint at the line that calls StringUtils.equals. When the execution stops at that line, you can step into (press F5) the source of StringUtils.equals method.


2. You can step through this method and inspect variables/add expressions. Line numbers between /* */ indicates the class was compiled with debug info, which enables Eclipse to step through the code.


3. Finally it calls equals() in java.lang.String. You cannot step into String.equals() as it was not compiled with debug info (javac -g). If you wanted to step through such code, you’d have to recompile the class with -g option. I have not tested this.


Below image shows how StringUtils and String classes look after they are decompiled. Notice String class does not have line numbers.



  1. JD-Eclipse plugin – Home page:
  2. Apache Commons jar

Happy Debugging!

Posted in Eclipse | Tagged , , , , , , | Leave a comment

Tolerance beats Whatever beats Tolerance

As we grow old, many things change. The way we react to things, change.

For example our tone when discussing a sporting icon, political leader, current happenings change over time. We go through a phase where we want our opinion to be accepted and celebrated. There in lies a problem. This entire quest of people agreeing and liking (Eff you FB) every darn thing you do. It’s like you are imprisoned by the approval of others, on things you do in your life.

We continue to evolve and mature as we pass through life. A major component of this evolvement is your understanding about yourself. Personally I have become increasingly intolerant about things that annoy me. These things could be from anything trivial to more significant. So what do you do when you find yourself in a place or discussion or situation you would rather not be. You either suck it up and tolerate or go by the whatever style.

There are 2 realizations: your time is limited. So don’t waste it in tolerating. And second is some people don’t deserve your whatever style.

As we grow older, the time you spend in tolerating will decrease whereas the effort you spend in keeping people who don’t deserve your whatever style will increase. In doing so if you feel something did not come out right, don’t worry! You don’t have to get everything right.

Posted in Uncategorized | Leave a comment

Sachin Tendulkar – Beyond Records…

I started writing this after Sachin called it a day from ODIs, it was in my drafts, and now it’s time to bid adieu from Test matches.

After the champion announced retirement from ODIs, zillions of posts were published. They mostly discuss records and they don’t lie. He has created records that were not even imagined when he made debut.  But there is a wow factor beyond…

1988 (15 years old): He was playing for Shardashram Vidyamandir at this point and there was a joke when Sachin hits a straight drive, fielder should be loaned a scooter to fetch the ball. Sachin & Kambli were batting at 192 and 182 at close of play. Their coach had instructed them to declare the innings next morning. But Achrekar was away from the match and these 2 continued to bat next morning. They went on to make 664 run partnership. At lunch, Sachin called up coach to inform about the score and then the innings was declared immediately. This 15-year old was terrified by his coach but his hunger for scoring runs took over. After all, 15-year olds are excused for creating world records.

1989 (16): Sachin made his debut in a test series at Pakistan. Waqar made his debut in the same series. The 4th test match was at Sialkot, which was the fastest wicket ever seen. It had so much grass, cows could have bred there. The 16-year old got hit on his nose by a fiery Waqar bouncer and had blood pouring all over his jersey. Physio, 12th man, non-striker were running towards him with a stretcher. The kiddo put some water on his face and refused to leave the ground. Next ball he stood 1 step back in the crease anticipating the yorker and smashed a straight drive for a boundary. 16-year olds are not expected to have such courage. 

1990 (17): Ranji Finals. Mumbai needed 355 from 67 overs to win the match against Haryana led by Kapil Dev at Wankhede. Word spread that Vengsarkar and Tendulkar have launched a counter-attack and reports said the turn out on 5th day was around 18,000. This is a  time when there was no concept of power play mentality. Sachin smashed 96 of 75 balls launching himself against the likes of Kapil Dev and Chetan Sharma.

1998 (25): Warne nightmare. Very few would argue Warne was the best spinner ever. Shane Warne had a reputation to turn the ball on a glass and was expected to rock Indian batting line up. Sachin had practiced hard, added new shots to his armory and all were talking about Sachin-Warne contest. Sachin got stuck into him right from their practice match. He was in his climb and smashed Warne tirelessly. He just didn’t stop. After tour of India and Sharjah series, Warne admitted: I’ll be going to bed having nightmares of Sachin just running down the wicket and belting me back over the head for six. He knew what it would take to succeed against Warne and he went after it.

1998 (25): Sharjah! The finals of Coca Cola cup at Sharjah coincided with Sachin’s 25th birth day and the ritual of getting gifts was about to be reversed. But 2 days prior we had to play, almost like a semi final, Australia and make sure our run rate doesn’t fall below NZ. Sachin hit 143 runs in that match and that century of his had a major impact on at least my generation. All my society friends were just talking about how he hit those 6s, Tony Greig’s commentary, it was summer vacation and all of us were in his world. He gave the Oz bowlers 1 day break, before he again got stuck into them on his 25th. Many kids of my generation had found their hero. We started mimicking him in what ever ways we could. I remember he once said he prefers to wear his left pad before right. Since that day I am wearing my left sock before right…

1999 (26): Test match vs Pakistan at Chennai. Sachin hit a century in 2nd innings and was steering India towards victory. He was battling not only a Wasim-Waqar-Saqlain attack but also his own back. His back pain was gaining over his knock and he decided to go for boundaries as he could no longer run. But he perished with India needing just 17 and 3 wickets in hand. India went on to lose the match by 12 runs. It’s a very rare situation when Man of the Match is awarded to a player from losing team in a test match. But that knock of his was rarest of rare. He wasn’t ready to take the award and was weeping like a school boy in dressing room. He told his coach, Anshuman Gaekwad, his team lost because of him. He should have stayed longer to win the match. He was a sensation by this time but he never lost ground.

1999 (26): India had started another World Cup poorly losing 1st match. The worst was waiting for Sachin. He received the news of his father passing away and he flew back for final rituals. Some of us may know what kind of blow that is, others don’t even want to think about it. In the mean time, we had lost 2nd match too against Zimbabwe. Sachin to every ones surprise turned up for the 3rd match against Kenya. Before the match, Sachin was practicing at ground wearing black glasses. Harsha Bhogle asked if he could give a quick bite before the match, Sachin obliged. Harsha asked him how difficult is it for him to turn up after such a huge loss. Tendulkar was looking at mic and could not say a word for 30 sec. He removed his glasses and all Harsha saw was red eyes soaked in tears. That video was never broadcast. Sachin had hit 21 ODI centuries before that. To my knowledge all of them came when opening the innings. He came in at No. 4 in that match after almost 20 overs. I was watching this match with my Dad and my Dad knew that there is no way he won’t hit a century in this match. And of course, he wasn’t wrong!

2007 (34): T20 World Cup. Sachin withdrew from playing at T20 World Cup in SA that gave more youngsters a chance to prove themselves. May be he thought it’s a more demanding format than his body could handle. He had to start playing selectively to perform longer for India. Mind you, there was no skill shortage here. He’s till date the only Indian player to win Orange cap in IPL. Every player is the best judge of his game.

2010 (37): The money element. Sachin was offered the highest ever deal an Indian sportsperson had ever bagged. But he turned down the offer as it was to endorse alcohol. It’s not that he did’nt want more money, but it was not in line with his principles and values.

2011 (38): India wins WC. Sachin had conquered all possible records in batting and the world kept asking him what is he seeking for? And he and his team were after World Cup. The entire team was kicked to win it. He was the highest scorer for India and 2nd highest overall. By now he was not only playing as a cricketer, but also as a mentor for youngsters. That hug from Yuvraj, carrying on shoulder by Virat and Bhajji, was a small testament.

2012 (39): India tours Australia. Sachin was touring for the last time to play 4 test matches. He was struggling in this series. He’s by far the biggest crowd puller and this is not just in India. He walked in and out 16 times in 4 test matches. On all 16 occasions, there was not a single spectator in the stadium who did not give him a standing ovation. And this we are talking about Oz crowd. No records books will show this gesture.

2013 (40): Back to domestic cricket. He had a dismal test series against Australia late 2012. He was out of form but was still hungry. Australia was scheduled to tour India for 4 test matches in Feb 2013. He went back to domestic cricket to get back in touch. He played Ranji semis and finals and also participated in Irani cup. When the world was talking about his retirement, he was practicing longer than youngsters in the nets. He refused to stay failed at 40.

2013 (40): Vijay had scored 167 against Aus in a test match at Hyderabad. The fall of his wicket meant the entry of batsman at number 4. Whenever India plays a test match, the biggest cheer is when India’s number 4 comes out to bat. There is an explosion in the stadium. Sachin waited behind the boundary line until Vijay crossed him. Vijay was 5 years and 228 days when Sachin made his international debut.

There are so many more of such instances. People like him more for the way he has handled success and conducted himself. He’s easily the most analysed cricketer and a role model for young cricketers. I am so proud that he has represented India.

Your’s Truly

Posted in Cricket | 1 Comment