monga 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (42) hide show
  1. data/.gitignore +17 -0
  2. data/Gemfile +9 -0
  3. data/LICENSE.txt +22 -0
  4. data/README.md +110 -0
  5. data/Rakefile +13 -0
  6. data/lib/monga/client.rb +8 -0
  7. data/lib/monga/clients/client.rb +24 -0
  8. data/lib/monga/clients/master_slave_client.rb +5 -0
  9. data/lib/monga/clients/replica_set_client.rb +101 -0
  10. data/lib/monga/collection.rb +108 -0
  11. data/lib/monga/connection.rb +23 -0
  12. data/lib/monga/connection_pool.rb +38 -0
  13. data/lib/monga/connections/em_connection.rb +152 -0
  14. data/lib/monga/connections/primary.rb +46 -0
  15. data/lib/monga/connections/secondary.rb +13 -0
  16. data/lib/monga/cursor.rb +175 -0
  17. data/lib/monga/database.rb +97 -0
  18. data/lib/monga/exceptions.rb +9 -0
  19. data/lib/monga/miner.rb +72 -0
  20. data/lib/monga/request.rb +122 -0
  21. data/lib/monga/requests/delete.rb +23 -0
  22. data/lib/monga/requests/get_more.rb +19 -0
  23. data/lib/monga/requests/insert.rb +29 -0
  24. data/lib/monga/requests/kill_cursors.rb +25 -0
  25. data/lib/monga/requests/query.rb +49 -0
  26. data/lib/monga/requests/update.rb +25 -0
  27. data/lib/monga/response.rb +11 -0
  28. data/lib/monga.rb +30 -0
  29. data/monga.gemspec +26 -0
  30. data/spec/helpers/mongodb.rb +59 -0
  31. data/spec/helpers/truncate.rb +15 -0
  32. data/spec/monga/collection_spec.rb +448 -0
  33. data/spec/monga/connection_pool_spec.rb +50 -0
  34. data/spec/monga/connection_spec.rb +64 -0
  35. data/spec/monga/cursor_spec.rb +186 -0
  36. data/spec/monga/database_spec.rb +67 -0
  37. data/spec/monga/replica_set_client_spec.rb +46 -0
  38. data/spec/monga/requests/delete_spec.rb +0 -0
  39. data/spec/monga/requests/insert_spec.rb +0 -0
  40. data/spec/monga/requests/query_spec.rb +28 -0
  41. data/spec/spec_helper.rb +29 -0
  42. metadata +185 -0
