mongo 1.0 → 1.1.5
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.
- data/LICENSE.txt +1 -13
- data/{README.rdoc → README.md} +129 -149
- data/Rakefile +94 -58
- data/bin/mongo_console +21 -0
- data/docs/1.0_UPGRADE.md +21 -0
- data/docs/CREDITS.md +123 -0
- data/docs/FAQ.md +112 -0
- data/docs/GridFS.md +158 -0
- data/docs/HISTORY.md +185 -0
- data/docs/REPLICA_SETS.md +75 -0
- data/docs/TUTORIAL.md +247 -0
- data/docs/WRITE_CONCERN.md +28 -0
- data/lib/mongo/collection.rb +225 -105
- data/lib/mongo/connection.rb +374 -315
- data/lib/mongo/cursor.rb +122 -77
- data/lib/mongo/db.rb +109 -85
- data/lib/mongo/exceptions.rb +6 -0
- data/lib/mongo/gridfs/grid.rb +19 -11
- data/lib/mongo/gridfs/grid_ext.rb +36 -9
- data/lib/mongo/gridfs/grid_file_system.rb +15 -9
- data/lib/mongo/gridfs/grid_io.rb +49 -16
- data/lib/mongo/gridfs/grid_io_fix.rb +38 -0
- data/lib/mongo/repl_set_connection.rb +290 -0
- data/lib/mongo/util/conversions.rb +3 -1
- data/lib/mongo/util/core_ext.rb +17 -4
- data/lib/mongo/util/pool.rb +125 -0
- data/lib/mongo/util/server_version.rb +2 -0
- data/lib/mongo/util/support.rb +12 -0
- data/lib/mongo/util/uri_parser.rb +71 -0
- data/lib/mongo.rb +23 -7
- data/{mongo-ruby-driver.gemspec → mongo.gemspec} +9 -7
- data/test/auxillary/1.4_features.rb +2 -2
- data/test/auxillary/authentication_test.rb +1 -1
- data/test/auxillary/autoreconnect_test.rb +1 -1
- data/test/{slave_connection_test.rb → auxillary/slave_connection_test.rb} +6 -6
- data/test/bson/binary_test.rb +15 -0
- data/test/bson/bson_test.rb +537 -0
- data/test/bson/byte_buffer_test.rb +190 -0
- data/test/bson/hash_with_indifferent_access_test.rb +38 -0
- data/test/bson/json_test.rb +17 -0
- data/test/bson/object_id_test.rb +141 -0
- data/test/bson/ordered_hash_test.rb +197 -0
- data/test/collection_test.rb +195 -15
- data/test/connection_test.rb +93 -56
- data/test/conversions_test.rb +1 -1
- data/test/cursor_fail_test.rb +75 -0
- data/test/cursor_message_test.rb +43 -0
- data/test/cursor_test.rb +93 -32
- data/test/db_api_test.rb +28 -55
- data/test/db_connection_test.rb +2 -3
- data/test/db_test.rb +45 -40
- data/test/grid_file_system_test.rb +14 -6
- data/test/grid_io_test.rb +36 -7
- data/test/grid_test.rb +54 -10
- data/test/replica_sets/connect_test.rb +84 -0
- data/test/replica_sets/count_test.rb +35 -0
- data/test/{replica → replica_sets}/insert_test.rb +17 -14
- data/test/replica_sets/pooled_insert_test.rb +55 -0
- data/test/replica_sets/query_secondaries.rb +80 -0
- data/test/replica_sets/query_test.rb +41 -0
- data/test/replica_sets/replication_ack_test.rb +64 -0
- data/test/replica_sets/rs_test_helper.rb +29 -0
- data/test/safe_test.rb +68 -0
- data/test/support/hash_with_indifferent_access.rb +199 -0
- data/test/support/keys.rb +45 -0
- data/test/support_test.rb +19 -0
- data/test/test_helper.rb +53 -15
- data/test/threading/{test_threading_large_pool.rb → threading_with_large_pool_test.rb} +2 -2
- data/test/threading_test.rb +2 -2
- data/test/tools/repl_set_manager.rb +241 -0
- data/test/tools/test.rb +13 -0
- data/test/unit/collection_test.rb +70 -7
- data/test/unit/connection_test.rb +18 -39
- data/test/unit/cursor_test.rb +7 -8
- data/test/unit/db_test.rb +14 -17
- data/test/unit/grid_test.rb +49 -0
- data/test/unit/pool_test.rb +9 -0
- data/test/unit/repl_set_connection_test.rb +82 -0
- data/test/unit/safe_test.rb +125 -0
- metadata +132 -51
- data/bin/bson_benchmark.rb +0 -59
- data/bin/fail_if_no_c.rb +0 -11
- data/examples/admin.rb +0 -43
- data/examples/capped.rb +0 -22
- data/examples/cursor.rb +0 -48
- data/examples/gridfs.rb +0 -44
- data/examples/index_test.rb +0 -126
- data/examples/info.rb +0 -31
- data/examples/queries.rb +0 -70
- data/examples/simple.rb +0 -24
- data/examples/strict.rb +0 -35
- data/examples/types.rb +0 -36
- data/test/replica/count_test.rb +0 -34
- data/test/replica/pooled_insert_test.rb +0 -54
- data/test/replica/query_test.rb +0 -39
data/examples/gridfs.rb
DELETED
|
@@ -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'] || Connection::DEFAULT_PORT
|
|
11
|
-
|
|
12
|
-
puts "Connecting to #{host}:#{port}"
|
|
13
|
-
db = Connection.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
|
data/examples/index_test.rb
DELETED
|
@@ -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'] || Connection::DEFAULT_PORT
|
|
9
|
-
|
|
10
|
-
puts ">> Connecting to #{host}:#{port}"
|
|
11
|
-
db = Connection.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"
|
data/examples/info.rb
DELETED
|
@@ -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'] || Connection::DEFAULT_PORT
|
|
9
|
-
|
|
10
|
-
puts "Connecting to #{host}:#{port}"
|
|
11
|
-
db = Connection.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
|
data/examples/queries.rb
DELETED
|
@@ -1,70 +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'] || Connection::DEFAULT_PORT
|
|
10
|
-
|
|
11
|
-
puts "Connecting to #{host}:#{port}"
|
|
12
|
-
db = Connection.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
|
-
|
|
23
|
-
# Count.
|
|
24
|
-
puts "There are #{coll.count()} records."
|
|
25
|
-
|
|
26
|
-
# Find all records. find() returns a Cursor.
|
|
27
|
-
cursor = coll.find()
|
|
28
|
-
|
|
29
|
-
# Print them. Note that all records have an _id automatically added by the
|
|
30
|
-
# database. See pk.rb for an example of how to use a primary key factory to
|
|
31
|
-
# generate your own values for _id.
|
|
32
|
-
cursor.each { |row| pp row }
|
|
33
|
-
|
|
34
|
-
# Cursor has a to_a method that slurps all records into memory.
|
|
35
|
-
rows = coll.find().to_a
|
|
36
|
-
rows.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
|
-
coll.find('a' => 1)
|
|
41
|
-
|
|
42
|
-
# Find records sort by 'a', skip 1, limit 2 records.
|
|
43
|
-
# Sort can be single name, array, or hash.
|
|
44
|
-
coll.find({}, {:skip => 1, :limit => 2, :sort => 'a'})
|
|
45
|
-
|
|
46
|
-
# Find all records with 'a' > 1. There is also $lt, $gte, and $lte.
|
|
47
|
-
coll.find({'a' => {'$gt' => 1}})
|
|
48
|
-
coll.find({'a' => {'$gt' => 1, '$lte' => 3}})
|
|
49
|
-
|
|
50
|
-
# Find all records with 'a' in a set of values.
|
|
51
|
-
coll.find('a' => {'$in' => [1,2]})
|
|
52
|
-
|
|
53
|
-
# Find by regexp
|
|
54
|
-
coll.find('a' => /[1|2]/)
|
|
55
|
-
|
|
56
|
-
# Print query explanation
|
|
57
|
-
pp coll.find('a' => /[1|2]/).explain()
|
|
58
|
-
|
|
59
|
-
# Use a hint with a query. Need an index. Hints can be stored with the
|
|
60
|
-
# collection, in which case they will be used with all queries, or they can be
|
|
61
|
-
# specified per query, in which case that hint overrides the hint associated
|
|
62
|
-
# with the collection if any.
|
|
63
|
-
coll.create_index('a')
|
|
64
|
-
coll.hint = 'a'
|
|
65
|
-
|
|
66
|
-
# You will see a different explanation now that the hint is in place
|
|
67
|
-
pp coll.find('a' => /[1|2]/).explain()
|
|
68
|
-
|
|
69
|
-
# Override hint for single query
|
|
70
|
-
coll.find({'a' => 1}, :hint => 'b')
|
data/examples/simple.rb
DELETED
|
@@ -1,24 +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'] || Connection::DEFAULT_PORT
|
|
9
|
-
|
|
10
|
-
puts "Connecting to #{host}:#{port}"
|
|
11
|
-
db = Connection.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
|
-
puts "There are #{coll.count()} records in the test collection. Here they are:"
|
|
21
|
-
coll.find().each { |doc| puts doc.inspect }
|
|
22
|
-
|
|
23
|
-
# Destroy the collection
|
|
24
|
-
coll.drop
|
data/examples/strict.rb
DELETED
|
@@ -1,35 +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'] || Connection::DEFAULT_PORT
|
|
9
|
-
|
|
10
|
-
puts "Connecting to #{host}:#{port}"
|
|
11
|
-
db = Connection.new(host, port).db('ruby-mongo-examples')
|
|
12
|
-
|
|
13
|
-
db.drop_collection('does-not-exist')
|
|
14
|
-
db.create_collection('test')
|
|
15
|
-
|
|
16
|
-
db.strict = true
|
|
17
|
-
|
|
18
|
-
begin
|
|
19
|
-
# Can't reference collection that does not exist
|
|
20
|
-
db.collection('does-not-exist')
|
|
21
|
-
puts "error: expected exception"
|
|
22
|
-
rescue => ex
|
|
23
|
-
puts "expected exception: #{ex}"
|
|
24
|
-
end
|
|
25
|
-
|
|
26
|
-
begin
|
|
27
|
-
# Can't create collection that already exists
|
|
28
|
-
db.create_collection('test')
|
|
29
|
-
puts "error: expected exception"
|
|
30
|
-
rescue => ex
|
|
31
|
-
puts "expected exception: #{ex}"
|
|
32
|
-
end
|
|
33
|
-
|
|
34
|
-
db.strict = false
|
|
35
|
-
db.drop_collection('test')
|
data/examples/types.rb
DELETED
|
@@ -1,36 +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'] || Connection::DEFAULT_PORT
|
|
10
|
-
|
|
11
|
-
puts "Connecting to #{host}:#{port}"
|
|
12
|
-
db = Connection.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 record with all sorts of values
|
|
19
|
-
coll.insert('array' => [1, 2, 3],
|
|
20
|
-
'string' => 'hello',
|
|
21
|
-
'hash' => {'a' => 1, 'b' => 2},
|
|
22
|
-
'date' => Time.now, # milliseconds only; microseconds are not stored
|
|
23
|
-
'oid' => ObjectID.new,
|
|
24
|
-
'binary' => Binary.new([1, 2, 3]),
|
|
25
|
-
'int' => 42,
|
|
26
|
-
'float' => 33.33333,
|
|
27
|
-
'regex' => /foobar/i,
|
|
28
|
-
'boolean' => true,
|
|
29
|
-
'where' => Code.new('this.x == 3'),
|
|
30
|
-
'dbref' => DBRef.new(coll.name, ObjectID.new),
|
|
31
|
-
'null' => nil,
|
|
32
|
-
'symbol' => :zildjian)
|
|
33
|
-
|
|
34
|
-
pp coll.find().next_document
|
|
35
|
-
|
|
36
|
-
coll.remove
|
data/test/replica/count_test.rb
DELETED
|
@@ -1,34 +0,0 @@
|
|
|
1
|
-
$:.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
|
|
2
|
-
require 'mongo'
|
|
3
|
-
require 'test/unit'
|
|
4
|
-
require 'test/test_helper'
|
|
5
|
-
|
|
6
|
-
# NOTE: this test should be run only if a replica pair is running.
|
|
7
|
-
class ReplicaPairCountTest < Test::Unit::TestCase
|
|
8
|
-
include Mongo
|
|
9
|
-
|
|
10
|
-
def setup
|
|
11
|
-
@conn = Mongo::Connection.new({:left => ["localhost", 27017], :right => ["localhost", 27018]}, nil)
|
|
12
|
-
@db = @conn.db('mongo-ruby-test')
|
|
13
|
-
@db.drop_collection("test-pairs")
|
|
14
|
-
@coll = @db.collection("test-pairs")
|
|
15
|
-
end
|
|
16
|
-
|
|
17
|
-
def test_correct_count_after_insertion_reconnect
|
|
18
|
-
@coll.insert({:a => 20}, :safe => true)
|
|
19
|
-
assert_equal 1, @coll.count
|
|
20
|
-
|
|
21
|
-
# Sleep to allow resync
|
|
22
|
-
sleep(3)
|
|
23
|
-
|
|
24
|
-
puts "Please disconnect the current master."
|
|
25
|
-
gets
|
|
26
|
-
|
|
27
|
-
rescue_connection_failure do
|
|
28
|
-
@coll.insert({:a => 30}, :safe => true)
|
|
29
|
-
end
|
|
30
|
-
@coll.insert({:a => 40}, :safe => true)
|
|
31
|
-
assert_equal 3, @coll.count, "Second count failed"
|
|
32
|
-
end
|
|
33
|
-
|
|
34
|
-
end
|
|
@@ -1,54 +0,0 @@
|
|
|
1
|
-
$:.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
|
|
2
|
-
require 'mongo'
|
|
3
|
-
require 'test/unit'
|
|
4
|
-
require 'test/test_helper'
|
|
5
|
-
|
|
6
|
-
# NOTE: this test should be run only if a replica pair is running.
|
|
7
|
-
class ReplicaPairPooledInsertTest < Test::Unit::TestCase
|
|
8
|
-
include Mongo
|
|
9
|
-
|
|
10
|
-
def setup
|
|
11
|
-
@conn = Mongo::Connection.new({:left => ["localhost", 27017], :right => ["localhost", 27018]}, nil, :pool_size => 10, :timeout => 5)
|
|
12
|
-
@db = @conn.db('mongo-ruby-test')
|
|
13
|
-
@db.drop_collection("test-pairs")
|
|
14
|
-
@coll = @db.collection("test-pairs")
|
|
15
|
-
end
|
|
16
|
-
|
|
17
|
-
def test_insert
|
|
18
|
-
expected_results = [-1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
|
|
19
|
-
@coll.save({:a => -1}, :safe => true)
|
|
20
|
-
puts "Please disconnect the current master."
|
|
21
|
-
gets
|
|
22
|
-
|
|
23
|
-
threads = []
|
|
24
|
-
10.times do |i|
|
|
25
|
-
threads[i] = Thread.new do
|
|
26
|
-
rescue_connection_failure do
|
|
27
|
-
@coll.save({:a => i}, :safe => true)
|
|
28
|
-
end
|
|
29
|
-
end
|
|
30
|
-
end
|
|
31
|
-
|
|
32
|
-
puts "Please reconnect the old master to make sure that the new master " +
|
|
33
|
-
"has synced with the previous master. Note: this may have happened already." +
|
|
34
|
-
"Note also that when connection with multiple threads, you may need to wait a few seconds" +
|
|
35
|
-
"after restarting the old master so that all the data has had a chance to sync." +
|
|
36
|
-
"This is a case of eventual consistency."
|
|
37
|
-
gets
|
|
38
|
-
results = []
|
|
39
|
-
|
|
40
|
-
rescue_connection_failure do
|
|
41
|
-
@coll.find.each {|r| results << r}
|
|
42
|
-
expected_results.each do |a|
|
|
43
|
-
assert results.any? {|r| r['a'] == a}, "Could not find record for a => #{a}"
|
|
44
|
-
end
|
|
45
|
-
end
|
|
46
|
-
|
|
47
|
-
@coll.save({:a => 10}, :safe => true)
|
|
48
|
-
@coll.find.each {|r| results << r}
|
|
49
|
-
(expected_results + [10]).each do |a|
|
|
50
|
-
assert results.any? {|r| r['a'] == a}, "Could not find record for a => #{a} on second find"
|
|
51
|
-
end
|
|
52
|
-
end
|
|
53
|
-
|
|
54
|
-
end
|
data/test/replica/query_test.rb
DELETED
|
@@ -1,39 +0,0 @@
|
|
|
1
|
-
$:.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
|
|
2
|
-
require 'mongo'
|
|
3
|
-
require 'test/unit'
|
|
4
|
-
require 'test/test_helper'
|
|
5
|
-
|
|
6
|
-
# NOTE: this test should be run only if a replica pair is running.
|
|
7
|
-
class ReplicaPairQueryTest < Test::Unit::TestCase
|
|
8
|
-
include Mongo
|
|
9
|
-
|
|
10
|
-
def setup
|
|
11
|
-
@conn = Mongo::Connection.new({:left => ["localhost", 27017], :right => ["localhost", 27018]}, nil)
|
|
12
|
-
@db = @conn.db('mongo-ruby-test')
|
|
13
|
-
@db.drop_collection("test-pairs")
|
|
14
|
-
@coll = @db.collection("test-pairs")
|
|
15
|
-
end
|
|
16
|
-
|
|
17
|
-
def test_query
|
|
18
|
-
@coll.save({:a => 20})
|
|
19
|
-
@coll.save({:a => 30})
|
|
20
|
-
@coll.save({:a => 40})
|
|
21
|
-
results = []
|
|
22
|
-
@coll.find.each {|r| results << r}
|
|
23
|
-
[20, 30, 40].each do |a|
|
|
24
|
-
assert results.any? {|r| r['a'] == a}, "Could not find record for a => #{a}"
|
|
25
|
-
end
|
|
26
|
-
|
|
27
|
-
puts "Please disconnect the current master."
|
|
28
|
-
gets
|
|
29
|
-
|
|
30
|
-
results = []
|
|
31
|
-
rescue_connection_failure do
|
|
32
|
-
@coll.find.each {|r| results << r}
|
|
33
|
-
[20, 30, 40].each do |a|
|
|
34
|
-
assert results.any? {|r| r['a'] == a}, "Could not find record for a => #{a}"
|
|
35
|
-
end
|
|
36
|
-
end
|
|
37
|
-
end
|
|
38
|
-
|
|
39
|
-
end
|