mongo-find_replace 0.18.3

Sign up to get free protection for your applications and to get access to all the features.
Files changed (68) hide show
  1. data/LICENSE.txt +202 -0
  2. data/README.rdoc +358 -0
  3. data/Rakefile +133 -0
  4. data/bin/bson_benchmark.rb +59 -0
  5. data/bin/fail_if_no_c.rb +11 -0
  6. data/examples/admin.rb +42 -0
  7. data/examples/capped.rb +22 -0
  8. data/examples/cursor.rb +48 -0
  9. data/examples/gridfs.rb +88 -0
  10. data/examples/index_test.rb +126 -0
  11. data/examples/info.rb +31 -0
  12. data/examples/queries.rb +70 -0
  13. data/examples/simple.rb +24 -0
  14. data/examples/strict.rb +35 -0
  15. data/examples/types.rb +36 -0
  16. data/lib/mongo.rb +61 -0
  17. data/lib/mongo/admin.rb +95 -0
  18. data/lib/mongo/collection.rb +664 -0
  19. data/lib/mongo/connection.rb +555 -0
  20. data/lib/mongo/cursor.rb +393 -0
  21. data/lib/mongo/db.rb +527 -0
  22. data/lib/mongo/exceptions.rb +60 -0
  23. data/lib/mongo/gridfs.rb +22 -0
  24. data/lib/mongo/gridfs/chunk.rb +90 -0
  25. data/lib/mongo/gridfs/grid_store.rb +555 -0
  26. data/lib/mongo/types/binary.rb +48 -0
  27. data/lib/mongo/types/code.rb +36 -0
  28. data/lib/mongo/types/dbref.rb +38 -0
  29. data/lib/mongo/types/min_max_keys.rb +58 -0
  30. data/lib/mongo/types/objectid.rb +219 -0
  31. data/lib/mongo/types/regexp_of_holding.rb +45 -0
  32. data/lib/mongo/util/bson_c.rb +18 -0
  33. data/lib/mongo/util/bson_ruby.rb +595 -0
  34. data/lib/mongo/util/byte_buffer.rb +222 -0
  35. data/lib/mongo/util/conversions.rb +97 -0
  36. data/lib/mongo/util/ordered_hash.rb +135 -0
  37. data/lib/mongo/util/server_version.rb +69 -0
  38. data/lib/mongo/util/support.rb +26 -0
  39. data/lib/mongo/util/xml_to_ruby.rb +112 -0
  40. data/mongo-ruby-driver.gemspec +28 -0
  41. data/test/replica/count_test.rb +34 -0
  42. data/test/replica/insert_test.rb +50 -0
  43. data/test/replica/pooled_insert_test.rb +54 -0
  44. data/test/replica/query_test.rb +39 -0
  45. data/test/test_admin.rb +67 -0
  46. data/test/test_bson.rb +397 -0
  47. data/test/test_byte_buffer.rb +81 -0
  48. data/test/test_chunk.rb +82 -0
  49. data/test/test_collection.rb +534 -0
  50. data/test/test_connection.rb +160 -0
  51. data/test/test_conversions.rb +120 -0
  52. data/test/test_cursor.rb +386 -0
  53. data/test/test_db.rb +254 -0
  54. data/test/test_db_api.rb +783 -0
  55. data/test/test_db_connection.rb +16 -0
  56. data/test/test_grid_store.rb +306 -0
  57. data/test/test_helper.rb +42 -0
  58. data/test/test_objectid.rb +156 -0
  59. data/test/test_ordered_hash.rb +168 -0
  60. data/test/test_round_trip.rb +114 -0
  61. data/test/test_slave_connection.rb +36 -0
  62. data/test/test_threading.rb +87 -0
  63. data/test/threading/test_threading_large_pool.rb +90 -0
  64. data/test/unit/collection_test.rb +52 -0
  65. data/test/unit/connection_test.rb +59 -0
  66. data/test/unit/cursor_test.rb +94 -0
  67. data/test/unit/db_test.rb +97 -0
  68. metadata +123 -0
