em-mongo 0.3.4 → 0.3.5

Sign up to get free protection for your applications and to get access to all the features.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.3.4
1
+ 0.3.5
@@ -14,8 +14,9 @@ module EM::Mongo
14
14
 
15
15
  skip = opts.delete(:skip) || 0
16
16
  limit = opts.delete(:limit) || 0
17
+ order = opts.delete(:order)
17
18
 
18
- @connection.find(@name, skip, limit, selector, nil, &blk)
19
+ @connection.find(@name, skip, limit, order, selector, nil, &blk)
19
20
  end
20
21
 
21
22
  def first(selector={}, opts={}, &blk)
@@ -99,12 +99,13 @@ module EM::Mongo
99
99
  send_command(message.to_s, req_id)
100
100
  end
101
101
 
102
- def find(collection_name, skip, limit, query, fields, &blk)
102
+ def find(collection_name, skip, limit, order, query, fields, &blk)
103
103
  message = BSON::ByteBuffer.new
104
104
  message.put_int(RESERVED) # query options
105
105
  BSON::BSON_RUBY.serialize_cstr(message, collection_name)
106
106
  message.put_int(skip)
107
107
  message.put_int(limit)
108
+ query = order.nil? ? query : construct_query_spec(query, order)
108
109
  message.put_array(BSON::BSON_CODER.serialize(query, false).to_a)
109
110
  message.put_array(BSON::BSON_CODER.serialize(fields, false).to_a) if fields
110
111
  req_id = new_request_id
@@ -112,6 +113,13 @@ module EM::Mongo
112
113
  send_command(message.to_s, req_id, &blk)
113
114
  end
114
115
 
116
+ def construct_query_spec(query, order)
117
+ spec = BSON::OrderedHash.new
118
+ spec['$query'] = query
119
+ spec['$orderby'] = Mongo::Support.format_order_clause(order) if order
120
+ spec
121
+ end
122
+
115
123
  # EM hooks
116
124
  def initialize(options={})
117
125
  @request_id = 0
@@ -0,0 +1,72 @@
1
+
2
+ # encoding: UTF-8
3
+
4
+ #
5
+ # --
6
+ # Copyright (C) 2008-2011 10gen Inc.
7
+ #
8
+ # Licensed under the Apache License, Version 2.0 (the "License");
9
+ # you may not use this file except in compliance with the License.
10
+ # You may obtain a copy of the License at
11
+ #
12
+ # http://www.apache.org/licenses/LICENSE-2.0
13
+ #
14
+ # Unless required by applicable law or agreed to in writing, software
15
+ # distributed under the License is distributed on an "AS IS" BASIS,
16
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17
+ # See the License for the specific language governing permissions and
18
+ # limitations under the License.
19
+ # ++
20
+
21
+ module Mongo
22
+ # Generic Mongo Ruby Driver exception class.
23
+ class MongoRubyError < StandardError; end
24
+
25
+ # Raised when MongoDB itself has returned an error.
26
+ class MongoDBError < RuntimeError; end
27
+
28
+ # Raised when configuration options cause connections, queries, etc., to fail.
29
+ class ConfigurationError < MongoRubyError; end
30
+
31
+ # Raised on fatal errors to GridFS.
32
+ class GridError < MongoRubyError; end
33
+
34
+ # Raised on fatal errors to GridFS.
35
+ class GridFileNotFound < GridError; end
36
+
37
+ # Raised on fatal errors to GridFS.
38
+ class GridMD5Failure < GridError; end
39
+
40
+ # Raised when invalid arguments are sent to Mongo Ruby methods.
41
+ class MongoArgumentError < MongoRubyError; end
42
+
43
+ # Raised on failures in connection to the database server.
44
+ class ConnectionError < MongoRubyError; end
45
+
46
+ # Raised on failures in connection to the database server.
47
+ class ReplicaSetConnectionError < ConnectionError; end
48
+
49
+ # Raised on failures in connection to the database server.
50
+ class ConnectionTimeoutError < MongoRubyError; end
51
+
52
+ # Raised when a connection operation fails.
53
+ class ConnectionFailure < MongoDBError; end
54
+
55
+ # Raised when authentication fails.
56
+ class AuthenticationError < MongoDBError; end
57
+
58
+ # Raised when a database operation fails.
59
+ class OperationFailure < MongoDBError; end
60
+
61
+ # Raised when a socket read operation times out.
62
+ class OperationTimeout < ::Timeout::Error; end
63
+
64
+ # Raised when a client attempts to perform an invalid operation.
65
+ class InvalidOperation < MongoDBError; end
66
+
67
+ # Raised when an invalid collection or database name is used (invalid namespace name).
68
+ class InvalidNSName < RuntimeError; end
69
+
70
+ # Raised when the client supplies an invalid value to sort by.
71
+ class InvalidSortValueError < MongoRubyError; end
72
+ end
@@ -26,7 +26,7 @@ describe EMMongo::Collection do
26
26
 
