What is Pagination?

In many use cases, there is a generic requirement to show the list of data to user. User should be able to see the list of data, to perform search and sorting operations on this data. User should also be able to navigate to any data record anytime. This requirement demands to fetch the large amount of data from database and list it on UI for the user. Then this data can be filtered using various search criteria and can be sorted also depending upon user input. Approach to fetch the data can be different depending upon architecture and implementation of application, like data can be fetched directly from database in case of embedded applications, or 2 tier architecture or through some services in 3 tier architecture. The process flow used to be like:
  • Take inputs from user what kind of data she wants to see
  • Interact with database directly or through service (depending upon architecture) to get this data 
  • Display this data to user
  • Filter or sort the data depending upon user action on UI
For example, open google.com. Search for any word. You will get search results in list form, and various page number given at the bottom of the page to navigate to different pages. 

Now if there are thousands of data records in database for given inputs, all of this data can not be shown to user in one go. Showing this on same screen will be having problems like: 
  • It will not be a good experience for user, as she will need to scroll up and down a lot and information will be unreadable practically.
  • It will not be good for performance of application, as application may get out of memory or may face slow response due to large amount of data in memory, or data transfer from database.
To solve first problem mentioned above, we need to divide the data in small parts to show the user one part at a time. For example, we can divide data in parts having 50 records each. Then each of the parts can be shown to user one by one. Once user see first part, she can request for next or previous part and hence corresponding 50 records can again be shown on screen to user. These parts are called pages, and this process is called pagination - which divides the large data in pages (having limited records) and show these to user.

Vedic Math - Multiplication of any Numbers

Note: Vedic Math Blog has been moved to http://vedicmath.vedantatree.com/. Please bookmark the new address for new and existing blogs.

In the previous article, we have discussed few cases of Multiplication with "vertically and crosswise" sutra. In this article, we shall learn the remaining cases.

E. Multiplying three digit by three digit numbers
For example: abc x pqr



Multiply:
1) vertically                                              (a x p)
2) crosswise in both directions and add    (a x q) + (b x p)
3) crosswise in both directions and add    (a x r) + (b x q) + (c x p)
4) crosswise in both directions and add    (b x r) + (c x q)
5) vertically                                              (c x r)

Step2 and Step4 are same as we did in the previous sections i.e. multiplying crosswise with two columns at a time. But in Step3, we multiply crosswise using the outer columns, then multiply vertically in the middle column and add these numbers.

Vedic Math - Multiplication of any numbers

Note: Vedic Math Blog has been moved to http://vedicmath.vedantatree.com/. Please bookmark the new address for new and existing blogs.

In this article, we shall discuss the Vedic Math sutra "vertically and crosswise". This is the General Formula which is applicable to all the cases of multiplication. You will found this method very useful in division also that we shall discuss later. In the previous sutras, you subtracted crosswise; now you will multiply crosswise.

A. Multiplication of two digit numbers
For ab x uv


The numbers are written from left to right.
Now multiply:
1) vertically                                              (a x u)
2) crosswise in both directions and add    (a x v) + (b x u)
3) vertically                                              (b x v)

The answer has the form:   au | av + bu | bv

Following is the example of two-digit multiplication will make this clear:
12 x 23
 1         2
 2         3
--------------
2 | 3 + 4 | 6
= 2 | 7 | 6
= 276

The previous examples involved no carry figures, so let us consider this case in the next example.
64 x 93
 6          4
 9          3
-----------------
54 | 18 + 36 | 12
=54 | 54 | 12
=54 | (54 + 1) | 2         (Carry over the 1)
=54 | 55 | 2
=(54 + 5) | 5 | 2          (Carry over the 5)
=59 | 5 | 2
64 x 93 =5952

The Algebraic Explaination is:
Let the two 2 digit numbers be (ax+b) and (ux+v). Note that x = 10. Now consider the product
(ax + b) (ux + v)
= au. x2 + avx + bux + b.v
= au. x2 + (av + bu)x + b.v
The first term i. e. the coefficient of  x2 is got by vertical multiplication of a and u
The middle term i. e. the coefficient of x is obtained by the cross-wise multiplication of a and v and of b and u and the addition of the two products
The independent term is arrived at by vertical multiplication of 'b' and 'v'

More about Singleton Design Pattern

In our previous article about Singleton Design Pattern, we have discussed about basics of its working and design. Today we are adding more points to it.

While implementing singleton, we need to take care that it should work well in multiple thread environment. So one option is to initialize the instance eagerly at the time of declaring the variable. But if we want to initialize the variable lazily, i.e. only when it is required, then we can make the method 'getSharedInstance' synchronized. But that will slow down the operations in every request. But we need synchronization only if instance is null and we need to create it.

