2010-05-07 19:33:27 +00:00
|
|
|
# encoding: UTF-8
|
|
|
|
|
2011-01-17 17:26:32 +00:00
|
|
|
# Copyright (C) 2008-2011 10gen Inc.
|
2008-11-22 01:00:51 +00:00
|
|
|
#
|
2009-02-15 13:24:14 +00:00
|
|
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
# you may not use this file except in compliance with the License.
|
|
|
|
# You may obtain a copy of the License at
|
2008-11-22 01:00:51 +00:00
|
|
|
#
|
2009-02-15 13:24:14 +00:00
|
|
|
# http://www.apache.org/licenses/LICENSE-2.0
|
2008-11-22 01:00:51 +00:00
|
|
|
#
|
2009-02-15 13:24:14 +00:00
|
|
|
# Unless required by applicable law or agreed to in writing, software
|
|
|
|
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
# See the License for the specific language governing permissions and
|
|
|
|
# limitations under the License.
|
2008-11-22 01:00:51 +00:00
|
|
|
|
2009-08-20 14:50:48 +00:00
|
|
|
module Mongo
|
2008-11-22 01:00:51 +00:00
|
|
|
|
2009-08-20 14:50:48 +00:00
|
|
|
# A cursor over query results. Returned objects are hashes.
|
|
|
|
class Cursor
|
|
|
|
include Enumerable
|
2011-08-05 15:52:45 +00:00
|
|
|
include Mongo::Constants
|
2011-09-06 15:38:19 +00:00
|
|
|
include Mongo::Conversions
|
2008-11-22 01:00:51 +00:00
|
|
|
|
2010-07-12 18:31:12 +00:00
|
|
|
attr_reader :collection, :selector, :fields,
|
2009-10-22 18:10:12 +00:00
|
|
|
:order, :hint, :snapshot, :timeout,
|
2011-08-05 15:52:45 +00:00
|
|
|
:full_collection_name, :transformer,
|
|
|
|
:options
|
2009-01-13 19:02:16 +00:00
|
|
|
|
2009-08-21 15:21:33 +00:00
|
|
|
# Create a new cursor.
|
|
|
|
#
|
2010-01-08 21:18:07 +00:00
|
|
|
# Note: cursors are created when executing queries using [Collection#find] and other
|
|
|
|
# similar methods. Application developers shouldn't have to create cursors manually.
|
|
|
|
#
|
|
|
|
# @return [Cursor]
|
2010-02-08 17:12:18 +00:00
|
|
|
#
|
|
|
|
# @core cursors constructor_details
|
2011-01-05 16:30:20 +00:00
|
|
|
def initialize(collection, opts={})
|
2011-02-20 16:03:32 +00:00
|
|
|
@cursor_id = nil
|
|
|
|
|
2009-10-22 18:10:12 +00:00
|
|
|
@db = collection.db
|
|
|
|
@collection = collection
|
2009-11-23 20:20:05 +00:00
|
|
|
@connection = @db.connection
|
2010-09-08 18:27:27 +00:00
|
|
|
@logger = @connection.logger
|
2009-10-22 18:10:12 +00:00
|
|
|
|
2011-05-10 19:40:06 +00:00
|
|
|
# Query selector
|
2011-01-05 16:30:20 +00:00
|
|
|
@selector = opts[:selector] || {}
|
2011-05-10 19:40:06 +00:00
|
|
|
|
|
|
|
# Special operators that form part of $query
|
2011-01-05 16:30:20 +00:00
|
|
|
@order = opts[:order]
|
2011-05-10 19:40:06 +00:00
|
|
|
@explain = opts[:explain]
|
2011-01-05 16:30:20 +00:00
|
|
|
@hint = opts[:hint]
|
|
|
|
@snapshot = opts[:snapshot]
|
2011-05-10 19:40:06 +00:00
|
|
|
@max_scan = opts.fetch(:max_scan, nil)
|
|
|
|
@return_key = opts.fetch(:return_key, nil)
|
|
|
|
@show_disk_loc = opts.fetch(:show_disk_loc, nil)
|
|
|
|
|
|
|
|
# Wire-protocol settings
|
|
|
|
@fields = convert_fields_for_query(opts[:fields])
|
|
|
|
@skip = opts[:skip] || 0
|
|
|
|
@limit = opts[:limit] || 0
|
|
|
|
@tailable = opts[:tailable] || false
|
2011-01-05 16:30:20 +00:00
|
|
|
@timeout = opts.fetch(:timeout, true)
|
2011-08-05 15:52:45 +00:00
|
|
|
@options = 0
|
2011-05-10 19:40:06 +00:00
|
|
|
|
|
|
|
# Use this socket for the query
|
2011-01-05 16:30:20 +00:00
|
|
|
@socket = opts[:socket]
|
2011-05-10 19:40:06 +00:00
|
|
|
|
2010-10-27 09:01:44 +00:00
|
|
|
@closed = false
|
|
|
|
@query_run = false
|
2011-05-10 19:40:06 +00:00
|
|
|
|
2011-03-18 19:03:37 +00:00
|
|
|
@transformer = opts[:transformer]
|
2011-08-29 21:49:58 +00:00
|
|
|
read = opts[:read] || collection.read_preference
|
|
|
|
@read_preference = read.is_a?(Hash) ? read.dup : read
|
2011-01-05 16:30:20 +00:00
|
|
|
batch_size(opts[:batch_size] || 0)
|
2009-10-22 18:10:12 +00:00
|
|
|
|
2009-11-23 20:20:05 +00:00
|
|
|
@full_collection_name = "#{@collection.db.name}.#{@collection.name}"
|
2010-09-09 18:12:12 +00:00
|
|
|
@cache = []
|
|
|
|
@returned = 0
|
2010-11-16 20:43:59 +00:00
|
|
|
|
2011-08-05 15:52:45 +00:00
|
|
|
if(!@timeout)
|
|
|
|
add_option(OP_QUERY_NO_CURSOR_TIMEOUT)
|
|
|
|
end
|
|
|
|
if(@connection.slave_ok?)
|
|
|
|
add_option(OP_QUERY_SLAVE_OK)
|
|
|
|
end
|
|
|
|
if(@tailable)
|
|
|
|
add_option(OP_QUERY_TAILABLE)
|
|
|
|
end
|
|
|
|
|
2010-11-16 20:43:59 +00:00
|
|
|
if @collection.name =~ /^\$cmd/ || @collection.name =~ /^system/
|
|
|
|
@command = true
|
|
|
|
else
|
|
|
|
@command = false
|
|
|
|
end
|
2009-08-20 14:50:48 +00:00
|
|
|
end
|
2009-01-13 19:02:16 +00:00
|
|
|
|
2011-08-05 21:54:38 +00:00
|
|
|
# Guess whether the cursor is alive on the server.
|
|
|
|
#
|
|
|
|
# Note that this method only checks whether we have
|
|
|
|
# a cursor id. The cursor may still have timed out
|
|
|
|
# on the server. This will be indicated in the next
|
|
|
|
# call to Cursor#next_document.
|
|
|
|
#
|
|
|
|
# @return [Boolean]
|
|
|
|
def alive?
|
|
|
|
@cursor_id && @cursor_id != 0
|
|
|
|
end
|
|
|
|
|
2010-01-08 21:18:07 +00:00
|
|
|
# Get the next document specified the cursor options.
|
|
|
|
#
|
|
|
|
# @return [Hash, Nil] the next document or Nil if no documents remain.
|
2009-12-16 19:03:15 +00:00
|
|
|
def next_document
|
2010-11-16 20:43:59 +00:00
|
|
|
refresh if @cache.length == 0
|
2009-12-16 19:03:15 +00:00
|
|
|
doc = @cache.shift
|
2009-01-23 16:47:22 +00:00
|
|
|
|
2009-12-16 19:03:15 +00:00
|
|
|
if doc && doc['$err']
|
|
|
|
err = doc['$err']
|
2009-01-23 16:47:22 +00:00
|
|
|
|
2009-08-20 14:50:48 +00:00
|
|
|
# If the server has stopped being the master (e.g., it's one of a
|
|
|
|
# pair but it has died or something like that) then we close that
|
2009-12-16 19:03:15 +00:00
|
|
|
# connection. The next request will re-open on master server.
|
2009-11-23 18:13:14 +00:00
|
|
|
if err == "not master"
|
2009-11-23 23:09:13 +00:00
|
|
|
@connection.close
|
2011-09-06 18:22:50 +00:00
|
|
|
raise ConnectionFailure.new(err, doc['code'], doc)
|
2009-11-23 18:13:14 +00:00
|
|
|
end
|
2009-01-23 16:47:22 +00:00
|
|
|
|
2011-09-06 18:22:50 +00:00
|
|
|
raise OperationFailure.new(err, doc['code'], doc)
|
2009-08-20 14:50:48 +00:00
|
|
|
end
|
2008-11-22 01:00:51 +00:00
|
|
|
|
2011-03-18 19:03:37 +00:00
|
|
|
if @transformer.nil?
|
|
|
|
doc
|
|
|
|
else
|
|
|
|
@transformer.call(doc) if doc
|
|
|
|
end
|
2009-08-20 14:50:48 +00:00
|
|
|
end
|
2011-03-15 18:24:34 +00:00
|
|
|
alias :next :next_document
|
2009-08-18 15:26:58 +00:00
|
|
|
|
2010-08-26 16:35:42 +00:00
|
|
|
# Reset this cursor on the server. Cursor options, such as the
|
|
|
|
# query string and the values for skip and limit, are preserved.
|
|
|
|
def rewind!
|
|
|
|
close
|
|
|
|
@cache.clear
|
|
|
|
@cursor_id = nil
|
|
|
|
@closed = false
|
|
|
|
@query_run = false
|
|
|
|
@n_received = nil
|
|
|
|
true
|
|
|
|
end
|
|
|
|
|
2010-03-19 18:31:31 +00:00
|
|
|
# Determine whether this cursor has any remaining results.
|
|
|
|
#
|
|
|
|
# @return [Boolean]
|
|
|
|
def has_next?
|
|
|
|
num_remaining > 0
|
|
|
|
end
|
|
|
|
|
2009-12-16 19:03:15 +00:00
|
|
|
# Get the size of the result set for this query.
|
2009-08-20 14:50:48 +00:00
|
|
|
#
|
2010-11-15 14:44:33 +00:00
|
|
|
# @param [Boolean] whether of not to take notice of skip and limit
|
|
|
|
#
|
2010-11-24 19:01:26 +00:00
|
|
|
# @return [Integer] the number of objects in the result set for this query.
|
2010-01-08 21:18:07 +00:00
|
|
|
#
|
|
|
|
# @raise [OperationFailure] on a database error.
|
2010-11-24 19:01:26 +00:00
|
|
|
def count(skip_and_limit = false)
|
|
|
|
command = BSON::OrderedHash["count", @collection.name, "query", @selector]
|
|
|
|
|
2010-11-15 14:44:33 +00:00
|
|
|
if skip_and_limit
|
2010-11-24 19:01:26 +00:00
|
|
|
command.merge!(BSON::OrderedHash["limit", @limit]) if @limit != 0
|
|
|
|
command.merge!(BSON::OrderedHash["skip", @skip]) if @skip != 0
|
2010-11-15 14:44:33 +00:00
|
|
|
end
|
|
|
|
|
2010-11-24 19:01:26 +00:00
|
|
|
command.merge!(BSON::OrderedHash["fields", @fields])
|
2010-11-15 14:44:33 +00:00
|
|
|
|
2009-11-23 20:20:05 +00:00
|
|
|
response = @db.command(command)
|
2010-06-01 02:52:03 +00:00
|
|
|
return response['n'].to_i if Mongo::Support.ok?(response)
|
2009-08-20 14:50:48 +00:00
|
|
|
return 0 if response['errmsg'] == "ns missing"
|
2011-09-06 18:22:50 +00:00
|
|
|
raise OperationFailure.new("Count failed: #{response['errmsg']}", response['code'], response)
|
2009-08-20 14:50:48 +00:00
|
|
|
end
|
2009-09-17 18:54:30 +00:00
|
|
|
|
2009-12-16 19:03:15 +00:00
|
|
|
# Sort this cursor's results.
|
2009-09-17 18:54:30 +00:00
|
|
|
#
|
|
|
|
# This method overrides any sort order specified in the Collection#find
|
2009-12-16 19:03:15 +00:00
|
|
|
# method, and only the last sort applied has an effect.
|
2010-01-08 21:18:07 +00:00
|
|
|
#
|
|
|
|
# @param [Symbol, Array] key_or_list either 1) a key to sort by or 2)
|
|
|
|
# an array of [key, direction] pairs to sort by. Direction should
|
|
|
|
# be specified as Mongo::ASCENDING (or :ascending / :asc) or Mongo::DESCENDING (or :descending / :desc)
|
|
|
|
#
|
|
|
|
# @raise [InvalidOperation] if this cursor has already been used.
|
|
|
|
#
|
|
|
|
# @raise [InvalidSortValueError] if the specified order is invalid.
|
2009-10-08 14:02:54 +00:00
|
|
|
def sort(key_or_list, direction=nil)
|
2009-09-17 19:07:18 +00:00
|
|
|
check_modifiable
|
2009-10-08 14:02:54 +00:00
|
|
|
|
|
|
|
if !direction.nil?
|
|
|
|
order = [[key_or_list, direction]]
|
|
|
|
else
|
|
|
|
order = key_or_list
|
|
|
|
end
|
|
|
|
|
2009-10-22 18:10:12 +00:00
|
|
|
@order = order
|
2009-09-04 23:04:11 +00:00
|
|
|
self
|
|
|
|
end
|
2009-01-07 19:07:17 +00:00
|
|
|
|
2010-01-08 21:18:07 +00:00
|
|
|
# Limit the number of results to be returned by this cursor.
|
2009-09-17 18:54:30 +00:00
|
|
|
#
|
|
|
|
# This method overrides any limit specified in the Collection#find method,
|
|
|
|
# and only the last limit applied has an effect.
|
2010-01-08 21:18:07 +00:00
|
|
|
#
|
|
|
|
# @return [Integer] the current number_to_return if no parameter is given.
|
|
|
|
#
|
|
|
|
# @raise [InvalidOperation] if this cursor has already been used.
|
2010-02-08 17:12:18 +00:00
|
|
|
#
|
|
|
|
# @core limit limit-instance_method
|
2009-10-22 18:10:12 +00:00
|
|
|
def limit(number_to_return=nil)
|
|
|
|
return @limit unless number_to_return
|
2009-09-05 18:25:49 +00:00
|
|
|
check_modifiable
|
|
|
|
|
2009-10-22 18:10:12 +00:00
|
|
|
@limit = number_to_return
|
2009-09-17 19:07:18 +00:00
|
|
|
self
|
2009-09-05 18:25:49 +00:00
|
|
|
end
|
2009-09-16 21:52:41 +00:00
|
|
|
|
|
|
|
# Skips the first +number_to_skip+ results of this cursor.
|
2009-10-22 18:10:12 +00:00
|
|
|
# Returns the current number_to_skip if no parameter is given.
|
2009-12-16 19:03:15 +00:00
|
|
|
#
|
2009-09-17 20:45:03 +00:00
|
|
|
# This method overrides any skip specified in the Collection#find method,
|
2009-09-17 18:54:30 +00:00
|
|
|
# and only the last skip applied has an effect.
|
2010-01-08 21:18:07 +00:00
|
|
|
#
|
|
|
|
# @return [Integer]
|
|
|
|
#
|
|
|
|
# @raise [InvalidOperation] if this cursor has already been used.
|
2009-10-22 18:10:12 +00:00
|
|
|
def skip(number_to_skip=nil)
|
|
|
|
return @skip unless number_to_skip
|
2009-09-05 18:25:49 +00:00
|
|
|
check_modifiable
|
|
|
|
|
2009-10-22 18:10:12 +00:00
|
|
|
@skip = number_to_skip
|
2009-09-17 19:07:18 +00:00
|
|
|
self
|
2009-09-05 18:25:49 +00:00
|
|
|
end
|
|
|
|
|
2010-09-09 18:12:12 +00:00
|
|
|
# Set the batch size for server responses.
|
|
|
|
#
|
|
|
|
# Note that the batch size will take effect only on queries
|
|
|
|
# where the number to be returned is greater than 100.
|
|
|
|
#
|
|
|
|
# @param [Integer] size either 0 or some integer greater than 1. If 0,
|
|
|
|
# the server will determine the batch size.
|
|
|
|
#
|
|
|
|
# @return [Cursor]
|
2011-06-16 14:21:40 +00:00
|
|
|
def batch_size(size=nil)
|
|
|
|
return @batch_size unless size
|
2010-09-09 18:12:12 +00:00
|
|
|
check_modifiable
|
|
|
|
if size < 0 || size == 1
|
|
|
|
raise ArgumentError, "Invalid value for batch_size #{size}; must be 0 or > 1."
|
|
|
|
else
|
2011-06-16 14:21:40 +00:00
|
|
|
@batch_size = @limit != 0 && size > @limit ? @limit : size
|
2010-09-09 18:12:12 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
self
|
|
|
|
end
|
|
|
|
|
2009-08-21 18:11:59 +00:00
|
|
|
# Iterate over each document in this cursor, yielding it to the given
|
|
|
|
# block.
|
2009-08-20 14:50:48 +00:00
|
|
|
#
|
2009-08-21 18:11:59 +00:00
|
|
|
# Iterating over an entire cursor will close it.
|
2010-01-08 21:18:07 +00:00
|
|
|
#
|
|
|
|
# @yield passes each document to a block for processing.
|
|
|
|
#
|
|
|
|
# @example if 'comments' represents a collection of comments:
|
|
|
|
# comments.find.each do |doc|
|
|
|
|
# puts doc['user']
|
|
|
|
# end
|
2009-08-20 14:50:48 +00:00
|
|
|
def each
|
2010-09-09 18:12:12 +00:00
|
|
|
#num_returned = 0
|
|
|
|
#while has_next? && (@limit <= 0 || num_returned < @limit)
|
|
|
|
while doc = next_document
|
|
|
|
yield doc #next_document
|
|
|
|
#num_returned += 1
|
2009-08-20 14:50:48 +00:00
|
|
|
end
|
|
|
|
end
|
2008-11-22 01:00:51 +00:00
|
|
|
|
2010-01-08 21:18:07 +00:00
|
|
|
# Receive all the documents from this cursor as an array of hashes.
|
|
|
|
#
|
2010-08-26 16:35:42 +00:00
|
|
|
# Notes:
|
|
|
|
#
|
|
|
|
# If you've already started iterating over the cursor, the array returned
|
|
|
|
# by this method contains only the remaining documents. See Cursor#rewind! if you
|
|
|
|
# need to reset the cursor.
|
|
|
|
#
|
|
|
|
# Use of this method is discouraged - in most cases, it's much more
|
2010-01-08 21:18:07 +00:00
|
|
|
# efficient to retrieve documents as you need them by iterating over the cursor.
|
2009-08-20 14:50:48 +00:00
|
|
|
#
|
2010-01-08 21:18:07 +00:00
|
|
|
# @return [Array] an array of documents.
|
2009-08-20 14:50:48 +00:00
|
|
|
def to_a
|
2010-08-26 16:35:42 +00:00
|
|
|
super
|
2009-08-20 14:50:48 +00:00
|
|
|
end
|
2009-01-14 15:42:56 +00:00
|
|
|
|
2010-01-08 21:18:07 +00:00
|
|
|
# Get the explain plan for this cursor.
|
|
|
|
#
|
|
|
|
# @return [Hash] a document containing the explain plan for this cursor.
|
2010-02-08 17:12:18 +00:00
|
|
|
#
|
|
|
|
# @core explain explain-instance_method
|
2009-08-20 14:50:48 +00:00
|
|
|
def explain
|
2011-08-05 15:52:45 +00:00
|
|
|
c = Cursor.new(@collection,
|
|
|
|
query_options_hash.merge(:limit => -@limit.abs, :explain => true))
|
2009-12-16 19:03:15 +00:00
|
|
|
explanation = c.next_document
|
2009-08-20 14:50:48 +00:00
|
|
|
c.close
|
2009-01-13 19:02:16 +00:00
|
|
|
|
2009-08-20 14:50:48 +00:00
|
|
|
explanation
|
|
|
|
end
|
2008-11-22 01:00:51 +00:00
|
|
|
|
2010-01-08 21:18:07 +00:00
|
|
|
# Close the cursor.
|
2009-08-20 14:50:48 +00:00
|
|
|
#
|
2009-10-21 14:11:33 +00:00
|
|
|
# Note: if a cursor is read until exhausted (read until Mongo::Constants::OP_QUERY or
|
|
|
|
# Mongo::Constants::OP_GETMORE returns zero for the cursor id), there is no need to
|
2010-01-08 21:18:07 +00:00
|
|
|
# close it manually.
|
2009-08-21 15:21:33 +00:00
|
|
|
#
|
2010-01-08 21:18:07 +00:00
|
|
|
# Note also: Collection#find takes an optional block argument which can be used to
|
|
|
|
# ensure that your cursors get closed.
|
|
|
|
#
|
|
|
|
# @return [True]
|
2009-08-20 14:50:48 +00:00
|
|
|
def close
|
2010-08-06 20:02:51 +00:00
|
|
|
if @cursor_id && @cursor_id != 0
|
2010-04-05 14:39:55 +00:00
|
|
|
message = BSON::ByteBuffer.new([0, 0, 0, 0])
|
2009-10-21 14:11:33 +00:00
|
|
|
message.put_int(1)
|
|
|
|
message.put_long(@cursor_id)
|
2010-09-08 18:27:27 +00:00
|
|
|
@logger.debug("MONGODB cursor.close #{@cursor_id}") if @logger
|
2011-05-10 18:21:23 +00:00
|
|
|
@connection.send_message(Mongo::Constants::OP_KILL_CURSORS, message, :connection => :reader)
|
2009-10-21 14:11:33 +00:00
|
|
|
end
|
2009-08-20 14:50:48 +00:00
|
|
|
@cursor_id = 0
|
2009-10-21 14:11:33 +00:00
|
|
|
@closed = true
|
2009-08-20 14:50:48 +00:00
|
|
|
end
|
2008-11-22 01:00:51 +00:00
|
|
|
|
2010-01-08 21:18:07 +00:00
|
|
|
# Is this cursor closed?
|
|
|
|
#
|
|
|
|
# @return [Boolean]
|
2009-08-21 15:21:33 +00:00
|
|
|
def closed?; @closed; end
|
|
|
|
|
2009-10-22 18:10:12 +00:00
|
|
|
# Returns an integer indicating which query options have been selected.
|
2010-01-08 21:18:07 +00:00
|
|
|
#
|
|
|
|
# @return [Integer]
|
|
|
|
#
|
|
|
|
# @see http://www.mongodb.org/display/DOCS/Mongo+Wire+Protocol#MongoWireProtocol-Mongo::Constants::OPQUERY
|
|
|
|
# The MongoDB wire protocol.
|
2009-10-22 18:10:12 +00:00
|
|
|
def query_opts
|
2011-08-05 15:52:45 +00:00
|
|
|
warn "The method Cursor#query_opts has been deprecated " +
|
|
|
|
"and will removed in v2.0. Use Cursor#options instead."
|
|
|
|
@options
|
|
|
|
end
|
|
|
|
|
|
|
|
# Add an option to the query options bitfield.
|
|
|
|
#
|
|
|
|
# @param opt a valid query option
|
|
|
|
#
|
2011-08-05 22:22:09 +00:00
|
|
|
# @raise InvalidOperation if this method is run after the cursor has bee
|
|
|
|
# iterated for the first time.
|
|
|
|
#
|
2011-08-05 22:15:48 +00:00
|
|
|
# @return [Integer] the current value of the options bitfield for this cursor.
|
2011-08-05 15:52:45 +00:00
|
|
|
#
|
|
|
|
# @see http://www.mongodb.org/display/DOCS/Mongo+Wire+Protocol#MongoWireProtocol-Mongo::Constants::OPQUERY
|
|
|
|
def add_option(opt)
|
2011-08-05 22:22:09 +00:00
|
|
|
check_modifiable
|
|
|
|
|
2011-08-05 15:52:45 +00:00
|
|
|
@options |= opt
|
2011-08-05 22:15:48 +00:00
|
|
|
@options
|
|
|
|
end
|
|
|
|
|
|
|
|
# Remove an option from the query options bitfield.
|
|
|
|
#
|
|
|
|
# @param opt a valid query option
|
|
|
|
#
|
2011-08-05 22:22:09 +00:00
|
|
|
# @raise InvalidOperation if this method is run after the cursor has bee
|
|
|
|
# iterated for the first time.
|
|
|
|
#
|
2011-08-05 22:15:48 +00:00
|
|
|
# @return [Integer] the current value of the options bitfield for this cursor.
|
|
|
|
#
|
|
|
|
# @see http://www.mongodb.org/display/DOCS/Mongo+Wire+Protocol#MongoWireProtocol-Mongo::Constants::OPQUERY
|
|
|
|
def remove_option(opt)
|
2011-08-05 22:22:09 +00:00
|
|
|
check_modifiable
|
|
|
|
|
2011-08-05 22:15:48 +00:00
|
|
|
@options &= ~opt
|
|
|
|
@options
|
2009-10-22 18:10:12 +00:00
|
|
|
end
|
|
|
|
|
2010-01-08 21:18:07 +00:00
|
|
|
# Get the query options for this Cursor.
|
|
|
|
#
|
|
|
|
# @return [Hash]
|
2009-10-22 18:10:12 +00:00
|
|
|
def query_options_hash
|
|
|
|
{ :selector => @selector,
|
2009-12-16 19:03:15 +00:00
|
|
|
:fields => @fields,
|
2011-02-20 15:22:39 +00:00
|
|
|
:skip => @skip,
|
|
|
|
:limit => @limit,
|
2009-12-16 19:03:15 +00:00
|
|
|
:order => @order,
|
|
|
|
:hint => @hint,
|
|
|
|
:snapshot => @snapshot,
|
2011-05-10 19:40:06 +00:00
|
|
|
:timeout => @timeout,
|
|
|
|
:max_scan => @max_scan,
|
|
|
|
:return_key => @return_key,
|
|
|
|
:show_disk_loc => @show_disk_loc }
|
2009-10-22 18:10:12 +00:00
|
|
|
end
|
|
|
|
|
2010-07-02 21:24:45 +00:00
|
|
|
# Clean output for inspect.
|
|
|
|
def inspect
|
|
|
|
"<Mongo::Cursor:0x#{object_id.to_s(16)} namespace='#{@db.name}.#{@collection.name}' " +
|
|
|
|
"@selector=#{@selector.inspect}>"
|
|
|
|
end
|
|
|
|
|
2009-08-21 15:03:56 +00:00
|
|
|
private
|
2008-11-22 01:00:51 +00:00
|
|
|
|
2010-01-08 21:18:07 +00:00
|
|
|
# Convert the +:fields+ parameter from a single field name or an array
|
2009-10-22 18:10:12 +00:00
|
|
|
# of fields names to a hash, with the field names for keys and '1' for each
|
|
|
|
# value.
|
|
|
|
def convert_fields_for_query(fields)
|
|
|
|
case fields
|
|
|
|
when String, Symbol
|
|
|
|
{fields => 1}
|
|
|
|
when Array
|
|
|
|
return nil if fields.length.zero?
|
2010-09-04 13:14:46 +00:00
|
|
|
fields.each_with_object({}) { |field, hash| hash[field] = 1 }
|
2010-03-22 03:33:37 +00:00
|
|
|
when Hash
|
|
|
|
return fields
|
2009-10-22 18:10:12 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2010-09-09 18:12:12 +00:00
|
|
|
# Return the number of documents remaining for this cursor.
|
2009-08-20 14:50:48 +00:00
|
|
|
def num_remaining
|
2010-09-08 18:34:28 +00:00
|
|
|
refresh if @cache.length == 0
|
2009-08-20 14:50:48 +00:00
|
|
|
@cache.length
|
|
|
|
end
|
2008-11-22 01:00:51 +00:00
|
|
|
|
2010-09-08 18:34:28 +00:00
|
|
|
def refresh
|
2009-12-16 19:03:15 +00:00
|
|
|
return if send_initial_query || @cursor_id.zero?
|
2010-04-05 14:39:55 +00:00
|
|
|
message = BSON::ByteBuffer.new([0, 0, 0, 0])
|
2009-10-20 19:44:46 +00:00
|
|
|
|
2009-11-04 22:46:15 +00:00
|
|
|
# DB name.
|
2010-07-12 18:31:12 +00:00
|
|
|
BSON::BSON_RUBY.serialize_cstr(message, "#{@db.name}.#{@collection.name}")
|
2009-10-20 19:44:46 +00:00
|
|
|
|
2010-04-12 15:57:14 +00:00
|
|
|
# Number of results to return.
|
2010-09-09 18:53:29 +00:00
|
|
|
if @limit > 0
|
|
|
|
limit = @limit - @returned
|
|
|
|
if @batch_size > 0
|
|
|
|
limit = limit < @batch_size ? limit : @batch_size
|
2010-09-09 18:12:12 +00:00
|
|
|
end
|
2010-09-09 18:53:29 +00:00
|
|
|
message.put_int(limit)
|
2010-09-09 18:12:12 +00:00
|
|
|
else
|
|
|
|
message.put_int(@batch_size)
|
|
|
|
end
|
2009-12-16 19:03:15 +00:00
|
|
|
|
2009-11-04 22:46:15 +00:00
|
|
|
# Cursor id.
|
|
|
|
message.put_long(@cursor_id)
|
2010-09-08 18:27:27 +00:00
|
|
|
@logger.debug("MONGODB cursor.refresh() for cursor #{@cursor_id}") if @logger
|
2010-11-16 20:43:59 +00:00
|
|
|
results, @n_received, @cursor_id = @connection.receive_message(
|
2011-08-29 21:49:58 +00:00
|
|
|
Mongo::Constants::OP_GET_MORE, message, nil, @socket, @command, @read_preference)
|
2010-09-09 18:12:12 +00:00
|
|
|
@returned += @n_received
|
2009-11-04 22:46:15 +00:00
|
|
|
@cache += results
|
2009-10-21 14:11:33 +00:00
|
|
|
close_cursor_if_query_complete
|
2009-08-20 14:50:48 +00:00
|
|
|
end
|
2008-11-22 01:00:51 +00:00
|
|
|
|
2009-11-23 23:09:13 +00:00
|
|
|
# Run query the first time we request an object from the wire
|
2011-02-02 14:51:13 +00:00
|
|
|
# TODO: should we be calling instrument_payload even if logging
|
|
|
|
# is disabled?
|
2009-12-16 19:03:15 +00:00
|
|
|
def send_initial_query
|
2009-08-20 14:50:48 +00:00
|
|
|
if @query_run
|
|
|
|
false
|
|
|
|
else
|
2009-11-02 20:04:06 +00:00
|
|
|
message = construct_query_message
|
2011-01-31 20:51:39 +00:00
|
|
|
@connection.instrument(:find, instrument_payload) do
|
2011-01-29 06:20:41 +00:00
|
|
|
results, @n_received, @cursor_id = @connection.receive_message(
|
2011-08-29 21:49:58 +00:00
|
|
|
Mongo::Constants::OP_QUERY, message, nil, @socket, @command, @read_preference)
|
2011-01-29 06:20:41 +00:00
|
|
|
@returned += @n_received
|
|
|
|
@cache += results
|
|
|
|
@query_run = true
|
|
|
|
close_cursor_if_query_complete
|
|
|
|
end
|
2009-08-20 14:50:48 +00:00
|
|
|
true
|
2008-11-22 01:00:51 +00:00
|
|
|
end
|
|
|
|
end
|
2009-08-20 14:50:48 +00:00
|
|
|
|
2009-11-02 20:04:06 +00:00
|
|
|
def construct_query_message
|
2010-04-05 14:39:55 +00:00
|
|
|
message = BSON::ByteBuffer.new
|
2011-08-05 15:52:45 +00:00
|
|
|
message.put_int(@options)
|
2010-07-12 18:31:12 +00:00
|
|
|
BSON::BSON_RUBY.serialize_cstr(message, "#{@db.name}.#{@collection.name}")
|
2009-10-22 18:10:12 +00:00
|
|
|
message.put_int(@skip)
|
|
|
|
message.put_int(@limit)
|
2010-03-30 16:26:57 +00:00
|
|
|
spec = query_contains_special_fields? ? construct_query_spec : @selector
|
2010-09-11 21:48:15 +00:00
|
|
|
message.put_binary(BSON::BSON_CODER.serialize(spec, false).to_s)
|
|
|
|
message.put_binary(BSON::BSON_CODER.serialize(@fields, false).to_s) if @fields
|
2009-10-20 19:44:46 +00:00
|
|
|
message
|
|
|
|
end
|
|
|
|
|
2011-01-29 06:20:41 +00:00
|
|
|
def instrument_payload
|
|
|
|
log = { :database => @db.name, :collection => @collection.name, :selector => selector }
|
|
|
|
log[:fields] = @fields if @fields
|
2011-08-08 22:31:30 +00:00
|
|
|
log[:skip] = @skip if @skip && (@skip != 0)
|
|
|
|
log[:limit] = @limit if @limit && (@limit != 0)
|
2011-01-29 06:20:41 +00:00
|
|
|
log[:order] = @order if @order
|
|
|
|
log
|
2009-11-02 20:04:06 +00:00
|
|
|
end
|
|
|
|
|
2010-03-30 16:26:57 +00:00
|
|
|
def construct_query_spec
|
|
|
|
return @selector if @selector.has_key?('$query')
|
2010-05-07 01:25:18 +00:00
|
|
|
spec = BSON::OrderedHash.new
|
2010-03-30 16:26:57 +00:00
|
|
|
spec['$query'] = @selector
|
2010-04-06 21:56:21 +00:00
|
|
|
spec['$orderby'] = Mongo::Support.format_order_clause(@order) if @order
|
2010-03-30 16:26:57 +00:00
|
|
|
spec['$hint'] = @hint if @hint && @hint.length > 0
|
|
|
|
spec['$explain'] = true if @explain
|
|
|
|
spec['$snapshot'] = true if @snapshot
|
2011-05-10 19:40:06 +00:00
|
|
|
spec['$maxscan'] = @max_scan if @max_scan
|
|
|
|
spec['$returnKey'] = true if @return_key
|
|
|
|
spec['$showDiskLoc'] = true if @show_disk_loc
|
2010-03-30 16:26:57 +00:00
|
|
|
spec
|
|
|
|
end
|
|
|
|
|
|
|
|
# Returns true if the query contains order, explain, hint, or snapshot.
|
|
|
|
def query_contains_special_fields?
|
|
|
|
@order || @explain || @hint || @snapshot
|
2009-10-20 19:44:46 +00:00
|
|
|
end
|
|
|
|
|
2009-08-20 14:50:48 +00:00
|
|
|
def to_s
|
2009-09-30 14:49:08 +00:00
|
|
|
"DBResponse(flags=#@result_flags, cursor_id=#@cursor_id, start=#@starting_from)"
|
2009-08-20 14:50:48 +00:00
|
|
|
end
|
2009-09-05 18:25:49 +00:00
|
|
|
|
2009-10-21 14:11:33 +00:00
|
|
|
def close_cursor_if_query_complete
|
2010-09-09 18:53:29 +00:00
|
|
|
if @limit > 0 && @returned >= @limit
|
2010-09-09 18:12:12 +00:00
|
|
|
close
|
|
|
|
end
|
2009-10-21 14:11:33 +00:00
|
|
|
end
|
|
|
|
|
2009-09-05 18:25:49 +00:00
|
|
|
def check_modifiable
|
|
|
|
if @query_run || @closed
|
|
|
|
raise InvalidOperation, "Cannot modify the query once it has been run or closed."
|
|
|
|
end
|
|
|
|
end
|
2008-11-22 01:00:51 +00:00
|
|
|
end
|
|
|
|
end
|