dm-optlock v 0.1.4

Posted by Moser on 10 Jan 2009

Update: dm-optlock now on github

I released a new version of dm-optlock. It is NOT compatible with v 0.1.2’s code, because replaced the set_locking_column method by a method called add_locking_column. Your DB should be compatible.

class Model
  include DataMapper::Resource
  ...
  add_locking_column :my_version_col
end

It takes a options hash just like DataMapper’s property method does. I added it to enforce some options on the locking column.

Yahoo! or better Oh-no!

Posted by Moser on 21 Dec 2008

I forgot my Yahoo!-ID for my flickR account. I know it’s nothing to be proud of… But what really annoys me is what Yahoo calls their customer support. I sent in their contact form with a description of my problem which I though to be easy to solve. One day later (!) they replied by a standard email describing how to get your lost password (!) and what mistakes one can make when typing one’s ID.

That was on Wednesday. Since then I got the same email four times signed by four different employees. Each time I replied with a description of my problem and ~24 hours later the same dumb email reached me.

In the meantime I already found out my ID using the english help pages (the german copy doesn’t seem to be complete), but I think I will continue playing mail ping pong with Yahoo for some time.

shared git repo + ssh = umask problems

Posted by Moser on 19 Dec 2008

When you want to share a little git repository on your own server the first thing that comes to your mind is SSH. It’s installed virtually everywhere, secure and easy to configure. In the beginning there are no problems, because everyone can clone your repo and if they don’t change files concurrently even pushing works. About one day latter you get umask problems and start to search the web for solutions. To make it short, here it is:

git repo-config core.sharedRepository true

This option makes git care about the permission stuff. I wasted about 10 hours of my lifetime for this one and found an odd thing about ~/.ssh/rc:

umask 002
date > ~/sshrc-executed

I added the second line, because I wasn’t sure if it was executed. It was (look at the file permissions):

...
-rw-rw-r--  1 moser git     29 Dec 19 18:41 sshrc-executed

But my umask didn’t last:

moser@HellAgent:~$ ssh xxx.net -n 'umask'
Password: 
0022

Don’t know why, if anyone knows tell me please.

How to use IGC data to geotag your photos

Posted by Moser on 07 Oct 2008

When taking photos on a cross-country flight it’s not always easy to remember where they were taken. And wouldn’t it be cool to look at your photos on flickr’s map? Either you buy a camera with an integrated GPS receiver which automatically stores a geotag in your picture file’s meta data or you use GPS data that’s gathered anyway: Your IGC files. What do you need?

Both programs are available for both Linux and Windows. My article is focused on Linux but the commands on windows will be quite similar. If you are running Debian or Ubuntu you can install these via

apt-get install gpscorrelate gpscorrelate-gui gpsbabel

The GPS Photo Correlator accepts GPS data in the GPX format. So you will need to convert your data into that format:

gpsbabel -i igc -o gpx <infile>.igc <outfile>.gpx

Now lets geotag your files:

gpscorrelate -g <gpxfile> -z +2:00 --show file(s)

“-z +/-XX:XX” is needed because the timestamp on your photos is probably not UTC. file(s) is either a list of files (“DSC2131.JPG DSC2322.JPG”) or a wildcard expression (“” for all photos in the directory or something like “DSC12.JPG”). If you add “–no-write” no EXIF data will be written and you can check if your photos would have been tagged right.

If you want flickr to use your photos’ EXIF geotags you have to change your account settings.

Optimistic Locking for Datamapper

Posted by Moser on 01 Oct 2008

Update(2009-3-3): dm-optlock now on github

Update(2009-1-10): New version

I should be learning for my test on next tuesday, but coding helps keeping me in a good mood. Here is a little gem I will need in a future project for which I’ve choosen merb + datamapper. Datamapper doesn’t support any row level locking. My code checks if the record you want to save was changed since you loaded it. If it was changed it raises an exception (DataMapper::StaleObjectError). It works quite like ActiveRecord’s optimistic locking. You need to define this column in your model you want to be locked:

property :lock_version, Integer, :default => 0

Anywhere in your code where you update your objects you should be prepared to handle the exception:

  if @obj.update_attributes({:dummy => 123})
    #successful
  else
    #validation errors?
  end
rescue DataMapper::StaleObjectError
  #tell the user that he was too slow
  #or even better: show him the conflicts and let him merge the changes
end

The gem can be downloaded here: dm-optlock-0.1.0.gem Please let me know what you think of it. I’ll try and put it on RubyForge, too.

(00:27) Little update: I added a ‘set_locking_column’ method. dm-optlock-0.1.2.gem

Update: dm-optlock is on Rubyforge. Now you can get it like this: gem install dm-optlock

Stable Array Sorting in Ruby

Posted by Moser on 18 Sep 2008

Ruby uses some kind of Quicksort to sort its arrays. Unfortunately Quicksort is not stable. When you get sorted data from your database, which almost certainly sorts faster than anything in Ruby, you sometimes may want the data to be resorted in a stable way.

I found following snippet:

class Array
  def stable_sort
    n = 0
    sort_by {|x| n+= 1; [x, n]}
  end
end

I extended it to accept a block like the original sort method does:

class Array
  def stable_sort
    n = 0
    c = lambda { |x| n+= 1; [x, n]}
    if block_given?
      sort { |a, b|
        yield(c.call(a), c.call(b))        
      }
    else
      sort_by &c
    end
  end
end