mongo 0.19.3 → 0.20

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 (65) hide show
  1. data/README.rdoc +7 -3
  2. data/Rakefile +16 -6
  3. data/bin/bson_benchmark.rb +2 -2
  4. data/examples/gridfs.rb +3 -2
  5. data/lib/mongo.rb +3 -26
  6. data/lib/mongo/collection.rb +69 -50
  7. data/lib/mongo/connection.rb +16 -41
  8. data/lib/mongo/cursor.rb +22 -32
  9. data/lib/mongo/db.rb +13 -5
  10. data/lib/mongo/exceptions.rb +11 -15
  11. data/lib/mongo/gridfs/grid.rb +14 -3
  12. data/lib/mongo/gridfs/grid_file_system.rb +28 -5
  13. data/lib/mongo/gridfs/grid_io.rb +42 -24
  14. data/lib/mongo/util/support.rb +13 -2
  15. data/mongo-ruby-driver.gemspec +3 -1
  16. data/test/collection_test.rb +62 -9
  17. data/test/connection_test.rb +21 -32
  18. data/test/conversions_test.rb +1 -1
  19. data/test/cursor_test.rb +2 -2
  20. data/test/db_api_test.rb +28 -27
  21. data/test/db_connection_test.rb +1 -1
  22. data/test/db_test.rb +23 -13
  23. data/test/grid_file_system_test.rb +30 -4
  24. data/test/grid_io_test.rb +14 -1
  25. data/test/grid_test.rb +59 -3
  26. data/test/test_helper.rb +4 -1
  27. data/test/threading/test_threading_large_pool.rb +1 -1
  28. data/test/threading_test.rb +1 -1
  29. data/test/unit/collection_test.rb +2 -2
  30. data/test/unit/cursor_test.rb +7 -0
  31. data/test/unit/db_test.rb +8 -8
  32. metadata +6 -46
  33. data/bin/gr.rb +0 -14
  34. data/lib/bson.rb +0 -46
  35. data/lib/bson/bson_c.rb +0 -20
  36. data/lib/bson/bson_ruby.rb +0 -601
  37. data/lib/bson/byte_buffer.rb +0 -224
  38. data/lib/bson/exceptions.rb +0 -39
  39. data/lib/bson/ordered_hash.rb +0 -140
  40. data/lib/bson/types/binary.rb +0 -54
  41. data/lib/bson/types/code.rb +0 -36
  42. data/lib/bson/types/dbref.rb +0 -40
  43. data/lib/bson/types/min_max_keys.rb +0 -58
  44. data/lib/bson/types/objectid.rb +0 -180
  45. data/lib/bson/types/regexp_of_holding.rb +0 -45
  46. data/lib/mongo/gridfs.rb +0 -29
  47. data/lib/mongo/gridfs/chunk.rb +0 -91
  48. data/lib/mongo/gridfs/grid_store.rb +0 -580
  49. data/lib/mongo/types/binary.rb +0 -52
  50. data/lib/mongo/types/code.rb +0 -36
  51. data/lib/mongo/types/dbref.rb +0 -40
  52. data/lib/mongo/types/min_max_keys.rb +0 -58
  53. data/lib/mongo/types/objectid.rb +0 -180
  54. data/lib/mongo/types/regexp_of_holding.rb +0 -45
  55. data/lib/mongo/util/bson_c.rb +0 -18
  56. data/lib/mongo/util/bson_ruby.rb +0 -606
  57. data/lib/mongo/util/byte_buffer.rb +0 -222
  58. data/lib/mongo/util/ordered_hash.rb +0 -140
  59. data/test/binary_test.rb +0 -15
  60. data/test/bson_test.rb +0 -459
  61. data/test/byte_buffer_test.rb +0 -81
  62. data/test/chunk_test.rb +0 -82
  63. data/test/grid_store_test.rb +0 -337
  64. data/test/objectid_test.rb +0 -125
  65. data/test/ordered_hash_test.rb +0 -172
@@ -1,36 +0,0 @@
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 BSON
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
@@ -1,40 +0,0 @@
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 BSON
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
@@ -1,58 +0,0 @@
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 BSON
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
@@ -1,180 +0,0 @@
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 BSON
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
@@ -1,45 +0,0 @@
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 BSON
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
@@ -1,29 +0,0 @@
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
- require 'mongo/gridfs/grid_store'
17
-
18
- # DEPRECATED. Plese see GridFileSystem and Grid classes.
19
- #
20
- # GridFS is a specification for storing large binary objects in MongoDB.
21
- # See the documentation for GridFS::GridStore
22
- #
23
- # @see GridFS::GridStore
24
- #
25
- # @core gridfs
26
- #
27
- # @deprecated
28
- module GridFS
29
- end
@@ -1,91 +0,0 @@
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 'mongo/types/objectid'
18
- require 'mongo/util/byte_buffer'
19
- require 'mongo/util/ordered_hash'
20
-
21
- module GridFS
22
-
23
- # A chunk stores a portion of GridStore data.
24
- # @deprecated
25
- class Chunk
26
-
27
- DEFAULT_CHUNK_SIZE = 1024 * 256
28
-
29
- attr_reader :object_id, :chunk_number
30
- attr_accessor :data
31
-
32
- def initialize(file, mongo_object={})
33
- @file = file
34
- @object_id = mongo_object['_id'] || Mongo::ObjectID.new
35
- @chunk_number = mongo_object['n'] || 0
36
-
37
- @data = ByteBuffer.new
38
- case mongo_object['data']
39
- when String
40
- mongo_object['data'].each_byte { |b| @data.put(b) }
41
- when ByteBuffer
42
- @data.put_array(mongo_object['data'].to_a)
43
- when Array
44
- @data.put_array(mongo_object['data'])
45
- when nil
46
- else
47
- raise "illegal chunk format; data is #{mongo_object['data'] ? (' ' + mongo_object['data'].class.name) : 'nil'}"
48
- end
49
- @data.rewind
50
- end
51
-
52
- def pos; @data.position; end
53
- def pos=(pos); @data.position = pos; end
54
- def eof?; !@data.more?; end
55
-
56
- def size; @data.size; end
57
- alias_method :length, :size
58
-
59
- def truncate
60
- if @data.position < @data.length
61
- curr_data = @data
62
- @data = ByteBuffer.new
63
- @data.put_array(curr_data.to_a[0...curr_data.position])
64
- end
65
- end
66
-
67
- def getc
68
- @data.more? ? @data.get : nil
69
- end
70
-
71
- def putc(byte)
72
- @data.put(byte)
73
- end
74
-
75
- def save
76
- coll = @file.chunk_collection
77
- coll.remove({'_id' => @object_id})
78
- coll.insert(to_mongo_object)
79
- end
80
-
81
- def to_mongo_object
82
- h = OrderedHash.new
83
- h['_id'] = @object_id
84
- h['files_id'] = @file.files_id
85
- h['n'] = @chunk_number
86
- h['data'] = data
87
- h
88
- end
89
-
90
- end
91
- end