Skip to content
May 26 14

Code challenge: Flow of control

by aloshbennett

Can you make this better?

The API needs to store the latest version of a record. It does so by checking the revision number of the record being inserted against the current version in the db. If the revision number of the new record is greater, it is inserted. Otherwise it is ignored. The db provides conditional APIs to perform check-and-update operations. Give it a shot.

DB APIs:

//Gets the current record. Returns null if no record is found.
public Record getRecord(String primaryId);
 
//Inserts a record. Throws exception if a record is already present.
public void insert(Record rec) throws ConstraintViolationException;
 
//Updates a record. Throws exception if the record is not of a 
//newer revision - basically the verison number should be increasing
public void update(Record rec) throws ConstraintViolationException;

And here is my attempt:

public void storeLatestVersion(Record newRec) {
    String primaryKey = newRec.primaryKey;
    Record currentRec = getRecord(primaryKey);
    if (currentRec==null) {
        try {
            insert(newRec);
            //done. lets go home
            return;
        } catch(ConstraintViolationException ce) {
            //Some one bet me to it. Let me fetch the current rec
            currentRec = getRecord(primaryKey);
        }
    }
 
    //at this point currentRec cannot be null.
    //making such an assumption makes me sad :(
 
    if (currentRec.versionNo < newRec.versionNo) {
        try {
            update(newRec);
            //yey! job well done
            return;
        } catch(ConstraintViolationException ce) {
            //someone has updated the record in the mean time?
            currentRec = getRecord(primaryKey);
        }
    }
 
    if (currentRec.versionNo >= newRec.versionNo) {
        //nothing to do as there is later version in the db.
        //lets ignore the current record.
        return;
    } else {
        //this can't be happening. something went wrong.
    }
}
Oct 23 11

30 sec puzzle: spot the null pointer

by aloshbennett

I got a null pointer exception at the line mentioned. What could have caused it?

private ARTTimerTask timerTask = ARTTimerTask.getInstance();
...
public void notify(String messageId, Long messageTime) {
    ...
    //NPE in the line below
    timerTask.addMessage(messageId,
                  System.currentTimeMillis() - messageTime);
}

Here’s the relevant section from ARTTimerTask:

private static final ARTTimerTask _INSTANCE 
                                = new ARTTimerTask();
 
public static ARTTimerTask getInstance() {
    return _INSTANCE;
}

(Debashish comes over to the desk, takes a look and proclaims, “This A*** B***** is pathetic”. 7 seconds!)

Apr 30 11

Moving on

by aloshbennett

I’ve decided to call it a day at Oracle. And its been a long day too.

As part of Innovation Labs at 24/7 Customer, my new organization, we are building a platform for predictive analysis and intervention in CLM area. A lot of exciting things on the plate already and the team is very passionate and dynamic. Small organizations are really nimble footed and flexible, and I’m liking it.

In fact, I find myself recruiting already. In case you are interested, reach out to me at gmail.com. The id would be aloshbennett. A more detailed job description can be found here.

Mar 31 11

Introduction to WebServices – IEEE Session

by aloshbennett

An introductory session on web services was conducted at BITS Plianl campus at Hyderabad in association with IEEE on 26th March 2011. The talks introduced the students to XML, WSDL and SOAP and REST styles of services.

I have to say that I am impressed with the students who turned up at the event. Some of them are into very interesting projects and it feels great to see them do things over and above the curriculum.

With me were Bhaskar Ghosh and Neil Ghosh who actually delivered the talks and conducted the hands-on lab.

Overall, a very pleasant experience. Special thanks to Dr Geetha Deviprasad who made the event possible.

The event is covered in detail at Neil’s blog.

Feb 15 11

HTML5 Canvas Sample – Building a maze

by aloshbennett

There are a lot of features that HTML5 bring. What makes canvas popular?


There!

Canvas provides a screen area onto which you could draw 2D shapes and images. The set of APIs are simple. But the strength of the canvas comes from how you could manipulate it using javascript.

We start by defining a canvas. Note that the canvas element would be rendered only if your browser supports it.

<canvas id="canvas" width="100" height="100">Your browser does not support canvas.</canvas>

