moped 1.0.0.alpha → 1.0.0.beta

Sign up to get free protection for your applications and to get access to all the features.

Potentially problematic release.


This version of moped might be problematic. Click here for more details.

@@ -11,8 +11,9 @@ module Moped
11
11
 
12
12
  # @param [String, Symbol] database the database to run this command on
13
13
  # @param [Hash] command the command to run
14
- def initialize(database, command)
15
- super database, :$cmd, command, limit: -1
14
+ # @param [Hash] additional query options
15
+ def initialize(database, command, options = {})
16
+ super database, :$cmd, command, options.merge(limit: -1)
16
17
  end
17
18
 
18
19
  def log_inspect
@@ -99,7 +99,16 @@ module Moped
99
99
 
100
100
  # @return [Hash] the first document that matches the selector.
101
101
  def first
102
- session.simple_query(operation)
102
+ reply = session.context.query(
103
+ operation.database,
104
+ operation.collection,
105
+ operation.selector,
106
+ fields: operation.fields,
107
+ flags: operation.flags,
108
+ skip: operation.skip,
109
+ limit: -1
110
+ )
111
+ reply.documents.first
103
112
  end
104
113
  alias one first
105
114
 
@@ -107,7 +116,7 @@ module Moped
107
116
  #
108
117
  # @yieldparam [Hash] document each matching document
109
118
  def each
110
- cursor = Cursor.new(session.with(retain_socket: true), operation)
119
+ cursor = Cursor.new(session, operation)
111
120
  cursor.to_enum.tap do |enum|
112
121
  enum.each do |document|
113
122
  yield document
@@ -129,6 +138,7 @@ module Moped
129
138
  key: key.to_s,
130
139
  query: selector
131
140
  )
141
+
132
142
  result["values"]
133
143
  end
134
144
 
@@ -151,16 +161,12 @@ module Moped
151
161
  # @param [Array] flags an array of operation flags. Valid values are:
152
162
  # +:multi+ and +:upsert+
153
163
  def update(change, flags = nil)
154
- update = Protocol::Update.new(
155
- operation.database,
156
- operation.collection,
157
- operation.selector,
158
- change,
159
- flags: flags
160
- )
161
-
162
164
  session.with(consistency: :strong) do |session|
163
- session.execute update
165
+ session.context.update operation.database,
166
+ operation.collection,
167
+ operation.selector,
168
+ change,
169
+ flags: flags
164
170
  end
165
171
  end
166
172
 
@@ -193,15 +199,11 @@ module Moped
193
199
  # @example
194
200
  # db[:people].find(name: "John").remove
195
201
  def remove
196
- delete = Protocol::Delete.new(
197
- operation.database,
198
- operation.collection,
199
- operation.selector,
200
- flags: [:remove_first]
201
- )
202
-
203
202
  session.with(consistency: :strong) do |session|
204
- session.execute delete
203
+ session.context.remove operation.database,
204
+ operation.collection,
205
+ operation.selector,
206
+ flags: [:remove_first]
205
207
  end
206
208
  end
207
209
 
@@ -210,14 +212,10 @@ module Moped
210
212
  # @example
211
213
  # db[:people].find(name: "John").remove_all
212
214
  def remove_all
213
- delete = Protocol::Delete.new(
214
- operation.database,
215
- operation.collection,
216
- operation.selector
217
- )
218
-
219
215
  session.with(consistency: :strong) do |session|
220
- session.execute delete
216
+ session.context.remove operation.database,
217
+ operation.collection,
218
+ operation.selector
221
219
  end
222
220
  end
223
221
 
@@ -37,6 +37,10 @@ module Moped
37
37
  # @return [Cluster] this session's cluster
38
38
  attr_reader :cluster
39
39
 
40
+ # @private
41
+ # @return [Context] this session's context
42
+ attr_reader :context
43
+
40
44
  # @param [Array] seeds an of host:port pairs
41
45
  # @param [Hash] options
42
46
  # @option options [Boolean] :safe (false) ensure writes are persisted
@@ -45,7 +49,8 @@ module Moped
45
49
  # @option options [Symbol, String] :database the database to use
46
50
  # @option options [:strong, :eventual] :consistency (:eventual)
47
51
  def initialize(seeds, options = {})
48
- @cluster = Cluster.new(seeds)
52
+ @cluster = Cluster.new(seeds, {})
53
+ @context = Context.new(self)
49
54
  @options = options
50
55
  @options[:consistency] ||= :eventual
51
56
  end
@@ -55,11 +60,16 @@ module Moped
55
60
  !!safety
56
61
  end
57
62
 
63
+ # @return [:strong, :eventual] the session's consistency
64
+ def consistency
65
+ options[:consistency]
66
+ end
67
+
58
68
  # Switch the session's current database.
