Latest Entries »

I love to listen music while coding. First of all it helps me block out external distractions and secondly it makes me concentrate better.

Though i prefer instrumental music while coding, its good to get pumped up with couple of songs :

ROCKY TRIBUTE – EYE OF THE TIGER

Queen – We Are The Champions

Now time for some instrumental music

P.S. list is full of unconventional music

Death Note Soundtrack – L’s Theme 

First one is L’s theme from Death Note, absolute EPIC !!

Requiem for a Dream – soundtrack

This one is from the movie Requiem for a dream, one of my all time favorite.

Ewan Dobson – Time 2 – Guitar

The Arrancar Theme

Spanish classical guitar from anime Bleach

Metallica – Call of Cthulhu 

Masterpiece from Metallica

Epic Trance Techno- Fantasy

Inception Soundtrack-Dream is Collapsing

Santana – Europa

Godot – The Fragrance of Dark Coffee

This theme combined with http://www.rainymood.com/ forms an awesome combo.

The Matrix Soundtrack – Clubbed to death

Apocalyptica – Quutamo live

Half life 2 – Vortal Combat

Lastly, an awesome soundtrack from Half life 2

Also there are some good soundtracks here  http://www.amazon.com/Social-Network-Trent-Reznor/dp/B0043ISH6O/ . Also some of them like Fur Elise http://www.youtube.com/watch?v=P-B4sjUve3E and Clair de Lune http://www.youtube.com/watch?v=-LXl4y6D-QI didn’t qualified cause it is difficult to not get lost in them.

Here is a short intro on using elasticsearch filters with java.

Combining filters

        XContentFilterBuilder filter1 = FilterBuilders.orFilter(
				FilterBuilders.termsFilter("disclaimer","searchKeyword"),
				FilterBuilders.termsFilter("foootnote", "searchKeyword"))
				.filterName(filter11);
