mongodb-mongo 0.12 → 0.13
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/README.rdoc +12 -12
- data/Rakefile +1 -1
- data/bin/bson_benchmark.rb +1 -1
- data/bin/mongo_console +3 -3
- data/bin/run_test_script +2 -2
- data/bin/standard_benchmark +3 -3
- data/examples/admin.rb +3 -3
- data/examples/benchmarks.rb +2 -2
- data/examples/blog.rb +4 -4
- data/examples/capped.rb +3 -3
- data/examples/cursor.rb +3 -3
- data/examples/gridfs.rb +4 -4
- data/examples/index_test.rb +11 -11
- data/examples/info.rb +3 -3
- data/examples/queries.rb +3 -3
- data/examples/simple.rb +3 -3
- data/examples/strict.rb +3 -3
- data/examples/types.rb +4 -9
- data/lib/mongo.rb +35 -3
- data/lib/mongo/admin.rb +56 -60
- data/lib/mongo/collection.rb +368 -320
- data/lib/mongo/connection.rb +166 -0
- data/lib/mongo/cursor.rb +206 -209
- data/lib/mongo/db.rb +478 -489
- data/lib/mongo/errors.rb +8 -9
- data/lib/mongo/gridfs/chunk.rb +66 -70
- data/lib/mongo/gridfs/grid_store.rb +406 -410
- data/lib/mongo/message/get_more_message.rb +8 -13
- data/lib/mongo/message/insert_message.rb +7 -11
- data/lib/mongo/message/kill_cursors_message.rb +7 -12
- data/lib/mongo/message/message.rb +58 -62
- data/lib/mongo/message/message_header.rb +19 -24
- data/lib/mongo/message/msg_message.rb +5 -9
- data/lib/mongo/message/opcodes.rb +10 -15
- data/lib/mongo/message/query_message.rb +42 -46
- data/lib/mongo/message/remove_message.rb +8 -12
- data/lib/mongo/message/update_message.rb +9 -13
- data/lib/mongo/query.rb +84 -88
- data/lib/mongo/types/binary.rb +13 -17
- data/lib/mongo/types/code.rb +9 -13
- data/lib/mongo/types/dbref.rb +10 -14
- data/lib/mongo/types/objectid.rb +103 -107
- data/lib/mongo/types/regexp_of_holding.rb +18 -22
- data/lib/mongo/types/undefined.rb +7 -10
- data/lib/mongo/util/bson.rb +4 -9
- data/lib/mongo/util/xml_to_ruby.rb +1 -3
- data/mongo-ruby-driver.gemspec +33 -32
- data/{tests → test}/mongo-qa/_common.rb +1 -1
- data/{tests → test}/mongo-qa/admin +1 -1
- data/{tests → test}/mongo-qa/capped +1 -1
- data/{tests → test}/mongo-qa/count1 +4 -4
- data/{tests → test}/mongo-qa/dbs +1 -1
- data/{tests → test}/mongo-qa/find +1 -1
- data/{tests → test}/mongo-qa/find1 +1 -1
- data/{tests → test}/mongo-qa/gridfs_in +2 -2
- data/{tests → test}/mongo-qa/gridfs_out +2 -2
- data/{tests → test}/mongo-qa/indices +2 -2
- data/{tests → test}/mongo-qa/remove +1 -1
- data/{tests → test}/mongo-qa/stress1 +1 -1
- data/{tests → test}/mongo-qa/test1 +1 -1
- data/{tests → test}/mongo-qa/update +1 -1
- data/{tests → test}/test_admin.rb +3 -3
- data/{tests → test}/test_bson.rb +4 -4
- data/{tests → test}/test_byte_buffer.rb +0 -0
- data/{tests → test}/test_chunk.rb +4 -4
- data/{tests → test}/test_collection.rb +42 -4
- data/{tests/test_mongo.rb → test/test_connection.rb} +35 -11
- data/test/test_cursor.rb +223 -0
- data/{tests → test}/test_db.rb +12 -12
- data/{tests → test}/test_db_api.rb +28 -33
- data/{tests → test}/test_db_connection.rb +3 -3
- data/{tests → test}/test_grid_store.rb +4 -4
- data/{tests → test}/test_message.rb +1 -1
- data/{tests → test}/test_objectid.rb +3 -3
- data/{tests → test}/test_ordered_hash.rb +0 -0
- data/{tests → test}/test_round_trip.rb +6 -2
- data/{tests → test}/test_threading.rb +3 -3
- data/test/test_xgen.rb +73 -0
- metadata +33 -32
- data/lib/mongo/mongo.rb +0 -164
- data/tests/test_cursor.rb +0 -121
@@ -4,11 +4,10 @@ require 'test/unit'
|
|
4
4
|
|
5
5
|
# NOTE: assumes Mongo is running
|
6
6
|
class DBAPITest < Test::Unit::TestCase
|
7
|
-
include
|
8
|
-
include XGen::Mongo::Driver
|
7
|
+
include Mongo
|
9
8
|
|
10
|
-
@@db =
|
11
|
-
|
9
|
+
@@db = Connection.new(ENV['MONGO_RUBY_DRIVER_HOST'] || 'localhost',
|
10
|
+
ENV['MONGO_RUBY_DRIVER_PORT'] || Connection::DEFAULT_PORT).db('ruby-mongo-test')
|
12
11
|
@@coll = @@db.collection('test')
|
13
12
|
|
14
13
|
def setup
|
@@ -501,25 +500,18 @@ class DBAPITest < Test::Unit::TestCase
|
|
501
500
|
cursor = @@coll.find()
|
502
501
|
rows = cursor.to_a
|
503
502
|
|
504
|
-
|
505
|
-
|
506
|
-
|
507
|
-
|
508
|
-
# Make sure we can still iterate after calling to_a
|
509
|
-
rows_with_each = cursor.collect{|row| row}
|
510
|
-
assert_equal rows, rows_with_each
|
503
|
+
assert_raise InvalidOperation do
|
504
|
+
cursor.to_a
|
505
|
+
end
|
511
506
|
|
512
|
-
|
507
|
+
cursor.each { |doc| fail "should be no docs in each now" }
|
513
508
|
end
|
514
509
|
|
515
510
|
def test_to_a_after_each
|
516
511
|
cursor = @@coll.find
|
517
512
|
cursor.each { |row| row }
|
518
|
-
|
513
|
+
assert_raise InvalidOperation do
|
519
514
|
cursor.to_a
|
520
|
-
fail "expected \"can't call\" error"
|
521
|
-
rescue => ex
|
522
|
-
assert_equal "can't call Cursor#to_a after calling Cursor#each", ex.to_s
|
523
515
|
end
|
524
516
|
end
|
525
517
|
|
@@ -536,8 +528,8 @@ class DBAPITest < Test::Unit::TestCase
|
|
536
528
|
@@coll.insert('a' => 3)
|
537
529
|
|
538
530
|
assert_equal 3, @@coll.count
|
539
|
-
assert_equal 1, @@coll.
|
540
|
-
assert_equal 2, @@coll.
|
531
|
+
assert_equal 1, @@coll.find('$where' => Code.new('this.a > 2')).count()
|
532
|
+
assert_equal 2, @@coll.find('$where' => Code.new('this.a > i', {'i' => 1})).count()
|
541
533
|
end
|
542
534
|
|
543
535
|
def test_eval
|
@@ -554,7 +546,7 @@ class DBAPITest < Test::Unit::TestCase
|
|
554
546
|
assert_equal 2, @@db.eval(Code.new("return i;", {"i" => 2}))
|
555
547
|
assert_equal 5, @@db.eval(Code.new("i + 3;", {"i" => 2}))
|
556
548
|
|
557
|
-
assert_raise
|
549
|
+
assert_raise OperationFailure do
|
558
550
|
@@db.eval("5 ++ 5;")
|
559
551
|
end
|
560
552
|
end
|
@@ -600,13 +592,30 @@ class DBAPITest < Test::Unit::TestCase
|
|
600
592
|
test = @@db.collection("test")
|
601
593
|
|
602
594
|
assert_equal [], test.group([], {}, {"count" => 0}, "function (obj, prev) { prev.count++; }")
|
595
|
+
assert_equal [], test.group([], {}, {"count" => 0}, "function (obj, prev) { prev.count++; }", true)
|
603
596
|
|
604
597
|
test.insert("a" => 2)
|
605
598
|
test.insert("b" => 5)
|
606
599
|
test.insert("a" => 1)
|
607
600
|
|
608
601
|
assert_equal 3, test.group([], {}, {"count" => 0}, "function (obj, prev) { prev.count++; }")[0]["count"]
|
602
|
+
assert_equal 3, test.group([], {}, {"count" => 0}, "function (obj, prev) { prev.count++; }", true)[0]["count"]
|
609
603
|
assert_equal 1, test.group([], {"a" => {"$gt" => 1}}, {"count" => 0}, "function (obj, prev) { prev.count++; }")[0]["count"]
|
604
|
+
assert_equal 1, test.group([], {"a" => {"$gt" => 1}}, {"count" => 0}, "function (obj, prev) { prev.count++; }", true)[0]["count"]
|
605
|
+
|
606
|
+
test.insert("a" => 2, "b" => 3)
|
607
|
+
expected = [{"a" => 2, "count" => 2},
|
608
|
+
{"a" => nil, "count" => 1},
|
609
|
+
{"a" => 1, "count" => 1}]
|
610
|
+
assert_equal expected, test.group(["a"], {}, {"count" => 0}, "function (obj, prev) { prev.count++; }")
|
611
|
+
assert_equal expected, test.group(["a"], {}, {"count" => 0}, "function (obj, prev) { prev.count++; }", true)
|
612
|
+
|
613
|
+
assert_raise OperationFailure do
|
614
|
+
test.group([], {}, {}, "5 ++ 5")
|
615
|
+
end
|
616
|
+
assert_raise OperationFailure do
|
617
|
+
test.group([], {}, {}, "5 ++ 5", true)
|
618
|
+
end
|
610
619
|
end
|
611
620
|
|
612
621
|
def test_deref
|
@@ -800,18 +809,4 @@ class DBAPITest < Test::Unit::TestCase
|
|
800
809
|
@@db.collection("test").find({}, :snapshot => true, :sort => 'a').to_a
|
801
810
|
end
|
802
811
|
end
|
803
|
-
|
804
|
-
# TODO this test fails with error message "Undefed Before end of object"
|
805
|
-
# That is a database error. The undefined type may go away.
|
806
|
-
|
807
|
-
# def test_insert_undefined
|
808
|
-
# doc = {'undef' => Undefined.new}
|
809
|
-
# @@coll.clear
|
810
|
-
# @@coll.insert(doc)
|
811
|
-
# p @@db.error # DEBUG
|
812
|
-
# assert_equal 1, @@coll.count
|
813
|
-
# row = @@coll.find().next_object
|
814
|
-
# assert_not_nil row
|
815
|
-
# end
|
816
|
-
|
817
812
|
end
|
@@ -5,12 +5,12 @@ require 'test/unit'
|
|
5
5
|
# NOTE: assumes Mongo is running
|
6
6
|
class DBConnectionTest < Test::Unit::TestCase
|
7
7
|
|
8
|
-
include
|
8
|
+
include Mongo
|
9
9
|
|
10
10
|
def test_no_exceptions
|
11
11
|
host = ENV['MONGO_RUBY_DRIVER_HOST'] || 'localhost'
|
12
|
-
port = ENV['MONGO_RUBY_DRIVER_PORT'] ||
|
13
|
-
db =
|
12
|
+
port = ENV['MONGO_RUBY_DRIVER_PORT'] || Connection::DEFAULT_PORT
|
13
|
+
db = Connection.new(host, port).db('ruby-mongo-demo')
|
14
14
|
coll = db.collection('test')
|
15
15
|
coll.clear
|
16
16
|
db.error
|
@@ -5,11 +5,11 @@ require 'mongo/gridfs'
|
|
5
5
|
|
6
6
|
class GridStoreTest < Test::Unit::TestCase
|
7
7
|
|
8
|
-
include
|
9
|
-
include
|
8
|
+
include Mongo
|
9
|
+
include GridFS
|
10
10
|
|
11
|
-
@@db =
|
12
|
-
|
11
|
+
@@db = Connection.new(ENV['MONGO_RUBY_DRIVER_HOST'] || 'localhost',
|
12
|
+
ENV['MONGO_RUBY_DRIVER_PORT'] || Connection::DEFAULT_PORT).db('ruby-mongo-test')
|
13
13
|
@@files = @@db.collection('fs.files')
|
14
14
|
@@chunks = @@db.collection('fs.chunks')
|
15
15
|
|
@@ -4,7 +4,7 @@ require 'test/unit'
|
|
4
4
|
|
5
5
|
class ObjectIDTest < Test::Unit::TestCase
|
6
6
|
|
7
|
-
include
|
7
|
+
include Mongo
|
8
8
|
|
9
9
|
def setup
|
10
10
|
@t = 42
|
@@ -53,8 +53,8 @@ class ObjectIDTest < Test::Unit::TestCase
|
|
53
53
|
|
54
54
|
def test_save_and_restore
|
55
55
|
host = ENV['MONGO_RUBY_DRIVER_HOST'] || 'localhost'
|
56
|
-
port = ENV['MONGO_RUBY_DRIVER_PORT'] ||
|
57
|
-
db =
|
56
|
+
port = ENV['MONGO_RUBY_DRIVER_PORT'] || Connection::DEFAULT_PORT
|
57
|
+
db = Connection.new(host, port).db('ruby-mongo-test')
|
58
58
|
coll = db.collection('test')
|
59
59
|
|
60
60
|
coll.clear
|
File without changes
|
@@ -13,7 +13,7 @@ require 'test/unit'
|
|
13
13
|
# of this project), then we find the BSON test files there and use those, too.
|
14
14
|
class RoundTripTest < Test::Unit::TestCase
|
15
15
|
|
16
|
-
include
|
16
|
+
include Mongo
|
17
17
|
|
18
18
|
@@ruby = nil
|
19
19
|
|
@@ -64,7 +64,11 @@ EOS
|
|
64
64
|
# generated)
|
65
65
|
def one_round_trip(dir, name)
|
66
66
|
obj = File.open(File.join(dir, "#{name}.xson")) { |f|
|
67
|
-
|
67
|
+
begin
|
68
|
+
XMLToRuby.new.xml_to_ruby(f)
|
69
|
+
rescue => ex # unsupported type
|
70
|
+
return
|
71
|
+
end
|
68
72
|
}
|
69
73
|
|
70
74
|
File.open(File.join(dir, "#{name}.bson"), 'rb') { |f|
|
@@ -4,11 +4,11 @@ require 'test/unit'
|
|
4
4
|
|
5
5
|
class TestThreading < Test::Unit::TestCase
|
6
6
|
|
7
|
-
include
|
7
|
+
include Mongo
|
8
8
|
|
9
9
|
@@host = ENV['MONGO_RUBY_DRIVER_HOST'] || 'localhost'
|
10
|
-
@@port = ENV['MONGO_RUBY_DRIVER_PORT'] ||
|
11
|
-
@@db =
|
10
|
+
@@port = ENV['MONGO_RUBY_DRIVER_PORT'] || Connection::DEFAULT_PORT
|
11
|
+
@@db = Connection.new(@@host, @@port).db('ruby-mongo-test')
|
12
12
|
@@coll = @@db.collection('thread-test-collection')
|
13
13
|
|
14
14
|
def test_threading
|
data/test/test_xgen.rb
ADDED
@@ -0,0 +1,73 @@
|
|
1
|
+
# Copyright (C) 2009 10gen Inc.
|
2
|
+
#
|
3
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
4
|
+
# you may not use this file except in compliance with the License.
|
5
|
+
# You may obtain a copy of the License at
|
6
|
+
#
|
7
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
8
|
+
#
|
9
|
+
# Unless required by applicable law or agreed to in writing, software
|
10
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
11
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
12
|
+
# See the License for the specific language governing permissions and
|
13
|
+
# limitations under the License.
|
14
|
+
|
15
|
+
$LOAD_PATH[0,0] = File.join(File.dirname(__FILE__), '..', 'lib')
|
16
|
+
require 'mongo'
|
17
|
+
require 'test/unit'
|
18
|
+
|
19
|
+
# TODO these tests should be removed - just testing for the deprecated
|
20
|
+
# XGen::Mongo::Driver include path
|
21
|
+
class TestXGen < Test::Unit::TestCase
|
22
|
+
@@db = XGen::Mongo::Driver::Connection.new(ENV['MONGO_RUBY_DRIVER_HOST'] || 'localhost',
|
23
|
+
ENV['MONGO_RUBY_DRIVER_PORT'] || XGen::Mongo::Driver::Connection::DEFAULT_PORT).db('ruby-mongo-test')
|
24
|
+
@@test = @@db.collection('test')
|
25
|
+
|
26
|
+
def setup
|
27
|
+
@@test.clear
|
28
|
+
end
|
29
|
+
|
30
|
+
def test_sort
|
31
|
+
@@test.save('x' => 2)
|
32
|
+
@@test.save('x' => 1)
|
33
|
+
@@test.save('x' => 3)
|
34
|
+
|
35
|
+
assert_equal 1, @@test.find({}, :sort => {'x' => XGen::Mongo::ASCENDING}).to_a()[0]['x']
|
36
|
+
assert_equal 3, @@test.find({}, :sort => {'x' => XGen::Mongo::DESCENDING}).to_a()[0]['x']
|
37
|
+
end
|
38
|
+
|
39
|
+
def test_gridfs
|
40
|
+
XGen::Mongo::GridFS::GridStore.open(@@db, 'foobar', 'w') { |f| f.write('hello world!') }
|
41
|
+
assert XGen::Mongo::GridFS::GridStore.exist?(@@db, 'foobar')
|
42
|
+
assert !XGen::Mongo::GridFS::GridStore.exist?(@@db, 'mike')
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
class TestXGenInclude < Test::Unit::TestCase
|
47
|
+
include XGen::Mongo::GridFS
|
48
|
+
include XGen::Mongo::Driver
|
49
|
+
include XGen::Mongo
|
50
|
+
|
51
|
+
@@db = Connection.new(ENV['MONGO_RUBY_DRIVER_HOST'] || 'localhost',
|
52
|
+
ENV['MONGO_RUBY_DRIVER_PORT'] || Connection::DEFAULT_PORT).db('ruby-mongo-test')
|
53
|
+
@@test = @@db.collection('test')
|
54
|
+
|
55
|
+
def setup
|
56
|
+
@@test.clear
|
57
|
+
end
|
58
|
+
|
59
|
+
def test_sort
|
60
|
+
@@test.save('x' => 2)
|
61
|
+
@@test.save('x' => 1)
|
62
|
+
@@test.save('x' => 3)
|
63
|
+
|
64
|
+
assert_equal 1, @@test.find({}, :sort => {'x' => ASCENDING}).to_a()[0]['x']
|
65
|
+
assert_equal 3, @@test.find({}, :sort => {'x' => DESCENDING}).to_a()[0]['x']
|
66
|
+
end
|
67
|
+
|
68
|
+
def test_gridfs
|
69
|
+
GridStore.open(@@db, 'foobar', 'w') { |f| f.write('hello world!') }
|
70
|
+
assert GridStore.exist?(@@db, 'foobar')
|
71
|
+
assert !GridStore.exist?(@@db, 'mike')
|
72
|
+
end
|
73
|
+
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: mongodb-mongo
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: "0.
|
4
|
+
version: "0.13"
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Jim Menard
|
@@ -44,6 +44,7 @@ files:
|
|
44
44
|
- examples/types.rb
|
45
45
|
- lib/mongo/admin.rb
|
46
46
|
- lib/mongo/collection.rb
|
47
|
+
- lib/mongo/connection.rb
|
47
48
|
- lib/mongo/cursor.rb
|
48
49
|
- lib/mongo/db.rb
|
49
50
|
- lib/mongo/gridfs/chunk.rb
|
@@ -61,7 +62,6 @@ files:
|
|
61
62
|
- lib/mongo/message/remove_message.rb
|
62
63
|
- lib/mongo/message/update_message.rb
|
63
64
|
- lib/mongo/message.rb
|
64
|
-
- lib/mongo/mongo.rb
|
65
65
|
- lib/mongo/query.rb
|
66
66
|
- lib/mongo/types/binary.rb
|
67
67
|
- lib/mongo/types/code.rb
|
@@ -104,33 +104,34 @@ signing_key:
|
|
104
104
|
specification_version: 2
|
105
105
|
summary: Ruby driver for the 10gen Mongo DB
|
106
106
|
test_files:
|
107
|
-
-
|
108
|
-
-
|
109
|
-
-
|
110
|
-
-
|
111
|
-
-
|
112
|
-
-
|
113
|
-
-
|
114
|
-
-
|
115
|
-
-
|
116
|
-
-
|
117
|
-
-
|
118
|
-
-
|
119
|
-
-
|
120
|
-
-
|
121
|
-
-
|
122
|
-
-
|
123
|
-
-
|
124
|
-
-
|
125
|
-
-
|
126
|
-
-
|
127
|
-
-
|
128
|
-
-
|
129
|
-
-
|
130
|
-
-
|
131
|
-
-
|
132
|
-
-
|
133
|
-
-
|
134
|
-
-
|
135
|
-
-
|
136
|
-
-
|
107
|
+
- test/mongo-qa/_common.rb
|
108
|
+
- test/mongo-qa/admin
|
109
|
+
- test/mongo-qa/capped
|
110
|
+
- test/mongo-qa/count1
|
111
|
+
- test/mongo-qa/dbs
|
112
|
+
- test/mongo-qa/find
|
113
|
+
- test/mongo-qa/find1
|
114
|
+
- test/mongo-qa/gridfs_in
|
115
|
+
- test/mongo-qa/gridfs_out
|
116
|
+
- test/mongo-qa/indices
|
117
|
+
- test/mongo-qa/remove
|
118
|
+
- test/mongo-qa/stress1
|
119
|
+
- test/mongo-qa/test1
|
120
|
+
- test/mongo-qa/update
|
121
|
+
- test/test_admin.rb
|
122
|
+
- test/test_bson.rb
|
123
|
+
- test/test_byte_buffer.rb
|
124
|
+
- test/test_chunk.rb
|
125
|
+
- test/test_collection.rb
|
126
|
+
- test/test_connection.rb
|
127
|
+
- test/test_cursor.rb
|
128
|
+
- test/test_db.rb
|
129
|
+
- test/test_db_api.rb
|
130
|
+
- test/test_db_connection.rb
|
131
|
+
- test/test_grid_store.rb
|
132
|
+
- test/test_message.rb
|
133
|
+
- test/test_objectid.rb
|
134
|
+
- test/test_ordered_hash.rb
|
135
|
+
- test/test_threading.rb
|
136
|
+
- test/test_round_trip.rb
|
137
|
+
- test/test_xgen.rb
|
data/lib/mongo/mongo.rb
DELETED
@@ -1,164 +0,0 @@
|
|
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/db'
|
18
|
-
|
19
|
-
module XGen
|
20
|
-
module Mongo
|
21
|
-
module Driver
|
22
|
-
|
23
|
-
# Represents a Mongo database server.
|
24
|
-
class Mongo
|
25
|
-
|
26
|
-
DEFAULT_PORT = 27017
|
27
|
-
|
28
|
-
# Create a Mongo database server instance. You specify either one or a
|
29
|
-
# pair of servers. If one, you also say if connecting to a slave is
|
30
|
-
# OK. In either case, the host default is "localhost" and port default
|
31
|
-
# is DEFAULT_PORT.
|
32
|
-
#
|
33
|
-
# If you specify a pair, pair_or_host is a hash with two keys :left
|
34
|
-
# and :right. Each key maps to either
|
35
|
-
# * a server name, in which case port is DEFAULT_PORT
|
36
|
-
# * a port number, in which case server is "localhost"
|
37
|
-
# * an array containing a server name and a port number in that order
|
38
|
-
#
|
39
|
-
# +options+ are passed on to each DB instance:
|
40
|
-
#
|
41
|
-
# :slave_ok :: Only used if one host is specified. If false, when
|
42
|
-
# connecting to that host/port a DB object will check to
|
43
|
-
# see if the server is the master. If it is not, an error
|
44
|
-
# is thrown.
|
45
|
-
#
|
46
|
-
# :auto_reconnect :: If a DB connection gets closed (for example, we
|
47
|
-
# have a server pair and saw the "not master"
|
48
|
-
# error, which closes the connection), then
|
49
|
-
# automatically try to reconnect to the master or
|
50
|
-
# to the single server we have been given. Defaults
|
51
|
-
# to +false+.
|
52
|
-
#
|
53
|
-
# Since that's so confusing, here are a few examples:
|
54
|
-
#
|
55
|
-
# Mongo.new # localhost, DEFAULT_PORT, !slave
|
56
|
-
# Mongo.new("localhost") # localhost, DEFAULT_PORT, !slave
|
57
|
-
# Mongo.new("localhost", 3000) # localhost, 3000, slave not ok
|
58
|
-
# # localhost, 3000, slave ok
|
59
|
-
# Mongo.new("localhost", 3000, :slave_ok => true)
|
60
|
-
# # localhost, DEFAULT_PORT, auto reconnect
|
61
|
-
# Mongo.new(nil, nil, :auto_reconnect => true)
|
62
|
-
#
|
63
|
-
# # A pair of servers. DB will always talk to the master. On socket
|
64
|
-
# # error or "not master" error, we will auto-reconnect to the
|
65
|
-
# # current master.
|
66
|
-
# Mongo.new({:left => ["db1.example.com", 3000],
|
67
|
-
# :right => "db2.example.com"}, # DEFAULT_PORT
|
68
|
-
# nil, :auto_reconnect => true)
|
69
|
-
#
|
70
|
-
# # Here, :right is localhost/DEFAULT_PORT. No auto-reconnect.
|
71
|
-
# Mongo.new({:left => ["db1.example.com", 3000]})
|
72
|
-
#
|
73
|
-
# When a DB object first connects to a pair, it will find the master
|
74
|
-
# instance and connect to that one.
|
75
|
-
def initialize(pair_or_host=nil, port=nil, options={})
|
76
|
-
@pair = case pair_or_host
|
77
|
-
when String
|
78
|
-
[[pair_or_host, port ? port.to_i : DEFAULT_PORT]]
|
79
|
-
when Hash
|
80
|
-
connections = []
|
81
|
-
connections << pair_val_to_connection(pair_or_host[:left])
|
82
|
-
connections << pair_val_to_connection(pair_or_host[:right])
|
83
|
-
connections
|
84
|
-
when nil
|
85
|
-
[['localhost', DEFAULT_PORT]]
|
86
|
-
end
|
87
|
-
@options = options
|
88
|
-
end
|
89
|
-
|
90
|
-
# Return the XGen::Mongo::Driver::DB named +db_name+. The slave_ok and
|
91
|
-
# auto_reconnect options passed in via #new may be overridden here.
|
92
|
-
# See DB#new for other options you can pass in.
|
93
|
-
def db(db_name, options={})
|
94
|
-
XGen::Mongo::Driver::DB.new(db_name, @pair, @options.merge(options))
|
95
|
-
end
|
96
|
-
|
97
|
-
# Returns a hash containing database names as keys and disk space for
|
98
|
-
# each as values.
|
99
|
-
def database_info
|
100
|
-
doc = single_db_command('admin', :listDatabases => 1)
|
101
|
-
h = {}
|
102
|
-
doc['databases'].each { |db|
|
103
|
-
h[db['name']] = db['sizeOnDisk'].to_i
|
104
|
-
}
|
105
|
-
h
|
106
|
-
end
|
107
|
-
|
108
|
-
# Returns an array of database names.
|
109
|
-
def database_names
|
110
|
-
database_info.keys
|
111
|
-
end
|
112
|
-
|
113
|
-
# Not implemented.
|
114
|
-
def clone_database(from)
|
115
|
-
raise "not implemented"
|
116
|
-
end
|
117
|
-
|
118
|
-
# Not implemented.
|
119
|
-
def copy_database(from_host, from_db, to_db)
|
120
|
-
raise "not implemented"
|
121
|
-
end
|
122
|
-
|
123
|
-
# Drops the database +name+.
|
124
|
-
def drop_database(name)
|
125
|
-
single_db_command(name, :dropDatabase => 1)
|
126
|
-
end
|
127
|
-
|
128
|
-
protected
|
129
|
-
|
130
|
-
# Turns an array containing a host name string and a
|
131
|
-
# port number integer into a [host, port] pair array.
|
132
|
-
def pair_val_to_connection(a)
|
133
|
-
case a
|
134
|
-
when nil
|
135
|
-
['localhost', DEFAULT_PORT]
|
136
|
-
when String
|
137
|
-
[a, DEFAULT_PORT]
|
138
|
-
when Integer
|
139
|
-
['localhost', a]
|
140
|
-
when Array
|
141
|
-
a
|
142
|
-
end
|
143
|
-
end
|
144
|
-
|
145
|
-
# Send cmd (a hash, possibly ordered) to the admin database and return
|
146
|
-
# the answer. Raises an error unless the return is "ok" (DB#ok?
|
147
|
-
# returns +true+).
|
148
|
-
def single_db_command(db_name, cmd)
|
149
|
-
db = nil
|
150
|
-
begin
|
151
|
-
db = db(db_name)
|
152
|
-
doc = db.db_command(cmd)
|
153
|
-
raise "error retrieving database info: #{doc.inspect}" unless db.ok?(doc)
|
154
|
-
doc
|
155
|
-
ensure
|
156
|
-
db.close if db
|
157
|
-
end
|
158
|
-
end
|
159
|
-
|
160
|
-
end
|
161
|
-
end
|
162
|
-
end
|
163
|
-
end
|
164
|
-
|