123456789_123456789_123456789_123456789_123456789_

Class: Mongo::Collection::View::Builder::MapReduce

Relationships & Source Files
Super Chains via Extension / Inclusion / Inheritance
Class Chain:
self, Forwardable
Inherits: Object
Defined in: lib/mongo/collection/view/builder/map_reduce.rb

Overview

Builds a map/reduce specification from the view and options.

Since:

  • 2.2.0

Constant Summary

  • MAPPINGS =

    The mappings from ruby options to the map/reduce options.

    Since:

    • 2.2.0

    # File 'lib/mongo/collection/view/builder/map_reduce.rb', line 32
    BSON::Document.new(
      finalize: 'finalize',
      js_mode: 'jsMode',
      out: 'out',
      scope: 'scope',
      verbose: 'verbose',
      bypass_document_validation: 'bypassDocumentValidation',
      collation: 'collation',
    ).freeze

Class Method Summary

Instance Attribute Summary

Instance Method Summary

Constructor Details

.new(map, reduce, view, options) ⇒ MapReduce

Initialize the builder.

Examples:

Initialize the builder.

MapReduce.new(map, reduce, view, options)

Parameters:

  • map (String)

    The map function.

  • reduce (String)

    The reduce function.

  • view (Collection::View)

    The collection view.

  • options (Hash)

    The map/reduce options.

Since:

  • 2.2.0

[ GitHub ]

  
# File 'lib/mongo/collection/view/builder/map_reduce.rb', line 67

def initialize(map, reduce, view, options)
  @map = map
  @reduce = reduce
  @view = view
  @options = options
end

Instance Attribute Details

#mapString (readonly)

Returns:

  • (String)

    map The map function.

Since:

  • 2.2.0

[ GitHub ]

  
# File 'lib/mongo/collection/view/builder/map_reduce.rb', line 45

attr_reader :map

#optionsHash (readonly)

Returns:

  • (Hash)

    options The map/reduce specific options.

Since:

  • 2.2.0

[ GitHub ]

  
# File 'lib/mongo/collection/view/builder/map_reduce.rb', line 54

attr_reader :options

#reduceString (readonly)

Returns:

  • (String)

    reduce The reduce function.

Since:

  • 2.2.0

[ GitHub ]

  
# File 'lib/mongo/collection/view/builder/map_reduce.rb', line 48

attr_reader :reduce

#viewCollection::View (readonly)

Returns:

Since:

  • 2.2.0

[ GitHub ]

  
# File 'lib/mongo/collection/view/builder/map_reduce.rb', line 51

attr_reader :view

Instance Method Details

#map_reduce_command (private)

Since:

  • 2.2.0

[ GitHub ]

  
# File 'lib/mongo/collection/view/builder/map_reduce.rb', line 103

def map_reduce_command
  command = BSON::Document.new(
    :mapReduce => collection.name,
    :map => map,
    :reduce => reduce,
    :query => filter,
    :out => { inline: 1 },
  )
  # Shouldn't this use self.read ?
  if collection.read_concern
    command[:readConcern] = Options::Mapper.transform_values_to_strings(
      collection.read_concern)
  end
  command.update(view_options)
  command.update(options.slice(:collation))

  # Read preference isn't simply passed in the command payload
  # (it may need to be converted to wire protocol flags).
  # Ideally it should be removed here, however due to Mongoid 7
  # using this method and requiring :read to be returned from it,
  # we cannot do this just yet - see RUBY-2932.
  #command.delete(:read)

  command.merge!(Options::Mapper.transform_documents(options, MAPPINGS))
  command
end

#specificationHash

Get the specification to pass to the map/reduce operation.

Examples:

Get the specification.

builder.specification

Returns:

  • (Hash)

    The specification.

Since:

  • 2.2.0

[ GitHub ]

  
# File 'lib/mongo/collection/view/builder/map_reduce.rb', line 82

def specification
  spec = {
    selector: map_reduce_command,
    db_name: database.name,
    # Note that selector just above may also have a read preference
    # specified, per the #map_reduce_command method below.
    read: read,
    session: options[:session]
  }
  write?(spec) ? spec.merge!(write_concern: write_concern) : spec
end

#view_options (private)

Since:

  • 2.2.0

[ GitHub ]

  
# File 'lib/mongo/collection/view/builder/map_reduce.rb', line 130

def view_options
  @view_options ||= (opts = view.options.dup
                     opts.delete(:session)
                     opts)
end

#write?(spec) ⇒ Boolean (private)

Since:

  • 2.2.0

[ GitHub ]

  
# File 'lib/mongo/collection/view/builder/map_reduce.rb', line 96

def write?(spec)
  if out = spec[:selector][:out]
    out.is_a?(String) ||
      (out.respond_to?(:keys) && out.keys.first.to_s.downcase != View::MapReduce::INLINE)
  end
end