mongo 0.1.0 → 0.15
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/README.rdoc +268 -71
- data/Rakefile +27 -62
- data/bin/bson_benchmark.rb +59 -0
- data/bin/mongo_console +3 -3
- data/bin/run_test_script +19 -0
- data/bin/standard_benchmark +109 -0
- data/examples/admin.rb +41 -0
- data/examples/benchmarks.rb +42 -0
- data/examples/blog.rb +76 -0
- data/examples/capped.rb +23 -0
- data/examples/cursor.rb +47 -0
- data/examples/gridfs.rb +87 -0
- data/examples/index_test.rb +125 -0
- data/examples/info.rb +30 -0
- data/examples/queries.rb +69 -0
- data/examples/simple.rb +23 -0
- data/examples/strict.rb +34 -0
- data/examples/types.rb +35 -0
- data/lib/mongo.rb +9 -2
- data/lib/mongo/admin.rb +65 -68
- data/lib/mongo/collection.rb +379 -117
- data/lib/mongo/connection.rb +151 -0
- data/lib/mongo/cursor.rb +271 -216
- data/lib/mongo/db.rb +500 -315
- data/lib/mongo/errors.rb +26 -0
- data/lib/mongo/gridfs.rb +16 -0
- data/lib/mongo/gridfs/chunk.rb +92 -0
- data/lib/mongo/gridfs/grid_store.rb +464 -0
- data/lib/mongo/message.rb +16 -0
- data/lib/mongo/message/get_more_message.rb +24 -13
- data/lib/mongo/message/insert_message.rb +29 -11
- data/lib/mongo/message/kill_cursors_message.rb +23 -12
- data/lib/mongo/message/message.rb +74 -62
- data/lib/mongo/message/message_header.rb +35 -24
- data/lib/mongo/message/msg_message.rb +21 -9
- data/lib/mongo/message/opcodes.rb +26 -15
- data/lib/mongo/message/query_message.rb +63 -43
- data/lib/mongo/message/remove_message.rb +29 -12
- data/lib/mongo/message/update_message.rb +30 -13
- data/lib/mongo/query.rb +97 -89
- data/lib/mongo/types/binary.rb +25 -21
- data/lib/mongo/types/code.rb +30 -0
- data/lib/mongo/types/dbref.rb +19 -23
- data/lib/mongo/types/objectid.rb +130 -116
- data/lib/mongo/types/regexp_of_holding.rb +27 -31
- data/lib/mongo/util/bson.rb +273 -160
- data/lib/mongo/util/byte_buffer.rb +32 -28
- data/lib/mongo/util/ordered_hash.rb +88 -42
- data/lib/mongo/util/xml_to_ruby.rb +18 -15
- data/mongo-ruby-driver.gemspec +103 -0
- data/test/mongo-qa/_common.rb +8 -0
- data/test/mongo-qa/admin +26 -0
- data/test/mongo-qa/capped +22 -0
- data/test/mongo-qa/count1 +18 -0
- data/test/mongo-qa/dbs +22 -0
- data/test/mongo-qa/find +10 -0
- data/test/mongo-qa/find1 +15 -0
- data/test/mongo-qa/gridfs_in +16 -0
- data/test/mongo-qa/gridfs_out +17 -0
- data/test/mongo-qa/indices +49 -0
- data/test/mongo-qa/remove +25 -0
- data/test/mongo-qa/stress1 +35 -0
- data/test/mongo-qa/test1 +11 -0
- data/test/mongo-qa/update +18 -0
- data/{tests → test}/test_admin.rb +25 -16
- data/test/test_bson.rb +268 -0
- data/{tests → test}/test_byte_buffer.rb +0 -0
- data/test/test_chunk.rb +84 -0
- data/test/test_collection.rb +282 -0
- data/test/test_connection.rb +101 -0
- data/test/test_cursor.rb +321 -0
- data/test/test_db.rb +196 -0
- data/test/test_db_api.rb +798 -0
- data/{tests → test}/test_db_connection.rb +4 -3
- data/test/test_grid_store.rb +284 -0
- data/{tests → test}/test_message.rb +1 -1
- data/test/test_objectid.rb +105 -0
- data/{tests → test}/test_ordered_hash.rb +55 -0
- data/{tests → test}/test_round_trip.rb +13 -9
- data/test/test_threading.rb +37 -0
- metadata +74 -32
- data/bin/validate +0 -51
- data/lib/mongo/mongo.rb +0 -74
- data/lib/mongo/types/undefined.rb +0 -31
- data/tests/test_bson.rb +0 -135
- data/tests/test_cursor.rb +0 -66
- data/tests/test_db.rb +0 -51
- data/tests/test_db_api.rb +0 -349
- data/tests/test_objectid.rb +0 -88
@@ -0,0 +1,151 @@
|
|
1
|
+
# --
|
2
|
+
# Copyright (C) 2008-2009 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
|
+
# ++
|
16
|
+
|
17
|
+
require 'mongo/db'
|
18
|
+
|
19
|
+
module Mongo
|
20
|
+
|
21
|
+
# A connection to MongoDB.
|
22
|
+
class Connection
|
23
|
+
|
24
|
+
DEFAULT_PORT = 27017
|
25
|
+
|
26
|
+
# Create a Mongo database server instance. You specify either one or a
|
27
|
+
# pair of servers. If one, you also say if connecting to a slave is
|
28
|
+
# OK. In either case, the host default is "localhost" and port default
|
29
|
+
# is DEFAULT_PORT.
|
30
|
+
#
|
31
|
+
# If you specify a pair, pair_or_host is a hash with two keys :left
|
32
|
+
# and :right. Each key maps to either
|
33
|
+
# * a server name, in which case port is DEFAULT_PORT
|
34
|
+
# * a port number, in which case server is "localhost"
|
35
|
+
# * an array containing a server name and a port number in that order
|
36
|
+
#
|
37
|
+
# +options+ are passed on to each DB instance:
|
38
|
+
#
|
39
|
+
# :slave_ok :: Only used if one host is specified. If false, when
|
40
|
+
# connecting to that host/port a DB object will check to
|
41
|
+
# see if the server is the master. If it is not, an error
|
42
|
+
# is thrown.
|
43
|
+
#
|
44
|
+
# :auto_reconnect :: If a DB connection gets closed (for example, we
|
45
|
+
# have a server pair and saw the "not master"
|
46
|
+
# error, which closes the connection), then
|
47
|
+
# automatically try to reconnect to the master or
|
48
|
+
# to the single server we have been given. Defaults
|
49
|
+
# to +false+.
|
50
|
+
# :logger :: Optional Logger instance to which driver usage information
|
51
|
+
# will be logged.
|
52
|
+
#
|
53
|
+
# Since that's so confusing, here are a few examples:
|
54
|
+
#
|
55
|
+
# Connection.new # localhost, DEFAULT_PORT, !slave
|
56
|
+
# Connection.new("localhost") # localhost, DEFAULT_PORT, !slave
|
57
|
+
# Connection.new("localhost", 3000) # localhost, 3000, slave not ok
|
58
|
+
# # localhost, 3000, slave ok
|
59
|
+
# Connection.new("localhost", 3000, :slave_ok => true)
|
60
|
+
# # localhost, DEFAULT_PORT, auto reconnect
|
61
|
+
# Connection.new(nil, nil, :auto_reconnect => true)
|
62
|
+
#
|
63
|
+
# # A pair of servers. DB will always talk to the master. On socket
|
64
|
+
# # error or "not master" error, we will auto-reconnect to the
|
65
|
+
# # current master.
|
66
|
+
# Connection.new({:left => ["db1.example.com", 3000],
|
67
|
+
# :right => "db2.example.com"}, # DEFAULT_PORT
|
68
|
+
# nil, :auto_reconnect => true)
|
69
|
+
#
|
70
|
+
# # Here, :right is localhost/DEFAULT_PORT. No auto-reconnect.
|
71
|
+
# Connection.new({:left => ["db1.example.com", 3000]})
|
72
|
+
#
|
73
|
+
# When a DB object first connects to a pair, it will find the master
|
74
|
+
# instance and connect to that one.
|
75
|
+
def initialize(pair_or_host=nil, port=nil, options={})
|
76
|
+
@pair = case pair_or_host
|
77
|
+
when String
|
78
|
+
[[pair_or_host, port ? port.to_i : DEFAULT_PORT]]
|
79
|
+
when Hash
|
80
|
+
connections = []
|
81
|
+
connections << pair_val_to_connection(pair_or_host[:left])
|
82
|
+
connections << pair_val_to_connection(pair_or_host[:right])
|
83
|
+
connections
|
84
|
+
when nil
|
85
|
+
[['localhost', DEFAULT_PORT]]
|
86
|
+
end
|
87
|
+
|
88
|
+
@options = options
|
89
|
+
end
|
90
|
+
|
91
|
+
# Return the Mongo::DB named +db_name+. The slave_ok and
|
92
|
+
# auto_reconnect options passed in via #new may be overridden here.
|
93
|
+
# See DB#new for other options you can pass in.
|
94
|
+
def db(db_name, options={})
|
95
|
+
DB.new(db_name, @pair, @options.merge(options))
|
96
|
+
end
|
97
|
+
|
98
|
+
# Returns a hash containing database names as keys and disk space for
|
99
|
+
# each as values.
|
100
|
+
def database_info
|
101
|
+
doc = single_db_command('admin', :listDatabases => 1)
|
102
|
+
h = {}
|
103
|
+
doc['databases'].each { |db|
|
104
|
+
h[db['name']] = db['sizeOnDisk'].to_i
|
105
|
+
}
|
106
|
+
h
|
107
|
+
end
|
108
|
+
|
109
|
+
# Returns an array of database names.
|
110
|
+
def database_names
|
111
|
+
database_info.keys
|
112
|
+
end
|
113
|
+
|
114
|
+
# Drops the database +name+.
|
115
|
+
def drop_database(name)
|
116
|
+
single_db_command(name, :dropDatabase => 1)
|
117
|
+
end
|
118
|
+
|
119
|
+
protected
|
120
|
+
|
121
|
+
# Turns an array containing a host name string and a
|
122
|
+
# port number integer into a [host, port] pair array.
|
123
|
+
def pair_val_to_connection(a)
|
124
|
+
case a
|
125
|
+
when nil
|
126
|
+
['localhost', DEFAULT_PORT]
|
127
|
+
when String
|
128
|
+
[a, DEFAULT_PORT]
|
129
|
+
when Integer
|
130
|
+
['localhost', a]
|
131
|
+
when Array
|
132
|
+
a
|
133
|
+
end
|
134
|
+
end
|
135
|
+
|
136
|
+
# Send cmd (a hash, possibly ordered) to the admin database and return
|
137
|
+
# the answer. Raises an error unless the return is "ok" (DB#ok?
|
138
|
+
# returns +true+).
|
139
|
+
def single_db_command(db_name, cmd)
|
140
|
+
db = nil
|
141
|
+
begin
|
142
|
+
db = db(db_name)
|
143
|
+
doc = db.db_command(cmd)
|
144
|
+
raise "error retrieving database info: #{doc.inspect}" unless db.ok?(doc)
|
145
|
+
doc
|
146
|
+
ensure
|
147
|
+
db.close if db
|
148
|
+
end
|
149
|
+
end
|
150
|
+
end
|
151
|
+
end
|
data/lib/mongo/cursor.rb
CHANGED
@@ -1,229 +1,284 @@
|
|
1
|
-
# --
|
2
1
|
# Copyright (C) 2008-2009 10gen Inc.
|
3
2
|
#
|
4
|
-
#
|
5
|
-
#
|
6
|
-
#
|
3
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
4
|
+
# you may not use this file except in compliance with the License.
|
5
|
+
# You may obtain a copy of the License at
|
7
6
|
#
|
8
|
-
#
|
9
|
-
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
10
|
-
# FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License
|
11
|
-
# for more details.
|
7
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
12
8
|
#
|
13
|
-
#
|
14
|
-
#
|
15
|
-
#
|
9
|
+
# Unless required by applicable law or agreed to in writing, software
|
10
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
11
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
12
|
+
# See the License for the specific language governing permissions and
|
13
|
+
# limitations under the License.
|
16
14
|
|
17
15
|
require 'mongo/message'
|
18
16
|
require 'mongo/util/byte_buffer'
|
19
17
|
require 'mongo/util/bson'
|
20
18
|
|
21
|
-
module
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
|
118
|
-
|
119
|
-
|
120
|
-
|
121
|
-
|
122
|
-
|
123
|
-
|
124
|
-
|
125
|
-
|
126
|
-
|
127
|
-
|
128
|
-
|
129
|
-
|
130
|
-
|
131
|
-
|
132
|
-
|
133
|
-
|
134
|
-
|
135
|
-
|
136
|
-
|
137
|
-
|
138
|
-
|
139
|
-
|
140
|
-
|
141
|
-
|
142
|
-
|
143
|
-
|
144
|
-
|
145
|
-
|
146
|
-
|
147
|
-
|
148
|
-
|
149
|
-
|
150
|
-
|
151
|
-
|
152
|
-
|
153
|
-
|
154
|
-
|
155
|
-
|
156
|
-
|
157
|
-
|
158
|
-
|
159
|
-
|
160
|
-
|
161
|
-
|
162
|
-
|
163
|
-
|
164
|
-
|
165
|
-
|
166
|
-
|
167
|
-
|
168
|
-
|
169
|
-
|
170
|
-
|
171
|
-
|
172
|
-
|
173
|
-
|
174
|
-
|
175
|
-
|
176
|
-
|
177
|
-
|
178
|
-
|
179
|
-
|
180
|
-
|
181
|
-
|
182
|
-
|
183
|
-
|
184
|
-
|
185
|
-
|
186
|
-
|
187
|
-
|
188
|
-
|
189
|
-
|
190
|
-
|
191
|
-
|
192
|
-
|
193
|
-
|
194
|
-
|
195
|
-
|
196
|
-
|
19
|
+
module Mongo
|
20
|
+
|
21
|
+
# A cursor over query results. Returned objects are hashes.
|
22
|
+
class Cursor
|
23
|
+
|
24
|
+
include Enumerable
|
25
|
+
|
26
|
+
RESPONSE_HEADER_SIZE = 20
|
27
|
+
|
28
|
+
attr_reader :db, :collection, :query
|
29
|
+
|
30
|
+
# Create a new cursor.
|
31
|
+
#
|
32
|
+
# Should not be called directly by application developers.
|
33
|
+
def initialize(db, collection, query, admin=false)
|
34
|
+
@db, @collection, @query, @admin = db, collection, query, admin
|
35
|
+
@cache = []
|
36
|
+
@closed = false
|
37
|
+
@query_run = false
|
38
|
+
end
|
39
|
+
|
40
|
+
# Return the next object or nil if there are no more. Raises an error
|
41
|
+
# if necessary.
|
42
|
+
def next_object
|
43
|
+
refill_via_get_more if num_remaining == 0
|
44
|
+
o = @cache.shift
|
45
|
+
|
46
|
+
if o && o['$err']
|
47
|
+
err = o['$err']
|
48
|
+
|
49
|
+
# If the server has stopped being the master (e.g., it's one of a
|
50
|
+
# pair but it has died or something like that) then we close that
|
51
|
+
# connection. If the db has auto connect option and a pair of
|
52
|
+
# servers, next request will re-open on master server.
|
53
|
+
@db.close if err == "not master"
|
54
|
+
|
55
|
+
raise err
|
56
|
+
end
|
57
|
+
|
58
|
+
o
|
59
|
+
end
|
60
|
+
|
61
|
+
# Get the size of the results set for this query.
|
62
|
+
#
|
63
|
+
# Returns the number of objects in the results set for this query. Does
|
64
|
+
# not take limit and skip into account. Raises OperationFailure on a
|
65
|
+
# database error.
|
66
|
+
def count
|
67
|
+
command = OrderedHash["count", @collection.name,
|
68
|
+
"query", @query.selector,
|
69
|
+
"fields", @query.fields()]
|
70
|
+
response = @db.db_command(command)
|
71
|
+
return response['n'].to_i if response['ok'] == 1
|
72
|
+
return 0 if response['errmsg'] == "ns missing"
|
73
|
+
raise OperationFailure, "Count failed: #{response['errmsg']}"
|
74
|
+
end
|
75
|
+
|
76
|
+
# Sort this cursor's result
|
77
|
+
#
|
78
|
+
# Takes either a hash of field names as keys and 1/-1 as values; 1 ==
|
79
|
+
# ascending, -1 == descending, or array of field names (all assumed to be
|
80
|
+
# sorted in ascending order).
|
81
|
+
#
|
82
|
+
# Raises InvalidOperation if this cursor has already been used.
|
83
|
+
#
|
84
|
+
# This method overrides any sort order specified in the Collection#find
|
85
|
+
# method, and only the last sort applied has an effect
|
86
|
+
def sort(order)
|
87
|
+
check_modifiable
|
88
|
+
@query.order_by = order
|
89
|
+
self
|
90
|
+
end
|
91
|
+
|
92
|
+
# Limits the number of results to be returned by this cursor.
|
93
|
+
#
|
94
|
+
# Raises InvalidOperation if this cursor has already been used.
|
95
|
+
#
|
96
|
+
# This method overrides any limit specified in the Collection#find method,
|
97
|
+
# and only the last limit applied has an effect.
|
98
|
+
def limit(number_to_return)
|
99
|
+
check_modifiable
|
100
|
+
raise ArgumentError, "limit requires an integer" unless number_to_return.is_a? Integer
|
101
|
+
|
102
|
+
@query.number_to_return = number_to_return
|
103
|
+
self
|
104
|
+
end
|
105
|
+
|
106
|
+
# Skips the first +number_to_skip+ results of this cursor.
|
107
|
+
#
|
108
|
+
# Raises InvalidOperation if this cursor has already been used.
|
109
|
+
#
|
110
|
+
# This method overrides any skip specified in the Collection#find method,
|
111
|
+
# and only the last skip applied has an effect.
|
112
|
+
def skip(number_to_skip)
|
113
|
+
check_modifiable
|
114
|
+
raise ArgumentError, "skip requires an integer" unless number_to_skip.is_a? Integer
|
115
|
+
|
116
|
+
@query.number_to_skip = number_to_skip
|
117
|
+
self
|
118
|
+
end
|
119
|
+
|
120
|
+
# Iterate over each document in this cursor, yielding it to the given
|
121
|
+
# block.
|
122
|
+
#
|
123
|
+
# Iterating over an entire cursor will close it.
|
124
|
+
def each
|
125
|
+
num_returned = 0
|
126
|
+
while more? && (@query.number_to_return <= 0 || num_returned < @query.number_to_return)
|
127
|
+
yield next_object()
|
128
|
+
num_returned += 1
|
129
|
+
end
|
130
|
+
end
|
131
|
+
|
132
|
+
# Return all of the documents in this cursor as an array of hashes.
|
133
|
+
#
|
134
|
+
# Raises InvalidOperation if this cursor has already been used (including
|
135
|
+
# any previous calls to this method).
|
136
|
+
#
|
137
|
+
# Use of this method is discouraged - iterating over a cursor is much
|
138
|
+
# more efficient in most cases.
|
139
|
+
def to_a
|
140
|
+
raise InvalidOperation, "can't call Cursor#to_a on a used cursor" if @query_run
|
141
|
+
rows = []
|
142
|
+
num_returned = 0
|
143
|
+
while more? && (@query.number_to_return <= 0 || num_returned < @query.number_to_return)
|
144
|
+
rows << next_object()
|
145
|
+
num_returned += 1
|
146
|
+
end
|
147
|
+
rows
|
148
|
+
end
|
149
|
+
|
150
|
+
# Returns an explain plan record for this cursor.
|
151
|
+
def explain
|
152
|
+
limit = @query.number_to_return
|
153
|
+
@query.explain = true
|
154
|
+
@query.number_to_return = -limit.abs
|
155
|
+
|
156
|
+
c = Cursor.new(@db, @collection, @query)
|
157
|
+
explanation = c.next_object
|
158
|
+
c.close
|
159
|
+
|
160
|
+
@query.explain = false
|
161
|
+
@query.number_to_return = limit
|
162
|
+
explanation
|
163
|
+
end
|
164
|
+
|
165
|
+
# Close the cursor.
|
166
|
+
#
|
167
|
+
# Note: if a cursor is read until exhausted (read until OP_QUERY or
|
168
|
+
# OP_GETMORE returns zero for the cursor id), there is no need to
|
169
|
+
# close it by calling this method.
|
170
|
+
#
|
171
|
+
# Collection#find takes an optional block argument which can be used to
|
172
|
+
# ensure that your cursors get closed. See the documentation for
|
173
|
+
# Collection#find for details.
|
174
|
+
def close
|
175
|
+
@db.send_to_db(KillCursorsMessage.new(@cursor_id)) if @cursor_id
|
176
|
+
@cursor_id = 0
|
177
|
+
@closed = true
|
178
|
+
end
|
179
|
+
|
180
|
+
# Returns true if this cursor is closed, false otherwise.
|
181
|
+
def closed?; @closed; end
|
182
|
+
|
183
|
+
private
|
184
|
+
|
185
|
+
def read_all
|
186
|
+
read_message_header
|
187
|
+
read_response_header
|
188
|
+
read_objects_off_wire
|
189
|
+
end
|
190
|
+
|
191
|
+
def read_objects_off_wire
|
192
|
+
while doc = next_object_on_wire
|
193
|
+
@cache << doc
|
194
|
+
end
|
195
|
+
end
|
196
|
+
|
197
|
+
def read_message_header
|
198
|
+
MessageHeader.new.read_header(@db)
|
199
|
+
end
|
200
|
+
|
201
|
+
def read_response_header
|
202
|
+
header_buf = ByteBuffer.new
|
203
|
+
header_buf.put_array(@db.receive_full(RESPONSE_HEADER_SIZE).unpack("C*"))
|
204
|
+
raise "Short read for DB response header; expected #{RESPONSE_HEADER_SIZE} bytes, saw #{header_buf.length}" unless header_buf.length == RESPONSE_HEADER_SIZE
|
205
|
+
header_buf.rewind
|
206
|
+
@result_flags = header_buf.get_int
|
207
|
+
@cursor_id = header_buf.get_long
|
208
|
+
@starting_from = header_buf.get_int
|
209
|
+
@n_remaining = header_buf.get_int
|
210
|
+
if @n_received
|
211
|
+
@n_received += @n_remaining
|
212
|
+
else
|
213
|
+
@n_received = @n_remaining
|
214
|
+
end
|
215
|
+
if @query.number_to_return > 0 and @n_received >= @query.number_to_return
|
216
|
+
close()
|
217
|
+
end
|
218
|
+
end
|
219
|
+
|
220
|
+
def num_remaining
|
221
|
+
refill_via_get_more if @cache.length == 0
|
222
|
+
@cache.length
|
223
|
+
end
|
224
|
+
|
225
|
+
# Internal method, not for general use. Return +true+ if there are
|
226
|
+
# more records to retrieve. We do not check @query.number_to_return;
|
227
|
+
# #each is responsible for doing that.
|
228
|
+
def more?
|
229
|
+
num_remaining > 0
|
230
|
+
end
|
231
|
+
|
232
|
+
def next_object_on_wire
|
233
|
+
# if @n_remaining is 0 but we have a non-zero cursor, there are more
|
234
|
+
# to fetch, so do a GetMore operation, but don't do it here - do it
|
235
|
+
# when someone pulls an object out of the cache and it's empty
|
236
|
+
return nil if @n_remaining == 0
|
237
|
+
object_from_stream
|
238
|
+
end
|
239
|
+
|
240
|
+
def refill_via_get_more
|
241
|
+
if send_query_if_needed or @cursor_id == 0
|
242
|
+
return
|
243
|
+
end
|
244
|
+
@db._synchronize {
|
245
|
+
@db.send_to_db(GetMoreMessage.new(@admin ? 'admin' : @db.name, @collection.name, @cursor_id))
|
246
|
+
read_all
|
247
|
+
}
|
248
|
+
end
|
249
|
+
|
250
|
+
def object_from_stream
|
251
|
+
buf = ByteBuffer.new
|
252
|
+
buf.put_array(@db.receive_full(4).unpack("C*"))
|
253
|
+
buf.rewind
|
254
|
+
size = buf.get_int
|
255
|
+
buf.put_array(@db.receive_full(size - 4).unpack("C*"), 4)
|
256
|
+
@n_remaining -= 1
|
257
|
+
buf.rewind
|
258
|
+
BSON.new.deserialize(buf)
|
259
|
+
end
|
260
|
+
|
261
|
+
def send_query_if_needed
|
262
|
+
# Run query first time we request an object from the wire
|
263
|
+
if @query_run
|
264
|
+
false
|
265
|
+
else
|
266
|
+
@db._synchronize {
|
267
|
+
@db.send_query_message(QueryMessage.new(@admin ? 'admin' : @db.name, @collection.name, @query))
|
268
|
+
@query_run = true
|
197
269
|
read_all
|
198
|
-
|
199
|
-
|
200
|
-
|
201
|
-
|
202
|
-
|
203
|
-
|
204
|
-
|
205
|
-
|
206
|
-
|
207
|
-
|
208
|
-
|
209
|
-
|
210
|
-
|
211
|
-
def send_query_if_needed
|
212
|
-
# Run query first time we request an object from the wire
|
213
|
-
unless @query_run
|
214
|
-
hints = @hint_fields || @collection.hint_fields
|
215
|
-
old_hints = @query.hint_fields
|
216
|
-
@query.hint_fields = hints
|
217
|
-
@db.send_query_message(QueryMessage.new(@db.name, @collection.name, @query))
|
218
|
-
@query_run = true
|
219
|
-
@query.hint_fields = old_hints
|
220
|
-
read_all
|
221
|
-
end
|
222
|
-
end
|
223
|
-
|
224
|
-
def to_s
|
225
|
-
"DBResponse(flags=#@result_flags, cursor_id=#@cursor_id, start=#@starting_from, n_returned=#@n_returned)"
|
226
|
-
end
|
270
|
+
}
|
271
|
+
true
|
272
|
+
end
|
273
|
+
end
|
274
|
+
|
275
|
+
def to_s
|
276
|
+
"DBResponse(flags=#@result_flags, cursor_id=#@cursor_id, start=#@starting_from)"
|
277
|
+
end
|
278
|
+
|
279
|
+
def check_modifiable
|
280
|
+
if @query_run || @closed
|
281
|
+
raise InvalidOperation, "Cannot modify the query once it has been run or closed."
|
227
282
|
end
|
228
283
|
end
|
229
284
|
end
|