So one approach could be to use double 'null' check as given below:

getSharedInstance()
{
// first check to make sure that synchronization comes in effect only if instance is null
 if (instance == null )
 {
  // synchronizing to avoid access my multiple threads
  synchronized (this)
  {
   // check again if instance is not null, as first check of not null may be passed by more than one thread
   if (instance == null)
   {
    instance = new Instance();
   }
  }
 }
 return instance;
}

But in above approach, we are still synchronizing a code block, and checking the instance for 'null' twice. There can be another better approach as given below: 

public class SingletonObject 
{
  public static SingletonObject getSharedInstance ()
 {
   return SingletonHolder.sharedInstance;
 }

 private static class SingletonHolder 
{
 static SingletonObject sharedInstance = new SingletonObject();
}
}

Here no synchronization is required in code. The instance will be initialized lazily only when request will hit getSharedInstance method for the first time, as inner static class will be initialized then.

With these approaches, you need to take care of few points like
  • Cloning should not be supported
  • Constructor of class should be private
  • Serialization should be controlled, i.e. we should override readResolve method to return the shared instance.

But later 'Joshua Bloch' has proposed another approach, termed as best approach for implementing Singleton Patter. This is to use 'enum' for implementation. Example is given below:

// Enum Singleton
public enum SingletonObject
{
  SHAREDINSTANCE;

  public void someMethod()
  {
    .....
  }
}

And it is done. Now you can use it like

SingletonObject.SHAREDINSTANCE.someMethod()

It is perfectly thread safe without any double check for null or synchronization. Check in serialization methods are also not required, because Enum are serialization specially in Java by just writing their names in stream.

With this approach, there is no chance to have more than one instance of object with any of the hook. From Joshua book - 
This approach is functionally equivalent to the public field approach, except that it is more concise, provides the serialization machinery for free, and provides an ironclad guarantee against multiple instantiation, even in the face of sophisticated serialization or reflection attacks. While this approach has yet to be widely adopted, a single-element enum type is the best way to implement a singleton.

Solution for 'NullPointerException' on Eclipse Startup

We faced a scenario where we abruptly had to kill the java while eclipse was running on system. However, when we tried to run the Eclipse again, it just get hanged and console/logs exposed us following error:

[weblogic@localhost oepe-indigo-12.1.1.0.1]$ /home/weblogic/Programs/Oracle/oepe-indigo-12.1.1.0.1/eclipse -vm /usr/java/jdk1.6.0_33/bin
LogFilter.isLoggable threw a non-fatal unchecked exception as follows:
java.lang.NullPointerException
at org.eclipse.core.internal.runtime.Log.isLoggable(Log.java:101)
org.eclipse.equinox.log.internal.ExtendedLogReaderServiceFactory.safeIsLoggable(ExtendedLogReaderServiceFactory.java:59)
org.eclipse.equinox.log.internal.ExtendedLogReaderServiceFactory.logPrivileged(ExtendedLogReaderServiceFactory.java:164)
org.eclipse.equinox.log.internal.ExtendedLogReaderServiceFactory.log(ExtendedLogReaderServiceFactory.java:150)
org.eclipse.equinox.log.internal.ExtendedLogServiceFactory.log(ExtendedLogServiceFactory.java:65)
org.eclipse.equinox.log.internal.ExtendedLogServiceImpl.log(ExtendedLogServiceImpl.java:87)
org.eclipse.equinox.log.internal.LoggerImpl.log(LoggerImpl.java:54)
org.eclipse.core.internal.runtime.Log.log(Log.java:60)
org.tigris.subversion.clientadapter.javahl.Activator.isAvailable(Activator.java:92)
org.tigris.subversion.clientadapter.Activator.getClientAdapter(Activator.java:67)
org.tigris.subversion.subclipse.core.SVNClientManager.getAdapter(SVNClientManager.java:127)
org.tigris.subversion.subclipse.core.SVNClientManager.getSVNClient(SVNClientManager.java:94)
org.tigris.subversion.subclipse.core.SVNProviderPlugin.getSVNClient(SVNProviderPlugin.java:462)
org.tigris.subversion.subclipse.core.status.RecursiveStatusUpdateStrategy.statusesToUpdate(RecursiveStatusUpdateStrategy.java:62)
org.tigris.subversion.subclipse.core.status.StatusCacheManager.refreshStatus(StatusCacheManager.java:270)
org.tigris.subversion.subclipse.core.resourcesListeners.FileModificationManager.refreshStatus(FileModificationManager.java:220)
org.tigris.subversion.subclipse.core.resourcesListeners.FileModificationManager.resourceChanged(FileModificationManager.java:161)
org.eclipse.core.internal.events.NotificationManager$1.run(NotificationManager.java:291)
org.eclipse.core.runtime.SafeRunner.run(SafeRunner.java:42)
org.eclipse.core.internal.events.NotificationManager.notify(NotificationManager.java:285)
org.eclipse.core.internal.events.NotificationManager.broadcastChanges(NotificationManager.java:149)
org.eclipse.core.internal.resources.Workspace.broadcastPostChange(Workspace.java:395)
org.eclipse.core.internal.resources.Workspace.endOperation(Workspace.java:1530)
org.eclipse.core.internal.resources.InternalWorkspaceJob.run(InternalWorkspaceJob.java:45)
org.eclipse.core.internal.jobs.Worker.run(Worker.java:54)