@@ -0,0 +1,48 @@
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/util/byte_buffer'
18
+
19
+ module Mongo
20
+
21
+ # An array of binary bytes with a MongoDB subtype. See the subtype
22
+ # constants for reference.
23
+ #
24
+ # Use this class when storing binary data in documents.
25
+ class Binary < ByteBuffer
26
+
27
+ SUBTYPE_BYTES = 0x02
28
+ SUBTYPE_UUID = 0x03
29
+ SUBTYPE_MD5 = 0x05
30
+ SUBTYPE_USER_DEFINED = 0x80
31
+
32
+ # One of the SUBTYPE_* constants. Default is SUBTYPE_BYTES.
33
+ attr_accessor :subtype
34
+
35
+ # Create a buffer for storing binary data in MongoDB.
36
+ #
37
+ # @param [Array] initia_data
38
+ # @param [Fixnum] one of four values specifying a BSON binary subtype. Possible values are
39
+ # SUBTYPE_BYTES, SUBTYPE_UUID, SUBTYPE_MD5, and SUBTYPE_USER_DEFINED.
40
+ #
41
+ # @see http://www.mongodb.org/display/DOCS/BSON#BSON-noteondatabinary BSON binary subtypes.
42
+ def initialize(initial_data=[], subtype=SUBTYPE_BYTES)
43
+ super(initial_data)
44
+ @subtype = subtype
45
+ end
46
+
47
+ end
48
+ end
@@ -0,0 +1,36 @@
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
+ module Mongo
18
+
19
+ # JavaScript code to be evaluated by MongoDB.
20
+ class Code < String
21
+
22
+ # Hash mapping identifiers to their values
23
+ attr_accessor :scope
24
+
25
+ # Wrap code to be evaluated by MongoDB.
26
+ #
27
+ # @param [String] code the JavaScript code.
28
+ # @param [Hash] a document mapping identifiers to values, which
29
+ # represent the scope in which the code is to be executed.
30
+ def initialize(code, scope={})
31
+ super(code)
32
+ @scope = scope
33
+ end
34
+
35
+ end
36
+ end
@@ -0,0 +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
+
17
+ module Mongo
18
+
19
+ # A reference to another object in a MongoDB database.
20
+ class DBRef
21
+
22
+ attr_reader :namespace, :object_id
23
+
24
+ # Create a DBRef. Use this class in conjunction with DB#dereference.
25
+ #
26
+ # @param [String] a collection name
27
+ # @param [ObjectID] an object id
28
+ def initialize(namespace, object_id)
29
+ @namespace = namespace
30
+ @object_id = object_id
31
+ end
32
+
33
+ def to_s
34
+ "ns: #{namespace}, id: #{object_id}"
35
+ end
36
+
37
+ end
38
+ end
@@ -0,0 +1,58 @@
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
+ module Mongo
18
+
19
+ # A class representing the BSON MaxKey type. MaxKey will always compare greater than
20
+ # all other BSON types and values.
21
+ #
22
+ # @example Sorting (assume @numbers is a collection):
23
+ #
24
+ # >> @numbers.save({"n" => Mongo::MaxKey.new})
25
+ # >> @numbers.save({"n" => 0})
26
+ # >> @numbers.save({"n" => 5_000_000})
27
+ # >> @numbers.find.sort("n").to_a
28
+ # => [{"_id"=>4b5a050c238d3bace2000004, "n"=>0},
29
+ # {"_id"=>4b5a04e6238d3bace2000002, "n"=>5_000_000},
30
+ # {"_id"=>4b5a04ea238d3bace2000003, "n"=>#<Mongo::MaxKey:0x1014ef410>},
31
+ # ]
32
+ class MaxKey
33
+
34
+ def ==(obj)
35
+ obj.class == MaxKey
36
+ end
37
+ end
38
+
39
+ # A class representing the BSON MinKey type. MinKey will always compare less than
40
+ # all other BSON types and values.
41
+ #
42
+ # @example Sorting (assume @numbers is a collection):
43
+ #
44
+ # >> @numbers.save({"n" => Mongo::MinKey.new})
45
+ # >> @numbers.save({"n" => -1_000_000})
46
+ # >> @numbers.save({"n" => 1_000_000})
47
+ # >> @numbers.find.sort("n").to_a
48
+ # => [{"_id"=>4b5a050c238d3bace2000004, "n"=>#<Mongo::MinKey:0x1014ef410>},
49
+ # {"_id"=>4b5a04e6238d3bace2000002, "n"=>-1_000_000},
50
+ # {"_id"=>4b5a04ea238d3bace2000003, "n"=>1_000_000},
51
+ # ]
52
+ class MinKey
53
+
54
+ def ==(obj)
55
+ obj.class == MinKey
56
+ end
57
+ end
58
+ end
@@ -0,0 +1,219 @@
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 'thread'
18
+ require 'socket'
19
+ require 'digest/md5'
20
+
21
+ module Mongo
22
+
23
+ # ObjectID class for documents in MongoDB.
24
+ class ObjectID
25
+ # This is the legacy byte ordering for Babble. Versions of the Ruby
26
+ # driver prior to 0.14 used this byte ordering when converting ObjectID
27
+ # instances to and from strings. If you have string representations of
28
+ # ObjectIDs using the legacy byte ordering make sure to use the
29
+ # to_s_legacy and from_string_legacy methods, or convert your strings
30
+ # with ObjectID#legacy_string_convert
31
+ BYTE_ORDER = [7, 6, 5, 4, 3, 2, 1, 0, 11, 10, 9, 8]
32
+
33
+ @@lock = Mutex.new
34
+ @@index = 0
35
+
36
+ # Create a new object id. If no parameter is given, an id corresponding
37
+ # to the ObjectID BSON data type will be created. This is a 12-byte value
38
+ # consisting of a 4-byte timestamp, a 3-byte machine id, a 2-byte process id,
39
+ # and a 3-byte counter.
40
+ #
41
+ # @param [Array] data should be an array of bytes. If you want
42
+ # to generate a standard MongoDB object id, leave this argument blank.
43
+ def initialize(data=nil)
44
+ @data = data || generate
45
+ end
46
+
47
+ def self.legal?(str)
48
+ len = BYTE_ORDER.length * 2
49
+ str =~ /([0-9a-f]+)/i
50
+ match = $1
51
+ str && str.length == len && match == str
52
+ end
53
+
54
+ # Create an object id from the given time. This is useful for doing range
55
+ # queries; it works because MongoDB's object ids begin
56
+ # with a timestamp.
57
+ #
58
+ # @param [Time] time a utc time to encode as an object id.
59
+ #
60
+ # @return [Mongo::ObjectID]
61
+ #
62
+ # @example Return all document created before Jan 1, 2010.
63
+ # time = Time.utc(2010, 1, 1)
64
+ # time_id = ObjectID.from_time(time)
65
+ # collection.find({'_id' => {'$lt' => time_id}})
66
+ def self.from_time(time)
67
+ self.new([time.to_i,0,0].pack("NNN").unpack("C12"))
68
+ end
69
+
70
+ # Adds a primary key to the given document if needed.
71
+ #
72
+ # @param [Hash] doc a document requiring an _id.
73
+ #
74
+ # @return [Mongo::ObjectID, Object] returns a newly-created or
75
+ # current _id for the given document.
76
+ def self.create_pk(doc)
77
+ doc.has_key?(:_id) || doc.has_key?('_id') ? doc : doc.merge!(:_id => self.new)
78
+ end
79
+
80
+ # Check equality of this object id with another.
81
+ #
82
+ # @param [Mongo::ObjectID] object_id
83
+ def eql?(object_id)
84
+ @data == object_id.instance_variable_get("@data")
85
+ end
86
+ alias_method :==, :eql?
87
+
88
+ # Get a unique hashcode for this object.
89
+ # This is required since we've defined an #eql? method.
90
+ #
91
+ # @return [Integer]
92
+ def hash
93
+ @data.hash
94
+ end
95
+
96
+ # Get an array representation of the object id.
97
+ #
98
+ # @return [Array]
99
+ def to_a
100
+ @data.dup
101
+ end
102
+
103
+ # Given a string representation of an ObjectID, return a new ObjectID
104
+ # with that value.
105
+ #
106
+ # @param [String] str
107
+ #
108
+ # @return [Mongo::ObjectID]
109
+ def self.from_string(str)
110
+ raise InvalidObjectID, "illegal ObjectID format" unless legal?(str)
111
+ data = []
112
+ 12.times do |i|
113
+ data[i] = str[i * 2, 2].to_i(16)
114
+ end
115
+ self.new(data)
116
+ end
117
+
118
+ # @deprecated
119
+ # Create a new ObjectID given a string representation of an ObjectID
120
+ # using the legacy byte ordering. This method may eventually be
121
+ # removed. If you are not sure that you need this method you should be
122
+ # using the regular from_string.
123
+ def self.from_string_legacy(str)
124
+ warn "Support for legacy object ids has been DEPRECATED."
125
+ raise InvalidObjectID, "illegal ObjectID format" unless legal?(str)
126
+ data = []
127
+ BYTE_ORDER.each_with_index { |string_position, data_index|
128
+ data[data_index] = str[string_position * 2, 2].to_i(16)
129
+ }
130
+ self.new(data)
131
+ end
132
+
133
+ # Get a string representation of this object id.
134
+ #
135
+ # @return [String]
136
+ def to_s
137
+ str = ' ' * 24
138
+ 12.times do |i|
139
+ str[i * 2, 2] = '%02x' % @data[i]
140
+ end
141
+ str
142
+ end
143
+ alias_method :inspect, :to_s
144
+
145
+ # Convert to MongoDB extended JSON format. Since JSON includes type information,
146
+ # but lacks an ObjectID type, this JSON format encodes the type using an $id key.
147
+ #
148
+ # @return [String] the object id represented as MongoDB extended JSON.
149
+ def to_json(escaped=false)
150
+ "{\"$oid\": \"#{to_s}\"}"
151
+ end
152
+
153
+ # @deprecated
154
+ # Get a string representation of this ObjectID using the legacy byte
155
+ # ordering. This method may eventually be removed. If you are not sure
156
+ # that you need this method you should be using the regular to_s.
157
+ def to_s_legacy
158
+ warn "Support for legacy object ids has been DEPRECATED."
159
+ str = ' ' * 24
160
+ BYTE_ORDER.each_with_index { |string_position, data_index|
161
+ str[string_position * 2, 2] = '%02x' % @data[data_index]
162
+ }
163
+ str
164
+ end
165
+
166
+ # @deprecated
167
+ # Convert a string representation of an ObjectID using the legacy byte
168
+ # ordering to the proper byte ordering. This method may eventually be
169
+ # removed. If you are not sure that you need this method it is probably
170
+ # unnecessary.
171
+ def self.legacy_string_convert(str)
172
+ warn "Support for legacy object ids has been DEPRECATED."
173
+ legacy = ' ' * 24
174
+ BYTE_ORDER.each_with_index do |legacy_pos, pos|
175
+ legacy[legacy_pos * 2, 2] = str[pos * 2, 2]
176
+ end
177
+ legacy
178
+ end
179
+
180
+ # Return the UTC time at which this ObjectID was generated. This may
181
+ # be used in lieu of a created_at timestamp since this information
182
+ # is always encoded in the object id.
183
+ #
184
+ # @return [Time] the time at which this object was created.
185
+ def generation_time
186
+ Time.at(@data.pack("C4").unpack("N")[0]).utc
187
+ end
188
+
189
+ private
190
+
191
+ # We need to define this method only if CBson isn't loaded.
192
+ unless defined? CBson
193
+ def generate
194
+ oid = ''
195
+
196
+ # 4 bytes current time
197
+ time = Time.new.to_i
198
+ oid += [time].pack("N")
199
+
200
+ # 3 bytes machine
201
+ oid += Digest::MD5.digest(Socket.gethostname)[0, 3]
202
+
203
+ # 2 bytes pid
204
+ oid += [Process.pid % 0xFFFF].pack("n")
205
+
206
+ # 3 bytes inc
207
+ oid += [get_inc].pack("N")[1, 3]
208
+
209
+ oid.unpack("C12")
210
+ end
211
+ end
212
+
213
+ def get_inc
214
+ @@lock.synchronize do
215
+ @@index = (@@index + 1) % 0xFFFFFF
216
+ end
217
+ end
218
+ end
219
+ end
@@ -0,0 +1,45 @@
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
+ module Mongo
18
+
19
+ # A Regexp that can hold on to extra options and ignore them. Mongo
20
+ # regexes may contain option characters beyond 'i', 'm', and 'x'. (Note
21
+ # that Mongo only uses those three, but that regexes coming from other
22
+ # languages may store different option characters.)
23
+ #
24
+ # Note that you do not have to use this class at all if you wish to
25
+ # store regular expressions in Mongo. The Mongo and Ruby regex option
26
+ # flags are the same. Storing regexes is discouraged, in any case.
27
+ #
28
+ # @deprecated
29
+ class RegexpOfHolding < Regexp
30
+
31
+ attr_accessor :extra_options_str
32
+
33
+ # @deprecated we're no longer supporting this.
34
+ # +str+ and +options+ are the same as Regexp. +extra_options_str+
35
+ # contains all the other flags that were in Mongo but we do not use or
36
+ # understand.
37
+ def initialize(str, options, extra_options_str)
38
+ warn "RegexpOfHolding is deprecated; the modifiers i, m, and x will be stored automatically as BSON." +
39
+ "If you're only storing the options i, m, and x, you can safely ignore this message."
40
+ super(str, options)
41
+ @extra_options_str = extra_options_str
42
+ end
43
+ end
44
+
45
+ end