kbaum-mongo 0.18.3p

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.
Files changed (72) hide show
  1. data/LICENSE.txt +202 -0
  2. data/README.rdoc +339 -0
  3. data/Rakefile +138 -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/collection.rb +609 -0
  17. data/lib/mongo/connection.rb +672 -0
  18. data/lib/mongo/cursor.rb +403 -0
  19. data/lib/mongo/db.rb +555 -0
  20. data/lib/mongo/exceptions.rb +66 -0
  21. data/lib/mongo/gridfs/chunk.rb +91 -0
  22. data/lib/mongo/gridfs/grid.rb +79 -0
  23. data/lib/mongo/gridfs/grid_file_system.rb +101 -0
  24. data/lib/mongo/gridfs/grid_io.rb +338 -0
  25. data/lib/mongo/gridfs/grid_store.rb +580 -0
  26. data/lib/mongo/gridfs.rb +25 -0
  27. data/lib/mongo/types/binary.rb +52 -0
  28. data/lib/mongo/types/code.rb +36 -0
  29. data/lib/mongo/types/dbref.rb +40 -0
  30. data/lib/mongo/types/min_max_keys.rb +58 -0
  31. data/lib/mongo/types/objectid.rb +180 -0
  32. data/lib/mongo/types/regexp_of_holding.rb +45 -0
  33. data/lib/mongo/util/bson_c.rb +18 -0
  34. data/lib/mongo/util/bson_ruby.rb +606 -0
  35. data/lib/mongo/util/byte_buffer.rb +222 -0
  36. data/lib/mongo/util/conversions.rb +87 -0
  37. data/lib/mongo/util/ordered_hash.rb +140 -0
  38. data/lib/mongo/util/server_version.rb +69 -0
  39. data/lib/mongo/util/support.rb +26 -0
  40. data/lib/mongo.rb +63 -0
  41. data/mongo-ruby-driver.gemspec +28 -0
  42. data/test/auxillary/autoreconnect_test.rb +42 -0
  43. data/test/binary_test.rb +15 -0
  44. data/test/bson_test.rb +427 -0
  45. data/test/byte_buffer_test.rb +81 -0
  46. data/test/chunk_test.rb +82 -0
  47. data/test/collection_test.rb +515 -0
  48. data/test/connection_test.rb +160 -0
  49. data/test/conversions_test.rb +120 -0
  50. data/test/cursor_test.rb +379 -0
  51. data/test/db_api_test.rb +780 -0
  52. data/test/db_connection_test.rb +16 -0
  53. data/test/db_test.rb +272 -0
  54. data/test/grid_file_system_test.rb +210 -0
  55. data/test/grid_io_test.rb +78 -0
  56. data/test/grid_store_test.rb +334 -0
  57. data/test/grid_test.rb +87 -0
  58. data/test/objectid_test.rb +125 -0
  59. data/test/ordered_hash_test.rb +172 -0
  60. data/test/replica/count_test.rb +34 -0
  61. data/test/replica/insert_test.rb +50 -0
  62. data/test/replica/pooled_insert_test.rb +54 -0
  63. data/test/replica/query_test.rb +39 -0
  64. data/test/slave_connection_test.rb +36 -0
  65. data/test/test_helper.rb +42 -0
  66. data/test/threading/test_threading_large_pool.rb +90 -0
  67. data/test/threading_test.rb +87 -0
  68. data/test/unit/collection_test.rb +61 -0
  69. data/test/unit/connection_test.rb +117 -0
  70. data/test/unit/cursor_test.rb +93 -0
  71. data/test/unit/db_test.rb +98 -0
  72. metadata +127 -0
