Monday, January 16, 2017

Angular Factory for $resource and dynamic header content

I created a Factory recipe for my $resource instance.

There are multiple ways to create it. I used this approach.

  angular.
    module('myModule').
    factory('myService', ['$resource', '$localStorage',
      function($resource, $localStorage) {
        var url = 'xxx';
        return $resource(url + '/my-resource/:id', { id: '@id'}, {
          query: {
            method: 'GET',
            headers: {
              'Content-Type': 'application/json',
              'Accept': 'application/json',
              'X-Auth-Token': $localStorage.token
            },
            isArray: true
          },
 ...
]);
It was working fine till I notice this issue.

If I logout and login as different user the old token was being used!!!

This is because the Service and Factory instances are Singleton. On logout we clear data and not the object of the app in the browser. The first value of $localStorage.token is used to create the instance and that does not get updated even when the value $localStorage.token changes.

One approach to fix this would be create 
  angular.
    module('myModule').
    factory('myService', ['$resource', '$localStorage',
      function($resource, $localStorage) {
        var url = 'xxx';
return { GetInstance: GetInstance };
function GetInstance(token) {
        return $resource(url + '/my-resource/:id', { id: '@id'}, {
          query: {
            method: 'GET',
            headers: {
              'Content-Type': 'application/json',
              'Accept': 'application/json',
              'X-Auth-Token': token
            },
            isArray: true
          },
}
 ...
]);

In the controller  myService.GetInstance( $localStorage.token).query()

This is more work.

The better approach would be to get the 'X-Auth' dynamically. This can be achieved by using a function instead of value.

angular.
    module('myModule').
    factory('myService', ['$resource', '$localStorage',
      function($resource, $localStorage) {
        var url = 'xxx';
function GetToken() {
 return $localStorage.token;
}
        return $resource(url + '/my-resource/:id', { id: '@id'}, {
          query: {
            method: 'GET',
            headers: {
              'Content-Type': 'application/json',
              'Accept': 'application/json',
              'X-Auth-Token': GetToken
            },
            isArray: true
          },
 ...
]);

Thursday, December 22, 2016

Sublime copying all text that match a pattern

I had a text file containing numbers in between text that I wanted to copy, move to a new file and format them.

Since the numbers were of of fixed length and at the same index I could have copied them using vertical select. The vertical select is tricky if the file is big and it wouldn't help if the pattern to looks for is different and can occur anywhere in the text.

I knew the first way was to write a regular expression for the pattern that I was looking for. I could highlight all the matches with my pattern. But now how do I copy it?

https://forum.sublimetext.com/t/copy-matching-lines/5877/2

I couldn't beleive it could be as simple as clicking Find All. The Find All gives a cursor to all the match with text selected. Amazing!!!

So Ctrl+c will copy all of them. Bravo!

I put the data on another file where I have to format them. This was easy. I used replace and group capture.

Find :      (455\d\d\d\d\d\d)
Replace : ($1),

The above will put all the matches within ( ... ),

Again I would have used Find All and used the cursor to modify all the words and put the brackets around them.

Thanks to sublime!!

Wednesday, December 21, 2016

Creating a dynamic table out of list of numbers to join


I had a list of ids and I wanted to check which rows are not present in the DB.

We can use 'in (1, 2, 3)' and that would say which ones are in, and 'not in' would return rows outside the ids I had.

So I wanted to make a table out of the ids and use left or right join.

I could not find any simple way of doing this and then the approach mentioned here http://stackoverflow.com/questions/8002178/how-to-select-ids-in-an-array-of-ids

select * from (
select TO_NUMBER(xt.column_value) id1 from
xmltable(
'123456,
123457,
123458,
123459') xt) a
left join my_table mt on mt.id = a.id1
where mt.id is null

Indeed I had 1000 of Ids in my list and not the above 4 :)

I choose left join as my_table had thousands of rows.

I really appreciate effort of anyone to answer on SO.

Tuesday, December 20, 2016

Query join with View - Oracle Performance

I had a slow running query that joined on two views, A and B. I had this wrong understanding that the query is slow as it prepares the whole view and then runs the query on the view. This was totally incorrect.

Consider view in a join as a sub-query. The optimizer would try to minimize the cost.In my case the join with view A performed better than the join with view B. Both had approximately same amount of data.

I went through the following articleshttps://blogs.oracle.com/optimizer/entry/optimizer_transformations_view_merging_part_1https://blogs.oracle.com/optimizer/entry/optimizer_transformations_view_merging_part_2https://blogs.oracle.com/optimizer/entry/basics_of_join_predicate_pushdown_in_oracle
And the explain plan proved it all.The join with view A was merged as index joins in the query with base table. The reason being view A had none of this (was a simple select with inner join)
  • UNION ALL/UNION view
  • Outer-joined view
  • Anti-joined view
  • Semi-joined view
  • DISTINCT view
  • GROUP-BY view

While View B had many outer joins and an outer join with another view, C. The outer joins on tables were moved to View pushed predicate.


The optimizer does a view merge for a view with outer join when it is possible to do so.

The join on view C was expensive as it was a HASH join.

Thursday, August 18, 2016

Avro - schema change compatibility

I was under the impression that I can add an optional field to Avro schema and consumers with old schema would still be able to read the message whether or not the newly added optional filed is set or not in the record.

So I added an optional field to an Avro schema, generated an Avro from it and I was able to read it from consumers using the old and new schema (this might not work if the schema changes are not at the end).

But this is not how it should work as the Avro schema is not forward compatible. And that is what happened and one of the consumers started to fail.

So I was surprised why my code was working and the other code (using older schema) was throwing errors when reading the avro created out of new schema. This is where it was failing


    BinaryDecoder binaryDecoder = DecoderFactory.get().binaryDecoder(is, null);
    while (!binaryDecoder.isEnd()) {
      DatumReader<MyClass> datumReader = new SpecificDatumReader< MyClass >(MyClass.class);
      datumReader.read(null, binaryDecoder); // failure here
    }

The difference was, I didn't had the while loop in my code, assuming the input stream represented a single record, while the other consumer was handling a batch of requests.

So the above code would read part of the avro byte array as per the old schema in the first iteration and the remaining bytes for the new field (even though it is not set) in the second iteration. Since the left bytes was not a complete record it was reporting errors.

Tuesday, March 29, 2016

Map.get - KeyNotFound error

It is interesting how different languages approach towards a given problem.

Let's have a look at how different languages react while trying to read a key from dictionary

Java

map.get('key_that_does_not_exist')

=> null

Ruby

map['key_that_does_not_exist']

=> nil 

Python

>> x['key_that_does_not_exist']
Traceback (most recent call last):
 File "<stdin>", line 1, in <module>
KeyError: 'key_that_does_not_exist'

>> x.get('key_that_does_not_exist)
=> None

Scala

val x = Map("a" -> "1”)
x("key_that_does_not_exist”) // Throws exception
x.get("key_that_does_not_exist”) // None

Sunday, February 22, 2015

JDK in OSX

I wanted to figure out the Java installation on OSX so that I can easily switch between JDK 6,7 and 8.

I got my answer from this post http://stackoverflow.com/questions/15120745/need-help-understanding-oracles-java-on-mac

In summary

When we install Java using a dmg it goes here /Library/Java/JavaVirtualMachines/

This command is handy to figure this out

$ /usr/libexec/java_home 

/Library/Java/JavaVirtualMachines/jdk1.8.0_31.jdk/Contents/Home

java_home man
https://developer.apple.com/library/mac/documentation/Darwin/Reference/ManPages/man1/java_home.1.html

Let's see how this gets connected with java command.

$ java -version
java version "1.8.0_31"
Java(TM) SE Runtime Environment (build 1.8.0_31-b13)
Java HotSpot(TM) 64-Bit Server VM (build 25.31-b07, mixed mode)

$ which java
/usr/bin/java

$ ls -al /usr/bin/java
lrwxr-xr-x  1 root  wheel  74 Apr  2  2014 /usr/bin/java -> /System/Library/Frameworks/JavaVM.framework/Versions/Current/Commands/java
</pre >

/System/Library/Frameworks/JavaVM.framework/Versions/Current/Commands/java is actually a proxy to actual installation of java!

Let's see where does this proxy end up

$ sudo dtrace -n 'syscall::posix_spawn:entry { trace(copyinstr(arg1)); }' -c "java -version"
dtrace: description 'syscall::posix_spawn:entry ' matched 1 probe
dtrace: pid 19903 has exited
CPU     ID                    FUNCTION:NAME
  0    638                posix_spawn:entry   /Library/Java/JavaVirtualMachines/jdk1.8.0_31.jdk/Contents/Home/bin/java

from the post
A combination of factors are considered. JAVA_HOME is used if set (try JAVA_HOME=/tmp java). If JAVA_HOME is not set then the list of all virtual machines on the system is discovered. The JAVA_VERSION and JAVA_ARCH environment variables are used, if set, to filter the list of virtual machines to a particular version and supported architecture. The resulting list is then sorted by architecture (preferring 64-bit over 32-bit) and version (newer is better), and the best match is returned.
The proxy is intelligent enough to find the Java installation and java will work for you. But what if you want to switch between multiple JDKs. Simple, pass the instruction through JAVA_HOME. export JAVA_HOME=/Library/Java/JavaVirtualMachines/jdk1.7.0_71.jdk/Contents/Home

No need to add to the PATH :) 

I love this proxy concept. An Apple A Day, Keeps lot of worries Away

Sunday, January 11, 2015

eclipse

Adding more support to your eclipse (say you have only Java support and you want to add web support)

1. Download eclipse with Web support
2. Or, add required module through Help - Install New Software - http://download.eclipse.org/releases/luna (your release)

Extract the downloaded zip/tar. Run ./eclipse or double click the Eclipse App from inside the downloaded folder. Eclipse needs java in path or JAVA_HOME set.

Plugins are stored inside the downloaded eclipse directory. eclipse.ini path <downloaded eclipse directory>/Eclipse.app/Contents/MacOS/eclipse.ini. So they work across workspaces.

The preferences are per workspace so they are installed inside workspace. e.g. Run configurations and editor preferences. There are ways to keep them common across workspaces.

Eclipse Java and Configurations

Update the eclipse.ini as per your need.

AspectJ Support

Add plugins from https://eclipse.org/ajdt/downloads/

AspectJ Compiler and AspectJ Development Tools

Maven Support

Add m2e connectors from m2e marketplace

Open pom file. Overview section would show error. Click on the error - Click discover new m2e connectors

Maven Integration for AJDT, m2 connector for build-helper-maven-plugin

eclipse and maven

I have become a fan of Maven (there is no surprise to it).

Download and install instructions (bottom of the page) on this page http://maven.apache.org/download.cgi. It is better to use the zip/gz and set the appropriate env than using the yum installation.

Default repository location - ~/.m2
Settings.xml - ls $M2_HOME/conf

Maven eclipse integration - http://maven.apache.org/eclipse-plugin.html
It comes with the eclipse j2ee installation (check if you have import maven project option. If not install the plugin)

Now let's configure Eclipse to use the maven we downloaded (so that we use same maven and same settings as used from console)

Preferences - Maven

1. Installations: Click Add and point to the extracted maven directory. Make it default. Eclipse comes with embedded maven and that will be used otherwise.
2. User Settings - Point to the settings.xml that you wish to into the Global and User Setting. You can point to the one that comes default with the maven package, use a company specific one or your custom one.
3. Make sure the Local Repository is same as you expect. (matches what you intended from the console)
You can look into other settings but with this we are good to go.

To import a maven project into eclipse. File - Import - Existing maven project. Now eclipse honors your project POM file for all project lifecycle options (build, deploy). Update to POM file automatically reflect in the project. To manually perform this task. Right click Project - Maven - Update project

Create a new Maven project is. New -> Maven project

Update the Java version for your Maven project

  <build>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>3.2</version>
        <configuration>
          <source>${java.version}</source>
          <target>${java.version}</target>
        </configuration>
      </plugin>
    </plugins>

  </build>

Define java.version property with the version you want. The connector automatically associates to the installed jres (in your eclipse) to the version you specified.

e.g. Say you have Jdk 1.7 and 1.8 installed and added to your eclipse.

java.version = 1.7 - associates jdk 1.7 to the project
java.version = 1.8 - associates jdk 1.8 to the project
java.version = 1.6 - associates jdk 1.7 to the project

we can have source and target at different version (source always at higher). Say we want to write code in 1.7 which will run on a 1.6 jre installation (you are under migration but your servers are still at 1.6). This will keep the JRE at 1.7 but set the compiler to 1.6.

Some of the important aspects to consider

1. Scope in a dependency - compile, runtime, provided, test, ..
2. Maven life cycles
3. Sub modules
4. Optional is a valuable keyword for dependency if you are creating a library. This will help users to ignore that dependency add their own. Say, a newer version of Logger.

'maven deploy' pushes your build artifact to the nexus repository (you have to specify repository in your maven file or it can be present in the setting.xml (top pom))

When you specify a dependency in your pom three things might happen (Maven dependencies has the references)

1. if the project is in the same workspace (uses the project directly)
2. If the project is not present it looks into the local repository (~/.m2/repository)
3. If not found in the local repository it will download from nexus and add to local repository

# My build does not pick changes from a dependent jar

The third one is a tricky one (there are configurations to play with how the dependency is resolved). Suppose you change API but have not updated the version of the artifact. When you build this project and deploy the nexus is updated with the latest changes. Now a dependent project does a maven build but still finds the old jar. This is due to the local repository already having the jar from previous build. So it is always recommended to update the artifact version.

Sunday, July 13, 2014

Exception - How to manage different exceptions your code wants to throw?

Often I have seen multiple Exception types created to handle different types of exceptions. In Java they all become individual classes. They might all extend from a common Exception class defined by you. But as Java does not support inheritance of constructors we end up having multiple classes with similar code. Not if it was Ruby code :)

Anyways. I got the following thought from the way Http defines Status Codes.

http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html

Define broad level Exception classes.

Informational, Success, Bad Request, Server Error. You may or may not have Redirection. In each of these Exception classes have status code. If possible, try to keep the status codes matching to the http status codes.

Now you have limited set of classes and you can set specific status code based on a scenario.

class InformationalException {
  public static enum InformationalStatus {
     // you have avoid it by using directly http status from commons client
  }

  public InformationalException(InformationalStatus status, String message, Throwable th) {
  }
}

The good news is most of the clients understand the http codes and it is no longer a magic.

Less code is less pain.

Thursday, June 12, 2014

Oracle Database - reading a CLOB through sqlplus

If you execute  through sqlplus 'select <clob column>' you will get truncated data. To see the complete data in sqlplus

set buffer <X>

select DBMS_LOB.substr(column, <buffer size>) from table

Note: This is required for sqlplus. JDBC and hibernate handle it directly so this 'select column' should be good enough.

Wednesday, June 11, 2014

SQL equal or not equal and Mr NULL

I created a table with a column

IS_CONFIDENTIAL CHAR(1)

and I added this clause to my query if the user should not see the confidential data

AND IS_CONFIDENTIAL <> 'Y'

Now the user could not see the confidential records. All good!

But he could not see few other records which were not confidential ?? I checked the table and found records with IS_CONFIDENTIAL values 'Y','N' and NULL and the records with NULL value didn't show up !!

Oracle Database 11g Enterprise Edition Release 11.2.0.3.0 - 64bit Production

IS_CONFIDENTIAL <> 'Y' does not mean it to return rows with NULL value !!

This is what I did (though there could be multiple solutions and may be better :))

IS_CONFIDENTIAL NOT NULL  CHAR(1)

Or I should have changed my query to AND ( IS_CONFIDENTIAL = 'N' OR IS_CONFIDENTIAL IS NULL)

Note: I agree that positive check is always better. But I thought what if there are more values than Y and N, IS_CONFIDENTIAL<> 'Y' was the choice for me.

Bulb: Did you know you can enable hibernate to print the queries it executes! It is very helpful.

Bulb: CHAR vs VARCHAR


curl command to error out on HTTP error codes

$ curl -I -f "http://json.org/example"
HTTP/1.1 200 OK
Date: Tue, 10 Jun 2014 20:01:02 GMT
Server: Apache
...

Ah! but it does not provide the / at the tail. We get a 404.

$ curl -I "http://json.org/example/"
HTTP/1.1 404 Not Found
Date: Tue, 10 Jun 2014 20:01:15 GMT
Server: Apache
...

echo $? will return 0 in both the case.

What if we are using it inside a script and want the process to fail on non success HTTP status?

Use –f option

$ curl -I -f "http://json.org/example/"
curl: (22) The requested URL returned error: 404

$ echo $?
22

The man says:

       -f, --fail
              (HTTP)  Fail  silently (no output at all) on server errors. This is mostly done to better enable scripts etc to better deal with failed attempts. In
              normal cases when a HTTP server fails to deliver a document, it returns an HTML document stating so (which often also describes why and more).  This
              flag will prevent curl from outputting that and return error 22.

              This  method  is  not  fail-safe  and  there  are occasions where non-successful response codes will slip through, especially when authentication is
              involved (response codes 401 and 407).

Note: -I is better option to user than --request HEAD, as --request HEAD request will hang for a while :)

Tuesday, May 20, 2014

session_privs and session_roles

So my friend was getting 'SYS.DBA_IND_COLUMNS table does not exit' while he was trying to run

desc dba_ind_coulmns

I was able to run the same query. So of course we are in the territory of authorizations. But how do I prove it?

Then can session_privs and session_roles into picture. For me the result was

SQL> select * from session_privs;

PRIVILEGE
----------------------------------------
CREATE SESSION
ALTER SESSION
SELECT ANY TABLE
SELECT ANY SEQUENCE

SQL> select * from session_roles;

ROLE
------------------------------
CONNECT
SELECT_CATALOG_ROLE
HS_ADMIN_ROLE
HS_ADMIN_SELECT_ROLE
HS_ADMIN_EXECUTE_ROLE
ADHOC

6 rows selected.

His user has more privilege but less roles.

For him the role was ADHOC_DML and no admin table roles and hence it was proved why he got 'table does not exist' error.

Another interesting table to know is v$session.

select SID,MACHINE,USERNAME from V$SESSION;

Friday, May 9, 2014

Git commands

> Log commits for a user since. Print in one line and with abbreviated commit

git log --committer=appandey --no-merges --since=2014-04-24 --pretty=oneline --abbrev-commit

> add all java files

git add \*.java

> Commit on someone else's behalf (same team)

git commit --author='author' -m '..'

fatal: No existing author found with 'author'

git commit --author='author <author@company.com>' -m '..'

e.g. git commit --author="amodpandey <amodpandey@gmail.com>" -m '...'

Monday, May 5, 2014

Did you know java TimeUnit class

I am not sure why Date din't do it, Calendar didn't do it and Apache DateUtils didn't do it?

Though every other product in every other company would need date diff !!!

I was introduced to TimeUnit through a retry wrapper class where I had to specify retry time using TimeUnit. So no longer we need a variable like private static final long numberOfSecondsInMinutes  = 60; :)

Date diff is a two liner with this class

1. Get the diff in millis
long diff = date1.getTime() - date2.getTime();

2. TimeUnit.MILLISECONDS.toDays(diff);

nJoy

Friday, April 18, 2014

Http Accept - 406

There are many HTTP headers which go unnoticed. Accept is one such header.

Accept is a way to tell the server what the client accepts. A server can smartly handle this.

e.g. Accept: application/xml to a service returns xml and Accept: application/json to same end point should return json.

Sometimes we have the server side code as to strictly say we support requests with Accept: application/json. I have seen ROR doing it when the return is of type JSON.

If the Web server detects that the data it wants to return is not acceptable to the client, it returns a header containing the 406 error code. 

I have observed the below behavior with our ROR set up

Success

Accept: application/xml,application/json
Accept: application/json,application/xml"
Accept: application/xml;q=0.9,application/json
Accept: */*

Failure (406)

Accept: */*, application/xml;
Accept: application/xml,*/*;
Accept: application/xml;q=0.9,*/*;
Accept: application/xml;

Note: I see */* is successful if Accept has only one entry with */*, else it expects application/json somewhere.

Chrome browser by default sends (may depend on browser and version)

  1. Accept:
    text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8

So again it will fail. There are browser add ons which can help manipulate any of the HTTP headers for calls made through the browser. I have to use that for this call.

HTH someone, someday

Sunday, March 2, 2014

The Orthodox: Java getter and setter

Let me start by saying, 'I hate it'. I always wrote it for the sake of writing it. Before making any decision I would like to bounce it with the community.

In the recent years most of us write Java classes that fall into one of these categories.

1. The so called POJO - Only attributes with getters and setters. Mostly used for serialization/de-serialization (to DB row, to JSON, to XML or any thing). - The carriers

2. The business processing classes where we tend not to have any instance variables. These are like the singleton instances doing the processing accepting/returning some POJO. - The performers

e.g.

class Customer {

public Integer addCustomer(MyCustomerBean bean) {
}

public MyCustomerBean get(Integer custid) {
}
}

So there is no instance variable dependency between two methods.

Further we get details from multiple such POJOs (e.g. MyAccountBean, MyCustomerBean) and put it into another POJO that is response to a service or UI call. These days annotation based mappers are quite famous, which would give you ResponseVO from  MyCustomerBean and MyAccountBean with a single line of code. I do remember how much tough life was without them (e.g. Castor for XML)

In none of these classes, ResponseVO, MyCustomerBean or MyAccountBean, the getters and setters do anything other than getting the variable and setting the variable. Is there any reason for which we keep doing it and blame it on to encapsulation??

Give me any good reason they are required for and yet they occupy 30-40% of your code! Thanks to eclipse to provide us with getters and setters generators. Just to add to the misery we add javadocs to such code ( again thanks to the auto javadoc generators).

Let's think whether they are required for our POJOs' or not? Can we have our POJO attributes public and lessen the pain.

Do I suggest having the Customer class with all the attributes and associated methods? Probably not. Today MyCustomerBean might a hibernate dependent bean and tomorrow it may be Toplink based. Let's give getters and setters a peaceful send off.

It may sound touche but YAGNI principle is really important to follow when the life of a code is too small to think about improvements, mostly it is re-writes.

What if I need to add a logic in my getters and setters won't help! :)

Code less that makes sense!
Happy Coding!

Wednesday, December 18, 2013

Third party calls (CORS), cookies and more...

1. Cookies set from the parent domain are by default not sent to the third party domain in xhr calls even if they from the common parent.

e.g. a.example.com and b.example.com

withCredential = true does the trick

2. Cookies set by the third party domain are not set on the client. The client ignores those headers.

Hibernate: OneToOne Mapping and fetch type

If a bean has a property that has mapping that is OneToOne mapping it is better to make the FetchType to EAGER.

EAGER FetchType will add to the main query.

LAZY makes the property to be loaded later, but is not the case with OneToOne mapping. After the bean is loaded all the OneToOne properties are loaded through a separate query if the FetchType is LAZY and will slow the loading.

Take away: OneToOne mapping FetchType.EAGER