def save_doc(doc, bulk = false, batch = false)
      if doc['_attachments']
        doc['_attachments'] = encode_attachments(doc['_attachments'])
      end

      if bulk
        @bulk_save_cache << doc
        bulk_save if @bulk_save_cache.length >= @bulk_save_cache_limit
        return {'ok' => true} # Compatibility with Document#save
      elsif !bulk && @bulk_save_cache.length > 0
        bulk_save
      end
      result = if doc['_id']
        slug = escape_docid(doc['_id'])
        begin
          doc_path = "#{path}/#{slug}"
          doc_path << "?batch=ok" if batch
          connection.put doc_path, doc
        rescue CouchRest::NotFound
          puts "resource not found when saving even though an id was passed"
          slug = doc['_id'] = server.next_uuid
          connection.put "#{path}/#{slug}", doc
        end
      else
        slug = doc['_id'] = @server.next_uuid
        connection.put "#{path}/#{slug}", doc
      end
      if result['ok']
        doc['_id'] = result['id']
        doc['_rev'] = result['rev']
        doc.database = self if doc.respond_to?(:database=)
      end
      result
    end
def save_doc(doc, bulk = false, batch = false)
      if doc['_attachments']
        doc['_attachments'] = encode_attachments(doc['_attachments'])
      end

      if bulk
        @bulk_save_cache << doc
        bulk_save if @bulk_save_cache.length >= @bulk_save_cache_limit
        return {'ok' => true} # Compatibility with Document#save
      elsif !bulk && @bulk_save_cache.length > 0
        bulk_save
      end
      result = if doc['_id']
        slug = escape_docid(doc['_id'])
        begin
          doc_path = "#{path}/#{slug}"
          doc_path << "?batch=ok" if batch
          connection.put doc_path, doc
        rescue CouchRest::NotFound
          puts "resource not found when saving even though an id was passed"
          slug = doc['_id'] = server.next_uuid
          connection.put "#{path}/#{slug}", doc
        end
      else
        slug = doc['_id'] = @server.next_uuid
        connection.put "#{path}/#{slug}", doc
      end
      if result['ok']
        doc['_id'] = result['id']
        doc['_rev'] = result['rev']
        doc.database = self if doc.respond_to?(:database=)
      end
      result
    end
Save a document to CouchDB. This will use the <tt>_id</tt> field from
 the document as the id for PUT, or request a new UUID from CouchDB, if
 no <tt>_id</tt> is present on the document. IDs are attached to
 documents on the client side because POST has the curious property of
 being automatically retried by proxies in the event of network
 segmentation and lost responses.

 If <tt>bulk</tt> 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.

 If <tt>batch</tt> 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."