Archive for the ‘technology’ Category

Building Strings using Java MessageFormat

Monday, March 15th, 2010

Until I used SL4J, I didn't know that we can templatize String objects in a simple way. As most of us, use Log4J & apache commons logging framework for logging, we know general logging using these widely used open source frameworks. But there is more we can learn from these open source packages than just logging.

A simple debug in layman terms will be similar to writing System.out.println (String)

logger.debug("In "+location+", I said "+myMessage+" "+count+" times ");

to print "In Sunnyvale, I said Hello World! 7 times" in the log, if location="Sunnyvale", myMessage="Hello World!" and count=7

It is a simple debug statement which involves multiple String concatenation.

With SL4J, we can do the above debug like this

logger.debug("I said {} {} times ",myMessage, count);

which prints the same "I said Hello World! 7 times" in the log. Easy? huh!

After using it for a while, I thought this can be very well used for other string construction programs.

For eg, I need to make an API call to with various parameters which says userid=arun&location=sunnyvale&action=welcome

A common way of doing is :

StringBuilder sb = new StringBuilder();
sb.append("userid=").append(userid)
  .append("&location=").append(location)
  .append("&action=").append(action);

 doSomething(sb.toString());

SL4J's MessageFormatter

Using SL4J's MessageFormatter we can do

 doSomething(myFormat("userid={}&location={}&action={}", userid, location,action));

 private static String myFormat(String pattern, Object...argArray){
  return MessageFormatter.arrayFormat(pattern, argArray);
 }

I wish I can avoid my private method - myFormat, which automatically constructs array from varargs parameters.

Java MessageFormat

We already have MessageFormat from Sun(since JDK 1.4.2), which does exactly same & also supports varargs by default.

So we can do

 doSomething(
      MessageFormat.format("userid={0}&location={1}&action={2}", userid, location,action)
  );
 

There is a minor difference - we need to provide parameter number within {}, so it can pick the correct parameter to fill in flower braces. This is really useful for specific scenarios, which makes us not to worry about parameter order.

We can very well do,

 doSomething(MessageFormat.format("userid={2}&location={1}&action={0}", action,location,userid));
 

which constructs what I want.

MessageFormat is really powerful helper class for constructing String objects dynamically, without the overhead of String concatenation. It also keeps the code clean & concise. Typical use cases are internationalization (changing the text dynamically based on locale), General error message construction, String url Construction or anything in similar lines, where we know the countable set of variables to construct a string.

Web Application Performance Tuning tips related to MySQL

Friday, January 15th, 2010

While I was browsing for MySQL Performance Tuning tips online, I found these presentations. You already might know some of these tips. Some are very trivial but yet miss our mind during design process. Some of them are unconventional, followed by high traffic websites.

First one is an video presentation by Jay Pipes in Google. Second is a set of slides from MySQL Conference 2008 by Bjørn Hansen

Performance Tuning Best Practices for MySQL

Real World Web: Performance & Scalability, MySQL Edition

More to follow

Reversing a String using Recursion (substring) and StringBuilder in Java

Sunday, December 13th, 2009

Recently I attended a job interview, in which they asked me to write an algorithm to reverse a String - "Hello World" to "dlroW olleH".

It sounded simple and I wrote something like below.

private String reverse(String str) {

 char[] charArray=str.toCharArray();
 String reverse = "";
 for (int i = charArray.length-1,j=0; i >= 0 ; i --,j++) {
		reverse +=charArray[i];
 }
 return reverse;
}

Then I was asked, how I will write if I need to reverse using recursion. Well, I couldn't write it at that point, I said, I will do it something like this

function reverse(String rev) {
 if (size of rev == 1) {
    return rev;
 }
return  (lastChar + reverse(substr(first to last but one)))
}

It appeared simple, but made me to think there is something wrong in my approach. I felt that guilt until I reached home. After going home I couldn't resist writing a small program for reversing a String using multiple approaches.

Below are the scenarios I tried :

  1. Reversing a String using recursion & substring
  2. Reversing manually char by char, but concatenating to a String
  3. Directly using reverse() function of java StringBuilder
  4. Reversing manually char by char, but appending to a StringBuilder
  5. Reversing manually char by char, inserting chars to a new char array

1. Reversing a String using recursion & substring

Exactly like what I did in the interview.