And the solution we found was, it happens because some of the metadata file got corrupted during abrupt exit. So we follows the steps mentioned below to get eclipse started again:
  1. cd .metadata/.plugins
  2. mv org.eclipse.core.resources org.eclipse.core.resources.bak
  3. Start eclipse
  4. It will start but will show errors with opened tabs. This is because we have changed the resources folder, which eclipse use for managing the resources
  5. Close all opened tabs
  6. Exit from Eclipse
  7. Delete any newly create org.eclipse.core.resources folder
  8. Rename back the 'org.eclipse.core.resources.bak' to 'org.eclipse.core.resources'
  9. Start Eclipse
  10. It should start fine with all projects
 Hopefully you won't face any problem further, but will depend on scenario. Please share your experience if that was different.

Vedic Math - Squaring numbers near base

Note: Vedic Math Blog has been moved to http://vedicmath.vedantatree.com/. Please bookmark the new address for new and existing blogs.

In the previous article, we have discussed about the method of multiplication by using the base value. In this article, we shall learn the squaring of numbers by using base value. Squaring numbers near base is much easier as there is no possibility of different cases that we discussed earlier for multiplication, like
1. One number is above the base and the other number is below it
2. Numbers near different bases like multiplier is near to different base and multiplicand is near to different base.

So it is comparatively simpler. Here, we can use the sub sutra “whatever the extent of its deficiency, lessen it still further to that extent; and also set up the square of that deficiency”.

In this, first corollary is “All from 9 and the last from 10”. This method will work for any type of squaring. There is another method by taking the sutra "Vertically and Crosswise" but that we will discuss later.

Suppose we have to find the square of 8. The following will be the steps for it:
1. We shall take the nearest power of 10 (10 itself in this case) as our base.
2. 8 is '2' lesser than 10, so we shall decrease 2 from 8 (i.e. 8 - 2 = 6). This will become the left side of answer.
3. And, for right part of answer, we write down the square of that deficiency i.e. 2 x 2 = 4
4. Thus 8 x 8 = 64

In exactly the same manner, we say
72 = (7-3)  |  32
     =  4  |  9
     =  49

92 = (9-1)  |  12
     =  8  |  1
     =  81

62 = (6-4)  |  42
     =  2     |     6                (Here, since right side is 2 digit number, so '1' will be carried to its left)
                   1
     =  3     |   6        
     =  36

How to Manage Inter-Project Dependencies using Jenkins


We have explained about Use of Jenkins and its Configuration in one of the previous blog. Here we shall discuss, how Jenkins can be used to work with inter-dependent projects. Managing the CI environment with multiple inter-dependent projects is quite important, as it is general scenario with projects.

If there are multiple projects having dependencies, then requirements would be:
  1. If Project A is dependent on Project B, then a successful build of Project B should trigger the build for Project A also. In generic words, the build of current project should trigger the build for other dependent projects also. It is required to ensure the integrity of whole project dependency chain after any change.
  2. The dependent projects should be able to pick the latest jars from Projects on which these are dependent. Different mechanism can be used for this. One of these could be to modify the build system of every dependent project to pick the required jars from downstream project
  3. This way, finally whole project chain will be built again on change in any of the project (from down to up)

Now, how we can achieve these requirements in Jenkins. Here we are describing this with an example.
  • Suppose there are three projects, Project 1, Project 2 and Project 3. 
  • Project 1 is dependent on Project 2. Project 2 is dependent on Project 3.
  • Assumption is that build of every project produce the distribute-able jar etc in a specific folder
  • Now update the 'build' for Project 3 in a way, so that before compiling its own code base, it picks the output of Project 2 build from its output folder and synchronize it in its own library folders. Of course, assumptions are to have a fixed project structure (preferably relative directory structure).
  • Similarly update the 'build' for Project 2 in a way, that before compiling its own code base, it picks the output of Project 1 'build' from its output folder and synchronize it in own library folders.
  • Create a Job in Jenkins for Project1, as described with previous article
  • Add post build actions to this job to trigger the build of Project2
  • Create a Job for Project2
  • Add post build actions to this job to trigger the build of Project 3
  • Create job for Project3
  • Using this setup, whenever Project1 will be build, it will trigger the job for Project2
  • Project 2 will pick the depdencies from Project1 output folder and will build its own code base. Build will place the output files in some specific folder.
  • Similarly Project3 build will be triggered and it will pick the dependencies from Project2 output folders