Next, we define an array in javascript to hold the maze data, initialize it and start building our maze by venturing out into the first cell.

    var board = new Array();
 
    function maze() {
        init();            //initialize the maze
        explorePath(1,1);           //start exploring
    }
 
    function init() {
        var canvas = document.getElementById("canvas");
        if (canvas.getContext) {
            context = canvas.getContext("2d");
            //paint the maze black
        }
        for(i=0; i< SIZE; i++) {
            board[i] = new Array();
            for(j=0; j<SIZE; j++) {
                board[i][j] = 0; //fill the board array with zeroes
            }
        }
    }

For the explorePath method, we use a recursive algorithm

Paint the cell white
Initialize options list {UP, DOWN, FORWARD, BACKWARD}
While options:
    Select option randomly
    If valid option - Cell is unoccupied, we don't create tunnels etc.
         explorePath(option)
     remove explore option from list
end loop.

In short, we are doing a back-tracking traversal (DFS) of the option tree.

The algorithm works fine until we try to put a delay between each explorePath call to get that nice effect.
You see, it is not really easy to sleep() in javascript. There is no equivalent of Java sleep.

What we could do is this:

  setTimeOut('explorePath()', 100);

This would call the explorePath after a delay of 100 milliseconds. But, the call to setTimeOut() is non-blocking and it would continue execution while scheduling explorePath() to be executed after 100 milliseconds.

We could maintain our own call stack and choose not to call the explorePath() recursively. But it is easier to just push the maze positions to be painted white into a stack and continue execution. Once the display stack is ready we could pop from it and render onto canvas in a delayed manner using setTimeOut().

Finally, we could add a nice touch to the maze by not changing its directions too often, but in a timely manner based on a seeded probability.

Here’s the complete source (right click and view source). And don’t forget to save your maze as a PNG (again, right click and save).

Dec 19 10

Implementation and Testing of Software Projects

by aloshbennett

Computer Society of India, Hyderabad Chapter had organized a Workshop on Project Development and I was lucky to give a talk on its implementation and testing aspects. Among the speakers was Mr Raju Kanchibhotla, Chairman of CSI, Hyderabad who gave an inspiring talk on taking IT to the masses.

Here are the slides from the talk.

Implementation and Testing of Software Projects

Dec 13 10

Functional programming the LISP way

by aloshbennett

Lately, there’s been a lot of talk about functional programming. The newer generation languages advertise themselves as multi-paradigm ( as opposed to java’s object oriented style). Java is doing catching up by bringing in Project Lambda in JDK 8, and Predicates has already started becoming popular (here’s a good starter by Arun Vydianathan).

This post is an introduction to a very potent and heady LISP cocktail of Higher Order Functions, Lamdas and Macros.

Summary:

Higher Order Functions: Functions that take other function as its argument. A function that filters a list can be converted into higher order function by providing the actual filtering function as an argument.

Lambda Expression: Dynamically creates functions and attaches a context to them. The filtering function can be created at runtime as a lambda and its bounds set at runtime.

Macro: Creates code at runtime by substituting the macro with corresponding expression. While Lambdas let you replace the bound conditions, macros let you decide the structure of your filter function.

Higher Order Functions
You could print all students belonging to “ELX” department from a list like this:

(defun print-students (students-lst)
  (loop for student in students-lst
       do ( if (equal "ELX" (getf student :department))
	       (print student))))

Now if you want to print the students who had received ‘A’ grade, you are at a loss. You cannot do that without writing a new function by duplicating the looping logic.

Enter Higher Order Functions. Higher Order Functions are functions which take another function as its argument. The example above can be modified into a generic higher order function if we could abstract the evaluation logic into another function.

Consider the following re-write

(defun print-students (students-lst, eval-student)
  (loop for student in students-lst
       do ( if (funcall eval-student student)
	       (print student))))
 
(defun eval-elx-department (student)
  ( if (equal "ELX" (getf student :department)) t))
 
(defun eval-a-grade (student)
  ( if (equal "A" (getf student :grade)) t))
 