/**
 * Reverses the string using recursion of substrings
 *
 * @param str - string to be reversed
 * @return reversed string
 */
  private static String reverse1(String str) {
    if (str.length() <= 0) {
        return str;
    }

    int len = str.length();

    return str.substring(len-1,len)+(reverse1(str.substring(0,len-1)));
  }

2. Reversing manually char by char, but concatenating to a String

Again, exactly like what I did in the interview.

/**
 * Reverses the string char by char and concatinating to a new String
 *
 * @param str - String to be reversed
 * @return reversed string
 */
  private static String reverse2(String str) {

   char[] charArray=str.toCharArray();
   String reverse = "";
   for (int i = charArray.length-1,j=0; i >= 0 ; i --,j++) {
		reverse +=charArray[i];
   }

   return reverse;
  }

3. Directly using reverse() function of java StringBuilder

Here, as you can see I am directly calling reverse() function of StringBuilder instead of manually reversing it.

/**
 * More simple way in java, using StringBuilder's reverse method
 *
 * @param str - string to be reversed
 * @return - reversed string
 */
  private static String reverse3(String str) {
    return new StringBuilder(str).reverse().toString();
  }

4. Reversing manually char by char, but appending to a StringBuilder

Here instead of concatenating to a String, I am appending the reversed characters to a StringBuilder

/**
 * Reverses the string char by char, but appends to a StringBuilder
 *
 * @param str - String to be reversed
 * @return reversed string
 */
  private static String reverse4(String str) {

   char[] charArray=str.toCharArray();
   StringBuilder reverse=new StringBuilder();
   for (int i = charArray.length-1; i >= 0 ; i --) {
		reverse.append(charArray[i]);
   }

   return reverse.toString();
  }

5. Reversing manually char by char, inserting chars to a new char array

Here instead of concatenating to a String or appending to a StringBuilder, I am inserting the reversed chars into new char array.

/**
 * Reverses the string char by char and building reverse char array
 *
 * @param str - String to be reversed
 * @return reversed string
 */
  private static String reverse5(String str) {

       char[] charArray=str.toCharArray();
       char[] reverse = new char[charArray.length];

       for (int i=charArray.length-1,j=0; i >= 0;) {
	     reverse[j++] = charArray[i--];
       }

   return new String(reverse);
  }

Called all methods one after the other from Reverse.main() & noting the elapsed time after each call. I used a original string with length > 2000. Something like below.

"Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. Sed ut perspiciatis unde omnis iste natus error sit voluptatem accusantium doloremque laudantium, totam rem aperiam, eaque ipsa quae ab illo inventore veritatis et quasi architecto beatae vitae dicta sunt explicabo. Nemo enim ipsam voluptatem quia voluptas sit aspernatur aut odit aut fugit, sed quia consequuntur magni dolores eos qui ratione voluptatem sequi nesciunt. Neque porro quisquam est, qui dolorem ipsum quia dolor sit amet, consectetur, adipisci velit, sed quia non numquam eius modi tempora incidunt ut labore et dolore magnam aliquam quaerat voluptatem. Ut enim ad minima veniam, quis nostrum exercitationem ullam corporis suscipit laboriosam, nisi ut aliquid ex ea commodi consequatur? Quis autem vel eum iure reprehenderit qui in ea voluptate velit esse quam nihil molestiae consequatur, vel illum qui dolorem eum fugiat quo voluptas nulla pariatur? But I must explain to you how all this mistaken idea of denouncing pleasure and praising pain was born and I will give you a complete account of the system, and expound the actual teachings of the great explorer of the truth, the master-builder of human happiness. No one rejects, dislikes, or avoids pleasure itself, because it is pleasure, but because those who do not know how to pursue pleasure rationally encounter consequences that are extremely painful. Nor again is there anyone who loves or pursues or desires to obtain pain of itself, because it is pain, but because occasionally circumstances occur in which toil and pain can procure him some great pleasure. To take a trivial example, which of us ever undertakes laborious physical exercise, except to obtain some advantage from it? But who has any right to find fault with a man who chooses to enjoy a pleasure that has no annoying consequences, or one who avoids a pain that produces no resultant pleasure?"

Results :

