Mar 27

When your web application accesses data in the model, it normally does it in couple of different contexts. You are either listing all available resources in a “summary” context, or you are listing the details of a single resource in a “detailed” context. I’m sure you’ll find cases where this is not true, but for the most part it holds true.

Lets take a Contact resource as an example:

  1.  
  2.   class Contact
  3.     include DataMapper::Resource
  4.     property :id, Fixnum, :serial => true
  5.     property :display_name, String
  6.     property :first_name, String
  7.     property :last_name, String
  8.     property :work_phone, String
  9.     property :home_phone, String
  10.     property :email, String
  11.     # ..
  12.     # more properites
  13.     # ..
  14.     property :notes_updated, DateTime
  15.     property :notes_updated_by_id, Fixnum
  16.     property :notes, String
  17.   end
  18.  

You build a RESTful interface to show your new contact resource. Lets say our interface includes:

  • /contacts
  • /contacts/1
  • /contacts/1/notes

This gives you 3 contexts for data access (”summary”, “detailed” and “notes”). Every time someone goes to the /contacts URL your ORM retrieves all the data in the contacts table, maps it into Contact instances and returns an array of these instances. Then you do something like this:

  1.  
  2.   # In your controller
  3.   @contacts = Contact.all()
  4.  
  5.   # And in your view something like
  6.   <% @contacts.each do |contact| %>
  7.     <tr>
  8.       <td><%= contact.display_name %></td>
  9.     </tr>
  10.   <% end %>
  11.  

What a waste! Your ORM loaded 10+ fields from the database. Mapped them to variables in memory and then you only used contact.display_name (maybe contact.id). No problem, your ORM is smart, it can lazy load. So you do something like:

  1.  
  2.   class Contact
  3.     include DataMapper::Resource
  4.     property :id, Fixnum, :serial => true
  5.     property :display_name, String
  6.     property :first_name, String, :lazy => true
  7.     property :last_name, String, :lazy => true
  8.     property :work_phone, String, :lazy => true
  9.     property :home_phone, String, :lazy => true
  10.     property :email, String, :lazy => true
  11.     # ..
  12.     # more properites
  13.     # ..
  14.     property :notes_updated, DateTime, :lazy => true
  15.     property :notes_updated_by_id, Fixnum, :lazy => true
  16.     property :notes, String, :lazy => true
  17.   end
  18.  

That works great. When a user list all your contacts, the ORM only pulls out the id and display_name field. Now a user hits /contacts/1 URL.

The problem is your detailed view is a bit more verbose and outputs many more properties. All the lazy properties are loaded on first access. This means one query per lazy property read. Not great. Whats worse is if another part of your code does a Contact.all() and then iterate over the array accessing lazy properties.

The solution. In the soon to be release 0.9.0 of DataMapper lazy loaded properties are going to have contexts. When you read the first lazy property in a context, all the properties in that context for all the instances are loaded in one query. You can do something like this:

  1.  
  2.   class Contact
  3.     include DataMapper::Resource
  4.     property :id, Fixnum, :serial => true
  5.     property :display_name, String
  6.     property :first_name, String, :lazy => [:detailed,:notes]
  7.     property :last_name, String, :lazy => [:detailed,:notes]
  8.     property :work_phone, String, :lazy => [:detailed]
  9.     property :home_phone, String, :lazy => [:detailed]
  10.     property :email, String, :lazy => [:detailed]
  11.     # ..
  12.     # more properites
  13.     # ..
  14.     property :notes_updated, DateTime, :lazy => [:notes]
  15.     property :notes_updated_by_id, Fixnum, :lazy => [:notes]
  16.     property :notes, String, :lazy => [:notes]
  17.   end
  18.  

– until next time –

Mar 26
15 Things To Write About
icon1 guyvdb | icon2 Ruby | icon4 03 26th, 2008| icon3No Comments »

Starting a blog is all about having something to say. Something that others may find interesting. Wanting to be successful at blogging, I regularly need something interesting to say. Some homework, come up with 15 10 on-topic ideas that I can write a few paragraphs on. In no particular order, here they are:

  1. DataMapper 0.9.0 Validations
  2. Context Based Recursive Serialization for DataMapper Resources
  3. Access Control List Plugin for Merb/DataMapper
  4. Using the English Gem
  5. DataMapper/Merb Class Diagram Creator - ala RailRoad
  6. Object/Relational Mismatch - Strategies for Inheritance
  7. GEdit - A Powerful Ruby Development Environment
  8. Role Player - Using Polymorphic Association to put Everything into a Role
  9. DataMapper LazyLoading with Contexts
  10. ? (Still thinking)

Over the next ~10 days, you know what I am going to write about.

– until next time –

Mar 25
Welcome
icon1 guyvdb | icon2 Uncategorized | icon4 03 25th, 2008| icon3No Comments »

About Me

I live in Johannesburg, South Africa. I work in the IT sector. For the last few years, most of my time has been devoted to a project for a physical security equipment manufacture. The project is made up of an embedded micro-processor (ARM7), a home grown embedded OS and a web application. It governs security at multi-dwelling residential complexes.

Current Work

The current version of the product was written in C (on the embedded hardware) and Java (for the web application). The next release, which I am busy with, is a complete re-write. We are moving to a preemptive multi-tasking kernel on the embedded hardware (still in C of course), a RESTful API (written in my newly found favorite language Ruby) on the server side and for the browser, a RIA client (written in ActionScript/Flash).

Starting a Blog

So that’s a little bit about me. Now for the what and why of this blog. This is not my first attempt at starting a blog. I have begun half heartedly before. Slowly petering out. This time, I have invested in a domain name and setup the blog on my own server. I hope this will build some momentum which will spur me onward.

Reading about “how to start a successful blog” on the plethora of blogs that dish out that kind of advice, I have noted that I should:

  • take care of all the technical stuff
  • decide what to blog about
  • get a first post out the door
  • get into the habit of blogging
  • promote your blog
  • practice, practice, practice
  • ignore the lack of readers :)

I have taken care of all the technical stuff. I have a domain name, the server is set up, everything is ready to go. Next, to pick a subject. What am I going to blog about. Ruby to start. Maybe a touch of embedded development and C. Right now I am getting the first post out the door.

Only time will tell - do I get into the habit of blogging? I already have the subject for my next entry. Pick 10 subjects related to Ruby that I want to blog about.

– until next time –