2009-01-06 15:51:01 +00:00
|
|
|
# Copyright (C) 2008-2009 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
|
|
|
|
|
|
|
require 'mongo/message'
|
|
|
|
require 'mongo/util/byte_buffer'
|
|
|
|
require 'mongo/util/bson'
|
|
|
|
|
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
|
2008-11-22 01:00:51 +00:00
|
|
|
|
2009-08-20 14:50:48 +00:00
|
|
|
include Enumerable
|
2008-11-22 01:00:51 +00:00
|
|
|
|
2009-08-20 14:50:48 +00:00
|
|
|
RESPONSE_HEADER_SIZE = 20
|
2008-11-22 01:00:51 +00:00
|
|
|
|
2009-08-20 14:50:48 +00:00
|
|
|
attr_reader :db, :collection, :query
|
2009-01-13 19:02:16 +00:00
|
|
|
|
2009-08-21 15:21:33 +00:00
|
|
|
# Create a new cursor.
|
|
|
|
#
|
|
|
|
# Should not be called directly by application developers.
|
2009-08-20 14:50:48 +00:00
|
|
|
def initialize(db, collection, query, admin=false)
|
|
|
|
@db, @collection, @query, @admin = db, collection, query, admin
|
|
|
|
@cache = []
|
|
|
|
@closed = false
|
|
|
|
@query_run = false
|
|
|
|
end
|
2009-01-13 19:02:16 +00:00
|
|
|
|
2009-08-20 14:50:48 +00:00
|
|
|
# Return the next object or nil if there are no more. Raises an error
|
|
|
|
# if necessary.
|
|
|
|
def next_object
|
|
|
|
refill_via_get_more if num_remaining == 0
|
|
|
|
o = @cache.shift
|
2009-01-23 16:47:22 +00:00
|
|
|
|
2009-08-20 14:50:48 +00:00
|
|
|
if o && o['$err']
|
|
|
|
err = o['$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
|
|
|
|
# connection. If the db has auto connect option and a pair of
|
|
|
|
# servers, next request will re-open on master server.
|
|
|
|
@db.close if err == "not master"
|
2009-01-23 16:47:22 +00:00
|
|
|
|
2009-08-20 14:50:48 +00:00
|
|
|
raise err
|
|
|
|
end
|
2008-11-22 01:00:51 +00:00
|
|
|
|
2009-08-20 14:50:48 +00:00
|
|
|
o
|
|
|
|
end
|
2009-08-18 15:26:58 +00:00
|
|
|
|
2009-08-20 14:50:48 +00:00
|
|
|
# Get the size of the results set for this query.
|
|
|
|
#
|
|
|
|
# Returns the number of objects in the results set for this query. Does
|
|
|
|
# not take limit and skip into account. Raises OperationFailure on a
|
|
|
|
# database error.
|
|
|
|
def count
|
|
|
|
command = OrderedHash["count", @collection.name,
|
2009-08-24 21:21:49 +00:00
|
|
|
"query", @query.selector,
|
|
|
|
"fields", @query.fields()]
|
2009-08-20 14:50:48 +00:00
|
|
|
response = @db.db_command(command)
|
|
|
|
return response['n'].to_i if response['ok'] == 1
|
|
|
|
return 0 if response['errmsg'] == "ns missing"
|
|
|
|
raise OperationFailure, "Count failed: #{response['errmsg']}"
|
|
|
|
end
|
2009-09-17 18:54:30 +00:00
|
|
|
|
|
|
|
# Sort this cursor's result
|
|
|
|
#
|
|
|
|
# Takes either a hash of field names as keys and 1/-1 as values; 1 ==
|
|
|
|
# ascending, -1 == descending, or array of field names (all assumed to be
|
|
|
|
# sorted in ascending order).
|
|
|
|
#
|
|
|
|
# Raises InvalidOperation if this cursor has already been used.
|
2009-09-04 23:04:11 +00:00
|
|
|
#
|
2009-09-17 18:54:30 +00:00
|
|
|
# This method overrides any sort order specified in the Collection#find
|
|
|
|
# method, and only the last sort applied has an effect
|
2009-09-05 19:08:02 +00:00
|
|
|
def sort(order)
|
2009-09-17 19:07:18 +00:00
|
|
|
check_modifiable
|
2009-09-05 19:08:02 +00:00
|
|
|
@query.order_by = order
|
2009-09-04 23:04:11 +00:00
|
|
|
self
|
|
|
|
end
|
2009-01-07 19:07:17 +00:00
|
|
|
|
2009-09-16 21:52:41 +00:00
|
|
|
# Limits the number of results to be returned by this cursor.
|
2009-09-05 18:25:49 +00:00
|
|
|
#
|
2009-09-17 18:54:30 +00:00
|
|
|
# Raises InvalidOperation if this cursor has already been used.
|
|
|
|
#
|
|
|
|
# This method overrides any limit specified in the Collection#find method,
|
|
|
|
# and only the last limit applied has an effect.
|
2009-09-05 18:25:49 +00:00
|
|
|
def limit(number_to_return)
|
|
|
|
check_modifiable
|
|
|
|
raise ArgumentError, "limit requires an integer" unless number_to_return.is_a? Integer
|
|
|
|
|
|
|
|
@query.number_to_return = 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-09-05 18:25:49 +00:00
|
|
|
#
|
2009-09-17 18:54:30 +00:00
|
|
|
# Raises InvalidOperation if this cursor has already been used.
|
|
|
|
#
|
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.
|
2009-09-16 21:52:41 +00:00
|
|
|
def skip(number_to_skip)
|
2009-09-05 18:25:49 +00:00
|
|
|
check_modifiable
|
2009-09-16 21:52:41 +00:00
|
|
|
raise ArgumentError, "skip requires an integer" unless number_to_skip.is_a? Integer
|
2009-09-05 18:25:49 +00:00
|
|
|
|
|
|
|
@query.number_to_skip = number_to_skip
|
2009-09-17 19:07:18 +00:00
|
|
|
self
|
2009-09-05 18:25:49 +00:00
|
|
|
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.
|
2009-08-20 14:50:48 +00:00
|
|
|
def each
|
2009-08-21 18:11:59 +00:00
|
|
|
num_returned = 0
|
2009-09-16 21:52:41 +00:00
|
|
|
while more? && (@query.number_to_return <= 0 || num_returned < @query.number_to_return)
|
2009-08-21 18:11:59 +00:00
|
|
|
yield next_object()
|
|
|
|
num_returned += 1
|
2009-08-20 14:50:48 +00:00
|
|
|
end
|
|
|
|
end
|
2008-11-22 01:00:51 +00:00
|
|
|
|
2009-08-21 18:11:59 +00:00
|
|
|
# Return all of the documents in this cursor as an array of hashes.
|
2009-08-20 14:50:48 +00:00
|
|
|
#
|
2009-08-21 18:11:59 +00:00
|
|
|
# Raises InvalidOperation if this cursor has already been used (including
|
|
|
|
# any previous calls to this method).
|
2009-08-20 14:50:48 +00:00
|
|
|
#
|
2009-08-21 18:11:59 +00:00
|
|
|
# Use of this method is discouraged - iterating over a cursor is much
|
|
|
|
# more efficient in most cases.
|
2009-08-20 14:50:48 +00:00
|
|
|
def to_a
|
2009-08-21 18:11:59 +00:00
|
|
|
raise InvalidOperation, "can't call Cursor#to_a on a used cursor" if @query_run
|
|
|
|
rows = []
|
2009-08-20 14:50:48 +00:00
|
|
|
num_returned = 0
|
2009-09-16 21:52:41 +00:00
|
|
|
while more? && (@query.number_to_return <= 0 || num_returned < @query.number_to_return)
|
2009-08-21 18:11:59 +00:00
|
|
|
rows << next_object()
|
2009-08-20 14:50:48 +00:00
|
|
|
num_returned += 1
|
|
|
|
end
|
2009-08-21 18:11:59 +00:00
|
|
|
rows
|
2009-08-20 14:50:48 +00:00
|
|
|
end
|
2009-01-14 15:42:56 +00:00
|
|
|
|
2009-08-21 15:21:33 +00:00
|
|
|
# Returns an explain plan record for this cursor.
|
2009-08-20 14:50:48 +00:00
|
|
|
def explain
|
2009-09-16 14:39:52 +00:00
|
|
|
limit = @query.number_to_return
|
2009-08-20 14:50:48 +00:00
|
|
|
@query.explain = true
|
2009-09-16 14:39:52 +00:00
|
|
|
@query.number_to_return = -limit.abs
|
2009-01-14 15:42:56 +00:00
|
|
|
|
2009-08-20 14:50:48 +00:00
|
|
|
c = Cursor.new(@db, @collection, @query)
|
|
|
|
explanation = c.next_object
|
|
|
|
c.close
|
2009-01-13 19:02:16 +00:00
|
|
|
|
2009-09-16 14:39:52 +00:00
|
|
|
@query.explain = false
|
|
|
|
@query.number_to_return = limit
|
2009-08-20 14:50:48 +00:00
|
|
|
explanation
|
|
|
|
end
|
2008-11-22 01:00:51 +00:00
|
|
|
|
2009-08-20 14:50:48 +00:00
|
|
|
# Close the cursor.
|
|
|
|
#
|
|
|
|
# Note: if a cursor is read until exhausted (read until OP_QUERY or
|
|
|
|
# OP_GETMORE returns zero for the cursor id), there is no need to
|
|
|
|
# close it by calling this method.
|
2009-08-21 15:21:33 +00:00
|
|
|
#
|
|
|
|
# Collection#find takes an optional block argument which can be used to
|
|
|
|
# ensure that your cursors get closed. See the documentation for
|
|
|
|
# Collection#find for details.
|
2009-08-20 14:50:48 +00:00
|
|
|
def close
|
|
|
|
@db.send_to_db(KillCursorsMessage.new(@cursor_id)) if @cursor_id
|
|
|
|
@cursor_id = 0
|
|
|
|
@closed = true
|
|
|
|
end
|
2008-11-22 01:00:51 +00:00
|
|
|
|
2009-08-21 15:21:33 +00:00
|
|
|
# Returns true if this cursor is closed, false otherwise.
|
|
|
|
def closed?; @closed; end
|
|
|
|
|
2009-08-21 15:03:56 +00:00
|
|
|
private
|
2008-11-22 01:00:51 +00:00
|
|
|
|
2009-08-20 14:50:48 +00:00
|
|
|
def read_all
|
|
|
|
read_message_header
|
|
|
|
read_response_header
|
|
|
|
read_objects_off_wire
|
|
|
|
end
|
2008-11-22 01:00:51 +00:00
|
|
|
|
2009-08-20 14:50:48 +00:00
|
|
|
def read_objects_off_wire
|
|
|
|
while doc = next_object_on_wire
|
|
|
|
@cache << doc
|
|
|
|
end
|
|
|
|
end
|
2008-11-22 01:00:51 +00:00
|
|
|
|
2009-08-20 14:50:48 +00:00
|
|
|
def read_message_header
|
|
|
|
MessageHeader.new.read_header(@db)
|
|
|
|
end
|
2008-11-22 01:00:51 +00:00
|
|
|
|
2009-08-20 14:50:48 +00:00
|
|
|
def read_response_header
|
|
|
|
header_buf = ByteBuffer.new
|
|
|
|
header_buf.put_array(@db.receive_full(RESPONSE_HEADER_SIZE).unpack("C*"))
|
|
|
|
raise "Short read for DB response header; expected #{RESPONSE_HEADER_SIZE} bytes, saw #{header_buf.length}" unless header_buf.length == RESPONSE_HEADER_SIZE
|
|
|
|
header_buf.rewind
|
|
|
|
@result_flags = header_buf.get_int
|
|
|
|
@cursor_id = header_buf.get_long
|
|
|
|
@starting_from = header_buf.get_int
|
2009-09-30 14:49:08 +00:00
|
|
|
@n_remaining = header_buf.get_int
|
|
|
|
if @n_received
|
|
|
|
@n_received += @n_remaining
|
|
|
|
else
|
|
|
|
@n_received = @n_remaining
|
|
|
|
end
|
|
|
|
if @query.number_to_return > 0 and @n_received >= @query.number_to_return
|
|
|
|
close()
|
|
|
|
end
|
2009-08-20 14:50:48 +00:00
|
|
|
end
|
2008-11-22 01:00:51 +00:00
|
|
|
|
2009-08-20 14:50:48 +00:00
|
|
|
def num_remaining
|
|
|
|
refill_via_get_more if @cache.length == 0
|
|
|
|
@cache.length
|
|
|
|
end
|
2008-11-22 01:00:51 +00:00
|
|
|
|
2009-08-21 15:03:56 +00:00
|
|
|
# Internal method, not for general use. Return +true+ if there are
|
2009-09-16 21:52:41 +00:00
|
|
|
# more records to retrieve. We do not check @query.number_to_return;
|
|
|
|
# #each is responsible for doing that.
|
2009-08-21 15:03:56 +00:00
|
|
|
def more?
|
|
|
|
num_remaining > 0
|
|
|
|
end
|
2008-11-22 01:00:51 +00:00
|
|
|
|
2009-08-20 14:50:48 +00:00
|
|
|
def next_object_on_wire
|
|
|
|
# if @n_remaining is 0 but we have a non-zero cursor, there are more
|
|
|
|
# to fetch, so do a GetMore operation, but don't do it here - do it
|
|
|
|
# when someone pulls an object out of the cache and it's empty
|
|
|
|
return nil if @n_remaining == 0
|
|
|
|
object_from_stream
|
|
|
|
end
|
2008-11-22 01:00:51 +00:00
|
|
|
|
2009-08-20 14:50:48 +00:00
|
|
|
def refill_via_get_more
|
|
|
|
if send_query_if_needed or @cursor_id == 0
|
|
|
|
return
|
|
|
|
end
|
|
|
|
@db._synchronize {
|
|
|
|
@db.send_to_db(GetMoreMessage.new(@admin ? 'admin' : @db.name, @collection.name, @cursor_id))
|
|
|
|
read_all
|
|
|
|
}
|
|
|
|
end
|
2008-11-22 01:00:51 +00:00
|
|
|
|
2009-08-20 14:50:48 +00:00
|
|
|
def object_from_stream
|
|
|
|
buf = ByteBuffer.new
|
|
|
|
buf.put_array(@db.receive_full(4).unpack("C*"))
|
|
|
|
buf.rewind
|
|
|
|
size = buf.get_int
|
|
|
|
buf.put_array(@db.receive_full(size - 4).unpack("C*"), 4)
|
|
|
|
@n_remaining -= 1
|
|
|
|
buf.rewind
|
|
|
|
BSON.new.deserialize(buf)
|
|
|
|
end
|
2009-01-13 19:02:16 +00:00
|
|
|
|
2009-08-20 14:50:48 +00:00
|
|
|
def send_query_if_needed
|
|
|
|
# Run query first time we request an object from the wire
|
|
|
|
if @query_run
|
|
|
|
false
|
|
|
|
else
|
|
|
|
@db._synchronize {
|
|
|
|
@db.send_query_message(QueryMessage.new(@admin ? 'admin' : @db.name, @collection.name, @query))
|
|
|
|
@query_run = true
|
|
|
|
read_all
|
|
|
|
}
|
|
|
|
true
|
2008-11-22 01:00:51 +00:00
|
|
|
end
|
|
|
|
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
|
|
|
|
|
|
|
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
|