Class: Mongo::Collection::View::MapReduce
- Inherits:
-
Object
- Object
- Mongo::Collection::View::MapReduce
- Extended by:
- Forwardable
- Defined in:
- lib/mongo/collection/view/map_reduce.rb
Overview
Provides behavior around a map/reduce operation on the collection view.
Constant Summary collapse
- INLINE =
The inline option.
'inline'.freeze
- REROUTE =
Deprecated.
Reroute message.
'Rerouting the MapReduce operation to the primary server.'.freeze
Constants included from Loggable
Instance Attribute Summary collapse
-
#map_function ⇒ String
readonly
Map The map function.
-
#reduce_function ⇒ String
readonly
Reduce The reduce function.
-
#view ⇒ View
readonly
View The collection view.
Attributes included from Immutable
Instance Method Summary collapse
-
#each {|Each| ... } ⇒ Enumerator
Iterate through documents returned by the map/reduce.
-
#execute ⇒ Mongo::Operation::Result
Execute the map reduce, without doing a fetch query to retrieve the results if outputted to a collection.
-
#finalize(function = nil) ⇒ MapReduce, String
Set or get the finalize function for the operation.
-
#initialize(view, map, reduce, options = {}) ⇒ MapReduce
constructor
Initialize the map/reduce for the provided collection view, functions and options.
-
#js_mode(value = nil) ⇒ MapReduce, ...
Set or get the jsMode flag for the operation.
-
#out(location = nil) ⇒ MapReduce, Hash
Set or get the output location for the operation.
-
#out_collection_name ⇒ Object
Returns the collection name where the map-reduce result is written to.
-
#out_database_name ⇒ Object
Returns the database name where the map-reduce result is written to.
-
#scope(object = nil) ⇒ MapReduce, Hash
Set or get a scope on the operation.
-
#verbose(value = nil) ⇒ MapReduce, Hash
Whether to include the timing information in the result.
Methods included from Retryable
#read_worker, #select_server, #write_worker
Methods included from Loggable
#log_debug, #log_error, #log_fatal, #log_info, #log_warn, #logger
Constructor Details
#initialize(view, map, reduce, options = {}) ⇒ MapReduce
Initialize the map/reduce for the provided collection view, functions and options.
121 122 123 124 125 126 127 128 |
# File 'lib/mongo/collection/view/map_reduce.rb', line 121 def initialize(view, map, reduce, = {}) @view = view @map_function = map.dup.freeze @reduce_function = reduce.dup.freeze @options = BSON::Document.new().freeze client.log_warn('The map_reduce operation is deprecated, please use the aggregation pipeline instead') end |
Instance Attribute Details
#map_function ⇒ String (readonly)
Returns map The map function.
48 49 50 |
# File 'lib/mongo/collection/view/map_reduce.rb', line 48 def map_function @map_function end |
#reduce_function ⇒ String (readonly)
Returns reduce The reduce function.
51 52 53 |
# File 'lib/mongo/collection/view/map_reduce.rb', line 51 def reduce_function @reduce_function end |
#view ⇒ View (readonly)
Returns view The collection view.
45 46 47 |
# File 'lib/mongo/collection/view/map_reduce.rb', line 45 def view @view end |
Instance Method Details
#each {|Each| ... } ⇒ Enumerator
Iterate through documents returned by the map/reduce.
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 |
# File 'lib/mongo/collection/view/map_reduce.rb', line 71 def each @cursor = nil session = client.get_session(@options) server = cluster.next_primary(nil, session) context = Operation::Context.new(client: client, session: session, operation_timeouts: view.operation_timeouts) if server.load_balancer? # Connection will be checked in when cursor is drained. connection = server.pool.check_out(context: context) result = send_initial_query_with_connection(connection, context.session, context: context) result = send_fetch_query_with_connection(connection, session) unless inline? else result = send_initial_query(server, context) result = send_fetch_query(server, session) unless inline? end @cursor = Cursor.new(view, result, server, session: session) if block_given? @cursor.each do |doc| yield doc end else @cursor.to_enum end end |
#execute ⇒ Mongo::Operation::Result
Execute the map reduce, without doing a fetch query to retrieve the results
if outputted to a collection.
231 232 233 234 235 236 237 238 239 |
# File 'lib/mongo/collection/view/map_reduce.rb', line 231 def execute view.send(:with_session, @options) do |session| write_concern = view.write_concern_with_session(session) context = Operation::Context.new(client: client, session: session) nro_write_with_retry(write_concern, context: context) do |connection, txn_num, context| send_initial_query_with_connection(connection, session, context: context) end end end |
#finalize(function = nil) ⇒ MapReduce, String
Set or get the finalize function for the operation.
106 107 108 |
# File 'lib/mongo/collection/view/map_reduce.rb', line 106 def finalize(function = nil) configure(:finalize, function) end |
#js_mode(value = nil) ⇒ MapReduce, ...
Set or get the jsMode flag for the operation.
141 142 143 |
# File 'lib/mongo/collection/view/map_reduce.rb', line 141 def js_mode(value = nil) configure(:js_mode, value) end |
#out(location = nil) ⇒ MapReduce, Hash
Set or get the output location for the operation.
165 166 167 |
# File 'lib/mongo/collection/view/map_reduce.rb', line 165 def out(location = nil) configure(:out, location) end |
#out_collection_name ⇒ Object
Returns the collection name where the map-reduce result is written to. If the result is returned inline, returns nil.
171 172 173 174 175 176 177 |
# File 'lib/mongo/collection/view/map_reduce.rb', line 171 def out_collection_name if [:out].respond_to?(:keys) [:out][OUT_ACTIONS.find do |action| [:out][action] end] end || [:out] end |
#out_database_name ⇒ Object
Returns the database name where the map-reduce result is written to. If the result is returned inline, returns nil.
181 182 183 184 185 186 187 188 189 |
# File 'lib/mongo/collection/view/map_reduce.rb', line 181 def out_database_name if [:out] if [:out].respond_to?(:keys) && (db = [:out][:db]) db else database.name end end end |
#scope(object = nil) ⇒ MapReduce, Hash
Set or get a scope on the operation.
202 203 204 |
# File 'lib/mongo/collection/view/map_reduce.rb', line 202 def scope(object = nil) configure(:scope, object) end |
#verbose(value = nil) ⇒ MapReduce, Hash
Whether to include the timing information in the result.
218 219 220 |
# File 'lib/mongo/collection/view/map_reduce.rb', line 218 def verbose(value = nil) configure(:verbose, value) end |