diff --git a/lib/couchrest/core/database.rb b/lib/couchrest/core/database.rb
index 96c4a0d..547e35c 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
@@ -175,6 +189,15 @@ module CouchRest
result
end
+ # Save a document to CouchDB in bulk mode. See #save_doc's +bulk+ argument.
+ def bulk_save_doc(doc)
+ save_doc(doc, true)
+ end
+
+ # Save a document to CouchDB in batch mode. See #save_doc's +batch+ argument.
+ def batch_save_doc(doc)
+ save_doc(doc, false, true)
+ end
# POST an array of documents to CouchDB. If any of the documents are
# missing ids, supply one from the uuid cache.