@@ -0,0 +1,40 @@
1
+ # --
2
+ # Copyright (C) 2008-2010 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
+ #
29
+ # @core dbrefs constructor_details
30
+ def initialize(namespace, object_id)
31
+ @namespace = namespace
32
+ @object_id = object_id
33
+ end
34
+
35
+ def to_s
36
+ "ns: #{namespace}, id: #{object_id}"
37
+ end
38
+
39
+ end
40
+ end
@@ -0,0 +1,58 @@
1
+ # --
2
+ # Copyright (C) 2008-2010 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,180 @@
1
+ # --
2
+ # Copyright (C) 2008-2010 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
+ # Generates MongoDB object ids.
24
+ #
25
+ # @core objectids
26
+ class ObjectID
27
+ @@lock = Mutex.new
28
+ @@index = 0
29
+
30
+ # Create a new object id. If no parameter is given, an id corresponding
31
+ # to the ObjectID BSON data type will be created. This is a 12-byte value
32
+ # consisting of a 4-byte timestamp, a 3-byte machine id, a 2-byte process id,
33
+ # and a 3-byte counter.
34
+ #
35
+ # @param [Array] data should be an array of bytes. If you want
36
+ # to generate a standard MongoDB object id, leave this argument blank.
37
+ def initialize(data=nil)
38
+ @data = data || generate
39
+ end
40
+
41
+ # Determine if the supplied string is legal. Legal strings will
42
+ # consist of 24 hexadecimal characters.
43
+ #
44
+ # @param [String] str
45
+ #
46
+ # @return [Boolean]
47
+ def self.legal?(str)
48
+ len = 24
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
+ # Get a string representation of this object id.
119
+ #
120
+ # @return [String]
121
+ def to_s
122
+ str = ' ' * 24
123
+ 12.times do |i|
124
+ str[i * 2, 2] = '%02x' % @data[i]
125
+ end
126
+ str
127
+ end
128
+
129
+ def inspect
130
+ "ObjectID('#{to_s}')"
131
+ end
132
+
133
+ # Convert to MongoDB extended JSON format. Since JSON includes type information,
134
+ # but lacks an ObjectID type, this JSON format encodes the type using an $id key.
135
+ #
136
+ # @return [String] the object id represented as MongoDB extended JSON.
137
+ def to_json(escaped=false)
138
+ "{\"$oid\": \"#{to_s}\"}"
139
+ end
140
+
141
+ # Return the UTC time at which this ObjectID was generated. This may
142
+ # be used in lieu of a created_at timestamp since this information
143
+ # is always encoded in the object id.
144
+ #
145
+ # @return [Time] the time at which this object was created.
146
+ def generation_time
147
+ Time.at(@data.pack("C4").unpack("N")[0]).utc
148
+ end
149
+
150
+ private
151
+
152
+ # We need to define this method only if CBson isn't loaded.
153
+ unless defined? CBson
154
+ def generate
155
+ oid = ''
156
+
157
+ # 4 bytes current time
158
+ time = Time.new.to_i
159
+ oid += [time].pack("N")
160
+
161
+ # 3 bytes machine
162
+ oid += Digest::MD5.digest(Socket.gethostname)[0, 3]
163
+
164
+ # 2 bytes pid
165
+ oid += [Process.pid % 0xFFFF].pack("n")
166
+
167
+ # 3 bytes inc
168
+ oid += [get_inc].pack("N")[1, 3]
169
+
170
+ oid.unpack("C12")
171
+ end
172
+ end
173
+
174
+ def get_inc
175
+ @@lock.synchronize do
176
+ @@index = (@@index + 1) % 0xFFFFFF
177
+ end
178
+ end
179
+ end
180
+ end
@@ -0,0 +1,45 @@
1
+ # --
2
+ # Copyright (C) 2008-2010 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
@@ -0,0 +1,18 @@
1
+ # A thin wrapper for the CBson class
2
+ class BSON_C
3
+
4
+ def self.serialize(obj, check_keys=false, move_id=false)
5
+ ByteBuffer.new(CBson.serialize(obj, check_keys, move_id))
6
+ end
7
+
8
+ def self.deserialize(buf=nil)
9
+ if buf.is_a? String
10
+ to_deserialize = ByteBuffer.new(buf) if buf
11
+ else
12
+ buf = ByteBuffer.new(buf.to_a) if buf
13
+ end
14
+ buf.rewind
15
+ CBson.deserialize(buf.to_s)
16
+ end
17
+
18
+ end