Making today worse so tomorrow seems better.

Getting a Good View of Your Couch

So far I have been doing all my CouchDB queries using “temporary views” which are Javascript strings which are POSTed to the database and used to select the records you’re interested in. A more efficient way to do it is to save the Javascript strings as documents, these can be called with less traffic and a simpler API. “Permanent views” also create cached indexes, so they offer performance advantages over temporary views as well.

Tim Kofol took my ideas and ran with them, creating an ingenious way to maintain CouchDB views right in the Ruby code. Witness:

class Post < Blarg::CouchBase 
  couch_accessor :tags

  couch_view :comments_view, %[
    function(doc) {
      if (doc.type  "Comment") {
        map(doc.post_id, doc);
      }
    }
  ]

  couch_view :all_tags_view, %[
    function(doc) {
      if (doc.type  ‘Post’) {
        map(null, doc.tags);
      }
    }
  ]

  def self.all_tags   
    self.all_tags_view.flatten.uniq.sort
  end

  def comments
    comments = self.class.comments_view(:key => self.document_id)
    comments.sort{|a,b| a.created_at <=> b.created_at}
  end
end

With the views relevant to this class defined in the class itself, we need to load them into CouchDB. I added Rake task which finds all views defined in the project and loads them into CouchDB, creating them or replacing older versions.

rake db:views:load

… more

Posted by Jon Guymon on Jan 16 2008 | Filed Under: couchdb ruby tech programming blarg

Bj Makes Attachment_fu Happy

The attachment_fu plugin for Rails is great, and it’s support for S3 as a backend sounds really handy. Sadly tho ugh, it isn’t practical for anything other a demo or a proof-of-concept. Luckily its limitations can be worked around with the cunning use of Bj .

When you upload a file to a Rails app, your browser waits while Mongrel buffers the file, then your browser waits some more while Rails processes the fully uploaded file. Normally this “processing” is simply copying the file from where Mongrel put it to where Rails wants it, no big whup. The problem arises when this “processing” is substantially more time consuming, like, say transmitting the file to S3. There’s no guarantee that this will take place in a reasonable amount of time and meanwhile, not only is the user’s browser getting nearer to timing out, but that whole Rails instance is going to block and no one else can use it either. Rails is single threaded, remember?

The solution is to write the file to disk like normal, then spawn a process in the background to take care of uploading it to S3. This is where Bj comes in. Install thusly:

./script/plugin install http://codeforpeople.rubyforge.org/svn/rails/plugins/bj

./script/bj setup

rake db:migrate

Bj is a light weight work queue that uses your app’s database as a store. Requests are put into the bj_job table and run one at a time outside of the mongrel_rails process. Say we have a model UploadFile that uses attachment_fu:

… more

Posted by Jon Guymon on Jan 12 2008 | Filed Under: ruby rails tech programming

First Post

What do we have here?

It would seem to be yet another blog, and it is. This one, however, is built with merb and CouchDB so building it gave me a chance to experiment with some interesting projects that I don’t get to use in my daily life as a professional web developer.

The code for this site is available to the many-eyes beasts of the Interwebs here:

git://sprocket.slackworks.com/srv/git/blarg.git

It requires merb and friends, as well as an instance of CouchDB to be running somewhere. There are a few reasons I chose merb over Rails (which is what I generally use professionally) to do this:

  • It comes with no pre-packaged ORM. I wanted to use CouchDB, so none of the existing ORMs would really work for me. My previous experiences of trying to rip ActiveRecord out of Rails have been frustrating.
  • It has more flexible routing than Rails. I realize there are ways to accomplish the things I’m doing here in Rails, but the merb routes seem clearer to me, less hand-wavy. And perhaps most importantly:
  • It’s not Rails. I use Rails all day long, and I wanted to see what else is out there.

… more

Posted by Jon Guymon on Dec 18 2007 | Filed Under: tech programming ruby couchdb merb blarg