PageRenderTime 98ms CodeModel.GetById 22ms RepoModel.GetById 1ms app.codeStats 0ms

/lib/mongo/cursor.rb

https://github.com/ryanfitz/mongo-ruby-driver
Ruby | 430 lines | 239 code | 48 blank | 143 comment | 50 complexity | 00adf40059157e7130c91e88a73d388b MD5 | raw file
  1. # encoding: UTF-8
  2. # Copyright (C) 2008-2010 10gen Inc.
  3. #
  4. # Licensed under the Apache License, Version 2.0 (the "License");
  5. # you may not use this file except in compliance with the License.
  6. # You may obtain a copy of the License at
  7. #
  8. # http://www.apache.org/licenses/LICENSE-2.0
  9. #
  10. # Unless required by applicable law or agreed to in writing, software
  11. # distributed under the License is distributed on an "AS IS" BASIS,
  12. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. # See the License for the specific language governing permissions and
  14. # limitations under the License.
  15. module Mongo
  16. # A cursor over query results. Returned objects are hashes.
  17. class Cursor
  18. include Mongo::Conversions
  19. include Enumerable
  20. attr_reader :collection, :selector, :fields,
  21. :order, :hint, :snapshot, :timeout,
  22. :full_collection_name, :batch_size
  23. # Create a new cursor.
  24. #
  25. # Note: cursors are created when executing queries using [Collection#find] and other
  26. # similar methods. Application developers shouldn't have to create cursors manually.
  27. #
  28. # @return [Cursor]
  29. #
  30. # @core cursors constructor_details
  31. def initialize(collection, options={})
  32. @db = collection.db
  33. @collection = collection
  34. @connection = @db.connection
  35. @logger = @connection.logger
  36. @selector = options[:selector] || {}
  37. @fields = convert_fields_for_query(options[:fields])
  38. @skip = options[:skip] || 0
  39. @limit = options[:limit] || 0
  40. @order = options[:order]
  41. @hint = options[:hint]
  42. @snapshot = options[:snapshot]
  43. @timeout = options[:timeout] || true
  44. @explain = options[:explain]
  45. @socket = options[:socket]
  46. @tailable = options[:tailable] || false
  47. batch_size(options[:batch_size] || 0)
  48. @full_collection_name = "#{@collection.db.name}.#{@collection.name}"
  49. @cache = []
  50. @closed = false
  51. @query_run = false
  52. @returned = 0
  53. end
  54. # Get the next document specified the cursor options.
  55. #
  56. # @return [Hash, Nil] the next document or Nil if no documents remain.
  57. def next_document
  58. refresh if @cache.length == 0#empty?# num_remaining == 0
  59. doc = @cache.shift
  60. if doc && doc['$err']
  61. err = doc['$err']
  62. # If the server has stopped being the master (e.g., it's one of a
  63. # pair but it has died or something like that) then we close that
  64. # connection. The next request will re-open on master server.
  65. if err == "not master"
  66. @connection.close
  67. raise ConnectionFailure, err
  68. end
  69. raise OperationFailure, err
  70. end
  71. doc
  72. end
  73. # Reset this cursor on the server. Cursor options, such as the
  74. # query string and the values for skip and limit, are preserved.
  75. def rewind!
  76. close
  77. @cache.clear
  78. @cursor_id = nil
  79. @closed = false
  80. @query_run = false
  81. @n_received = nil
  82. true
  83. end
  84. # Determine whether this cursor has any remaining results.
  85. #
  86. # @return [Boolean]
  87. def has_next?
  88. num_remaining > 0
  89. end
  90. # Get the size of the result set for this query.
  91. #
  92. # @return [Integer] the number of objects in the result set for this query. Does
  93. # not take limit and skip into account.
  94. #
  95. # @raise [OperationFailure] on a database error.
  96. def count
  97. command = BSON::OrderedHash["count", @collection.name,
  98. "query", @selector,
  99. "fields", @fields]
  100. response = @db.command(command)
  101. return response['n'].to_i if Mongo::Support.ok?(response)
  102. return 0 if response['errmsg'] == "ns missing"
  103. raise OperationFailure, "Count failed: #{response['errmsg']}"
  104. end
  105. # Sort this cursor's results.
  106. #
  107. # This method overrides any sort order specified in the Collection#find
  108. # method, and only the last sort applied has an effect.
  109. #
  110. # @param [Symbol, Array] key_or_list either 1) a key to sort by or 2)
  111. # an array of [key, direction] pairs to sort by. Direction should
  112. # be specified as Mongo::ASCENDING (or :ascending / :asc) or Mongo::DESCENDING (or :descending / :desc)
  113. #
  114. # @raise [InvalidOperation] if this cursor has already been used.
  115. #
  116. # @raise [InvalidSortValueError] if the specified order is invalid.
  117. def sort(key_or_list, direction=nil)
  118. check_modifiable
  119. if !direction.nil?
  120. order = [[key_or_list, direction]]
  121. else
  122. order = key_or_list
  123. end
  124. @order = order
  125. self
  126. end
  127. # Limit the number of results to be returned by this cursor.
  128. #
  129. # This method overrides any limit specified in the Collection#find method,
  130. # and only the last limit applied has an effect.
  131. #
  132. # @return [Integer] the current number_to_return if no parameter is given.
  133. #
  134. # @raise [InvalidOperation] if this cursor has already been used.
  135. #
  136. # @core limit limit-instance_method
  137. def limit(number_to_return=nil)
  138. return @limit unless number_to_return
  139. check_modifiable
  140. raise ArgumentError, "limit requires an integer" unless number_to_return.is_a? Integer
  141. @limit = number_to_return
  142. self
  143. end
  144. # Skips the first +number_to_skip+ results of this cursor.
  145. # Returns the current number_to_skip if no parameter is given.
  146. #
  147. # This method overrides any skip specified in the Collection#find method,
  148. # and only the last skip applied has an effect.
  149. #
  150. # @return [Integer]
  151. #
  152. # @raise [InvalidOperation] if this cursor has already been used.
  153. def skip(number_to_skip=nil)
  154. return @skip unless number_to_skip
  155. check_modifiable
  156. raise ArgumentError, "skip requires an integer" unless number_to_skip.is_a? Integer
  157. @skip = number_to_skip
  158. self
  159. end
  160. # Set the batch size for server responses.
  161. #
  162. # Note that the batch size will take effect only on queries
  163. # where the number to be returned is greater than 100.
  164. #
  165. # @param [Integer] size either 0 or some integer greater than 1. If 0,
  166. # the server will determine the batch size.
  167. #
  168. # @return [Cursor]
  169. def batch_size(size=0)
  170. check_modifiable
  171. if size < 0 || size == 1
  172. raise ArgumentError, "Invalid value for batch_size #{size}; must be 0 or > 1."
  173. else
  174. @batch_size = size > @limit ? @limit : size
  175. end
  176. self
  177. end
  178. # Iterate over each document in this cursor, yielding it to the given
  179. # block.
  180. #
  181. # Iterating over an entire cursor will close it.
  182. #
  183. # @yield passes each document to a block for processing.
  184. #
  185. # @example if 'comments' represents a collection of comments:
  186. # comments.find.each do |doc|
  187. # puts doc['user']
  188. # end
  189. def each
  190. #num_returned = 0
  191. #while has_next? && (@limit <= 0 || num_returned < @limit)
  192. while doc = next_document
  193. yield doc #next_document
  194. #num_returned += 1
  195. end
  196. end
  197. # Receive all the documents from this cursor as an array of hashes.
  198. #
  199. # Notes:
  200. #
  201. # If you've already started iterating over the cursor, the array returned
  202. # by this method contains only the remaining documents. See Cursor#rewind! if you
  203. # need to reset the cursor.
  204. #
  205. # Use of this method is discouraged - in most cases, it's much more
  206. # efficient to retrieve documents as you need them by iterating over the cursor.
  207. #
  208. # @return [Array] an array of documents.
  209. def to_a
  210. super
  211. end
  212. # Get the explain plan for this cursor.
  213. #
  214. # @return [Hash] a document containing the explain plan for this cursor.
  215. #
  216. # @core explain explain-instance_method
  217. def explain
  218. c = Cursor.new(@collection, query_options_hash.merge(:limit => -@limit.abs, :explain => true))
  219. explanation = c.next_document
  220. c.close
  221. explanation
  222. end
  223. # Close the cursor.
  224. #
  225. # Note: if a cursor is read until exhausted (read until Mongo::Constants::OP_QUERY or
  226. # Mongo::Constants::OP_GETMORE returns zero for the cursor id), there is no need to
  227. # close it manually.
  228. #
  229. # Note also: Collection#find takes an optional block argument which can be used to
  230. # ensure that your cursors get closed.
  231. #
  232. # @return [True]
  233. def close
  234. if @cursor_id && @cursor_id != 0
  235. message = BSON::ByteBuffer.new([0, 0, 0, 0])
  236. message.put_int(1)
  237. message.put_long(@cursor_id)
  238. @logger.debug("MONGODB cursor.close #{@cursor_id}") if @logger
  239. @connection.send_message(Mongo::Constants::OP_KILL_CURSORS, message, nil)
  240. end
  241. @cursor_id = 0
  242. @closed = true
  243. end
  244. # Is this cursor closed?
  245. #
  246. # @return [Boolean]
  247. def closed?; @closed; end
  248. # Returns an integer indicating which query options have been selected.
  249. #
  250. # @return [Integer]
  251. #
  252. # @see http://www.mongodb.org/display/DOCS/Mongo+Wire+Protocol#MongoWireProtocol-Mongo::Constants::OPQUERY
  253. # The MongoDB wire protocol.
  254. def query_opts
  255. opts = 0
  256. opts |= Mongo::Constants::OP_QUERY_NO_CURSOR_TIMEOUT unless @timeout
  257. opts |= Mongo::Constants::OP_QUERY_SLAVE_OK if @connection.slave_ok?
  258. opts |= Mongo::Constants::OP_QUERY_TAILABLE if @tailable
  259. opts
  260. end
  261. # Get the query options for this Cursor.
  262. #
  263. # @return [Hash]
  264. def query_options_hash
  265. { :selector => @selector,
  266. :fields => @fields,
  267. :skip => @skip_num,
  268. :limit => @limit_num,
  269. :order => @order,
  270. :hint => @hint,
  271. :snapshot => @snapshot,
  272. :timeout => @timeout }
  273. end
  274. # Clean output for inspect.
  275. def inspect
  276. "<Mongo::Cursor:0x#{object_id.to_s(16)} namespace='#{@db.name}.#{@collection.name}' " +
  277. "@selector=#{@selector.inspect}>"
  278. end
  279. private
  280. # Convert the +:fields+ parameter from a single field name or an array
  281. # of fields names to a hash, with the field names for keys and '1' for each
  282. # value.
  283. def convert_fields_for_query(fields)
  284. case fields
  285. when String, Symbol
  286. {fields => 1}
  287. when Array
  288. return nil if fields.length.zero?
  289. fields.each_with_object({}) { |field, hash| hash[field] = 1 }
  290. when Hash
  291. return fields
  292. end
  293. end
  294. # Return the number of documents remaining for this cursor.
  295. def num_remaining
  296. refresh if @cache.length == 0
  297. @cache.length
  298. end
  299. def refresh
  300. return if send_initial_query || @cursor_id.zero?
  301. message = BSON::ByteBuffer.new([0, 0, 0, 0])
  302. # DB name.
  303. BSON::BSON_RUBY.serialize_cstr(message, "#{@db.name}.#{@collection.name}")
  304. # Number of results to return.
  305. if @limit > 0
  306. limit = @limit - @returned
  307. if @batch_size > 0
  308. limit = limit < @batch_size ? limit : @batch_size
  309. end
  310. message.put_int(limit)
  311. else
  312. message.put_int(@batch_size)
  313. end
  314. # Cursor id.
  315. message.put_long(@cursor_id)
  316. @logger.debug("MONGODB cursor.refresh() for cursor #{@cursor_id}") if @logger
  317. results, @n_received, @cursor_id = @connection.receive_message(Mongo::Constants::OP_GET_MORE,
  318. message, nil, @socket)
  319. @returned += @n_received
  320. @cache += results
  321. close_cursor_if_query_complete
  322. end
  323. # Run query the first time we request an object from the wire
  324. def send_initial_query
  325. if @query_run
  326. false
  327. else
  328. message = construct_query_message
  329. @logger.debug query_log_message if @logger
  330. results, @n_received, @cursor_id = @connection.receive_message(Mongo::Constants::OP_QUERY, message, nil, @socket)
  331. @returned += @n_received
  332. @cache += results
  333. @query_run = true
  334. close_cursor_if_query_complete
  335. true
  336. end
  337. end
  338. def construct_query_message
  339. message = BSON::ByteBuffer.new
  340. message.put_int(query_opts)
  341. BSON::BSON_RUBY.serialize_cstr(message, "#{@db.name}.#{@collection.name}")
  342. message.put_int(@skip)
  343. message.put_int(@limit)
  344. spec = query_contains_special_fields? ? construct_query_spec : @selector
  345. message.put_binary(BSON::BSON_CODER.serialize(spec, false).to_s)
  346. message.put_binary(BSON::BSON_CODER.serialize(@fields, false).to_s) if @fields
  347. message
  348. end
  349. def query_log_message
  350. "#{@db.name}['#{@collection.name}'].find(#{@selector.inspect}, #{@fields ? @fields.inspect : '{}'})" +
  351. "#{@skip != 0 ? ('.skip(' + @skip.to_s + ')') : ''}#{@limit != 0 ? ('.limit(' + @limit.to_s + ')') : ''}" +
  352. "#{@order ? ('.sort(' + @order.inspect + ')') : ''}"
  353. end
  354. def construct_query_spec
  355. return @selector if @selector.has_key?('$query')
  356. spec = BSON::OrderedHash.new
  357. spec['$query'] = @selector
  358. spec['$orderby'] = Mongo::Support.format_order_clause(@order) if @order
  359. spec['$hint'] = @hint if @hint && @hint.length > 0
  360. spec['$explain'] = true if @explain
  361. spec['$snapshot'] = true if @snapshot
  362. spec
  363. end
  364. # Returns true if the query contains order, explain, hint, or snapshot.
  365. def query_contains_special_fields?
  366. @order || @explain || @hint || @snapshot
  367. end
  368. def to_s
  369. "DBResponse(flags=#@result_flags, cursor_id=#@cursor_id, start=#@starting_from)"
  370. end
  371. def close_cursor_if_query_complete
  372. if @limit > 0 && @returned >= @limit
  373. close
  374. end
  375. end
  376. def check_modifiable
  377. if @query_run || @closed
  378. raise InvalidOperation, "Cannot modify the query once it has been run or closed."
  379. end
  380. end
  381. end
  382. end