From 62ea68df63e51cc13bf521f134d57967c92c8741 Mon Sep 17 00:00:00 2001 From: Igal Koshevoy Date: Sun, 15 Nov 2009 23:14:09 -0800 Subject: [PATCH] Improved CouchRest::Database#save_doc, added "batch" mode to significantly speed up saves at cost of lower durability gurantees. --- lib/couchrest/core/database.rb | 20 +++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) diff --git a/lib/couchrest/core/database.rb b/lib/couchrest/core/database.rb index 96c4a0d..7ddd16a 100644 --- a/lib/couchrest/core/database.rb +++ b/lib/couchrest/core/database.rb @@ -129,7 +129,7 @@ module CouchRest end end end - + # Save a document to CouchDB. This will use the _id field from # the document as the id for PUT, or request a new UUID from CouchDB, if # no _id is present on the document. IDs are attached to @@ -139,7 +139,19 @@ module CouchRest # # If bulk is true (false by default) the document is cached for bulk-saving later. # Bulk saving happens automatically when #bulk_save_cache limit is exceded, or on the next non bulk save. - def save_doc(doc, bulk = false) + # + # If batch is true (false by default) the document is saved in + # batch mode, "used to achieve higher throughput at the cost of lower + # guarantees. When [...] sent using this option, it is not immediately + # written to disk. Instead it is stored in memory on a per-user basis for a + # second or so (or the number of docs in memory reaches a certain point). + # After the threshold has passed, the docs are committed to disk. Instead + # of waiting for the doc to be written to disk before responding, CouchDB + # sends an HTTP 202 Accepted response immediately. batch=ok is not suitable + # for crucial data, but it ideal for applications like logging which can + # accept the risk that a small proportion of updates could be lost due to a + # crash." + def save_doc(doc, bulk = false, batch = false) if doc['_attachments'] doc['_attachments'] = encode_attachments(doc['_attachments']) end @@ -153,7 +165,9 @@ module CouchRest result = if doc['_id'] slug = escape_docid(doc['_id']) begin - CouchRest.put "#{@root}/#{slug}", doc + uri = "#{@root}/#{slug}" + uri << "?batch=ok" if batch + CouchRest.put uri, doc rescue HttpAbstraction::ResourceNotFound p "resource not found when saving even tho an id was passed" slug = doc['_id'] = @server.next_uuid