123456789_123456789_123456789_123456789_123456789_

Class: Mongo::BulkWrite::ResultCombiner Private

Do not use. This class is for internal use only.
Relationships & Source Files
Inherits: Object
Defined in: lib/mongo/bulk_write/result_combiner.rb

Overview

Combines bulk write results together.

Since:

  • 2.1.0

Class Method Summary

Instance Attribute Summary

Instance Method Summary

Instance Attribute Details

#countInteger (readonly)

Returns:

  • (Integer)

    count The number of documents in the entire batch.

Since:

  • 2.1.0

[ GitHub ]

  
# File 'lib/mongo/bulk_write/result_combiner.rb', line 29

attr_reader :count

#resultsHash (readonly)

Returns:

  • (Hash)

    results The results hash.

Since:

  • 2.1.0

[ GitHub ]

  
# File 'lib/mongo/bulk_write/result_combiner.rb', line 32

attr_reader :results

Instance Method Details

#combine!(result, count)

Adds a result to the overall results.

Examples:

Add the result.

combiner.combine!(result, count)

Parameters:

  • result (Operation::Result)

    The result to combine.

  • count (Integer)

    The count of requests in the batch.

Since:

  • 2.1.0

[ GitHub ]

  
# File 'lib/mongo/bulk_write/result_combiner.rb', line 58

def combine!(result, count)
  # Errors can be communicated by the server in a variety of fields:
  # writeError, writeErrors, writeConcernError, writeConcernErrors.
  # Currently only errors given in writeConcernErrors will cause
  # counts not to be added, because this behavior is covered by the
  # retryable writes tests. It is possible that some or all of the
  # other errors should also be excluded when combining counts and
  # ids, and it is also possible that only a subset of these error
  # fields is actually possible in the context of bulk writes.
  unless result.write_concern_error?
    combine_counts!(result)
    combine_ids!(result)
  end
  combine_errors!(result)
  @count += count
  @acknowledged = result.acknowledged?
end

#combine_counts!(result) (private)

Since:

  • 2.1.0

[ GitHub ]

  
# File 'lib/mongo/bulk_write/result_combiner.rb', line 89

def combine_counts!(result)
  Result::FIELDS.each do |field|
    if result.respond_to?(field) && value = result.send(field)
      results.merge!(field => (results[field] || 0) + value)
    end
  end
end

#combine_errors!(result) (private)

Since:

  • 2.1.0

[ GitHub ]

  
# File 'lib/mongo/bulk_write/result_combiner.rb', line 108

def combine_errors!(result)
  combine_write_errors!(result)
  combine_write_concern_errors!(result)
end

#combine_ids!(result) (private)

Since:

  • 2.1.0

[ GitHub ]

  
# File 'lib/mongo/bulk_write/result_combiner.rb', line 97

def combine_ids!(result)
  if result.respond_to?(Result::INSERTED_IDS)
    results[Result::INSERTED_IDS] = (results[Result::INSERTED_IDS] || []) +
                                      result.inserted_ids
  end
  if result.respond_to?(Result::UPSERTED)
    results[Result::UPSERTED_IDS] = (results[Result::UPSERTED_IDS] || []) +
                                      result.upserted.map{ |doc| doc['_id'] }
  end
end

#combine_write_concern_errors!(result) (private)

Since:

  • 2.1.0

[ GitHub ]

  
# File 'lib/mongo/bulk_write/result_combiner.rb', line 123

def combine_write_concern_errors!(result)
  if write_concern_errors = result.aggregate_write_concern_errors(count)
    results['writeConcernErrors'] = (results['writeConcernErrors'] || []) +
                                             write_concern_errors
  end
end

#combine_write_errors!(result) (private)

Since:

  • 2.1.0

[ GitHub ]

  
# File 'lib/mongo/bulk_write/result_combiner.rb', line 113

def combine_write_errors!(result)
  if write_errors = result.aggregate_write_errors(count)
    results.merge!(
      'writeErrors' => ((results['writeErrors'] || []) << write_errors).flatten
    )
  else
    result.validate!
  end
end

#resultBulkWrite::Result

Get the final result.

Returns:

Since:

  • 2.1.0

[ GitHub ]

  
# File 'lib/mongo/bulk_write/result_combiner.rb', line 83

def result
  BulkWrite::Result.new(results, @acknowledged).validate!
end