em-mongo 0.2.4 → 0.2.6

Sign up to get free protection for your applications and to get access to all the features.
@@ -31,6 +31,11 @@ module EM::Mongo
31
31
  obj
32
32
  end
33
33
 
34
+ def update(selector, updater, opts={})
35
+ @connection.update(@name, selector, updater, opts)
36
+ true
37
+ end
38
+
34
39
  def remove(obj = {})
35
40
  @connection.delete(@name, obj)
36
41
  true
@@ -1,8 +1,9 @@
1
1
  module EM::Mongo
2
- DEFAULT_IP = "127.0.0.1"
3
- DEFAULT_PORT = 27017
4
- DEFAULT_DB = "db"
5
- DEFAULT_NS = "ns"
2
+ DEFAULT_IP = "127.0.0.1"
3
+ DEFAULT_PORT = 27017
4
+ DEFAULT_DB = "db"
5
+ DEFAULT_NS = "ns"
6
+ DEFAULT_QUERY_DOCS = 101
6
7
 
7
8
  module EMConnection
8
9
  class Error < Exception;
@@ -33,12 +34,6 @@ module EM::Mongo
33
34
  @is_connected
34
35
  end
35
36
 
36
- # XXX RMongo interface
37
- def collection(db = DEFAULT_DB, ns = DEFAULT_NS)
38
- raise "Not connected" if not connected?
39
- EM::Mongo::Collection.new(db, ns, self)
40
- end
41
-
42
37
  def new_request_id
43
38
  @request_id += 1
44
39
  end
@@ -77,6 +72,23 @@ module EM::Mongo
77
72
  send_command(message.to_s, req_id)
78
73
  end
79
74
 
75
+ def update(collection_name, selector, document, options)
76
+ message = BSON::ByteBuffer.new([0, 0, 0, 0])
77
+ BSON::BSON_RUBY.serialize_cstr(message, collection_name)
78
+
79
+ flags = 0
80
+ flags += 1 if options[:upsert]
81
+ flags += 2 if options[:multi]
82
+ message.put_int(flags)
83
+
84
+ message.put_array(BSON::BSON_CODER.serialize(selector, false, true).to_a)
85
+ message.put_array(BSON::BSON_CODER.serialize(document, true, true).to_a)
86
+
87
+ req_id = new_request_id
88
+ message.prepend!(message_headers(OP_UPDATE, req_id, message))
89
+ send_command(message.to_s, req_id)
90
+ end
91
+
80
92
  def delete(collection_name, selector)
81
93
  message = BSON::ByteBuffer.new([0, 0, 0, 0])
82
94
  BSON::BSON_RUBY.serialize_cstr(message, collection_name)
@@ -102,20 +114,21 @@ module EM::Mongo
102
114
 
103
115
  # EM hooks
104
116
  def initialize(options={})
105
- @request_id = 0
106
- @responses = {}
107
- @is_connected = false
108
- @host = options[:host] || DEFAULT_IP
109
- @port = options[:port] || DEFAULT_PORT
117
+ @request_id = 0
118
+ @responses = {}
119
+ @is_connected = false
120
+ @host = options[:host] || DEFAULT_IP
121
+ @port = options[:port] || DEFAULT_PORT
122
+ @on_unbind = options[:unbind_cb] || proc {}
110
123
 
111
124
  @on_close = proc {
112
- raise Error, "could not connect to server #{@host}:#{@port}"
125
+ raise Error, "failure with mongodb server #{@host}:#{@port}"
113
126
  }
114
127
  timeout options[:timeout] if options[:timeout]
115
- errback{ @on_close.call }
128
+ errback { @on_close.call }
116
129
  end
117
130
 
118
- def self.connect(host = DEFAULT_IP, port = DEFAUL_PORT, timeout = nil)
131
+ def self.connect(host = DEFAULT_IP, port = DEFAULT_PORT, timeout = nil)
119
132
  opt = {:host => host, :port => port, :timeout => timeout}
120
133
  EM.connect(host, port, self, opt)
121
134
  end
@@ -123,26 +136,28 @@ module EM::Mongo
123
136
  def connection_completed
124
137
  @buffer = BSON::ByteBuffer.new
125
138
  @is_connected = true
126
- @on_close = proc{
127
- }
128
139
  succeed
129
140
  end
130
141
 
131
142
 
132
143
  def message_received?
133
144
  size = @buffer.get_int
145
+ #@buffer.put_int(size, -1)
134
146
  @buffer.rewind
135
- @buffer.size >= size-4 ? true : false
147
+
148
+ if @buffer.size >= size-4
149
+ true
150
+ else
151
+ false
152
+ end
136
153
  end
137
154
 
138
155
  def receive_data(data)
139
156
 
140
- @buffer.put_array(data.unpack('C*'))
141
- @buffer.rewind
157
+ @buffer.append!(BSON::ByteBuffer.new(data.unpack('C*')))
142
158
  return if @buffer.size < STANDARD_HEADER_SIZE
143
159
 
144
160
  if message_received?
145
-
146
161
  # Header
147
162
  header = BSON::ByteBuffer.new
148
163
  header.put_array(@buffer.get(STANDARD_HEADER_SIZE))
@@ -181,12 +196,12 @@ module EM::Mongo
181
196
 
182
197
  if size > @buffer.size
