Thursday, March 31, 2011

Setting up logging for Hibernate

In fact it took me a while to find out that I am actually missing a jar in the classpath while doing the same. Had the below jars in the application classpath,

slf4j-api-1.6.1.jar
slf4j-log4j12-1.6.1.jar
log4j-1.2.16.jar and had log4j.properties in the classpath. Make sure you have your log4j.properties preceeding over other files (if at all others exist).

My log4j.properties are copied from hibernate's example properties
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.out
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n

log4j.rootLogger=info,stdout,R

log4j.logger.org.hibernate=debug
log4j.logger.org.hibernate.test=info
log4j.logger.org.hibernate.tool.hbm2ddl=debug
log4j.logger.org.hibernate.sql.ordering.antlr.OrderByFragmentTranslator=warn

log4j.appender.R=org.apache.log4j.RollingFileAppender
log4j.appender.R.File=hibernate.log

log4j.appender.R.MaxFileSize=100KB
# Keep one backup file
log4j.appender.R.MaxBackupIndex=1

log4j.appender.R.layout=org.apache.log4j.PatternLayout
log4j.appender.R.layout.ConversionPattern=%p %t %c - %m%n

Monday, March 28, 2011

Regex - difference between reluctant, greedy and possessive quantifiers

This one , I keep on forgetting so I thought of putting a post so that I shall not forget this again and it benefits others who had a hard time in understanding java's regex tutorial.

There are three types of regex quantifiers in regex possessive (a + character identifies this), greedy ( default so no character to identify this), reluctant ( a - character identifies this).

For example \w means any word character. Let take a word "blackdogishere". Regex "\w*dog" matches "blackdogishere". First lets examine what "\w*dog" represents, "*" means zero or more character. "\w*" means zero or more word character. "\w*dog" means, zero or more word charactesr followed by "dog". Note that dog also represents a word character. By default the quantifier is greedy so when "\w*dog" matches against "blackdogishere", \w* matches the entire string "blackdogishere" but then as the regex character moves on, it backtracks and hence "\w*dog" matches "blackdogishere".

Now if we consider reluctant quantifier which is identified by ? charcter. "\w*?dog" also matches "blackdogishere" but in some different way.  Reluctant quantifier eats the character one by one. So "\w*?" matches "black" and then "\w*?dog" matches "blackdog".

Possessive quantifiers (identified by + character) eats the character in a full stretch and never back tracks. "\w*+" eats all characters in "blackdogishere" which never returns back and hence "\w*+dog" does not match the string "blackdogishere".

Believe I have explained the three types of quantifiers in a simple way.

Tuesday, March 22, 2011

Java an In - Memory Indexing Technique

Scenerio Given to me:

You have a set of XML documents that have data related to Bank. i.e name, place, account name, country etc. Users would like to search based on some important fields. For example a Bank data could have 10 columns and users would like to search based on 2-3 columns. Search has to be as fast as possible. 

My Solution:

1. Create an ArrayList that holds the list of BankData object. Each XML bank data corresponds to Java BankData object.
2. Create an Index object for each column that needs to be searched. Index object is a wrapper over java's TreeMap object where TreeMap's key is the column value and TreeMap's value is the index at which corresponding BankData exist in the ArrayList.
3. While reading each XML data construct a BankData object and put it in an ArrayList. Note the index at which BankData has been added in the ArrayList.
4. For each searchable columns, get the corresponding Index object and put column value and Arraylist index the TreeMap.
5. On search give the user input to the corresponding Index Object. It searches the TreeMap and identifies the ArrayList index and gets the real BankData from the ArrayList.

Monday, March 7, 2011

England Vs South Africa My First world cup - on stadium experience

It was on 6th March 2011. I bought ticket for this match for Rs 1000.
Tried hard to get a ticket for India match but could not get it
and got ticket for the above match believing that it will be a good
entertainer. I am not disappointed and it was a nail bitting finish.


Match starts at 9.30 am and I started from my room at 8 am as it will take 1 hour to reach
Chepauk from pallikaranai where I am staying. In the train able to see lot of
enthuthiastic fans chatting about cricket. It was at 9.20 am when I reached the stadium.
People started running as soon as train stopped there since there was a music
and loud cheer coming from the stadium.

Felt excited when entering as the atmosphere is very cheerful and staff are giving
plastic boards with runs printed on it. Also flags were given and some one gave me an England
flag. My neighboure in the queue asked for both the flags hoping that its a t-shirt. Found that
there are lot of freshers for that match just like me. While I entered saw de-villiers standing
near deep square leg ropes.

There are two jiant screens in which replays are shown immediately and in fact players are also referring
the jiant screen. Main difference between watching the match in the TV and the stadium is the elecrifying
atmosphere and the athletics shown by the players. International player standard is far far better than
league players. South Afrians fielders are true athletics in the field. Able to see the wonderful sight
of seeing Steyn bowling bouncers to england batsmen. Just wondering about the atmosphere when India is playing against pakistan. Its a game of spirit and its a game of emotions.

I spent 7.30 hours in the stadium and it was not at all boring as we get bored between 16 to 34 overs while watching it in the tv. It was all South Africa for almost all 90 overs. In the last 10 overs england got vital wickets and won the match. Eager to see more such matches and willing to spend for IPL matches too.

Saturday, March 5, 2011

Java garbage collection algorithms

There are two major types namely reference counting and tracing.

 Reference Counting:

Each object has a referenceCount that measures the number of reference objects that are pointing to it. i.e in other words if you create one more reference to that object, that count should increase by one. On garbage collection cycle, objects that have referenceCount as zero, will be removed from heap.

Tracing :

Starting from the root object, trace each object that are having references from the root object. Remove the objects that are not referenced from the root.

Tracing is achived through various techniques like Mark and Sweep, Copying,  Mark and Compact.

Mark and Sweep:

It has two steps. On first step live objects are marked through tracing. On second step all objects are visited and unmarked objects are removed from heap. Marked objects are unmarked for next tracing cycle.
Disadvantage of this approach is it has two steps and it could create holes in the heap. Garbage collection is done after stopping all the threads in the jvm.

Copying Collector:

Herealso all threads are stopped before staring the garbage collection. Heap is divided into active and inactive part. Once the active part is full, threads are stopped and tracing is started. During tracing, live objects are moved to the inactive part. While doing so, fragmentation is automatically done i.e objects are compacted in the other part. Once copying is completed active part is made as inactive part and vice versa. After copying inactive part is garbage collected. Garbage is collected through single pass, but twice the memory is needed as copying is performed from active part to inactive part.

Mark and Compact Collector:

It combines the advantages of both mark & sweep and copying collector. It has two phases as mark and compact phase.  During mark phase, objects are marked. In mark and sweep collection, boolean marked is stored in the object itself. Whereas here, it is stored in the handle. Handle is an object which has reference to the Class.object that it refers to and the pointer to the original object created in the heap. During mark phase, marked boolean is added here and not in the object. So when an object is changed, it is enough to update the handle and not reference objects that are pointing to it.  During compact phase ALL objects in the heap are examined. Objects that are marked are slided to the lower portion of the heap. First I took some time to understand this as I had questions like what will happen to the existing object? Then I realized that memory allocation is contigeous. Root element is starting with zero and then subsequent elements are sequentially stored in the heap.