Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Use a Benchmarking mixin instead of an Adapter wrapper

This patch completely drops the use of the adapter
cascade along with adapter wrappers.

This will hopefully work around the problems with
transactions and migrations, which both need real
AbstractAdapter descendants to work. The previous
adapter wrappers were'nt properly recognized which
basically rendered transactions and migrations
useless for the sake of being able to display some
benchmarking information in rails' server logs.

This patch should get around that by only extending
the adapter behavior with our benchmarking needs,
instead of wrapping it in a new object that's not
a descendant of AbstractAdapter.

Thx to Xavier Shay for the idea!

In the hopefully not so longterm, the benchmarking
mixin will go away anyway, because (at least) the
DO adapters will provide useful benchmarking info
by means of passing a struct containing the info
to a Connection#log(info) method that's defined in
ruby instead of only in the native driver
implementations (as is the case currently).
  • Loading branch information...
commit a0eb1fd1fbf09c222a777254f158b6fb3bf80077 1 parent cf455f8
@snusnu snusnu authored
View
45 lib/dm-rails/adapters.rb
@@ -1,45 +0,0 @@
-require 'dm-rails/adapters/benchmarking_adapter'
-
-module Rails
- module DataMapper
- module Adapters
-
- class Cascade
-
- def self.configure
- block_given? ? yield(cascade) : cascade
- end
-
- def self.instantiate(adapter, idx = 0)
- if idx < cascade.size
- instantiate(cascade[idx], idx + 1).new(adapter)
- else
- adapter
- end
- end
-
- def self.cascade
- @cascade ||= new
- end
-
-
- def push(adapter)
- @cascade << adapter
- end
-
- alias :use :push
-
- def size; @cascade.size end
- def [](idx); @cascade[idx] end
-
- private
-
- def initialize
- @cascade, @adapter = [], nil
- end
-
- end
-
- end
- end
-end
View
48 lib/dm-rails/adapters/adapter_wrapper.rb
@@ -1,48 +0,0 @@
-module DataMapper
- module Adapters
- module Wrapper
-
- def self.included(host)
- host.class_eval do
- instance_methods.each do |method|
- next if method =~ /\A__/ || %w[
- send class dup object_id kind_of? instance_of? respond_to? equal? freeze frozen?
- should should_not instance_variables instance_variable_set instance_variable_get
- instance_variable_defined? remove_instance_variable extend hash inspect copy_object
- ].include?(method.to_s)
- undef_method method
- end
- include InstanceMethods
- attr_reader :adapter
- end
- end
-
- module InstanceMethods
-
- def kind_of?(klass)
- super || adapter.kind_of?(klass)
- end
-
- def instance_of?(klass)
- super || adapter.instance_of?(klass)
- end
-
- def respond_to?(method, include_private = false)
- super || adapter.respond_to?(method, include_private)
- end
-
- private
-
- def initialize(adapter)
- @adapter = adapter
- end
-
- def method_missing(method, *args, &block)
- adapter.send(method, *args, &block)
- end
-
- end
-
- end
- end
-end
View
39 lib/dm-rails/adapters/benchmarking_adapter.rb
@@ -1,39 +0,0 @@
-require 'dm-rails/adapters/adapter_wrapper'
-
-module Rails
- module DataMapper
- module Adapters
-
- class BenchmarkingAdapter
-
- include ::DataMapper::Adapters::Wrapper
-
- def reset_runtime
- rt, @runtime = @runtime, 0
- rt
- end
-
- %w[ create read update delete ].each do |method|
- class_eval <<-RUBY, __FILE__, __LINE__ + 1
- def #{method}(*args, &block) # def create(*args, &block)
- result = nil # result = nil
- @runtime += Benchmark.ms do # @runtime += Benchmark.ms do
- result = adapter.#{method}(*args, &block) # result = adapter.create(*args, &block)
- end # end
- result # result
- end # end
- RUBY
- end
-
- private
-
- def initialize(adapter)
- super
- reset_runtime
- end
-
- end
-
- end
- end
-end
View
10 lib/dm-rails/railtie.rb
@@ -36,12 +36,6 @@ def configure_data_mapper(app)
)
end
- def setup_adapter_cascade(app)
- app.config.data_mapper.adapter_cascade.configure do |cascade|
- cascade.use Rails::DataMapper::Adapters::BenchmarkingAdapter
- end
- end
-
def setup_i18n_support(app)
::DataMapper::Model.append_inclusions(Rails::DataMapper::I18nSupport)
end
@@ -97,10 +91,6 @@ def preload_models(app)
setup_logger(app, Rails.logger)
end
- initializer 'data_mapper.adapter_cascade' do |app|
- setup_adapter_cascade(app)
- end
-
initializer 'data_mapper.i18n_support' do |app|
setup_i18n_support(app)
end
View
22 lib/dm-rails/railties/benchmarking_mixin.rb
@@ -0,0 +1,22 @@
+module DataMapper
+ module Adapters
+ module Benchmarking
+
+ %w(create read update delete).each do |method|
+
+ define_method method do |*args, &block|
+ result = nil
+ @runtime ||= 0
+ @runtime += Benchmark.ms { result = super }
+ result
+ end
+ end
+
+ def reset_runtime
+ rt, @runtime = @runtime, 0
+ rt.to_f
+ end
+
+ end
+ end
+end
View
8 lib/dm-rails/setup.rb
@@ -1,7 +1,7 @@
require 'active_support/core_ext/hash/except'
require 'dm-rails/configuration'
-require 'dm-rails/adapters'
+require 'dm-rails/railties/benchmarking_mixin'
module Rails
module DataMapper
@@ -21,7 +21,7 @@ def self.setup_logger(logger)
def self.setup_with_instrumentation(name, options)
puts "[datamapper] Setting up #{name.inspect} repository: '#{options['database']}' on #{options['adapter']}"
adapter = ::DataMapper.setup(name, options)
- ::DataMapper::Repository.adapters[adapter.name] = adapter_cascade(adapter)
+ adapter.extend ::DataMapper::Adapters::Benchmarking
end
def self.initialize_foreign_keys
@@ -30,9 +30,5 @@ def self.initialize_foreign_keys
end
end
- def self.adapter_cascade(adapter)
- Adapters::Cascade.instantiate(adapter)
- end
-
end
end
Please sign in to comment.
Something went wrong with that request. Please try again.