module Couch::BulkRequest::Post

Public Instance Methods

post_bulk(database, docs) click to toggle source

Flushes the given hashes to CouchDB

# File lib/couch.rb, line 221
def post_bulk(database, docs)
  body = {:docs => docs}.to_json #.force_encoding('utf-8')
  post("/#{database}/_bulk_docs", body)
end
post_bulk_if_big_enough(db, docs) click to toggle source
# File lib/couch.rb, line 241
def post_bulk_if_big_enough(db, docs)
  flush = (docs.to_json.bytesize / 1024 >= (options[:flush_size_mb]*1024) or docs.length >= options[:max_array_length])
  if flush
    post_bulk_throttled(db, docs)
    docs.clear
  end
  flush
end
post_bulk_throttled(db, docs, &block) click to toggle source
# File lib/couch.rb, line 226
def post_bulk_throttled(db, docs, &block)
  # puts "Flushing #{docs.length} docs"
  bulk = []
  docs.each do |doc|
    bulk << doc
    if bulk.to_json.bytesize/1024/1024 > options[:flush_size_mb] or bulk.length >= options[:max_array_length]
      handle_bulk_flush(bulk, db, block)
    end
  end
  if bulk.length > 0
    handle_bulk_flush(bulk, db, block)
  end
end

Private Instance Methods

handle_bulk_flush(bulk, db, block) click to toggle source
# File lib/couch.rb, line 252
def handle_bulk_flush(bulk, db, block)
  res = post_bulk(db, bulk)
  error_count=0
  if res.body
    begin
      JSON.parse(res.body).each do |d|
        error_count+=1 if d['error']
      end
    end
  end
  if error_count > 0
    puts "Bulk request completed with #{error_count} errors"
  end
  if block
    block.call(res)
  end
  bulk.clear
end