(print-students *students-lst* #'eval-a-grade)

Note: LISP built-in functions REMOVE-IF and REMOVE-IF-NOT takes a list and a evaluation function, applies the evaluation function to every element in the list and returns a new list which contains/doesn’t contain the element. The idea of predicates is very similar to this.

Lambda Expressions
If we could parameterize the grade from the eval-a-grade function, we would have a generic eval-grade and that would have been just great!
Unfortunately, the print-students function expects an evaluation function that takes in a single argument: student.
Using Lambda expressions, it is possible to custom make a function with a context. Lambda expressions are used to create anonymous functions with a scope attached to them. The variables used within the lambda expressions are evaluated in the context they are run in.

(defun eval-grade (grade)
  (lambda (student)
    ( if (equal grade (getf student :grade)) t)))
 
(print-students *students-lst* (eval-grade "A"))
(print-students *students-lst* (eval-grade "B"))

Macros
LISP macro is a very powerful beast that can be used to generate code dynamically. Instead of having multiple evaluation functions, it is possible to write a LISP macro to dynamically generate a lisp function based on the inputs you pass to it.

(defmacro eval-student (&key dept grade)
  `(lambda(student)
       (and ,(if dept
		 `(equal ,dept (getf student :department)))
	    ,(if grade
		 `(equal ,grade (getf student :age))))))
 
(print-students *students-lst* (eval-student :dept "ELX" :grade "A"))

The eval-student macro expands to

LAMBDA (STUDENT) 
  (AND
    (EQUAL "ELX" (GETF PERSON :DEPT))
    (EQUAL "A" (GETF PERSON :GRADE))))

Note: Though very powerful, it is easy to cut yourself using macros. Use discretion when using them.

Functional programming is a great way to modularize and reduce the size of your code. More than that, it is fun! Happy coding.

Nov 25 10

Shortly – URL shortner for Firefox

by aloshbennett

Introducing Shortly, an extension for firefox to create short URLs using bit.ly service.

Take it out for a spin and let me know what changes and features you’d like to see.

Nov 16 10

Reflection and the missing Security Manager

by aloshbennett

Here’s an interesting trick that’s been around for a long time:

Consider the Person class here, with password as a private data member.

public class Person {
 
   private String name;
   private String password;
 
   public String getName() {
       return name;
   }
 
   public boolean login(String password) {
      if(this.password(equals(password)) {
         ....
      }
   }
   ...
}

The Java scope rules does not allow me to access or modify the password field that’s declared private. All the same, I could do it using reflection as shown below:

   Person person = db.queryPerson("alosh");
   //System.out.println("password: "+person.password); -- won't compile
 
   Field field = person.getClass().getField("password");
   field.setAccessible(true);
   System.out.println("password: "+field.get(person));
   field.set(person, "welcome");
   person.login("welcome");
   ...

It all boils down to this line of code.

   field.setAccessible(true);

All reflection access to an object (methods, fields, constructors) is through the interface AccessibleObject which lets the reflected object suppress the normal access controls. By setting the access flag, the reflected object is now open.
But the access flags are not flipped before it checks with the security manager. Reflection and SecurityManager together provides the power to control access dynamically.

Our little trick could then be attributed to the SecurityManager. Or like in this case, the lack of a SecurityManager.

By default the JVM does not have a SecurityManager available. A security manager could be installed either by passing the following option to the jvm

-Djava.security.manager

or by setting one in the code

System.setSecurityManager(new SecurityManager());

(Now the snippet mentioned in the beginning will not work.)

* SecurityManager is not enabled by default in the JVM.
* Majority of the JEE servers out there don’t run a SecurityManager unless asked for.
* Many applications would not run with SecurityManager in place.

Isn’t it against Java’s principle of ‘Secure by Default’?

Oct 26 10

Unity to replace GNOME in Ubuntu 11.04

by aloshbennett

Starting with Ubuntu 11.04 (codenamed Natty Narwhal), Unity would be the default shell of choice for the desktop edition.

Though Unity (already available on the netbook editions) is far from ready, there are a couple of cool features it brings to the table.

The immediate changes are aimed at reducing the wastage of screen space. The application menu (in the maximized mode) gets integrated into the top panel. So does the status bar – this would be replaced by an indicator on the top panel. This is a huge relief for people like me who runs firefox with extensions that hide menubars, trying to salvage every bit of screen space.

There is also the promise of a Mac styled dock and application switcher.

The Unity project plans to include zeitgeist, a sophisticated data engine that understands user activities and correlates them. This can provide useful context to search and browsing applications.

There are also plans to evolve from directory based file storage to something that’s more intuitive and search oriented (yey!! first step towards tag based systems).

GNOME 3 is coming next April, and there is a worry that throwing in a new desktop shell would divide the development community. Nevertheless I like Ubuntu’s aggressiveness when it comes to redefining the image of Linux.

In the good spirit of choice, GNOME and KDE would be very much available as login options.