Social Icons

Pages

Featured Posts

Tuesday, April 19, 2016

Blockchain use cases

http://thefinanser.com/2016/01/the-heart-of-the-blockchain-use-case-digital-proof.html/

https://www.scribd.com/doc/303933279/Blockchain-Technology-and-Applications-from-a-Financial-Perspective

http://fsblog.accenture.com/capital-markets/wp-content/uploads/sites/2/2015/06/CM_ATS_POV_Blockchain_in_the_Investment_Bank-web.pdf

Monday, March 28, 2016

Software developer random useful things

Useful searches

regular expression to match all the words in a sentence after "="
\=(.*)

How to delete a tag in git and push it.

For example the name of the tag is payreceive-xxxx/payreceive-xxxxx-2.1.0
Then execute the below commands

git tag -d payreceive-xxxx/payreceive-xxxxx-2.1.0
git push origin :payreceive-xxxx/payreceive-xxxxx-2.1.0

Sunday, November 2, 2014

Oracle for update skip locked functionality explained

Often you will come across a use case where you have lot of data in your database which has to be processed from multiple machines. There are few ways to do this

One master server which allocates batches or work to different slave machines. For example first 100k records in slave 1, second 200k records in slave 2..and so on. In this case the master is responsible for ensuring that the same record is not processed in two different slave machines. The disadvantage of this approach is, the master is now controlling the shots and if for some reason the master node fails then it becomes tedious to control. Also master has to keep track of what slave machines are online and keep checking whether they are available and processing.

However in Oracle this new functionality called for update skip locked is introduced. An example of how this works is

You have a table which contains 100 records. Another process is adding records to this table at regular intervals.

select * from <table> where <condition> for update skip locked;

Slave 1 issues this query which picks all the records which meets the condition given. For example all the 100 records are now picked up by slave 1 for processing. Please note the for update, locks those records so if you give any update query at this point in time, the update query has to wait for slave 1 to commit or roll back the transaction. But a select query will return all the 100 records.

Now if another process adds 20 records in the table so the count is now 120. If a normal select query issued at this moment from Slave 2, then all the 120 records will be fetched.  However if the select query contains skip locked class added then only the records which are not locked by any other process is returned.

select * from <table> where <condition> for update skip locked;

This will only return the new added 20 records not the 120 records. So you can process the records from multiple machines and you dont have to allocate data ranges for different machines to process.

However this does have a catch. If you want to add rownum to the query then you have a problem. In the same example above, in the initial 100 records, if you select only the top 50 by adding rownum to the query like below.

select * from <table> where <condition> for update skip locked where rownum < 51;

It will return the first 50 records and if the Slave 2 issues the same query with the rownum < 51 , it will return zero rows. It looks like the Oracle adds the skip locked filter after applying row number filter. So it selects the first 50 by applying rownum and then it skips the locked records which returns only zero rows.

I am not sure is there any straight forward solution for this problem.  For now, if you dont have to restrict your records using rownum concept then you can use skip locked for general purposes.

Wednesday, November 27, 2013

Java 7 Tutorial - For beginners

In Java 7, few minor language changes are introduced. It is not a major change like Java 5.

You can use underscores in numeric literals. For example the below is allowed in Java 7

private long reallyLongNumber = 1_000_000_000_000l;
private float entryFee = 10.5_5f;

There are certain rules on where you can use the underscore. 

Also to make the code more compact some un ncessary overheads has been removed. For example one of the overhead with generics is that if you want to define a hash table with Long value as key and ArrayList of Long as value you need to declare like this.

//Normally you have to declare like this
Hashtable<Long, ArrayList<Long>> employeeListByBank = new Hashtable<Long, ArrayList<Long>>();

The new Hashtable part also you need to repeat the samething which makes it bit difficult. In SE 7 you dont have to do that. You can just declare like this.

HashMap<String, ArrayList<String>> employeeListByBankInSE7 = new HashMap<>();

Which makes the program little less complex. I agree this is not a major improvement but reduces bit of coding.

Full example code below.

package com.ravi.java7learning;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Hashtable;

public class Java7Improvements {

/**
* At the beginning or end of a number
*  Adjacent to a decimal point in a floating point literal
*  Prior to an F or L suffix
*  In positions where a string of digits is expected
*/
private long reallyLongNumber = 1_000_000_000_000l;
private float entryFee = 10.5_5f;
//private float wrongEntryFee = 10._55f; - This will give compilation error because adjacent to dot (.)

public static void main(String a[])
{
Java7Improvements instance = new Java7Improvements();
System.out.println("Long number with underscore allowed :"+instance.reallyLongNumber);
System.out.println("Float value with underscore :"+instance.entryFee);
}

private void lessVerboseForTypeArgumentsExample()
{
//Normally you have to declare like this
Hashtable<Long, ArrayList<Long>> employeeListByBank = new Hashtable<Long, ArrayList<Long>>();

//In SE 7 you can avoid this and declare like this
HashMap<String, ArrayList<String>> employeeListByBankInSE7 = new HashMap<>();

//This make the declaration less verbose. Note that you still need to provide the <> to make it
//generic.

HashMap<String, ArrayList<String>> withoutGenerics = new HashMap();

//Otherwise compiler will throw warning that, we are using raw type of hashmap.
}

}

