monga 0.0.2 → 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
Files changed (63) hide show
  1. data/.gitignore +1 -0
  2. data/.travis.yml +1 -0
  3. data/README.md +59 -3
  4. data/lib/monga/client.rb +51 -6
  5. data/lib/monga/clients/master_slave_client.rb +0 -5
  6. data/lib/monga/clients/replica_set_client.rb +32 -71
  7. data/lib/monga/clients/single_instance_client.rb +53 -0
  8. data/lib/monga/collection.rb +102 -41
  9. data/lib/monga/connection.rb +38 -13
  10. data/lib/monga/connection_pool.rb +6 -17
  11. data/lib/monga/connections/buffer.rb +33 -0
  12. data/lib/monga/connections/em_connection.rb +25 -56
  13. data/lib/monga/connections/em_proxy_connection.rb +80 -0
  14. data/lib/monga/connections/fibered_connection.rb +26 -0
  15. data/lib/monga/connections/fibered_proxy_connection.rb +23 -0
  16. data/lib/monga/connections/proxy_connection.rb +4 -0
  17. data/lib/monga/connections/tcp_connection.rb +57 -0
  18. data/lib/monga/cursor.rb +197 -95
  19. data/lib/monga/database.rb +175 -60
  20. data/lib/monga/{requests → protocol}/delete.rb +1 -2
  21. data/lib/monga/{requests → protocol}/get_more.rb +1 -1
  22. data/lib/monga/{requests → protocol}/insert.rb +1 -2
  23. data/lib/monga/{requests → protocol}/kill_cursors.rb +1 -1
  24. data/lib/monga/{requests → protocol}/query.rb +3 -3
  25. data/lib/monga/{requests → protocol}/update.rb +1 -1
  26. data/lib/monga/request.rb +27 -23
  27. data/lib/monga/utils/constants.rb +5 -0
  28. data/lib/monga/utils/exceptions.rb +11 -0
  29. data/lib/monga.rb +19 -11
  30. data/monga.gemspec +2 -2
  31. data/spec/helpers/mongodb.rb +115 -38
  32. data/spec/monga/block/collection_spec.rb +172 -0
  33. data/spec/monga/block/cursor_spec.rb +160 -0
  34. data/spec/monga/block/database_spec.rb +80 -0
  35. data/spec/monga/block/single_instance_client_spec.rb +31 -0
  36. data/spec/monga/em/collection_spec.rb +308 -0
  37. data/spec/monga/em/cursor_spec.rb +256 -0
  38. data/spec/monga/em/database_spec.rb +140 -0
  39. data/spec/monga/em/replica_set_client_spec.rb +86 -0
  40. data/spec/monga/em/single_instance_client_spec.rb +28 -0
  41. data/spec/monga/sync/collection_spec.rb +247 -0
  42. data/spec/monga/sync/cursor_spec.rb +211 -0
  43. data/spec/monga/sync/database_spec.rb +110 -0
  44. data/spec/monga/sync/replica_set_client_spec.rb +54 -0
  45. data/spec/monga/sync/single_instance_client_spec.rb +25 -0
  46. data/spec/spec_helper.rb +2 -20
  47. metadata +50 -38
  48. data/lib/monga/clients/client.rb +0 -24
  49. data/lib/monga/connections/primary.rb +0 -46
  50. data/lib/monga/connections/secondary.rb +0 -13
  51. data/lib/monga/exceptions.rb +0 -9
  52. data/lib/monga/miner.rb +0 -72
  53. data/lib/monga/response.rb +0 -11
  54. data/spec/helpers/truncate.rb +0 -15
  55. data/spec/monga/collection_spec.rb +0 -448
  56. data/spec/monga/connection_pool_spec.rb +0 -50
  57. data/spec/monga/connection_spec.rb +0 -64
  58. data/spec/monga/cursor_spec.rb +0 -186
  59. data/spec/monga/database_spec.rb +0 -67
  60. data/spec/monga/replica_set_client_spec.rb +0 -46
  61. data/spec/monga/requests/delete_spec.rb +0 -0
  62. data/spec/monga/requests/insert_spec.rb +0 -0
  63. data/spec/monga/requests/query_spec.rb +0 -28
@@ -1,186 +0,0 @@
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
@@ -1,67 +0,0 @@
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
@@ -1,46 +0,0 @@
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
@@ -1,28 +0,0 @@
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