59
69
  #
60
70
  # @example
61
71
  # session.use :moped
62
- # session[:people]. john, mary = session[:people].find.one # => { :name => "John" }
72
+ # session[:people].find.one # => { :name => "John" }
63
73
  #
64
74
  # @param [String] database the database to use
65
75
  def use(database)
@@ -111,7 +121,7 @@ module Moped
111
121
  # @return [Moped::Session] the new session
112
122
  def new(options = {})
113
123
  session = with(options)
114
- session.cluster.reconnect
124
+ session.instance_variable_set(:@cluster, cluster.dup)
115
125
 
116
126
  if block_given?
117
127
  yield session
@@ -155,64 +165,6 @@ module Moped
155
165
  # @raise (see Moped::Database#login)
156
166
  delegate :logout => :current_database
157
167
 
158
- # @private
159
- def current_database
160
- return @current_database if defined? @current_database
161
-
162
- if database = options[:database]
163
- set_current_database(database)
164
- else
165
- raise "No database set for session. Call #use or #with before accessing the database"
166
- end
167
- end
168
-
169
- # @private
170
- def simple_query(query)
171
- query.limit = -1
172
-
173
- query(query).documents.first
174
- end
175
-
176
- # @private
177
- def query(query)
178
- if options[:consistency] == :eventual
179
- query.flags |= [:slave_ok] if query.respond_to? :flags
180
- mode = :read
181
- else
182
- mode = :write
183
- end
184
-
185
- reply = socket_for(mode).execute(query)
186
-
187
- reply.tap do |reply|
188
- if reply.flags.include?(:query_failure)
189
- raise Errors::QueryFailure.new(query, reply.documents.first)
190
- end
191
- end
192
- end
193
-
194
- # @private
195
- def execute(op)
196
- mode = options[:consistency] == :eventual ? :read : :write
197
- socket = socket_for(mode)
198
-
199
- if safe?
200
- last_error = Protocol::Command.new(
201
- "admin", { getlasterror: 1 }.merge(safety)
202
- )
203
-
204
- socket.execute(op, last_error).documents.first.tap do |result|
205
- raise Errors::OperationFailure.new(
206
- op, result
207
- ) if result["err"] || result["errmsg"]
208
- end
209
- else
210
- socket.execute(op)
211
- end
212
- end
213
-
214
- private
215
-
216
168
  # @return [Boolean, Hash] the safety level for this session
217
169
  def safety
218
170
  safe = options[:safe]
@@ -227,11 +179,15 @@ module Moped
227
179
  end
228
180
  end
229
181
 
230
- def socket_for(mode)
231
- if options[:retain_socket]
232
- @socket ||= cluster.socket_for(mode)
182
+ private
183
+
184
+ def current_database
185
+ return @current_database if defined? @current_database
186
+
187
+ if database = options[:database]
188
+ set_current_database(database)
233
189
  else
234
- cluster.socket_for(mode)
190
+ raise "No database set for session. Call #use or #with before accessing the database"
235
191
  end
236
192
  end
237
193
 
@@ -239,15 +195,13 @@ module Moped
239
195
  @current_database = Database.new(self, database)
240
196
  end
241
197
 
242
- def dup
243
- session = super
244
- session.instance_variable_set :@options, options.dup
198
+ def initialize_copy(_)
199
+ @context = Context.new(self)
200
+ @options = @options.dup
245
201
 
246
202
  if defined? @current_database
247
- session.send(:remove_instance_variable, :@current_database)
203
+ remove_instance_variable :@current_database
248
204
  end
249
-
250
- session
251
205
  end
252
206
  end
253
207
  end
