Move to CouchDB Server-Side Updates
In a continuing effort to make the code more efficient and really, just plain faster, this afternoon I've been working with a teammate to update CouchRest, our ruby client to Couch, to handle server-side updates. Couch allows server-side updates - you basically write a javascript function that takes the document and the request and you can update the document as you see fit, and return something to the caller.
It's not bad, really. It should certainly make the updates a ton faster as right now we're reading, updating and writing back the complete document for a very small change - in one case just a single field. This is really where the document database falls down, and you long for a SQL statement where you can simply UPDATE and be done with it.
Still, it's nice to be able to write:
function(doc, req) { var ans = false; var fld = 'lead_assignment'; if (doc) { doc[fld] = JSON.parse(req.body); and = true; } return [doc, JSON.stringify({'updated': ans})]; }
and be able to make a change with:
def update_merchant_assignment(division, sf_id, stuff) return nil if (id = get_latest_results_docID(division, sf_id)).nil? Database.update('merchant_updater/add_assignment', :id => id, :body => stuff) end
It really simplifies the code, and it certainly cuts the bytes moved for an update way down. I'm hoping it's enough… we'll have to see how it goes.