2008-12-17 16:49:06 +00:00
|
|
|
# --
|
2009-01-06 15:51:01 +00:00
|
|
|
# Copyright (C) 2008-2009 10gen Inc.
|
2008-11-22 01:00:51 +00:00
|
|
|
#
|
|
|
|
# This program is free software: you can redistribute it and/or modify it
|
|
|
|
# under the terms of the GNU Affero General Public License, version 3, as
|
|
|
|
# published by the Free Software Foundation.
|
|
|
|
#
|
|
|
|
# This program is distributed in the hope that it will be useful, but WITHOUT
|
|
|
|
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
|
|
# FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License
|
|
|
|
# for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU Affero General Public License
|
|
|
|
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
2008-12-17 16:49:06 +00:00
|
|
|
# ++
|
2008-11-22 01:00:51 +00:00
|
|
|
|
|
|
|
require 'mongo/message'
|
|
|
|
require 'mongo/util/byte_buffer'
|
|
|
|
require 'mongo/util/bson'
|
|
|
|
|
|
|
|
module XGen
|
|
|
|
module Mongo
|
|
|
|
module Driver
|
|
|
|
|
2008-12-17 16:43:08 +00:00
|
|
|
# A cursor over query results. Returned objects are hashes.
|
2008-11-22 01:00:51 +00:00
|
|
|
class Cursor
|
|
|
|
|
|
|
|
include Enumerable
|
|
|
|
|
|
|
|
RESPONSE_HEADER_SIZE = 20
|
|
|
|
|
2009-01-13 20:51:41 +00:00
|
|
|
attr_reader :db, :collection, :query
|
2009-01-13 19:02:16 +00:00
|
|
|
|
2009-01-13 20:51:41 +00:00
|
|
|
def initialize(db, collection, query)
|
|
|
|
@db, @collection, @query = db, collection, query
|
|
|
|
@num_to_return = @query.number_to_return || 0
|
2009-01-07 19:07:17 +00:00
|
|
|
@cache = []
|
2008-11-22 01:00:51 +00:00
|
|
|
@closed = false
|
2009-01-07 19:22:53 +00:00
|
|
|
@can_call_to_a = true
|
2009-01-13 19:02:16 +00:00
|
|
|
@query_run = false
|
2008-11-22 01:00:51 +00:00
|
|
|
end
|
|
|
|
|
2009-01-13 19:02:16 +00:00
|
|
|
def closed?; @closed; end
|
|
|
|
|
2009-01-13 20:51:41 +00:00
|
|
|
# Set hint fields to use and return +self+. hint_fields may be a
|
|
|
|
# single field name or array of field names. May be +nil+. If no hint
|
|
|
|
# fields are specified, the ones in the collection are used if they
|
|
|
|
# exist.
|
|
|
|
def hint(hint_fields)
|
|
|
|
@hint_fields = hint_fields
|
|
|
|
self
|
|
|
|
end
|
|
|
|
|
2009-01-07 14:46:30 +00:00
|
|
|
# Return +true+ if there are more records to retrieve. We do not check
|
|
|
|
# @num_to_return; #each is responsible for doing that.
|
2008-11-22 01:00:51 +00:00
|
|
|
def more?
|
|
|
|
num_remaining > 0
|
|
|
|
end
|
|
|
|
|
2008-12-17 16:43:08 +00:00
|
|
|
# Return the next object. Raises an error if necessary.
|
2008-11-22 01:00:51 +00:00
|
|
|
def next_object
|
|
|
|
refill_via_get_more if num_remaining == 0
|
2009-01-07 19:07:17 +00:00
|
|
|
o = @cache.shift
|
2008-12-09 19:47:34 +00:00
|
|
|
raise o['$err'] if o['$err']
|
|
|
|
o
|
2008-11-22 01:00:51 +00:00
|
|
|
end
|
|
|
|
|
2009-01-07 14:46:30 +00:00
|
|
|
# Iterate over each object, yielding it to the given block. At most
|
|
|
|
# @num_to_return records are returned (or all of them, if
|
|
|
|
# @num_to_return is 0).
|
2009-01-07 19:07:17 +00:00
|
|
|
#
|
|
|
|
# If #to_a has already been called then this method uses the array
|
|
|
|
# that we store internally. In that case, #each can be called multiple
|
|
|
|
# times because it re-uses that array.
|
2009-01-07 19:22:53 +00:00
|
|
|
#
|
|
|
|
# You can call #each after calling #to_a (multiple times even, because
|
|
|
|
# it will use the internally-stored array), but you can't call #to_a
|
|
|
|
# after calling #each unless you also called it before calling #each.
|
|
|
|
# If you try to do that, an error will be raised.
|
2008-11-22 01:00:51 +00:00
|
|
|
def each
|
2009-01-07 19:07:17 +00:00
|
|
|
if @rows # Already turned into an array
|
|
|
|
@rows.each { |row| yield row }
|
|
|
|
else
|
|
|
|
num_returned = 0
|
|
|
|
while more? && (@num_to_return <= 0 || num_returned < @num_to_return)
|
|
|
|
yield next_object()
|
|
|
|
num_returned += 1
|
|
|
|
end
|
2009-01-07 19:22:53 +00:00
|
|
|
@can_call_to_a = false
|
2009-01-07 19:07:17 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2009-01-07 19:22:53 +00:00
|
|
|
# Return all of the rows (up to the +num_to_return+ value specified in
|
|
|
|
# #new) as an array. Calling this multiple times will work fine; it
|
|
|
|
# always returns the same array.
|
|
|
|
#
|
|
|
|
# Don't use this if you're expecting large amounts of data, of course.
|
|
|
|
# All of the returned rows are kept in an array stored in this object
|
|
|
|
# so it can be reused.
|
2009-01-07 19:07:17 +00:00
|
|
|
#
|
|
|
|
# You can call #each after calling #to_a (multiple times even, because
|
|
|
|
# it will use the internally-stored array), but you can't call #to_a
|
2009-01-07 19:22:53 +00:00
|
|
|
# after calling #each unless you also called it before calling #each.
|
|
|
|
# If you try to do that, an error will be raised.
|
2009-01-07 19:07:17 +00:00
|
|
|
def to_a
|
|
|
|
return @rows if @rows
|
2009-01-07 19:22:53 +00:00
|
|
|
raise "can't call Cursor#to_a after calling Cursor#each" unless @can_call_to_a
|
2009-01-07 19:07:17 +00:00
|
|
|
@rows = []
|
2009-01-07 14:46:30 +00:00
|
|
|
num_returned = 0
|
|
|
|
while more? && (@num_to_return <= 0 || num_returned < @num_to_return)
|
2009-01-07 19:07:17 +00:00
|
|
|
@rows << next_object()
|
2009-01-07 14:46:30 +00:00
|
|
|
num_returned += 1
|
2008-11-22 01:00:51 +00:00
|
|
|
end
|
2009-01-07 19:07:17 +00:00
|
|
|
@rows
|
2008-11-22 01:00:51 +00:00
|
|
|
end
|
|
|
|
|
2009-01-13 19:02:16 +00:00
|
|
|
# Returns an explain plan record.
|
|
|
|
def explain
|
|
|
|
sel = OrderedHash.new
|
2009-01-13 20:51:41 +00:00
|
|
|
sel['query'] = @query.selector
|
2009-01-13 19:02:16 +00:00
|
|
|
sel['$explain'] = true
|
2009-01-13 20:51:41 +00:00
|
|
|
c = Cursor.new(@db, @collection, Query.new(sel))
|
2009-01-13 19:02:16 +00:00
|
|
|
e = c.next_object
|
|
|
|
c.close
|
|
|
|
e
|
|
|
|
end
|
|
|
|
|
2008-12-17 16:43:08 +00:00
|
|
|
# Close the cursor.
|
2008-11-22 01:00:51 +00:00
|
|
|
def close
|
2009-01-13 19:02:16 +00:00
|
|
|
@db.send_to_db(KillCursorsMessage.new(@cursor_id)) if @cursor_id
|
2009-01-07 19:07:17 +00:00
|
|
|
@cache = []
|
2008-11-22 01:00:51 +00:00
|
|
|
@cursor_id = 0
|
|
|
|
@closed = true
|
|
|
|
end
|
|
|
|
|
|
|
|
protected
|
|
|
|
|
|
|
|
def read_all
|
|
|
|
read_message_header
|
|
|
|
read_response_header
|
|
|
|
read_objects_off_wire
|
|
|
|
end
|
|
|
|
|
|
|
|
def read_objects_off_wire
|
|
|
|
while doc = next_object_on_wire
|
2009-01-07 19:07:17 +00:00
|
|
|
@cache << doc
|
2008-11-22 01:00:51 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def read_message_header
|
|
|
|
MessageHeader.new.read_header(@db.socket)
|
|
|
|
end
|
|
|
|
|
|
|
|
def read_response_header
|
|
|
|
header_buf = ByteBuffer.new
|
|
|
|
header_buf.put_array(@db.socket.recv(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
|
|
|
|
@n_returned = header_buf.get_int
|
|
|
|
@n_remaining = @n_returned
|
|
|
|
end
|
|
|
|
|
|
|
|
def num_remaining
|
2009-01-07 19:07:17 +00:00
|
|
|
refill_via_get_more if @cache.length == 0
|
|
|
|
@cache.length
|
2008-11-22 01:00:51 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def next_object_on_wire
|
2009-01-13 19:02:16 +00:00
|
|
|
send_query_if_needed
|
2008-11-22 01:00:51 +00:00
|
|
|
# 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
|
|
|
|
|
|
|
|
def refill_via_get_more
|
2009-01-13 19:02:16 +00:00
|
|
|
send_query_if_needed
|
2008-11-22 01:00:51 +00:00
|
|
|
return if @cursor_id == 0
|
2009-01-13 20:38:46 +00:00
|
|
|
@db.send_to_db(GetMoreMessage.new(@db.name, @collection.name, @cursor_id))
|
2008-11-22 01:00:51 +00:00
|
|
|
read_all
|
|
|
|
end
|
|
|
|
|
|
|
|
def object_from_stream
|
|
|
|
buf = ByteBuffer.new
|
|
|
|
buf.put_array(@db.socket.recv(4).unpack("C*"))
|
|
|
|
buf.rewind
|
|
|
|
size = buf.get_int
|
|
|
|
buf.put_array(@db.socket.recv(size-4).unpack("C*"), 4)
|
|
|
|
@n_remaining -= 1
|
|
|
|
buf.rewind
|
2009-01-09 20:22:10 +00:00
|
|
|
BSON.new(@db).deserialize(buf)
|
2008-11-22 01:00:51 +00:00
|
|
|
end
|
|
|
|
|
2009-01-13 19:02:16 +00:00
|
|
|
def send_query_if_needed
|
|
|
|
# Run query first time we request an object from the wire
|
|
|
|
unless @query_run
|
2009-01-13 20:51:41 +00:00
|
|
|
hints = @hint_fields || @collection.hint_fields
|
|
|
|
hints = [hints] if hints.kind_of?(String)
|
|
|
|
query = if hints
|
|
|
|
h = {}
|
|
|
|
hints.each { |field| h[field] = 1 }
|
|
|
|
sel = OrderedHash.new
|
|
|
|
sel['query'] = @query.selector
|
|
|
|
sel['$hint'] = h
|
|
|
|
Query.new(sel)
|
|
|
|
else
|
|
|
|
@query
|
|
|
|
end
|
|
|
|
@db.send_query_message(QueryMessage.new(@db.name, @collection.name, query))
|
2009-01-13 19:02:16 +00:00
|
|
|
@query_run = true
|
|
|
|
read_all
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2008-11-22 01:00:51 +00:00
|
|
|
def to_s
|
|
|
|
"DBResponse(flags=#@result_flags, cursor_id=#@cursor_id, start=#@starting_from, n_returned=#@n_returned)"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|