mongo 1.8.0 → 1.8.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (80) hide show
  1. data/README.md +14 -29
  2. data/VERSION +1 -1
  3. data/lib/mongo.rb +3 -0
  4. data/lib/mongo/collection.rb +99 -49
  5. data/lib/mongo/cursor.rb +17 -17
  6. data/lib/mongo/db.rb +30 -14
  7. data/lib/mongo/gridfs/grid.rb +5 -3
  8. data/lib/mongo/gridfs/grid_file_system.rb +5 -3
  9. data/lib/mongo/gridfs/grid_io.rb +5 -3
  10. data/lib/mongo/legacy.rb +9 -2
  11. data/lib/mongo/mongo_client.rb +100 -72
  12. data/lib/mongo/mongo_replica_set_client.rb +46 -60
  13. data/lib/mongo/mongo_sharded_client.rb +5 -66
  14. data/lib/mongo/networking.rb +2 -1
  15. data/lib/mongo/util/node.rb +41 -42
  16. data/lib/mongo/util/pool.rb +15 -43
  17. data/lib/mongo/util/pool_manager.rb +16 -65
  18. data/lib/mongo/util/read_preference.rb +82 -0
  19. data/lib/mongo/util/sharding_pool_manager.rb +0 -86
  20. data/lib/mongo/util/ssl_socket.rb +2 -1
  21. data/lib/mongo/util/support.rb +8 -18
  22. data/lib/mongo/util/tcp_socket.rb +5 -4
  23. data/lib/mongo/util/thread_local_variable_manager.rb +29 -0
  24. data/lib/mongo/util/unix_socket.rb +23 -0
  25. data/lib/mongo/util/uri_parser.rb +31 -18
  26. data/lib/mongo/util/write_concern.rb +7 -2
  27. data/mongo.gemspec +1 -1
  28. data/test/auxillary/repl_set_auth_test.rb +2 -2
  29. data/test/bson/bson_test.rb +1 -1
  30. data/test/bson/byte_buffer_test.rb +24 -26
  31. data/test/bson/hash_with_indifferent_access_test.rb +11 -1
  32. data/test/functional/collection_test.rb +16 -16
  33. data/test/functional/connection_test.rb +1 -4
  34. data/test/functional/db_api_test.rb +14 -10
  35. data/test/functional/pool_test.rb +23 -31
  36. data/test/functional/timeout_test.rb +3 -5
  37. data/test/functional/uri_test.rb +10 -5
  38. data/test/replica_set/basic_test.rb +3 -8
  39. data/test/replica_set/client_test.rb +47 -31
  40. data/test/replica_set/complex_connect_test.rb +12 -10
  41. data/test/replica_set/connection_test.rb +8 -151
  42. data/test/replica_set/count_test.rb +9 -5
  43. data/test/replica_set/cursor_test.rb +17 -27
  44. data/test/replica_set/insert_test.rb +5 -10
  45. data/test/replica_set/query_test.rb +4 -9
  46. data/test/replica_set/read_preference_test.rb +200 -0
  47. data/test/replica_set/refresh_test.rb +54 -65
  48. data/test/replica_set/replication_ack_test.rb +16 -14
  49. data/test/sharded_cluster/basic_test.rb +30 -0
  50. data/test/test_helper.rb +33 -15
  51. data/test/threading/basic_test.rb +79 -0
  52. data/test/tools/mongo_config.rb +62 -22
  53. data/test/unit/client_test.rb +36 -14
  54. data/test/unit/collection_test.rb +23 -0
  55. data/test/unit/connection_test.rb +30 -14
  56. data/test/unit/cursor_test.rb +137 -7
  57. data/test/unit/db_test.rb +17 -4
  58. data/test/unit/grid_test.rb +2 -2
  59. data/test/unit/node_test.rb +2 -1
  60. data/test/unit/pool_manager_test.rb +29 -1
  61. data/test/unit/read_test.rb +15 -15
  62. data/test/unit/safe_test.rb +4 -4
  63. data/test/unit/write_concern_test.rb +4 -4
  64. metadata +134 -143
  65. data/examples/admin.rb +0 -43
  66. data/examples/capped.rb +0 -22
  67. data/examples/cursor.rb +0 -48
  68. data/examples/gridfs.rb +0 -44
  69. data/examples/index_test.rb +0 -126
  70. data/examples/info.rb +0 -31
  71. data/examples/queries.rb +0 -74
  72. data/examples/replica_set.rb +0 -26
  73. data/examples/simple.rb +0 -25
  74. data/examples/strict.rb +0 -35
  75. data/examples/types.rb +0 -36
  76. data/examples/web/thin/load.rb +0 -23
  77. data/examples/web/unicorn/load.rb +0 -25
  78. data/test/support/hash_with_indifferent_access.rb +0 -186
  79. data/test/support/keys.rb +0 -45
  80. data/test/threading/threading_with_large_pool_test.rb +0 -90