Java 7 Tutorial - Try with Resources Example

In SE7 try/catch has been improved with the feature called try with resources option.
Normally when we use any resources (like File reader, Input stream etc) we need to close them explicitly to avoid performance penalties. Also you need to use finally block also in case if there are any exception happens before you close them.

For example when you try to read input from a file you normally code like this

/*
 * Normal try/catch block.
 */
BufferedReader reader = null;
try {
reader = new BufferedReader(new FileReader(new File(
"c:\\tutorial\\HelloWorld.txt")));
String output = reader.readLine();
System.out.println("output :"+output);
reader.close();
} catch (IOException ioe) {
ioe.printStackTrace();
}
finally
{
try {
if(reader != null)
{
System.out.println("finally block called :");
reader.close();
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

This is too much verbose and you have close the reader. If you forgot to close the reader then it unnecessarily blocks the resources and it will be a big performance bottleneck.

To avoid that SE 7 comes up with a feature called try with resources option. Here you open the resources with in try and once the code with in the try block completes, compiler automatically closes the opened resources so you dont have to close them explicitly.

try (BufferedReader reader1 = new BufferedReader(new FileReader(
new File("c:\\tutorial\\HelloWorld.txt")))) {
String output = reader1.readLine();
System.out.println("output in try with resource :"+output);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

Complete working example given below.

package com.ravi.java7learning;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;

public class Java7TryWithResources {

public static void main(String[] a)
{
Java7TryWithResources sample = new Java7TryWithResources();
sample.tryWithResourcesExample();
}

private void tryWithResourcesExample() {
/*
* Normal try/catch block.
*/
BufferedReader reader = null;
try {
reader = new BufferedReader(new FileReader(new File(
"c:\\tutorial\\HelloWorld.txt")));
String output = reader.readLine();
System.out.println("output :"+output);
reader.close();
} catch (IOException ioe) {
ioe.printStackTrace();
}
finally
{
try {
if(reader != null)
{
System.out.println("finally block called :");
reader.close();
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

try (BufferedReader reader1 = new BufferedReader(new FileReader(
new File("c:\\tutorial\\HelloWorld.txt")))) {
String output = reader1.readLine();
System.out.println("output in try with resource :"+output);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}

Java 7 Tutorial - Strings in switch statement

In Java 7 you can use String in switch statement which was not possible before.

For example before SE 7 you have to use if, else if statements to compare the string values but now you can use Switch statements.

package com.ravi.java7learning;

public class Java7Feature {

public static void main(String[] a)
{
Java7Feature feature = new Java7Feature();
feature.switchDemo("No 1");
feature.switchDemoBeforeSE7("No 1");
}

private void switchDemoBeforeSE7(String input)
{
if(input.equals("No 1"))
{
System.out.println("You have entered :"+input);
} else if(input.equals("No 2")) {
System.out.println("You have entered :"+input);
} else if(input.equals("No 3")) {
System.out.println("You have entered :"+input);
} else if(input.equals("No 4")) {
System.out.println("You have entered :"+input);
} else if(input.equals("No 5")) {
System.out.println("You have entered :"+input);
} else {
System.out.println("You have entered something different :"+input);
}
}

private void switchDemo(String input)
{
switch(input)
{
case "No 1":
System.out.println("You have entered :"+input);
break;
case "No 2":
System.out.println("You have entered :"+input);
break;
case "No 3":
System.out.println("You have entered :"+input);
break;
case "No 4":
System.out.println("You have entered :"+input);
break;
case "No 5":
System.out.println("You have entered :"+input);
break;
default:
System.out.println("You have entered something different :"+input);
}
}

}



Java 7 Tutorial - Catching Multiple Exceptions

Java 7 allows catching multiple exceptions in the single catch block.
Prior to Java 7 if a method can create multiple exceptions then each
exception has to be handled seperatly.

So it must be
try
{
}
catch(IOException 1)
{
handleException(1);
}
catch(ParseException 2)
{
handleException(2)
}

This will lead to duplicate code in each catch block. Now with SE7 this can be
avoided and it can be handled in the same catch block.

package com.ravi.java7learning;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Date;
import java.text.ParseException;
import java.text.SimpleDateFormat;

public class SampleExceptionHandler {

public static void main(String a[])
{
try {
FileWriter writer = new FileWriter(new File("c:\\tutorial\\HelloWorld.txt"));
SimpleDateFormat format = new SimpleDateFormat();
Date parsedDate = format.parse("07/10/96 4:5 PM, PDT");
writer.write("HelloWorld");
writer.flush();
writer.close();
} catch (IOException | ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}

}

 
Blogger Templates