Monday, December 22, 2014

Stack Trace Error in Google Sets Intersection

Found a very puzzling bug in some code. I still do not quite understand WHY my fix, fixed it.
Essentially I was sometimes getting a stack trace exception

at com.google.common.collect.Sets$2.contains(Sets.java:655)
at com.google.common.collect.Sets$2.contains(Sets.java:655)

The offending code looked like this (NOT THE ACTUAL CODE)

for (SomeObject m : this) {
  commonTypes = Sets.intersection(commonTypes, m.getSet());
}


This would cause problems, it seemed to be related to this loop being executed a lot. Over 10 000 times, though the set sizes where never greater than 20 or so. Anyway this code fixed it.

for (SomeObject m : this) {
  Set<AttrType> tempTypes = Sets.intersection(commonTypes, m.getSet());
  commonTypes = new HashSet<>();
  commonTypes.addAll(tempTypes );
}


Thursday, December 18, 2014

Using GridFS with MongoJack


We use MongoJack to convert our POJO (plain java) classes to JSON so they can be saved easily into MongoDB. MongoJack also handles the reading of the BJSON from the MongoDB and creating the POJO again. MongoJack internally uses Jackson.

So to persist a java file we can just use the annotations
    @JsonCreator
    @JsonProperty


Then we save to the MongoDB with the JacksonDBCollection.
Our problem was that part of out POJO object was a byte[] that was over 16 Meg. MongoDB has a restriction on document size. So our POJO had a
    byte[] _content;

Massive documents need to be persisted with GridFS in MongoDB. My approach was to leave the Java object with _content but save that part to MongoDB using GridFS. Then add contentId to our POJO to point to the ID of the GridFS saved content.

Steps
1. Stop MongoJack from persisting _content was easy. Just add this annotation to the POJO
    @JsonIgnore
2. On insert save GridFS file and link to POJO with contentId
3. On load/get merely get the contentId and load the appropriated GridFS saved file and read it back into the POJO.

This method replaces the way that the POJO is saved, but leaves the POJO as is. The code does not care how the POJO is saved.




Tuesday, December 16, 2014

Learning MongoDB

Probably the best resource I have found for learning MongoDB quickly is The little MongoDB Book
Very much worth it to go through the book and it only takes an afternoon.

One thing that I immediately liked was the format of the find in the shell.
db.hits.update({page: 'unicorns'},{$inc: {hits: 1}}, {upsert:true});
It just reminds me of groovy and passing and returning json. I compare that to something that might be written in traditional java, which seems so clumsy.
       
           db.getHits().updateWithInc(String page, Integer inc, boolean upsert);

Where page, being a string we would have to be able to parse the string. Or pass an object. With groovy of course any object can just be flattened into JSON and rebuilt.

Far more flexibility, far simpler syntax.






Monday, April 28, 2014

Looking at Scala (and then play)

Decided to have a look at Scala, after reading about the inventor of Groovy's high praise of it.

Found a decent quick tutorial
http://www.scala-lang.org/docu/files/ScalaTutorial.pdf

From there I want to move on to the play framework




Wednesday, April 23, 2014

Grails Documentation

I find the best grails documentation is always


And you can look at specific version just by dropping in the version number instead of 'latest'
For instance for Grails 2.0.4


Tuesday, April 22, 2014

Looking at Ruby

Having become a bit disheartened at the lack of Grails/Groovy jobs in my area, I decide to have a little look at Ruby.
So far it is pretty depressing, the tutorials generally do not work or are horribly outdated.

One that did seem to work so far was:

http://andreapavoni.com/blog/2013/8/a-rails-4-tutorial-application-for-beginners

Obviously I can see the similarities between Grails & Rails. But I have to say thus far I am unimpressed by Rails. It seems far clumsier, with rake, db:migrates, bundles, etc. Even the console is horrible inferior.
Of course this is just my opinion after 2 hours.

Grails just feels more elegant and for sure was easier to get started. I really like the Grails GORM, as opposed to the Rails db script creation and then creating the classes manually. A relationship between two objects in the database is one line in Grails.

The main stumbling blocks I had with Grails in the early days was plugins, which I feel has been largely addressed now.
Ruby uses Gems which seem quite similar in nature to plugins.






Monday, February 24, 2014

Get rid of skype ads

I cannot stand being advertised to.
This is an easy fix for getting rid of all ads in Skype (6.11)
Just edit your hosts file to add the following two lines

     127.0.0.1 ad-emea.doubleclick.net
     127.0.0.1 rad.msn.com

Works a treat.