@@ -1,43 +0,0 @@
1
- $:.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
2
-
3
- require 'mongo'
4
- require 'pp'
5
-
6
- include Mongo
7
-
8
- host = ENV['MONGO_RUBY_DRIVER_HOST'] || 'localhost'
9
- port = ENV['MONGO_RUBY_DRIVER_PORT'] || MongoClient::DEFAULT_PORT
10
-
11
- puts "Connecting to #{host}:#{port}"
12
- client = MongoClient.new(host, port)
13
- db = client.db('ruby-mongo-examples')
14
- coll = db.create_collection('test')
15
-
16
- # Erase all records from collection, if any
17
- coll.remove
18
-
19
- admin = client['admin']
20
-
21
- # Profiling level set/get
22
- puts "Profiling level: #{admin.profiling_level}"
23
-
24
- # Start profiling everything
25
- admin.profiling_level = :all
26
-
27
- # Read records, creating a profiling event
28
- coll.find().to_a
29
-
30
- # Stop profiling
31
- admin.profiling_level = :off
32
-
33
- # Print all profiling info
34
- pp admin.profiling_info
35
-
36
- # Validate returns a hash if all is well and
37
- # raises an exception if there is a problem.
38
- info = db.validate_collection(coll.name)
39
- puts "valid = #{info['ok']}"
40
- puts info['result']
41
-
42
- # Destroy the collection
43
- coll.drop
@@ -1,22 +0,0 @@
1
- $:.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
2
- require 'mongo'
3
-
4
- include Mongo
5
-
6
- host = ENV['MONGO_RUBY_DRIVER_HOST'] || 'localhost'
7
- port = ENV['MONGO_RUBY_DRIVER_PORT'] || MongoClient::DEFAULT_PORT
8
-
9
- puts "Connecting to #{host}:#{port}"
10
- db = MongoClient.new(host, port).db('ruby-mongo-examples')
11
- db.drop_collection('test')
12
-
13
- # A capped collection has a max size and, optionally, a max number of records.
14
- # Old records get pushed out by new ones once the size or max num records is reached.
15
- coll = db.create_collection('test', :capped => true, :size => 1024, :max => 12)
16
-
17
- 100.times { |i| coll.insert('a' => i+1) }
18
-
19
- # We will only see the last 12 records
20
- coll.find().each { |row| p row }
21
-
22
- coll.drop
@@ -1,48 +0,0 @@
1
- $:.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
2
-
3
- require 'mongo'
4
- require 'pp'
5
-
6
- include Mongo
7
-
8
- host = ENV['MONGO_RUBY_DRIVER_HOST'] || 'localhost'
9
- port = ENV['MONGO_RUBY_DRIVER_PORT'] || MongoClient::DEFAULT_PORT
10
-
11
- puts "Connecting to #{host}:#{port}"
12
- db = MongoClient.new(host, port).db('ruby-mongo-examples')
13
- coll = db.collection('test')
14
-
15
- # Erase all records from collection, if any
16
- coll.remove
17
-
18
- # Insert 3 records
19
- 3.times { |i| coll.insert({'a' => i+1}) }
20
-
21
- # Cursors don't run their queries until you actually attempt to retrieve data
22
- # from them.
23
-
24
- # Find returns a Cursor, which is Enumerable. You can iterate:
25
- coll.find().each { |row| pp row }
26
-
27
- # You can turn it into an array:
28
- array = coll.find().to_a
29
-
30
- # You can iterate after turning it into an array (the cursor will iterate over
31
- # the copy of the array that it saves internally.)
32
- cursor = coll.find()
33
- array = cursor.to_a
34
- cursor.each { |row| pp row }
35
-
36
- # You can get the next object
37
- first_object = coll.find().next_document
38
-
39
- # next_document returns nil if there are no more objects that match
40
- cursor = coll.find()
41
- obj = cursor.next_document
42
- while obj
43
- pp obj
44
- obj = cursor.next_document
45
- end
46
-
47
- # Destroy the collection
48
- coll.drop
@@ -1,44 +0,0 @@
1
- $:.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
2
- def assert
3
- raise "Failed!" unless yield
4
- end
5
-
6
- require 'mongo'
7
- include Mongo
8
-
9
- host = ENV['MONGO_RUBY_DRIVER_HOST'] || 'localhost'
10
- port = ENV['MONGO_RUBY_DRIVER_PORT'] || MongoClient::DEFAULT_PORT
11
-
12
- puts "Connecting to #{host}:#{port}"
13
- db = MongoClient.new(host, port).db('ruby-mongo-examples')
14
-
15
- data = "hello, world!"
16
-
17
- grid = Grid.new(db)
18
-
19
- # Write a new file. data can be a string or an io object responding to #read.
20
- id = grid.put(data, :filename => 'hello.txt')
21
-
22
- # Read it and print out the contents
23
- file = grid.get(id)
24
- puts file.read
25
-
26
- # Delete the file
27
- grid.delete(id)
28
-
29
- begin
30
- grid.get(id)
31
- rescue => e
32
- assert {e.class == Mongo::GridError}
33
- end
34
-
35
- # Metadata
36
- id = grid.put(data, :filename => 'hello.txt', :content_type => 'text/plain', :metadata => {'name' => 'hello'})
37
- file = grid.get(id)
38
-
39
- p file.content_type
40
- p file.metadata.inspect
41
- p file.chunk_size
42
- p file.file_length
43
- p file.filename
44
- p file.data
@@ -1,126 +0,0 @@
1
- $:.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
2
-
3
- require 'mongo'
4
-
5
- include Mongo
6
-
7
- host = ENV['MONGO_RUBY_DRIVER_HOST'] || 'localhost'
8
- port = ENV['MONGO_RUBY_DRIVER_PORT'] || MongoClient::DEFAULT_PORT
9
-
10
- puts ">> Connecting to #{host}:#{port}"
11
- db = MongoClient.new(host, port).db('ruby-mongo-index_test')
12
-
13
- class Exception
14
- def errmsg
15
- "%s: %s\n%s" % [self.class, message, (backtrace || []).join("\n") << "\n"]
16
- end
17
- end
18
-
19
- puts ">> Dropping collection test"
20
- begin
21
- res = db.drop_collection('test')
22
- puts "dropped : #{res.inspect}"
23
- rescue => e
24
- puts "Error: #{e.errmsg}"
25
- end
26
-
27
- puts ">> Creating collection test"
28
- begin
29
- coll = db.collection('test')
30
- puts "created : #{coll.inspect}"
31
- rescue => e
32
- puts "Error: #{e.errmsg}"
33
- end
34
-
35
- OBJS_COUNT = 100
36
-
37
- puts ">> Generating test data"
38
- msgs = %w{hola hello aloha ciao}
39
- arr = (0...OBJS_COUNT).collect {|x| { :number => x, :rndm => (rand(5)+1), :msg => msgs[rand(4)] }}
40
- puts "generated"
41
-
42
- puts ">> Inserting data (#{arr.size})"
43
- coll.insert(arr)
44
- puts "inserted"
45
-
46
- puts ">> Creating index"
47
- #res = coll.create_index "all", :_id => 1, :number => 1, :rndm => 1, :msg => 1
48
- res = coll.create_index [[:number, 1], [:rndm, 1], [:msg, 1]]
49
- puts "created index: #{res.inspect}"
50
- # ============================ Mongo Log ============================
51
- # Fri Dec 5 14:45:02 Adding all existing records for ruby-mongo-console.test to new index
52
- # ***
53
- # Bad data or size in BSONElement::size()
54
- # bad type:30
55
- # totalsize:11 fieldnamesize:4
56
- # lastrec:
57
- # Fri Dec 5 14:45:02 ruby-mongo-console.system.indexes Assertion failure false jsobj.cpp a0
58
- # Fri Dec 5 14:45:02 database: ruby-mongo-console op:7d2 0
59
- # Fri Dec 5 14:45:02 ns: ruby-mongo-console.system.indexes
60
-
61
- puts ">> Gathering index information"
62
- begin
63
- res = coll.index_information
64
- puts "index_information : #{res.inspect}"
65
- rescue => e
66
- puts "Error: #{e.errmsg}"
67
- end
68
- # ============================ Console Output ============================
69
- # RuntimeError: Keys for index on return from db was nil. Coll = ruby-mongo-console.test
70
- # from ./bin/../lib/mongo/db.rb:135:in `index_information'
71
- # from (irb):11:in `collect'
72
- # from ./bin/../lib/mongo/cursor.rb:47:in `each'
73
- # from ./bin/../lib/mongo/db.rb:130:in `collect'
74
- # from ./bin/../lib/mongo/db.rb:130:in `index_information'
75
- # from ./bin/../lib/mongo/collection.rb:74:in `index_information'
76
- # from (irb):11
77
-
78
- puts ">> Dropping index"
79
- begin
80
- res = coll.drop_index "number_1_rndm_1_msg_1"
81
- puts "dropped : #{res.inspect}"
82
- rescue => e
83
- puts "Error: #{e.errmsg}"
84
- end
85
-
86
- # ============================ Console Output ============================
87
- # => {"nIndexesWas"=>2.0, "ok"=>1.0}
88
- # ============================ Mongo Log ============================
89
- # 0x41802a 0x411549 0x42bac6 0x42c1f6 0x42c55b 0x42e6f7 0x41631e 0x41a89d 0x41ade2 0x41b448 0x4650d2 0x4695ad
90
- # db/db(_Z12sayDbContextPKc+0x17a) [0x41802a]
91
- # db/db(_Z8assertedPKcS0_j+0x9) [0x411549]
92
- # db/db(_ZNK11BSONElement4sizeEv+0x1f6) [0x42bac6]
93
- # db/db(_ZN7BSONObj8getFieldEPKc+0xa6) [0x42c1f6]
94
- # db/db(_ZN7BSONObj14getFieldDottedEPKc+0x11b) [0x42c55b]
95
- # db/db(_ZN7BSONObj19extractFieldsDottedES_R14BSONObjBuilder+0x87) [0x42e6f7]
96
- # db/db(_ZN12IndexDetails17getKeysFromObjectER7BSONObjRSt3setIS0_St4lessIS0_ESaIS0_EE+0x24e) [0x41631e]
97
- # db/db(_Z12_indexRecordR12IndexDetailsR7BSONObj7DiskLoc+0x5d) [0x41a89d]
98
- # db/db(_Z18addExistingToIndexPKcR12IndexDetails+0xb2) [0x41ade2]
99
- # db/db(_ZN11DataFileMgr6insertEPKcPKvib+0x508) [0x41b448]
100
- # db/db(_Z14receivedInsertR7MessageRSt18basic_stringstreamIcSt11char_traitsIcESaIcEE+0x112) [0x4650d2]
101
- # db/db(_Z10connThreadv+0xb4d) [0x4695ad]
102
- # Fri Dec 5 14:45:02 ruby-mongo-console.system.indexes Caught Assertion insert, continuing
103
- # Fri Dec 5 14:47:59 CMD: deleteIndexes ruby-mongo-console.test
104
- # d->nIndexes was 2
105
- # alpha implementation, space not reclaimed
106
-
107
- puts ">> Gathering index information"
108
- begin
109
- res = coll.index_information
110
- puts "index_information : #{res.inspect}"
111
- rescue => e
112
- puts "Error: #{e.errmsg}"
113
- end
114
- # ============================ Console Output ============================
115
- # RuntimeError: Keys for index on return from db was nil. Coll = ruby-mongo-console.test
116
- # from ./bin/../lib/mongo/db.rb:135:in `index_information'
117
- # from (irb):15:in `collect'
118
- # from ./bin/../lib/mongo/cursor.rb:47:in `each'
119
- # from ./bin/../lib/mongo/db.rb:130:in `collect'
120
- # from ./bin/../lib/mongo/db.rb:130:in `index_information'
121
- # from ./bin/../lib/mongo/collection.rb:74:in `index_information'
122
- # from (irb):15
123
-
124
- puts ">> Closing connection"
125
- db.close
126
- puts "closed"
@@ -1,31 +0,0 @@
1
- $:.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
2
-
3
- require 'mongo'
4
-
5
- include Mongo
6
-
7
- host = ENV['MONGO_RUBY_DRIVER_HOST'] || 'localhost'
8
- port = ENV['MONGO_RUBY_DRIVER_PORT'] || MongoClient::DEFAULT_PORT
9
-
10
- puts "Connecting to #{host}:#{port}"
11
- db = MongoClient.new(host, port).db('ruby-mongo-examples')
12
- coll = db.collection('test')
13
-
14
- # Erase all records from collection, if any
15
- coll.remove
16
-
17
- # Insert 3 records
18
- 3.times { |i| coll.insert({'a' => i+1}) }
19
-
20
- # Collection names in database
21
- p db.collection_names
22
-
23
- # More information about each collection
24
- p db.collections_info
25
-
26
- # Index information
27
- coll.create_index('a')
28
- p db.index_information('test')
29
-
30
- # Destroy the collection
31
- coll.drop
@@ -1,74 +0,0 @@
1
- $:.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
2
-
3
- require 'mongo'
4
- require 'pp'
5
-
6
- include Mongo
7
-
8
- host = ENV['MONGO_RUBY_DRIVER_HOST'] || 'localhost'
9
- port = ENV['MONGO_RUBY_DRIVER_PORT'] || MongoClient::DEFAULT_PORT
10
-
11
- puts "Connecting to #{host}:#{port}"
12
- db = MongoClient.new(host, port).db('ruby-mongo-examples')
13
- coll = db.collection('test')
14
-
15
- # Remove all records, if any
16
- coll.remove
17
-
18
- # Insert three records
19
- coll.insert('a' => 1)
20
- coll.insert('a' => 2)
21
- coll.insert('b' => 3)
22
- coll.insert('c' => 'foo')
23
- coll.insert('c' => 'bar')
24
-
25
- # Count.
26
- puts "There are #{coll.count} records."
27
-
28
- # Find all records. find() returns a Cursor.
29
- puts "Find all records:"
30
- pp cursor = coll.find.to_a
31
-
32
- # Print them. Note that all records have an _id automatically added by the
33
- # database. See pk.rb for an example of how to use a primary key factory to
34
- # generate your own values for _id.
35
- puts "Print each document individually:"
36
- pp cursor.each { |row| pp row }
37
-
38
- # See Collection#find. From now on in this file, we won't be printing the
39
- # records we find.
40
- puts "Find one record:"
41
- pp coll.find('a' => 1).to_a
42
-
43
- # Find records sort by 'a', skip 1, limit 2 records.
44
- # Sort can be single name, array, or hash.
45
- puts "Skip 1, limit 2, sort by 'a':"
46
- pp coll.find({}, {:skip => 1, :limit => 2, :sort => 'a'}).to_a
47
-
48
- # Find all records with 'a' > 1. There is also $lt, $gte, and $lte.
49
- coll.find({'a' => {'$gt' => 1}})
50
- coll.find({'a' => {'$gt' => 1, '$lte' => 3}})
51
-
52
- # Find all records with 'a' in a set of values.
53
- puts "Find all records where a is $in [1, 2]:"
54
- pp coll.find('a' => {'$in' => [1,2]}).to_a
55
-
56
- puts "Find by regex:"
57
- pp coll.find({'c' => /f/}).to_a
58
-
59
- # Print query explanation
60
- puts "Print an explain:"
61
- pp coll.find({'c' => /f/}).explain
62
-
63
- # Use a hint with a query. Need an index. Hints can be stored with the
64
- # collection, in which case they will be used with all queries, or they can be
65
- # specified per query, in which case that hint overrides the hint associated
66
- # with the collection if any.
67
- coll.create_index('c')
68
- coll.hint = 'c'
69
-
70
- puts "Print an explain with index:"
71
- pp coll.find('c' => /[f|b]/).explain
72
-
73
- puts "Print an explain with natural order hint:"
74
- pp coll.find({'c' => /[f|b]/}, :hint => '$natural').explain
@@ -1,26 +0,0 @@
1
- # This code assumes a running replica set with at least one node at localhost:27017.
2
- require 'mongo'
3
-
4
- include Mongo
5
-
6
- cons = []
7
-
8
- 10.times do
9
- cons << MongoReplicaSetClient(['localhost:27017'], :read => :secondary)
10
- end
11
-
12
- ports = cons.map do |con|
13
- con.read_pool.port
14
- end
15
-
16
- puts "These ten connections will read from the following ports:"
17
- p ports
18
-
19
- cons[rand(10)]['foo']['bar'].remove
20
- 100.times do |n|
21
- cons[rand(10)]['foo']['bar'].insert({:a => n})
22
- end
23
-
24
- 100.times do |n|
25
- p cons[rand(10)]['foo']['bar'].find_one({:a => n})
26
- end
@@ -1,25 +0,0 @@
1
- $:.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
2
-
3
- require 'rubygems'
4
- require 'mongo'
5
-
6
- include Mongo
7
-
8
- host = ENV['MONGO_RUBY_DRIVER_HOST'] || 'localhost'
9
- port = ENV['MONGO_RUBY_DRIVER_PORT'] || MongoClient::DEFAULT_PORT
10
-
11
- puts "Connecting to #{host}:#{port}"
12
- db = MongoClient.new(host, port).db('ruby-mongo-examples')
13
- coll = db.collection('test')
14
-
15
- # Erase all records from collection, if any
16
- coll.remove
17
-
18
- # Insert 3 records
19
- 3.times { |i| coll.insert({'a' => i+1}) }
20
-
21
- puts "There are #{coll.count()} records in the test collection. Here they are:"
22
- coll.find().each { |doc| puts doc.inspect }
23
-
24
- # Destroy the collection
25
- coll.drop