Using this approach, we shall be able to have a perfectly integrated build system and will be able to track if any of the project in whole dependency chain breaks due to any commit. On change in any project, it will trigger the build of all dependent projects. Inter-project dependencies will be synchronized automatically.

How to Set Java Home and Path in Linux

This small article will describe how to set the Java Path in Linux.

  1. Open '/etc/profile' file
  2. Add following line at the end of this file
    1. export JAVA_HOME=/usr/java/jdk1.6.0_33                                              export PATH=$PATH:/usr/java/jdk1.6.0_33/bin
    2. Change the path of JDK as per your installation
  3. This should set the path
  4. If it is not solving your problem, 
  5. Open '/etc/bashrc' file
  6. Add following line at the end of this file
    1. export JAVA_HOME=/usr/java/jdk1.6.0_33
      export PATH=$PATH:/usr/java/jdk1.6.0_33/bin
    2. Change the path of JDK as per your installation
  7. If it still does not resolve the motive,
  8. Open directory '/etc/profile.d'
  9. Create a file 'java.sh' here (if it is not there)
  10. Add following lines to this file
    1. JAVA_HOME=/usr/java/jdk1.6.0_33
      PATH=$JAVA_HOME/bin:$PATH
      export JAVA_HOME
      export PATH
Scripts located in 'profile.d' directory runs in the end and hence override any other settings done. So it should finally help to have the Java Home set in system.

How to Set Hostname for Linux Machine

Here is a small trick for defining a fix 'hostname' for linux machine. Sometimes we face a problem that 'hostname' keeps on changing for Linux machine. At first level, you can check with /etc/hosts, if settings are proper there. The contents should be like:

127.0.0.1               localhost.localdomain localhost
::1                          localhost6.localdomain6 localhost6

But sometimes, even after having these settings fine, we still face above problem.

Next level of quick fix for this problem is given below:

  • Open /etc/rc.local
  • Add following lines  in this file
  • hostname localhost.localdomain (or any other hostname if you want)
  • Save the file
  • Restart the system
This file runs in the last during network configuration (at the time of login) hence any setting which is written here will take effect finally. So whatever in your system would be affecting the hostname, will be overridden here. And you will get the right 'hostname'.

Vedic Math - Base Multiplication Part-2

Note: Vedic Math Blog has been moved to http://vedicmath.vedantatree.com/. Please bookmark the new address for new and existing blogs.

In the previous article, we learnt multiplication by taking base. We discussed following three cases:
A. Numbers are below the base number
B. Numbers are above the base number
C. One number is above the base and the other number is below it

And now, we shall discuss rest of the two cases here:
D. Numbers are not near the base number, but are near a multiple of the base number, like 20, 30, 50 , 250 , 600 etc.
E. Numbers near different bases like multiplier is near to different base and multiplicand is near to different base.

Before going further, let us take few examples of larger numbers with the same method, which we have discussed in previous article.

Example: 6848 x 9997

 6848    -3152
 9997    -0003
----------------
 6845  |  9456     (Refer to previous article for details of approach)
----------------

Example: 87654 x 99995


Let us discuss the fourth case:
D. Numbers are not near the base number, but are near a multiple of the base number, like 20, 30, 50 , 250 , 600 etc.

As you know that this method is applicable in all the cases, but works best when numbers are close to the base. Here, we shall apply the same sutras  “All from 9 and the last from 10” and "Vertically and Crosswise"  discussed earlier and also the sub sutra i.e. "Proportionately".

Take example as 207 x 213 . Here the numbers are not near to any of the bases that we used before: 10, 100, 1000 etc. But these are close to '200'. So, when neither the multiplicand nor the multiplier is near to the convenient power of 10 then we can take a convenient multiple or sub-multiple of a suitable base (as our 'Working Base'). And then perform the necessary operation by multiply or divide the result proportionately. Like in this example, we take 100 as a 'theoretical base' and take multiple of 100 i.e. 200 (100 x 2) as our 'working base'.

207 x 213
  207    +007
  213    +013
  ------------
= 220  |  091
  ------------
= (220 x 200) | 91
= 44000 + 91
44091

As they are close to 200, therefore deviations are 7 and 13 as shown above. From the usual procedure (refer to previous article), we get, 220 | 91. Now since our base is 200, we multiply the left-hand part of the answer by 200 and add it to the right-hand part. That is, (220 x200) + 91 {(Left side x base) + right side}. The final answer is '44091'.