Reverse 1 (Recursive substring)                         : 22 milli seconds
Reverse 2 (Manual Reverse by concating to a new String) : 25 milli seconds
Reverse 3 (StringBuilder.reverse())                     : 1 milli seconds
Reverse 4 (Manual with StringBuilder.append())          : 3 milli seconds
Reverse 5 (Manual by creating new char array)           : 1 milli seconds

Conclusion :

If you are using Java as programming language, Java's StringBuilder.reverse() is the simplest & fastest method for reversing a String. Sometimes, it is better to use java's built-in util methods than writing our own complex methods just to show that we know recursion or We want to do it in our way. However, if you want to do this from first principles, 5th method gives the best results - O(N).

References

I liked this article written by Joel Spolsky in his blog - The Guerrilla Guide to Interviewing (version 3.0). If you are interviewing Software Engineers for your company or attending interviews, it is a good read.

Testing memcached from command line

Tuesday, October 27th, 2009

We use memcached to cache static or less dynamic content and use java client to communicate with memcached. Memcached is a simple but powerful cache server which sits out side of JVM. Since it sits out of JVM, a cluster of java servers can use single instance of memcached for storing content. Many argue that this will be single point of failure. But you can easily configure another memcached instance and configure the client to use it as failover (though you will loose all the cached objects if first server fails).

It is always good to test the server status & behavior through command line.

Assuming you have memcached server running on your localhost:11211, we can do following to check the statistics, a particular key, deleting a key, etc.,


$ telnet localhost 11211
Trying 127.0.0.1...
Connected to localhost.
Escape character is '^]'.
version
VERSION 1.2.2


stats
STAT pid 19516
STAT uptime 5897634
STAT time 1256702309
STAT version 1.2.2
STAT pointer_size 32
STAT rusage_user 91.789736
STAT rusage_system 124.147758
STAT curr_items 325
STAT total_items 13632131
STAT bytes 1449461
STAT curr_connections 11
STAT total_connections 1089
STAT connection_structures 12
STAT cmd_get 25936
STAT cmd_set 13632131
STAT get_hits 20400
STAT get_misses 5536
STAT evictions 0
STAT bytes_read 1056124842
STAT bytes_written 117570222
STAT limit_maxbytes 2147483648
STAT threads 1


get key AlreadyAddedKey.HelloWorld
VALUE AlreadyAddedKey.HelloWorld 0 33

http://www.avanathan.com

END

delete AlreadyAddedKey.HelloWorld
DELETED

delete NonExistingKey
NOT_FOUND

These few commands should should get you going.

For more details refer memcached protocol from Sixapart

String Date property in a spring bean – No need for CustomDateEditor

Tuesday, February 17th, 2009

A while back I struggled to use java.util.Date as property of a spring bean, as it is not straight forward. We need to write custom property editors. Spring is a fantastic framework which simplifies our lives to a great extent. But I couldn't digest this custom property editor logic for a basic object like Date.

After doing some research I found an easy way to bypass Custom Editor for setting date property in a bean.

DateUtils object from apache commons has a static method which takes String date & array of String (for date format) as parameter & returns java.util.Date object for the date String passed.
We can very well use this to set the Date property in a bean.


...

...

public class MyService {

    private Date startDate;

    public void setStartDate(Date startDate) {
        this.startDate = startDate;
    }

    public Date getStartDate() {
        return this.startDate;
    }
}

Bean configuration

  <bean id="myService" class="com.my.package.MyService">
     <property name="startDate">
         <bean id="sDate" class="org.apache.commons.lang.time.DateUtils" factory-method="parseDate">
             <constructor-arg value="11-02-2009 14:30"/>
             <constructor-arg>
                 <list><value>MM-dd-yyyy HH:mm</value></list>
            </constructor-arg>
         </bean>
     </property>
</bean>

The trick here is that using the "factory-method" of spring bean configuration, to invoke static method of DateUtils, which takes string & pattern array.

It is not necessary that we need to use DateUtils here. Any class which provides a static method to return java.util.Date taking String as parameter.

If we had a constructor in java.util.Date object which takes String date & date format, we could have very well used it here using construct-arg elements of the bean.

Finally, we can use the same logic for setting any property provided you have a static method in a class which returns exactly what you are trying to set taking String as input parameter.

Update :
Spring manual says we need can call static methods using factory-method (provided you have not already instantiated the property bean. In the above example it is DateUtils). If you have already instantiated the property bean, you can call a normal getter using factory-method as in this example