//filter name can be used later to find matched filter
		XContentFilterBuilder filter2 = FilterBuilders.orFilter(
				FilterBuilders.termsFilter("senderNumber","searchKeyword"),
				FilterBuilders.termsFilter("receiverNumber", "searchKeyword"))
				.filterName(filter22);

        XContentFilterBuilder filter33 = FilterBuilders.orFilter(
        		FilterBuilders.termFilter("messageText", "searchKeyword"),
        		FilterBuilders.termFilter("messageSubject", "searchKeyword").
        		filterName(filter33);

        // These filter can then be combined using one more filter
        XContentFilterBuilder messageFilter = FilterBuilders.orFilter(filter1, filter2, filter3);



Checking for presence of a keyword( i.e. NOT NULL)

Here’s an example in which a filter is created to check for the existence of a keyword “hiddenTag”.

XContentFilterBuilder existsFilter = FilterBuilders.notFilter(
        		FilterBuilders.missingFilter("hiddenTag"));
//This filter can then be combined with other filters like below
messageFilter = FilterBuilders.andFilter(messageFilter, notExistsFilter);



Pagination

By default only 10 results are returned. To return more results and apply pagination we can build our query like this :

qb = QueryBuilders.filteredQuery(qb, messageFilter);
builder.setQuery(qb).setFrom((pageNumber-1)*pageSize).setSize(pageSize);

I’ve been a heavy user of simpleDB  from past couple of months, here’s my advise to people working with date in simpleDB ::

As we all know that everything in simpleDB is a String, hence ALWAYS use a single time zone while storing dates, otherwise results might be error prone  as sorting is lexicographical. I’ll recommend Joda-Time(ISO8601 format) to store dates as it provides a quality replacement for the Java date and time classes with simple API. Preferably use Zulu time zone (i.e. GMT)

Here’s how to do it ::

DateTime date = new DateTime(DateTimeZone.forID("Etc/Zulu"));
message.setDate(date.toString());

While using date in your application return time in milliseconds as it is easier to use.The code below expresses the datetime as milliseconds from 1970-01-01T00:00:00Z

String time = message.getDate();
DateTimeFormatter fmt = ISODateTimeFormat.dateTime();
DateTime dt = fmt.parseDateTime(time);
time = Long.toString(dt.getMillis());

For my app, i’m storing data in Amazon SimpleDB, but since SimpleDB has a 1024 character per attribute maximum so larger values should be stored in S3.

I’ve a class named Details which can store MultipartFile as private byte[] multimedia ,which is to be stored in S3. This is how to do it


@Lob

@Basic(fetch=FetchType.LAZY)

@Column(name="multiMedia")

private byte[] 	multimedia;

The @Lob annotation specify that the  field should be persisted as a large object

Because the @Lob annotation is really just qualifying the basic mapping, it can also be accompanied by a @Column annotation when the name of the LOB column needs to be overridden from the assumed default name.

I have also marked this field to be loaded lazily, a common practice applied to LOBs that do not get referenced often.

Now simply convert your MultipartFile multimedia to a byte array as

byte[] mediaBytes = multimedia.getBytes();

and set this property in your object and persist it in SimpleDB. The MultipartFile will get stored in S3 with Lob-key in SimpleDB.

But if there is a chance of duplicate data, you should do this  using calls to S3 rather than using @Lob annotation in order to reduce redundancy and get more control.

JSON (JavaScript Object Notation) is a lightweight data-interchange format. It is easy for humans to read and write. It is easy for machines to parse and generate. These properties make JSON an ideal data-interchange language. While working in java, i found Google-gson library to be very useful .

From the official site:

Gson is a Java library that can be used to convert Java Objects into their JSON representation.It can also be used to convert a JSON string to an equivalent Java object. Gson can work with arbitrary Java objects including pre-existing objects that you do not have source-code of. There are a few open-source projects that can convert Java objects to JSON. However, most of them require that you place Java annotations in your classes something that you can not do if you do not have access to the source-code. Most also do not fully support the use of Java Generics. Gson considers both of these as very important design goals.

During my project i was having trouble converting list of json objects into list of corresponding java objects. This is how i finally did it :

 


String json1 = "[{\"contactName\":\"3\",\"contactNumber\":\"3\"},{\"contactName\":\"4\",\"contactNumber\":\"4\"}]";

JsonElement json = new JsonParser().parse(json1);

JsonArray array= json.getAsJsonArray();

Iterator iterator = array.iterator();

List<ContactDetail> details = new ArrayList<ContactDetail>();

while(iterator.hasNext()){
    JsonElement json2 = (JsonElement)iterator.next();
    Gson gson = new Gson();
    ContactDetail contact = gson.fromJson(json2, ContactDetail.class);
    //can set some values in contact, if required 
    details.add(contact);
}

Here ContactDetail class consists of String contactName and String contactNumber and their corresponding getters and setters.

Do leave a comment, in case there is any better way to do this.🙂

As suggested by Gobs in the comments, another way to do it is

Gson gson = new Gson();
Type collectionType = new TypeToken<List<ContactDetail>>(){}.getType();
List<ContactDetail> details = gson.fromJson(json1, collectionType);

But I did it the other way coz I also wanted to set some values in that object.

 

Vinay Deolalikar at HP Labs claim to have proved that P != NP . According to his paper…polynomial time algorithms succeed by successively “breaking up” the problem into smaller subproblems that are joined to each other through conditional independence. Consequently, polynomial time algorithms cannot solve problems in regimes where blocks whose order is the same as the underlying problem instance require simultaneous resolution. As anyone could predict, the alleged proof of one of the thorniest problems in CS has already been Slashdotted. As i’m not a hardcore mathematician, i’ll not go into details of this…but what is this P vs NP thing all about…lets explore !!

First of all P stands for polynomial time. NP stands for non-deterministic polynomial time. As the name implies, Polynomial time means that the complexity of the algorithm is O(n^k), where n is the size of your data and k is a constant.Complexity is time measured in the number of operations(e.g. comparisons in case of sorting)  it would take, as a function of the number of data items. Now we basically narrowed our discussion to deterministic vs non deterministic (gosh…i luv abstraction) . There is an abstract computational model, an imaginary computer called a Turing machine (TM).A Turing machine is a kind of state machine. At any time the machine is in any one of a finite number of states. Instructions for a Turing machine consist in specified conditions under which the machine will transition between one state and another. At any given time, the Turing Machine is in one of its states, and it is looking at a particular cell on the tape. Depending on what it reads from that cell, it can write a new symbol into that cell, move the tape one cell forward or backward, and go into a different state. This is called a state transition.Despite its simplicity, a Turing machine can be adapted to simulate the logic of any computer algorithm, and is particularly useful in explaining the functions of a CPU inside a computer.

We are basically concerned about 2 types of turing machines i) deterministic ii) non-deterministic

A deterministic Turing Machine only has one transition from each state for each symbol that it is reading off the tape, in contrast a non-deterministic Turing Machine may have several such transition, i.e. it is able to check several possibilities simultaneously. It has been proven that any problem that can be solved by a non-deterministic TM can be solved by a deterministic TM.When someone says P = NP , he means that one can build a deterministic TM for solving same problem in polynomial time which takes polynomial time in non-deterministic TM.So far nobody have been able to show that it can be done, but nobody has been able to prove that it cannot be done, either…lets  see Vinay Deolalikar’s claim get acceptance or not !!

Also, 2 terms most programmers come across are NP-complete and NP-hard. If someone tells you  that a problem is NP-complete, he basically means that it can be done in polynomial time on a non-deterministic TM, but it will take exponential time on a real computer. NP-hard ( non deterministic polynomial time hard), on the other hand, is a class of problems that are, informally, “at least as hard as the hardest problems in NP”.The precise definition here is that a problem X is NP-hard if there is an NP-complete problem Y such that Y is reducible to X in polynomial time. But since any NP-complete problem can be reduced to any other NP-complete problem in polynomial time, all NP-complete problems can be reduced to any NP-hard problem in polynomial time. Then if there is a solution to one NP-hard problem in polynomial time, there is a solution to all NP problems in polynomial time. Confusing…right ??….don’t go into details…jus pick the essential things  ;)

credits :: google