Archive

Archive for the ‘Programming’ Category

NHibernate exceptions

October 14th, 2008

After hours of travel, our hero finds himself suddenly in the midst of dark woods. Nailed to a tree there is a wooden sign, weather-beaten by the ages. It reads:

"Invalid Cast (check your mapping for property type mismatches); setter of classname"

Right besides the sign stands NHibernate, its expression unreadable. Our hero, unsure of which path to take, asks for directions as to what to fix.

NHibernate: “The problem lies…” – sweeps hand widely at the class – “that way”
Ricardo: “Where? Which way?”
NHibernate: “In a property…”
Ricardo: “Of course, all it has are properties! Which one?”
NHibernate: “No… I’m afraid I’ve said too much”

Ricardo Programming

Bus error on Erlang with Leopard 10.5.3

May 29th, 2008
Comments Off

I’ve just updated my OS X to the latest 10.5.3, and now Erlang refuses to run – I keep getting bus error. I uninstalled and attempted to build it from source using Macports, but that did not help – I get the same error when building hipe:


=== Entering application hipe
/opt/local/var/macports/build/_opt_local_var_macports_sources_rsync.macports.org_release_ports_lang_erlang/work/erlang-R12B-2/bin/i386-apple-darwin9.3.0/hipe_mkliterals -e > hipe_literals.hrl

erlc -W +debug_info +warn_obsolete_guard +inline -o../ebin hipe_rtl_arch.erl
make[3]: *** [../ebin/hipe_rtl_arch.beam] Bus error
make[2]: *** [opt] Error 2
make[1]: *** [opt] Error 2
make: *** [secondary_bootstrap_build] Error 2

Still haven’t found a fix, but thought about sending this out as Googlebait just in case.

Update: there is now a bug report at Macports.

Ricardo Programming

Java SE 6 on OS X

May 5th, 2008
Comments Off

So Apple finally releases Java SE 6 for the Mac, with its huge speed improvements, and now I can’t edit any single input field on either of the Java applications I use (including Moneydance and IntelliJ IDEA) because the fields show up greyed-out and are read-only. No, rebooting did not help. Seems I have to stick with Java 5.

Brilliant.

Ricardo Programming, Science and Technology

git add before commit

April 28th, 2008
Comments Off

I’ve been experimenting with git for version control of my document folder, before I use it professionally. It has several things I like, particularly the blazing speed and the fact that there’s a single .git directory instead of a myriad of .svn all over the place (which made it a bit of a pain to keep those directories in sync if one of of the sub-.svn’s got erased by, say, an over-eager script).

Coming from subversion and cvs, something struck me as odd: if you do git commit, it will only commit the changes that you have just added or removed – anything else needs to be explicitly added to the working index, even if you have added it before. Bit of a pain if you simply want to commit everything.

Digging around the git docs, there’s an option you can pass so that it will just commit all files you have previously told it about: git commit -a. There’s also an option invoked by git commit –interactive that causes it to ask you for each file if you want it to go on the current commit or not.

As to why that is:

What git add does is to move the current version of the named file to a special staging area, holding files that are ready to be committed. And what git commit (without other arguments) will do is to take the index and make a new revision out of what the index contains. git commit -a is just a convenience which adds all modified files to the index, and then commits the result.

How does this affect you? The first thing to remember is this one: only run git add on new files just before committing. Otherwise, you’ll commit the wrong contents of the file.

It takes a bit getting used to, but it’s a lovely tool so far. If you’re thinking about using it yourself, start by reading Git Magic, and then after the first three chapters move to Using git without feeling stupid and its part 2.

Ricardo Programming

Pragmatic Programming in Erlang, chapter 8

April 24th, 2008
Comments Off

Lately I’ve been reading Joe Armstrong’s Pragmatic Programming in Erlang, learning a couple new tricks. In chapter 8 he proposes a problem:


Write a ring benchmark. Create N processes in a ring. Send a message round the ring M times so that a total of N * M messages get sent. Time how long this takes for different values of N and M.

Here is my solution:

-module(ringproblem).
-export([start/2, ring/1, rpc/2, benchmark/3]).

% Spawns a function and registers it as an atom.
start(AnAtom, Fun) ->
    Pid = spawn(Fun),
    register(AnAtom, Pid).

