mongo 0.1.0 → 0.15
Sign up to get free protection for your applications and to get access to all the features.
- 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
@@ -1,58 +1,78 @@
|
|
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
|
+
|
1
17
|
require 'mongo/message/message'
|
2
18
|
require 'mongo/message/opcodes'
|
3
19
|
require 'mongo/util/ordered_hash'
|
4
20
|
|
5
|
-
module
|
6
|
-
module Mongo
|
7
|
-
module Driver
|
21
|
+
module Mongo
|
8
22
|
|
9
|
-
|
23
|
+
class QueryMessage < Message
|
10
24
|
|
11
|
-
|
25
|
+
attr_reader :query
|
12
26
|
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
27
|
+
def initialize(db_name, collection_name, query)
|
28
|
+
super(OP_QUERY)
|
29
|
+
@query = query
|
30
|
+
@collection_name = collection_name
|
31
|
+
write_int(0)
|
32
|
+
write_string("#{db_name}.#{collection_name}")
|
33
|
+
write_int(query.number_to_skip)
|
34
|
+
write_int(query.number_to_return)
|
35
|
+
sel = query.selector
|
36
|
+
if query.contains_special_fields
|
37
|
+
sel = OrderedHash.new
|
38
|
+
sel['query'] = query.selector
|
39
|
+
if query.order_by && query.order_by.length > 0
|
40
|
+
sel['orderby'] = case query.order_by
|
41
|
+
when String
|
42
|
+
{query.order_by => 1}
|
43
|
+
when Array
|
44
|
+
h = OrderedHash.new
|
45
|
+
query.order_by.each { |ob|
|
46
|
+
case ob
|
26
47
|
when String
|
27
|
-
|
28
|
-
when
|
29
|
-
h =
|
30
|
-
query.order_by.each { |ob| h[ob] = 1 }
|
31
|
-
h
|
32
|
-
when Hash # Should be an ordered hash, but this message doesn't care
|
33
|
-
query.order_by
|
48
|
+
h[ob] = 1
|
49
|
+
when Hash # should have one entry; will handle all
|
50
|
+
ob.each { |k,v| h[k] = v }
|
34
51
|
else
|
35
|
-
raise "illegal order_by
|
52
|
+
raise "illegal query order_by value #{query.order_by.inspect}"
|
36
53
|
end
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
sel['$explain'] = true
|
45
|
-
end
|
46
|
-
|
47
|
-
end
|
48
|
-
write_doc(sel)
|
49
|
-
write_doc(query.fields) if query.fields
|
50
|
-
end
|
51
|
-
|
52
|
-
def first_key(key)
|
53
|
-
@first_key = key
|
54
|
+
}
|
55
|
+
h
|
56
|
+
when Hash # Should be an ordered hash, but this message doesn't care
|
57
|
+
query.order_by
|
58
|
+
else
|
59
|
+
raise "illegal order_by: is a #{query.order_by.class.name}, must be String, Array, Hash, or OrderedHash"
|
60
|
+
end
|
54
61
|
end
|
62
|
+
sel['$hint'] = query.hint if query.hint && query.hint.length > 0
|
63
|
+
sel['$explain'] = true if query.explain
|
64
|
+
sel['$snapshot'] = true if query.snapshot
|
55
65
|
end
|
66
|
+
write_doc(sel)
|
67
|
+
write_doc(query.fields) if query.fields
|
68
|
+
end
|
69
|
+
|
70
|
+
def first_key(key)
|
71
|
+
@first_key = key
|
72
|
+
end
|
73
|
+
|
74
|
+
def to_s
|
75
|
+
"db.#{@collection_name}.#{@query}"
|
56
76
|
end
|
57
77
|
end
|
58
78
|
end
|
@@ -1,20 +1,37 @@
|
|
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
|
+
|
1
17
|
require 'mongo/message/message'
|
2
18
|
require 'mongo/message/opcodes'
|
3
19
|
|
4
|
-
module
|
5
|
-
|
6
|
-
|
20
|
+
module Mongo
|
21
|
+
|
22
|
+
class RemoveMessage < Message
|
7
23
|
|
8
|
-
|
24
|
+
def initialize(db_name, collection_name, sel)
|
25
|
+
@collection_name = collection_name
|
26
|
+
super(OP_DELETE)
|
27
|
+
write_int(0)
|
28
|
+
write_string("#{db_name}.#{collection_name}")
|
29
|
+
write_int(0) # flags?
|
30
|
+
write_doc(sel)
|
31
|
+
end
|
9
32
|
|
10
|
-
|
11
|
-
|
12
|
-
write_int(0)
|
13
|
-
write_string("#{db_name}.#{collection_name}")
|
14
|
-
write_int(0) # flags?
|
15
|
-
write_doc(sel)
|
16
|
-
end
|
17
|
-
end
|
33
|
+
def to_s
|
34
|
+
"#{@collection_name}.clear()"
|
18
35
|
end
|
19
36
|
end
|
20
37
|
end
|
@@ -1,21 +1,38 @@
|
|
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
|
+
|
1
17
|
require 'mongo/message/message'
|
2
18
|
require 'mongo/message/opcodes'
|
3
19
|
|
4
|
-
module
|
5
|
-
|
6
|
-
|
20
|
+
module Mongo
|
21
|
+
|
22
|
+
class UpdateMessage < Message
|
7
23
|
|
8
|
-
|
24
|
+
def initialize(db_name, collection_name, sel, obj, repsert)
|
25
|
+
@collection_name = collection_name
|
26
|
+
super(OP_UPDATE)
|
27
|
+
write_int(0)
|
28
|
+
write_string("#{db_name}.#{collection_name}")
|
29
|
+
write_int(repsert ? 1 : 0) # 1 if a repsert operation (upsert)
|
30
|
+
write_doc(sel)
|
31
|
+
write_doc(obj)
|
32
|
+
end
|
9
33
|
|
10
|
-
|
11
|
-
|
12
|
-
write_int(0)
|
13
|
-
write_string("#{db_name}.#{collection_name}")
|
14
|
-
write_int(repsert ? 1 : 0) # 1 if a repsert operation (upsert)
|
15
|
-
write_doc(sel)
|
16
|
-
write_doc(obj)
|
17
|
-
end
|
18
|
-
end
|
34
|
+
def to_s
|
35
|
+
"db.#{@collection_name}.update(#{@sel.inspect}, #{@obj.inspect})"
|
19
36
|
end
|
20
37
|
end
|
21
38
|
end
|
data/lib/mongo/query.rb
CHANGED
@@ -1,110 +1,118 @@
|
|
1
1
|
# --
|
2
2
|
# Copyright (C) 2008-2009 10gen Inc.
|
3
3
|
#
|
4
|
-
#
|
5
|
-
#
|
6
|
-
#
|
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
7
|
#
|
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.
|
8
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
12
9
|
#
|
13
|
-
#
|
14
|
-
#
|
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
15
|
# ++
|
16
16
|
|
17
17
|
require 'mongo/collection'
|
18
18
|
require 'mongo/message'
|
19
|
+
require 'mongo/types/code'
|
19
20
|
|
20
|
-
module
|
21
|
-
module Mongo
|
22
|
-
module Driver
|
21
|
+
module Mongo
|
23
22
|
|
24
|
-
|
25
|
-
|
26
|
-
|
23
|
+
# A query against a collection. A query's selector is a hash. See the
|
24
|
+
# Mongo documentation for query details.
|
25
|
+
class Query
|
27
26
|
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
27
|
+
attr_accessor :number_to_skip, :number_to_return, :order_by, :snapshot
|
28
|
+
# If true, $explain will be set in QueryMessage that uses this query.
|
29
|
+
attr_accessor :explain
|
30
|
+
# Either +nil+ or a hash (preferably an OrderedHash).
|
31
|
+
attr_accessor :hint
|
32
|
+
attr_reader :selector # writer defined below
|
34
33
|
|
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
|
-
when nil
|
72
|
-
{}
|
73
|
-
when String
|
74
|
-
{"$where" => sel}
|
75
|
-
when Hash
|
76
|
-
sel
|
77
|
-
end
|
78
|
-
end
|
34
|
+
# sel :: A hash describing the query. See the Mongo docs for details.
|
35
|
+
#
|
36
|
+
# return_fields :: If not +nil+, a single field name or an array of
|
37
|
+
# field names. Only those fields will be returned.
|
38
|
+
# (Called :fields in calls to Collection#find.)
|
39
|
+
#
|
40
|
+
# number_to_skip :: Number of records to skip before returning
|
41
|
+
# records. Default is 0.
|
42
|
+
#
|
43
|
+
# number_to_return :: Max number of records to return. (Called :limit
|
44
|
+
# in calls to Collection#find.) Default is 0 (all
|
45
|
+
# records).
|
46
|
+
#
|
47
|
+
# order_by :: If not +nil+, specifies record sort order. May be a
|
48
|
+
# String, Hash, OrderedHash, or Array. If a string, the
|
49
|
+
# results will be ordered by that field in ascending
|
50
|
+
# order. If an array, it should be an array of field names
|
51
|
+
# which will all be sorted in ascending order. If a hash,
|
52
|
+
# it may be either a regular Hash or an OrderedHash. The
|
53
|
+
# keys should be field names, and the values should be 1
|
54
|
+
# (ascending) or -1 (descending). Note that if it is a
|
55
|
+
# regular Hash then sorting by more than one field
|
56
|
+
# probably will not be what you intend because key order
|
57
|
+
# is not preserved. (order_by is called :sort in calls to
|
58
|
+
# Collection#find.)
|
59
|
+
#
|
60
|
+
# hint :: If not +nil+, specifies query hint fields. Must be either
|
61
|
+
# +nil+ or a hash (preferably an OrderedHash). See
|
62
|
+
# Collection#hint.
|
63
|
+
def initialize(sel={}, return_fields=nil, number_to_skip=0, number_to_return=0, order_by=nil, hint=nil, snapshot=nil)
|
64
|
+
@number_to_skip, @number_to_return, @order_by, @hint, @snapshot =
|
65
|
+
number_to_skip, number_to_return, order_by, hint, snapshot
|
66
|
+
@explain = nil
|
67
|
+
self.selector = sel
|
68
|
+
self.fields = return_fields
|
69
|
+
end
|
79
70
|
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
71
|
+
# Set query selector hash. If sel is Code/string, it will be used as a
|
72
|
+
# $where clause. (See Mongo docs for details.)
|
73
|
+
def selector=(sel)
|
74
|
+
@selector = case sel
|
75
|
+
when nil
|
76
|
+
{}
|
77
|
+
when Code
|
78
|
+
{"$where" => sel}
|
79
|
+
when String
|
80
|
+
{"$where" => Code.new(sel)}
|
81
|
+
when Hash
|
82
|
+
sel
|
83
|
+
end
|
84
|
+
end
|
86
85
|
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
nil
|
94
|
-
else
|
95
|
-
h = {}
|
96
|
-
@fields.each { |field| h[field] = 1 }
|
97
|
-
h
|
98
|
-
end
|
99
|
-
else # nil, anything else
|
100
|
-
nil
|
101
|
-
end
|
102
|
-
end
|
86
|
+
# Set fields to return. If +val+ is +nil+ or empty, all fields will be
|
87
|
+
# returned.
|
88
|
+
def fields=(val)
|
89
|
+
@fields = val
|
90
|
+
@fields = nil if @fields && @fields.empty?
|
91
|
+
end
|
103
92
|
|
104
|
-
|
105
|
-
|
93
|
+
def fields
|
94
|
+
case @fields
|
95
|
+
when String
|
96
|
+
{@fields => 1}
|
97
|
+
when Array
|
98
|
+
if @fields.length == 0
|
99
|
+
nil
|
100
|
+
else
|
101
|
+
h = {}
|
102
|
+
@fields.each { |field| h[field] = 1 }
|
103
|
+
h
|
106
104
|
end
|
105
|
+
else # nil, anything else
|
106
|
+
nil
|
107
107
|
end
|
108
108
|
end
|
109
|
+
|
110
|
+
def contains_special_fields
|
111
|
+
(@order_by != nil && @order_by.length > 0) || @explain || @hint || @snapshot
|
112
|
+
end
|
113
|
+
|
114
|
+
def to_s
|
115
|
+
"find(#{@selector.inspect})" + (@order_by ? ".sort(#{@order_by.inspect})" : "")
|
116
|
+
end
|
109
117
|
end
|
110
118
|
end
|
data/lib/mongo/types/binary.rb
CHANGED
@@ -1,34 +1,38 @@
|
|
1
1
|
# --
|
2
2
|
# Copyright (C) 2008-2009 10gen Inc.
|
3
3
|
#
|
4
|
-
#
|
5
|
-
#
|
6
|
-
#
|
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
7
|
#
|
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.
|
8
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
12
9
|
#
|
13
|
-
#
|
14
|
-
#
|
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
15
|
# ++
|
16
16
|
|
17
|
-
|
18
|
-
module Mongo
|
19
|
-
module Driver
|
17
|
+
require 'mongo/util/byte_buffer'
|
20
18
|
|
21
|
-
|
22
|
-
# BSON encoder will know to output the Mongo BINARY type.
|
23
|
-
class Binary < String; end
|
19
|
+
module Mongo
|
24
20
|
|
21
|
+
# An array of binary bytes with a Mongo subtype value.
|
22
|
+
class Binary < ByteBuffer
|
23
|
+
|
24
|
+
SUBTYPE_BYTES = 0x02
|
25
|
+
SUBTYPE_UUID = 0x03
|
26
|
+
SUBTYPE_MD5 = 0x05
|
27
|
+
SUBTYPE_USER_DEFINED = 0x80
|
28
|
+
|
29
|
+
# One of the SUBTYPE_* constants. Default is SUBTYPE_BYTES.
|
30
|
+
attr_accessor :subtype
|
31
|
+
|
32
|
+
def initialize(initial_data=[], subtype=SUBTYPE_BYTES)
|
33
|
+
super(initial_data)
|
34
|
+
@subtype = subtype
|
25
35
|
end
|
26
|
-
end
|
27
|
-
end
|
28
36
|
|
29
|
-
class String
|
30
|
-
# Convert a string into a XGen::Mongo::Driver::Binary
|
31
|
-
def to_mongo_binary
|
32
|
-
XGen::Mongo::Driver::Binary.new(self)
|
33
37
|
end
|
34
38
|
end
|