183
198
  @buffer = ''
184
- raise "Buffer Overflow: Failed to parse buffer"
199
+ raise "Buffer Overflow: Failed to parse document buffer: size: #{size}, expected: #{@buffer.size}"
185
200
  end
186
201
  buf.put_array(@buffer.get(size-4), 4)
187
202
 
188
203
  buf.rewind
189
- BSON::BSON_CODER.deserialize(buf)
204
+ BSON::BSON_RUBY.deserialize(buf)
190
205
  end
191
206
 
192
207
  @buffer.clear
@@ -200,19 +215,16 @@ module EM::Mongo
200
215
  end
201
216
 
202
217
  def send_data data
203
- log "send_data:#{data.size}"#, data
204
218
  super data
205
219
  end
206
220
 
207
221
  def unbind
208
- log "unbind"
209
222
  @is_connected = false
210
- @on_close.call unless $!
223
+ @on_unbind.call
211
224
  end
212
225
 
213
226
  def close
214
- log "close"
215
- @on_close = proc{ yield if block_given? }
227
+ @on_close = proc { yield if block_given? }
216
228
  if @responses.empty?
217
229
  close_connection
218
230
  else
@@ -223,8 +235,7 @@ module EM::Mongo
223
235
  private
224
236
 
225
237
  def log *args
226
- return
227
- pp args
238
+ puts args
228
239
  puts
229
240
  end
230
241
 
@@ -252,15 +263,10 @@ module EM::Mongo
252
263
  def initialize(host = DEFAULT_IP, port = DEFAULT_PORT, timeout = nil)
253
264
  @em_connection = EMConnection.connect(host, port, timeout)
254
265
  @db = {}
255
- self
256
266
  end
257
267
 
258
268
  def db(name = DEFAULT_DB)
259
- @db[name] = EM::Mongo::Database.new(name, @em_connection)
260
- end
261
-
262
- def collection(db = DEFAULT_DB, ns = DEFAULT_NS)
263
- @em_connection.collection(db, ns)
269
+ @db[name] ||= EM::Mongo::Database.new(name, @em_connection)
264
270
  end
265
271
 
266
272
  def close
data/lib/em-mongo.rb CHANGED
@@ -2,13 +2,12 @@
2
2
  require "eventmachine"
3
3
  begin; require "bson_ext"; rescue LoadError; require "bson"; end
4
4
 
5
-
6
5
  module EM::Mongo
7
6
 
8
7
  module Version
9
8
  MAJOR = 0
10
9
  MINOR = 2
11
- TINY = 4
10
+ TINY = 6
12
11
  STRING = [MAJOR, MINOR, TINY].join('.')
13
12
  end
14
13
 
@@ -63,6 +63,31 @@ describe EMMongo::Collection do
63
63
  end
64
64
  end
65
65
 
66
+ it 'should find large sets of objects' do
67
+ EM::Spec::Mongo.collection do |collection|
68
+ (0..1500).each { |n| collection.insert({n.to_s => n.to_s}) }
69
+ collection.find do |res|
70
+ res.size.should == EM::Mongo::DEFAULT_QUERY_DOCS
71
+ collection.find({}, {:limit => 1500}) do |res|
72
+ res.size.should == 1500
73
+ EM::Spec::Mongo.close
74
+ end
75
+ end
76
+ end
77
+ end
78
+
79
+ it 'should update an object' do
80
+ EM::Spec::Mongo.collection do |collection|
81
+ obj = collection.insert('hello' => 'world')
82
+ collection.update({'hello' => 'world'}, {'hello' => 'newworld'})
83
+ collection.find({'_id' => obj['_id']},{}) do |res|
84
+ res[0]['hello'].should == 'newworld'
85
+ EM::Spec::Mongo.close
86
+ end
87
+ end
88
+ end
89
+
90
+
66
91
  it 'should remove an object' do
67
92
  EM::Spec::Mongo.collection do |collection|
68
93
  obj = collection.insert('hello' => 'world')
@@ -16,11 +16,11 @@ describe EMMongo::Connection do
16
16
  it 'should close' do
17
17
  em do
18
18
  connection = EMMongo::Connection.new
19
- EM.next_tick do
19
+ EM.add_timer(1) do
20
20
  connection.connected?.should == true
21
21
  connection.close
22
22
  end
23
- EM.add_timer(1) do
23
+ EM.add_timer(2) do
24
24
  EM.next_tick do
25
25
  connection.connected?.should == false
26
26
  done
@@ -29,14 +29,6 @@ describe EMMongo::Connection do
29
29
  end
30
30
  end
31
31
 
32
- # Support the old RMongo interface for now
33
- it 'should instantiate a Collection' do
34
- EM::Spec::Mongo.connection do |connection|
35
- connection.collection.is_a?(EM::Mongo::Collection).should == true
36
- EM::Spec::Mongo.close
37
- end
38
- end
39
-
40
32
  it 'should instantiate a Databse' do
41
33
  EM::Spec::Mongo.connection do |connection|
42
34
  db1 = connection.db
metadata CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
5
5
  segments:
6
6
  - 0
7
7
  - 2
8
- - 4
9
- version: 0.2.4
8
+ - 6
9
+ version: 0.2.6
10
10
  platform: ruby
11
11
  authors:
12
12
  - bcg