%% This loop receives a parameter that's either the next Pid on the chain,
%% or null if this is the last process on the chain.  It then passes the
%% message to the next process. Once the cycle has been completed, it
%% calls back the original caller to let it know.
loop(F) ->
    receive
	{ From, 0, Counter, Message } ->
	    F ! die, % tell the next process to die
	    From ! { ended, Counter },
	    io:format("Cycle for ~p ended.~n", [Message]);
	{ From, Number, Counter, Message } ->
	    case F of
		void ->
		    firstProcess ! { From, Number - 1, Counter + 1, Message };
		_Other ->
		    F ! { From, Number, Counter + 1, Message }
	    end,
	    loop(F);
	die ->
	    case F of
		void ->
		    io:format("Last process died");
		_Other ->
		    F ! die,
		    void
	    end;
	Other ->
	    io:format("I don't know what to do with ~p.~n",[Other]),
	    loop(F)
    end.

%% Sets up a ring of at least 2 elements by recursively building it back
ring(Elements) when Elements >= 2 ->
    Pid = spawn(fun() -> loop(void) end),
    ring(Elements - 1, Pid).

%% If we have no more elements, this is the first process. Register it.
ring(0, Pid) ->
   register(firstProcess, Pid);
%% otherwise walk back
ring(N, Pid) ->
    PrevPid = spawn(fun() -> loop(Pid) end),
    ring(N-1, PrevPid).

%% Do a RPC call to the first process, which should have been registered, and
%% wait until we receive a message telling us that the cycle is done.
rpc(Times, Request) ->
    statistics(runtime),
    statistics(wall_clock),
    firstProcess ! { self(), Times, 0, Request },
    receive
	{ ended, Counter } ->
	    {_, Time1} = statistics(runtime),
	    {_, Time2} = statistics(wall_clock),
	    U1 = Time1 * 1000,
	    U2 = Time2 * 1000,
	    io:format("Ring time for ~p calls = ~p (~p) microseconds~n",  [Counter, U1, U2])
    end.

benchmark(Elements, Times, Request) ->
    ring(Elements),
    rpc(Times, Request).

You would run it like this:


Eshell V5.6.2 (abort with ^G)
1> c(ringproblem).
{ok,ringproblem}
2> ringproblem:benchmark(1000, 3000, "Round and Round").
Cycle for "Round and Round" ended.
Ring time for 3000000 calls = 8360000 (8416000) microseconds
ok
3>

And eventually, possibly a couple seconds later, you’ll get a notification that the last process has died.

Ricardo Programming

Development blog

March 15th, 2008
Comments Off

Speaking of which, I’m keeping a development blog at my company site. I’ll continue to publish the Grails plugins and any other code I create over there. Enjoy!

Ricardo Programming, Science and Technology

VMware 2.0 beta server performance

March 12th, 2008
Comments Off

VMware server has been in beta since November, 2007, and you can try it out for free by going to their site. Don’t. It’s unstable and has performance problems – I had to manually reboot our machine at least twice a week because the virtual servers became unresponsive. I originally thought that the problems were caused by the CentOS kernel I was using, but while the problems were lessened after changing it, they persisted.

After downgrading to (the also free) VMware server 1.0.4, the virtual servers have been working like a swiss watch for a couple weeks.

Ricardo Programming, Science and Technology

CentOS and VMWare Server

February 18th, 2008
Comments Off

If you’re using CentOS hosts on a VMWare server, you should read this.

Ricardo Programming, Science and Technology

Grails Image Tools plugin

November 2nd, 2007
Comments Off

A few days ago I released version 0.1 of my Grails ImageTools plugin, based off some work that I’ve done on personal projects. It provides functions for:

  • Image loading
  • Image saving
  • Cropping
  • Masking
  • Scaling

The latest version of the documentation will remain at the Grails wiki. You probably should also read my post on JAI and masking operations if you intend to use it to mask images.

Ricardo Programming

Zend debug breakpoints not working on Mac OS X

September 19th, 2007
Comments Off

It’s the revenge of PHP debugging: for some reason breakpoints just stopped working on my Eclipse PHP IDE. This is apparently a known issue, even on release 1.0 of PDT.

After much hair-pulling, I’ve figured out a workaround: while debugging only stops on a breakpoint the first time while using the integrated Eclipse browser, it does work when configured to use Firefox as an external browser (Camino gave me an error on launch). To change that option, go to Window | Web browser and select something different from Internal Web Browser.

Hope it helps those out there still looking for a solution.

Ricardo Programming