@@ -0,0 +1,186 @@
1
+ require 'spec_helper'
2
+
3
+ describe Monga::Cursor do
4
+ include Helpers::Truncate
5
+
6
+ describe "simple ops" do
7
+ before do
8
+ EM.run do
9
+ req = COLLECTION.safe_insert([
10
+ { author: "Madonna", title: "Burning Up" },
11
+ { author: "Madonna", title: "Freezing" },
12
+ { author: "Madonna", title: "Untitled Track 1" },
13
+ { author: "Madonna", title: "Untitled Track 2" },
14
+ { author: "Madonna", title: "Untitled Track 3" },
15
+ { author: "Madonna", title: "Untitled Track 4" },
16
+ { author: "Madonna", title: "Untitled Track 5" },
17
+ { author: "Radiohead", title: "Karma Police" },
18
+ ])
19
+ req.callback{ EM.stop }
20
+ req.errback{ |err| raise err }
21
+ end
22
+ end
23
+
24
+ it "should return one item" do
25
+ EM.run do
26
+ cursor = Monga::Cursor.new(DB, COLLECTION.name, { query: { author: "Madonna" }, limit: 1 }, {})
27
+ docs = []
28
+ cursor.each_doc do |doc|
29
+ docs << doc
30
+ end
31
+ cursor.callback do
32
+ docs.first["title"].must_equal "Burning Up"
33
+ EM.stop
34
+ end
35
+ cursor.errback{ |err| raise err }
36
+ end
37
+ end
38
+
39
+ it "should return two items" do
40
+ EM.run do
41
+ cursor = Monga::Cursor.new(DB, COLLECTION.name, { query: { author: "Madonna" }, limit: 2 }, {})
42
+ docs = []
43
+ cursor.each_doc do |doc|
44
+ docs << doc
45
+ end
46
+ cursor.callback do
47
+ docs.size.must_equal 2
48
+ docs.first["title"].must_equal "Burning Up"
49
+ docs.last["title"].must_equal "Freezing"
50
+ EM.stop
51
+ end
52
+ cursor.errback{ |err| raise err }
53
+ end
54
+ end
55
+
56
+ it "should skip two items" do
57
+ EM.run do
58
+ cursor = Monga::Cursor.new(DB, COLLECTION.name, { query: { author: "Madonna" }, limit: 2, skip: 2 })
59
+ docs = []
60
+ cursor.each_doc do |doc|
61
+ docs << doc
62
+ end
63
+ cursor.callback do
64
+ docs.size.must_equal 2
65
+ docs.first["title"].must_equal "Untitled Track 1"
66
+ docs.last["title"].must_equal "Untitled Track 2"
67
+ EM.stop
68
+ end
69
+ cursor.errback{ |err| raise err }
70
+ end
71
+ end
72
+
73
+ it "should select all" do
74
+ EM.run do
75
+ cursor = Monga::Cursor.new(DB, COLLECTION.name, { query: { author: "Madonna" } })
76
+ docs = []
77
+ cursor.each_doc do |doc|
78
+ docs << doc
79
+ end
80
+ cursor.callback do
81
+ docs.size.must_equal 7
82
+ docs.all?{ |doc| doc["author"] == "Madonna" }.must_equal true
83
+ EM.stop
84
+ end
85
+ cursor.errback{ |err| raise err }
86
+ end
87
+ end
88
+
89
+ it "should select all with batch_size option" do
90
+ EM.run do
91
+ cursor = Monga::Cursor.new(DB, COLLECTION.name, { query: { author: "Madonna" }, batch_size: 2 })
92
+ docs = []
93
+ cursor.each_doc do |doc|
94
+ docs << doc
95
+ end
96
+ cursor.callback do |res|
97
+ docs.size.must_equal 7
98
+ docs.all?{ |doc| doc["author"] == "Madonna" }.must_equal true
99
+ EM.stop
100
+ end
101
+ cursor.errback{ |err| raise err }
102
+ end
103
+ end
104
+
105
+ it "should select LIMIT < max with batch_size option" do
106
+ EM.run do
107
+ cursor = Monga::Cursor.new(DB, COLLECTION.name, { query: { author: "Madonna" }, batch_size: 2, limit: 5 })
108
+ docs = []
109
+ cursor.each_doc do |doc|
110
+ docs << doc
111
+ end
112
+ cursor.callback do
113
+ docs.size.must_equal 5
114
+ docs.all?{ |doc| doc["author"] == "Madonna" }.must_equal true
115
+ EM.stop
116
+ end
117
+ cursor.errback{ |err| raise err }
118
+ end
119
+ end
120
+
121
+ it "should select LIMIT > max with batch_size option" do
122
+ EM.run do
123
+ cursor = Monga::Cursor.new(DB, COLLECTION.name, { query: { author: "Madonna" }, batch_size: 2, limit: 15 })
124
+ docs = []
125
+ cursor.each_doc do |doc|
126
+ docs << doc
127
+ end
128
+ cursor.callback do
129
+ docs.size.must_equal 7
130
+ docs.all?{ |doc| doc["author"] == "Madonna" }.must_equal true
131
+ EM.stop
132
+ end
133
+ cursor.errback{ |err| raise err }
134
+ end
135
+ end
136
+
137
+ it "should kill cursor" do
138
+ EM.run do
139
+ cursor = Monga::Cursor.new(DB, COLLECTION.name, { query: { author: "Madonna" }, batch_size: 2, limit: 15 })
140
+ cursor.next_document.callback do |doc|
141
+ cursor.next_document.callback do |doc|
142
+ DB.cmd(cursorInfo: 1).callback do |resp|
143
+ resp.first["totalOpen"].must_equal 1
144
+ cursor.kill
145
+ DB.cmd(cursorInfo: 1).callback do |resp|
146
+ resp.first["totalOpen"].must_equal 0
147
+ req = cursor.next_document
148
+ req.callback do |resp|
149
+ fail "never called"
150
+ end
151
+ req.errback do |err|
152
+ err.class.must_equal Monga::Exceptions::CursorIsClosed
153
+ EM.stop
154
+ end
155
+ end
156
+ end
157
+ end
158
+ end
159
+ end
160
+ end
161
+
162
+ it "should kill marked cursors" do
163
+ EM.run do
164
+ cursor = Monga::Cursor.new(DB, COLLECTION.name, { query: { author: "Madonna" }, batch_size: 2, limit: 15 })
165
+ req = cursor.next_document
166
+ req.errback{ |err| raise err }
167
+ req.callback do
168
+ req = DB.cmd(cursorInfo: 1)
169
+ req.errback{ |err| raise err }
170
+ req.callback do |resp|
171
+ cursor.mark_to_kill
172
+ resp.first["totalOpen"].must_equal 1
173
+ EM.add_timer(2) do
174
+ req = DB.cmd(cursorInfo: 1)
175
+ req.errback{ |err| raise err }
176
+ req.callback do |resp|
177
+ resp.first["totalOpen"].must_equal 0
178
+ EM.stop
179
+ end
180
+ end
181
+ end
182
+ end
183
+ end
184
+ end
185
+ end
186
+ end
@@ -0,0 +1,67 @@
1
+ require 'spec_helper'
2
+
3
+ describe Monga::Database do
4
+ before do
5
+ EM.run do
6
+ req = DB.drop_collection("myCollection")
7
+ req.callback{ EM.stop }
8
+ req.errback{ EM.stop }
9
+ end
10
+ end
11
+
12
+ describe "create/drop" do
13
+
14
+ it "should create and then drop collection" do
15
+ EM.run do
16
+ req = DB.create_collection("myCollection")
17
+ req.errback{ |err| raise err }
18
+ req.callback do
19
+ req = DB.list_collections
20
+ req.errback{ |err| raise err }
21
+ req.callback do |res|
22
+ res.include?("myCollection").must_equal true
23
+ req = DB.drop_collection("myCollection")
24
+ req.errback{ |err| raise err }
25
+ req.callback do
26
+ req = DB.list_collections
27
+ req.errback{ |err| raise err }
28
+ req.callback do |res|
29
+ res.include?("myCollection").must_equal false
30
+ EM.stop
31
+ end
32
+ end
33
+ end
34
+ end
35
+ end
36
+ end
37
+ end
38
+
39
+ describe "create collection with options" do
40
+ it "should create cappet collection with 5kb size to store only 1 large doc" do
41
+ EM.run do
42
+ req = DB.create_collection("myCollection", capped: true, size: 5*1024)
43
+ req.callback do
44
+ collection = DB["myCollection"]
45
+ str = "h"*4*1024
46
+ req = collection.safe_insert({ data: str })
47
+ req.errback{ |err| raise err }
48
+ req.callback do
49
+ req = collection.safe_insert({ data: str })
50
+ req.errback{ |err| raise err }
51
+ req.callback do
52
+ req = collection.count()
53
+ req.errback{ |err| raise err }
54
+ req.callback do |res|
55
+ res.must_equal 1
56
+ req = DB.drop_collection("myCollection")
57
+ req.callback{ EM.stop }
58
+ req.errback{ |err| raise err }
59
+ end
60
+ end
61
+ end
62
+ end
63
+ req.errback{ |err| raise err }
64
+ end
65
+ end
66
+ end
67
+ end
@@ -0,0 +1,46 @@
1
+ require 'spec_helper'
2
+
3
+ describe Monga::ReplicaSetClient do
4
+ include Helpers::Truncate
5
+
6
+ it "should establish simple connection" do
7
+ EM.run do
8
+ 100.times do
9
+ RS_COLLECTION.insert({row: "test"})
10
+ end
11
+ EM.add_timer(0.1) do
12
+ req = RS_COLLECTION.count
13
+ req.callback do |n|
14
+ n.must_equal 100
15
+ EM.stop
16
+ end
17
+ req.errback do |err|
18
+ raise err
19
+ end
20
+ end
21
+ end
22
+ end
23
+
24
+ it "should find new primary" do
25
+ EM.run do
26
+ n = 0
27
+ EM.add_periodic_timer(0.01) do
28
+ if n == 101
29
+ n += 1
30
+ RS_COLLECTION.count.callback do |cnt|
31
+ cnt.must_equal 100
32
+ EM.stop
33
+ end
34
+ elsif n <= 100
35
+ if n == 10
36
+ primary = REPL_SET.primary
37
+ primary.stop
38
+ primary.start
39
+ end
40
+ n+= 1
41
+ RS_COLLECTION.insert({row: n.to_s})
42
+ end
43
+ end
44
+ end
45
+ end
46
+ end
File without changes
File without changes
@@ -0,0 +1,28 @@
1
+ require 'spec_helper'
2
+
3
+ describe Monga::Requests::Query do
4
+ include Helpers::Truncate
5
+
6
+ before do
7
+ EM.run do
8
+ documents = [
9
+ { author: "Madonna", title: "Burning Up" },
10
+ { author: "Madonna", title: "Freezing" },
11
+ { author: "Bjork", title: "Song" },
12
+ ]
13
+ Monga::Requests::Insert.new(DB, COLLECTION.name, { documents: documents }).perform
14
+ EM.add_timer(0.05){ EM.next_tick{ EM.stop }}
15
+ end
16
+ end
17
+
18
+ it "should fetch one document" do
19
+ EM.run do
20
+ command = { query: { author: "Madonna" }, limit: 2 }
21
+ req = Monga::Requests::Query.new(DB, COLLECTION.name, command).callback_perform
22
+ req.callback do |res|
23
+ EM.stop
24
+ end
25
+ end
26
+ end
27
+
28
+ end
@@ -0,0 +1,29 @@
1
+ LIB_PATH = File.expand_path('../../lib/monga', __FILE__)
2
+
3
+ require LIB_PATH
4
+ require 'helpers/truncate'
5
+ require 'helpers/mongodb'
6
+ require 'minitest/spec'
7
+ require 'minitest/autorun'
8
+ require 'minitest/reporters'
9
+ MiniTest::Reporters.use! MiniTest::Reporters::SpecReporter.new
10
+
11
+ EM.run do
12
+ CLIENT = Monga::Client.new(host: "localhost", port: 27017)
13
+ DB = CLIENT["dbTest"]
14
+ COLLECTION = DB["testCollection"]
15
+ EM.stop
16
+ end
17
+
18
+ INSTANCE = Mongodb::Instance.new(dbpath: "/tmp/mongodb/instance/")
19
+
20
+ REPL_SET_PORTS = [{ port: 29100 }, { port: 29200 }, { port: 29300 }]
21
+ EM.run do
22
+ REPL_SET = Mongodb::ReplicaSet.new(REPL_SET_PORTS)
23
+ RS_CLIENT = Monga::ReplicaSetClient.new(servers: REPL_SET_PORTS)
24
+ RS_DB = RS_CLIENT["dbTest"]
25
+ RS_COLLECTION = RS_DB["testCollection"]
26
+ EM.stop
27
+ end
28
+
29
+ # And welcome to callback Hell
metadata ADDED
@@ -0,0 +1,185 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: monga
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Petr Yanovich
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-04-01 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ~>
18
+ - !ruby/object:Gem::Version
19
+ version: '1.3'
20
+ none: false
21
+ type: :development
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: '1.3'
27
+ none: false
28
+ prerelease: false
29
+ name: bundler
30
+ - !ruby/object:Gem::Dependency
31
+ requirement: !ruby/object:Gem::Requirement
32
+ requirements:
33
+ - - ! '>='
34
+ - !ruby/object:Gem::Version
35
+ version: '0'
36
+ none: false
37
+ type: :development
38
+ version_requirements: !ruby/object:Gem::Requirement
39
+ requirements:
40
+ - - ! '>='
41
+ - !ruby/object:Gem::Version
42
+ version: '0'
43
+ none: false
44
+ prerelease: false
45
+ name: rake
46
+ - !ruby/object:Gem::Dependency
47
+ requirement: !ruby/object:Gem::Requirement
48
+ requirements:
49
+ - - ! '>='
50
+ - !ruby/object:Gem::Version
51
+ version: '0'
52
+ none: false
53
+ type: :runtime
54
+ version_requirements: !ruby/object:Gem::Requirement
55
+ requirements:
56
+ - - ! '>='
57
+ - !ruby/object:Gem::Version
58
+ version: '0'
59
+ none: false
60
+ prerelease: false
61
+ name: eventmachine
62
+ - !ruby/object:Gem::Dependency
63
+ requirement: !ruby/object:Gem::Requirement
64
+ requirements:
65
+ - - ! '>='
66
+ - !ruby/object:Gem::Version
67
+ version: '0'
68
+ none: false
69
+ type: :runtime
70
+ version_requirements: !ruby/object:Gem::Requirement
71
+ requirements:
72
+ - - ! '>='
73
+ - !ruby/object:Gem::Version
74
+ version: '0'
75
+ none: false
76
+ prerelease: false
77
+ name: bson
78
+ - !ruby/object:Gem::Dependency
79
+ requirement: !ruby/object:Gem::Requirement
80
+ requirements:
81
+ - - ! '>='
82
+ - !ruby/object:Gem::Version
83
+ version: '0'
84
+ none: false
85
+ type: :runtime
86
+ version_requirements: !ruby/object:Gem::Requirement
87
+ requirements:
88
+ - - ! '>='
89
+ - !ruby/object:Gem::Version
90
+ version: '0'
91
+ none: false
92
+ prerelease: false
93
+ name: bson_ext
94
+ description: MongoDB Ruby Evented Driver on EventMachine
95
+ email:
96
+ - fl00r@yandex.ru
97
+ executables: []
98
+ extensions: []
99
+ extra_rdoc_files: []
100
+ files:
101
+ - .gitignore
102
+ - Gemfile
103
+ - LICENSE.txt
104
+ - README.md
105
+ - Rakefile
106
+ - lib/monga.rb
107
+ - lib/monga/client.rb
108
+ - lib/monga/clients/client.rb
109
+ - lib/monga/clients/master_slave_client.rb
110
+ - lib/monga/clients/replica_set_client.rb
111
+ - lib/monga/collection.rb
112
+ - lib/monga/connection.rb
113
+ - lib/monga/connection_pool.rb
114
+ - lib/monga/connections/em_connection.rb
115
+ - lib/monga/connections/primary.rb
116
+ - lib/monga/connections/secondary.rb
117
+ - lib/monga/cursor.rb
118
+ - lib/monga/database.rb
119
+ - lib/monga/exceptions.rb
120
+ - lib/monga/miner.rb
121
+ - lib/monga/request.rb
122
+ - lib/monga/requests/delete.rb
123
+ - lib/monga/requests/get_more.rb
124
+ - lib/monga/requests/insert.rb
125
+ - lib/monga/requests/kill_cursors.rb
126
+ - lib/monga/requests/query.rb
127
+ - lib/monga/requests/update.rb
128
+ - lib/monga/response.rb
129
+ - monga.gemspec
130
+ - spec/helpers/mongodb.rb
131
+ - spec/helpers/truncate.rb
132
+ - spec/monga/collection_spec.rb
133
+ - spec/monga/connection_pool_spec.rb
134
+ - spec/monga/connection_spec.rb
135
+ - spec/monga/cursor_spec.rb
136
+ - spec/monga/database_spec.rb
137
+ - spec/monga/replica_set_client_spec.rb
138
+ - spec/monga/requests/delete_spec.rb
139
+ - spec/monga/requests/insert_spec.rb
140
+ - spec/monga/requests/query_spec.rb
141
+ - spec/spec_helper.rb
142
+ homepage: ''
143
+ licenses:
144
+ - MIT
145
+ post_install_message:
146
+ rdoc_options: []
147
+ require_paths:
148
+ - lib
149
+ required_ruby_version: !ruby/object:Gem::Requirement
150
+ requirements:
151
+ - - ! '>='
152
+ - !ruby/object:Gem::Version
153
+ hash: -1690207560468014577
154
+ version: '0'
155
+ segments:
156
+ - 0
157
+ none: false
158
+ required_rubygems_version: !ruby/object:Gem::Requirement
159
+ requirements:
160
+ - - ! '>='
161
+ - !ruby/object:Gem::Version
162
+ hash: -1690207560468014577
163
+ version: '0'
164
+ segments:
165
+ - 0
166
+ none: false
167
+ requirements: []
168
+ rubyforge_project:
169
+ rubygems_version: 1.8.25
170
+ signing_key:
171
+ specification_version: 3
172
+ summary: MongoDB Ruby Evented Driver on EventMachine
173
+ test_files:
174
+ - spec/helpers/mongodb.rb
175
+ - spec/helpers/truncate.rb
176
+ - spec/monga/collection_spec.rb
177
+ - spec/monga/connection_pool_spec.rb
178
+ - spec/monga/connection_spec.rb
179
+ - spec/monga/cursor_spec.rb
180
+ - spec/monga/database_spec.rb
181
+ - spec/monga/replica_set_client_spec.rb
182
+ - spec/monga/requests/delete_spec.rb
183
+ - spec/monga/requests/insert_spec.rb
184
+ - spec/monga/requests/query_spec.rb
185
+ - spec/spec_helper.rb