27
27
  it 'should find an object by attribute' do
28
28
  @conn, @coll = connection_and_collection
29
-
29
+
30
30
  @coll.insert("hello" => 'world')
31
31
  @coll.find({"hello" => "world"},{}) do |res|
32
32
  res.size.should >= 1
@@ -40,7 +40,7 @@ describe EMMongo::Collection do
40
40
 
41
41
  obj = @coll.insert({:_id => 1234, 'foo' => 'bar', :hello => 'world'})
42
42
  @coll.first({:_id => 1234},{}) do |res|
43
- res['hello'].should == 'world'
43
+ res['hello'].should == 'world'
44
44
  res['foo'].should == 'bar'
45
45
  done
46
46
  end
@@ -48,7 +48,7 @@ describe EMMongo::Collection do
48
48
 
49
49
  it 'should find an object by symbol' do
50
50
  @conn, @coll = connection_and_collection
51
-
51
+
52
52
  @coll.insert('hello' => 'world')
53
53
  @coll.find({:hello => "world"},{}) do |res|
54
54
  res.size.should >= 1
@@ -59,7 +59,7 @@ describe EMMongo::Collection do
59
59
 
60
60
  it 'should find an object by id' do
61
61
  @conn, @coll = connection_and_collection
62
-
62
+
63
63
  id = @coll.insert('hello' => 'world')
64
64
  @coll.find({:_id => id},{}) do |res|
65
65
  res.size.should >= 1
@@ -79,9 +79,31 @@ describe EMMongo::Collection do
79
79
  end
80
80
  end
81
81
 
82
+ it 'should find objects and sort by the order field' do
83
+ @conn, @coll = connection_and_collection
84
+
85
+ @coll.insert(:name => 'one', :position => 0)
86
+ @coll.insert(:name => 'three', :position => 2)
87
+ @coll.insert(:name => 'two', :position => 1)
88
+
89
+ @coll.find({}, {:order => 'position'}) do |res|
90
+ res[0]["name"].should == 'one'
91
+ res[1]["name"].should == 'two'
92
+ res[2]["name"].should == 'three'
93
+ done
94
+ end
95
+
96
+ @coll.find({}, {:order => [:position, :desc]}) do |res|
97
+ res[0]["name"].should == 'three'
98
+ res[1]["name"].should == 'two'
99
+ res[2]["name"].should == 'one'
100
+ done
101
+ end
102
+ end
103
+
82
104
  it 'should find large sets of objects' do
83
105
  @conn, @coll = connection_and_collection
84
-
106
+
85
107
  (0..1500).each { |n| @coll.insert({n.to_s => n.to_s}) }
86
108
  @coll.find do |res|
87
109
  res.size.should == EM::Mongo::DEFAULT_QUERY_DOCS
@@ -175,7 +197,7 @@ describe EMMongo::Collection do
175
197
 
176
198
  it 'should find an object using nested properties' do
177
199
  @conn, @coll = connection_and_collection
178
-
200
+
179
201
  @coll.insert({
180
202
  'name' => 'Google',
181
203
  'address' => {
@@ -205,7 +227,7 @@ describe EMMongo::Collection do
205
227
 
206
228
  it 'should find objects greater than something' do
207
229
  @conn, @coll = connection_and_collection
208
-
230
+
209
231
  number_hash.each do |num, word|
210
232
  @coll.insert('num' => num, 'word' => word)
211
233
  end
@@ -219,7 +241,7 @@ describe EMMongo::Collection do
219
241
 
220
242
  it 'should handle multiple pending queries' do
221
243
  @conn, @coll = connection_and_collection
222
-
244
+
223
245
  id = @coll.insert("foo" => "bar")
224
246
  received = 0
225
247
 
metadata CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
5
5
  segments:
6
6
  - 0
7
7
  - 3
8
- - 4
9
- version: 0.3.4
8
+ - 5
9
+ version: 0.3.5
10
10
  platform: ruby
11
11
  authors:
12
12
  - bcg
@@ -76,6 +76,7 @@ files:
76
76
  - lib/em-mongo/connection.rb
77
77
  - lib/em-mongo/conversions.rb
78
78
  - lib/em-mongo/database.rb
79
+ - lib/em-mongo/exceptions.rb
79
80
  - lib/em-mongo/support.rb
80
81
  - lib/em-mongo.rb
81
82
  - spec/integration/collection_spec.rb