@@ -0,0 +1,105 @@
1
+ module Moped
2
+ class Session
3
+
4
+ # @api private
5
+ class Context
6
+ extend Forwardable
7
+
8
+ def initialize(session)
9
+ @session = session
10
+ end
11
+
12
+ delegate :safety => :@session
13
+ delegate :safe? => :@session
14
+ delegate :consistency => :@session
15
+ delegate :cluster => :@session
16
+
17
+ def login(database, username, password)
18
+ cluster.auth[database.to_s] = [username, password]
19
+ end
20
+
21
+ def logout(database)
22
+ cluster.auth.delete database.to_s
23
+ end
24
+
25
+ def query(database, collection, selector, options = {})
26
+ if consistency == :eventual
27
+ options[:flags] ||= []
28
+ options[:flags] |= [:slave_ok]
29
+ end
30
+
31
+ with_node do |node|
32
+ node.query(database, collection, selector, options)
33
+ end
34
+ end
35
+
36
+ def command(database, command)
37
+ options = consistency == :eventual ? { :flags => [:slave_ok] } : {}
38
+ with_node do |node|
39
+ node.command(database, command, options)
40
+ end
41
+ end
42
+
43
+ def insert(database, collection, documents)
44
+ with_node do |node|
45
+ if safe?
46
+ node.pipeline do
47
+ node.insert(database, collection, documents)
48
+ node.command("admin", { getlasterror: 1 }.merge(safety))
49
+ end
50
+ else
51
+ node.insert(database, collection, documents)
52
+ end
53
+ end
54
+ end
55
+
56
+ def update(database, collection, selector, change, options = {})
57
+ with_node do |node|
58
+ if safe?
59
+ node.pipeline do
60
+ node.update(database, collection, selector, change, options)
61
+ node.command("admin", { getlasterror: 1 }.merge(safety))
62
+ end
63
+ else
64
+ node.update(database, collection, selector, change, options)
65
+ end
66
+ end
67
+ end
68
+
69
+ def remove(database, collection, selector, options = {})
70
+ with_node do |node|
71
+ if safe?
72
+ node.pipeline do
73
+ node.remove(database, collection, selector, options)
74
+ node.command("admin", { getlasterror: 1 }.merge(safety))
75
+ end
76
+ else
77
+ node.remove(database, collection, selector, options)
78
+ end
79
+ end
80
+ end
81
+
82
+ def get_more(*args)
83
+ raise NotImplementedError, "#get_more cannot be called on Context; it must be called directly on a node"
84
+ end
85
+
86
+ def kill_cursors(*args)
87
+ raise NotImplementedError, "#kill_cursors cannot be called on Context; it must be called directly on a node"
88
+ end
89
+
90
+ def with_node
91
+ if consistency == :eventual
92
+ cluster.with_secondary do |node|
93
+ yield node
94
+ end
95
+ else
96
+ cluster.with_primary do |node|
97
+ yield node
98
+ end
99
+ end
100
+ end
101
+
102
+ end
103
+
104
+ end
105
+ end
@@ -0,0 +1,32 @@
1
+ module Moped
2
+
3
+ # This module contains logic for easy access to objects that have a lifecycle
4
+ # on the current thread.
5
+ #
6
+ # Extracted from Mongoid's +Threaded+ module.
7
+ #
8
+ # @api private
9
+ module Threaded
10
+ extend self
11
+
12
+ # Begin a thread-local stack for +name+.
13
+ def begin(name)
14
+ stack(name).push true
15
+ end
16
+
17
+ # @return [Boolean] whether the stack is being executed
18
+ def executing?(name)
19
+ !stack(name).empty?
20
+ end
21
+
22
+ # End the thread-local stack for +name+.
23
+ def end(name)
24
+ stack(name).pop
25
+ end
26
+
27
+ # @return [Array] a named, thread-local stack.
28
+ def stack(name)
29
+ Thread.current["[moped]:#{name}-stack"] ||= []
30
+ end
31
+ end
32
+ end
@@ -1,4 +1,4 @@
1
1
  # encoding: utf-8
2
2
  module Moped
3
- VERSION = "1.0.0.alpha"
3
+ VERSION = "1.0.0.beta"
4
4
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: moped
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0.alpha
4
+ version: 1.0.0.beta
5
5
  prerelease: 6
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-04-13 00:00:00.000000000 Z
12
+ date: 2012-04-20 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rspec
@@ -74,11 +74,13 @@ files:
74
74
  - lib/moped/bson.rb
75
75
  - lib/moped/cluster.rb
76
76
  - lib/moped/collection.rb
77
+ - lib/moped/connection.rb
77
78
  - lib/moped/cursor.rb
78
79
  - lib/moped/database.rb
79
80
  - lib/moped/errors.rb
80
81
  - lib/moped/indexes.rb
81
82
  - lib/moped/logging.rb
83
+ - lib/moped/node.rb
82
84
  - lib/moped/protocol/command.rb
83
85
  - lib/moped/protocol/commands/authenticate.rb
84
86
  - lib/moped/protocol/commands.rb
@@ -92,9 +94,9 @@ files:
92
94
  - lib/moped/protocol/update.rb
93
95
  - lib/moped/protocol.rb
94
96
  - lib/moped/query.rb
95
- - lib/moped/server.rb
97
+ - lib/moped/session/context.rb
96
98
  - lib/moped/session.rb
97
- - lib/moped/socket.rb
99
+ - lib/moped/threaded.rb
98
100
  - lib/moped/version.rb
99
101
  - lib/moped.rb
100
102
  - MIT_LICENSE
@@ -113,7 +115,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
113
115
  version: '0'
114
116
  segments:
115
117
  - 0
116
- hash: -3885832787048717619
118
+ hash: 1633791536105860928
117
119
  required_rubygems_version: !ruby/object:Gem::Requirement
118
120
  none: false
119
121
  requirements: