Monday, April 19, 2010

Efficient multiple SQL inserts in RAILS - AR Extensions

Recently, I was required to generate multiple inserts in a RAILS project. I went with traditional way of looping through and creating AR objects.


#class definition
class CartoonCharacter < ActiveRecord::Base
end

#values for NEW objects
names = ["Tom", "Jerry", "Donald"]

#loop thru and create objects
names.each do |name|
CartoonCharacter.create!(:name => name)
end


It's not as bigger problem if you're just creating 2 or 3 records. Imagine you want to create 70,000 records. I was required to do the same. This approach killed my machine and it just took ages. I couldn't wait after 5 minutes and killed the process.

I started looking out for other options. I came across ar-extensions.

It is a nice gem for mass import and conversion of data to CSV. It also incorporates better finder support. All I've used is its mass import functionality.

Usage:

column_names = [:name, :age, :country]
values = []

#build up values
15000.times do
values << ["Tom", "65", "AU"]
end

#import
CartoonCharacter.import column_names, values


That's it. It worked like a charm. Yes, please don't forget to include adapter specific functionality.


require 'ar-extensions/adapters/mysql'
require 'ar-extensions/import/mysql'

Recently, some work has been done to extract the import functionality of ar-extenstions into activerecord-import.

More information available at: http://continuousthinking.com/tags/arext

Thursday, December 3, 2009

Java ME Early Access to rescue Mac J2ME Developers

Finally, we have an IDE for MAC J2ME developers. Most of the Mac users use MPowerPlayer SDK(mpp-sdk) to develop J2ME applications. It was hell of a pain to use it with NetBeans. It also lacked the Bluetooth API support.

Last month, Sun announced the launch of most awaited JAVA ME SDK 3.0. This can be downloaded at http://java.sun.com/javame/downloads/sdk30_mac.jsp. Please note, it is an Early Access release(not beta). So, it is expected to have some issues. Couple of issues I am aware of are:
  • Exceptions when launching JAVA ME SDK (Just ignore all the exceptions hitting 'Cancel')
  • Also, it runs out of memory after every 4-8 runs and will require Mac reboot(YES, mac reboot).
You might have to restart your Mac immediately after JAVA ME SDK installation.

Apart from these issues, it works like a MAGIC. It has got a nice environment based on NetBeans platform. I have tried mpp-sdk and Java WTK within Sun's Virtual Box, but I find JAVA ME EA to be the best.

Sunday, November 29, 2009

Frequently used git commands

Ignoring all the local uncommitted changes
$ git reset --hard


Ignoring changes to individual files
$ git checkout path_to_file


Tracking a remote branch (Creates a new local branch based on remote branch and switches to that branch)
$ git checkout --track -b local_name origin/remote_name


Deleting remote branch/tag
$ git push origin :heads/branch_name
$ git push origin :branch_name
$ git push origin :tag_name

$ git push origin :refs/tags/tag_name



Deleting local tag
$ git tag -d tag_name


Deleting local branch (already merged)
$ git branch -d branch_name


Deleting local branch (unmerged)
$ git branch -D branch_name


Merging with desired merge tool
$ git mergetool --tool=tool_name


Clearing stash
$ git stash clear


Reverting a commit
$ git revert HASH


Deleting untracked files
$ git clean -n(for dry-run) -f(for forceful clean)