lookout-data_fabric 1.5.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (53) hide show
  1. checksums.yaml +15 -0
  2. data/CHANGELOG +79 -0
  3. data/README.rdoc +125 -0
  4. data/Rakefile +93 -0
  5. data/TESTING.rdoc +30 -0
  6. data/example30/Gemfile +5 -0
  7. data/example30/Gemfile.lock +79 -0
  8. data/example30/README +256 -0
  9. data/example30/Rakefile +52 -0
  10. data/example30/app/controllers/accounts_controller.rb +22 -0
  11. data/example30/app/controllers/application_controller.rb +39 -0
  12. data/example30/app/controllers/figments_controller.rb +8 -0
  13. data/example30/app/helpers/application_helper.rb +2 -0
  14. data/example30/app/models/account.rb +3 -0
  15. data/example30/app/models/figment.rb +4 -0
  16. data/example30/app/views/accounts/index.html.erb +47 -0
  17. data/example30/app/views/layouts/application.html.erb +8 -0
  18. data/example30/config.ru +4 -0
  19. data/example30/config/application.rb +42 -0
  20. data/example30/config/boot.rb +13 -0
  21. data/example30/config/database.yml +21 -0
  22. data/example30/config/environment.rb +5 -0
  23. data/example30/config/environments/development.rb +26 -0
  24. data/example30/config/environments/production.rb +49 -0
  25. data/example30/config/environments/test.rb +35 -0
  26. data/example30/config/initializers/backtrace_silencers.rb +7 -0
  27. data/example30/config/initializers/inflections.rb +10 -0
  28. data/example30/config/initializers/mime_types.rb +5 -0
  29. data/example30/config/initializers/secret_token.rb +7 -0
  30. data/example30/config/initializers/session_store.rb +8 -0
  31. data/example30/config/locales/en.yml +5 -0
  32. data/example30/config/routes.rb +65 -0
  33. data/example30/db/migrate/20080702154628_create_accounts.rb +14 -0
  34. data/example30/db/migrate/20080702154820_create_figments.rb +14 -0
  35. data/example30/db/seeds.rb +7 -0
  36. data/example30/script/rails +6 -0
  37. data/example30/test/fixtures/accounts.yml +7 -0
  38. data/example30/test/functional/accounts_controller_test.rb +12 -0
  39. data/example30/test/integration/account_figments_test.rb +97 -0
  40. data/example30/test/performance/browsing_test.rb +9 -0
  41. data/example30/test/test_helper.rb +13 -0
  42. data/lib/data_fabric.rb +107 -0
  43. data/lib/data_fabric/connection_proxy.rb +176 -0
  44. data/lib/data_fabric/extensions.rb +39 -0
  45. data/lib/data_fabric/version.rb +5 -0
  46. data/test/connection_test.rb +142 -0
  47. data/test/database.yml +24 -0
  48. data/test/database.yml.mysql +36 -0
  49. data/test/database_test.rb +51 -0
  50. data/test/shard_test.rb +58 -0
  51. data/test/test_helper.rb +28 -0
  52. data/test/thread_test.rb +83 -0
  53. metadata +172 -0
@@ -0,0 +1,52 @@
1
+ # Add your own tasks in files placed in lib/tasks ending in .rake,
2
+ # for example lib/tasks/capistrano.rake, and they will automatically be available to Rake.
3
+
4
+ require File.expand_path('../config/application', __FILE__)
5
+ require 'rake'
6
+
7
+ Example30::Application.load_tasks
8
+
9
+ require 'fileutils'
10
+ include FileUtils::Verbose
11
+
12
+ namespace :db do
13
+ task :migrate do
14
+ require 'erb'
15
+ require 'logger'
16
+ require 'active_record'
17
+ reference = YAML::load(ERB.new(IO.read("config/database.yml")).result)
18
+ env = RAILS_ENV = ENV['RAILS_ENV'] || 'development'
19
+ DataFabric.logger = ActiveRecord::Base.logger = Logger.new(STDOUT)
20
+ ActiveRecord::Base.logger.level = Logger::INFO
21
+ ActiveRecord::Base.configurations = reference.dup
22
+ old_config = reference[env]
23
+ reference.each_key do |name|
24
+ next unless name.include? env
25
+ next if name.include? 'slave' # Replicated databases should not be touched directly
26
+
27
+ puts "Migrating #{name}"
28
+ ActiveRecord::Base.clear_active_connections!
29
+ ActiveRecord::Base.configurations[env] = reference[name]
30
+ ActiveRecord::Base.establish_connection RAILS_ENV
31
+ ActiveRecord::Migration.verbose = ENV["VERBOSE"] ? ENV["VERBOSE"] == "true" : true
32
+ ActiveRecord::Migrator.migrate("db/migrate/", ENV["VERSION"] ? ENV["VERSION"].to_i : nil)
33
+ end
34
+ end
35
+ end
36
+
37
+ task :default => [:test]
38
+
39
+ namespace :app do
40
+ task :prepare => [:clean, :migrate]
41
+
42
+ task :clean do
43
+ rm_rf 'log'
44
+ rm_rf Dir['db/*.sqlite3']
45
+ rm_f 'db/schema.rb'
46
+ end
47
+
48
+ task :migrate do
49
+ puts `rake db:migrate`
50
+ puts `rake RAILS_ENV=test db:migrate`
51
+ end
52
+ end
@@ -0,0 +1,22 @@
1
+ class AccountsController < ApplicationController
2
+ layout 'application'
3
+
4
+ def index
5
+ @accounts = Account.find(:all)
6
+ end
7
+
8
+ def choose
9
+ @account = Account.find(params[:id])
10
+ if @account
11
+ session[:account_id] = @account.id
12
+ flash[:notice] = "Selected account: #{@account.name}"
13
+ end
14
+ redirect_to '/'
15
+ end
16
+
17
+ def create
18
+ Account.create!(params[:acct])
19
+ flash[:notice] = "Account created successfully"
20
+ redirect_to '/'
21
+ end
22
+ end
@@ -0,0 +1,39 @@
1
+ # Filters added to this controller apply to all controllers in the application.
2
+ # Likewise, all the methods added will be available for all controllers.
3
+
4
+ class ApplicationController < ActionController::Base
5
+ helper :all # include all helpers, all the time
6
+
7
+ # See ActionController::RequestForgeryProtection for details
8
+ # Uncomment the :secret if you're not using the cookie session store
9
+ protect_from_forgery # :secret => '44f8cf8a0491c23ae99c031a900123cc'
10
+
11
+ # See ActionController::Base for details
12
+ # Uncomment this to filter the contents of submitted sensitive data parameters
13
+ # from your application log (in this case, all fields with names like "password").
14
+ # filter_parameter_logging :password
15
+
16
+ before_filter :find_account
17
+ around_filter :select_shard
18
+
19
+ private
20
+ def find_account
21
+ aid = session[:account_id]
22
+ if aid && aid.to_i != 0
23
+ begin
24
+ @account = Account.find(Integer(aid))
25
+ rescue ActiveRecord::RecordNotFound => e
26
+ RAILS_DEFAULT_LOGGER.warn "No such account #{aid}, skipping..."
27
+ end
28
+ end
29
+ session[:account_id] = nil unless @account
30
+ end
31
+
32
+ def select_shard(&block)
33
+ if @account
34
+ DataFabric.activate_shard(:shard => @account.shard, &block)
35
+ else
36
+ yield
37
+ end
38
+ end
39
+ end
@@ -0,0 +1,8 @@
1
+ class FigmentsController < ApplicationController
2
+
3
+ def create
4
+ @account.figments.create!(params[:figment])
5
+ flash[:notice] = "Figment created"
6
+ redirect_to '/'
7
+ end
8
+ end
@@ -0,0 +1,2 @@
1
+ module ApplicationHelper
2
+ end
@@ -0,0 +1,3 @@
1
+ class Account < ActiveRecord::Base
2
+ has_many :figments
3
+ end
@@ -0,0 +1,4 @@
1
+ class Figment < ActiveRecord::Base
2
+ data_fabric :shard_by => 'shard', :replicated => false
3
+ belongs_to :account
4
+ end
@@ -0,0 +1,47 @@
1
+ <hr/>
2
+ <h2>Accounts</h2>
3
+ <table>
4
+ <thead>
5
+ <tr><th>ID</th><th>Name</th><th>Shard</th><th>&nbsp;</th></tr>
6
+ </thead>
7
+ <tbody>
8
+ <% @accounts.each do |account| %>
9
+ <tr>
10
+ <td><%= account.id %></td>
11
+ <td><%= account.name %></td>
12
+ <td><%= account.shard %></td>
13
+ <td><%= link_to 'Choose', choose_account_path(account) %></td>
14
+ </tr>
15
+ <% end %>
16
+ </tbody>
17
+ </table>
18
+ <hr/>
19
+ <h2>New Account</h2>
20
+ <p>
21
+ <%= form_for :acct, :url => accounts_path do |f| %>
22
+ Name : <%= f.text_field :name %><br/>
23
+ Shard: <%= select 'acct', 'shard', [[0,0],[1,1]] %><br/>
24
+ <%= submit_tag 'Save' %>
25
+ <% end %>
26
+ </p>
27
+ <hr/>
28
+ <% if @account %>
29
+ <h2>Current Account: <%= @account.name %></h2>
30
+ <table>
31
+ <thead>
32
+ <tr><th>ID</th><th>Account</th><th>Value</th></tr>
33
+ </thead>
34
+ <tbody>
35
+ <% @account.figments.each do |fig| %>
36
+ <tr>
37
+ <td><%= fig.id %></td>
38
+ <td><%= fig.account.name %></td>
39
+ <td><%= fig.value %></td>
40
+ </tr>
41
+ <% end %>
42
+ </tbody>
43
+ </table>
44
+ Add Fig: <%= form_for :figment, :url => figments_path do |f| %><%= f.text_field :value %><%= submit_tag 'Save' %><% end %>
45
+ <% else %>
46
+ <em>No account selected.</em>
47
+ <% end %>
@@ -0,0 +1,8 @@
1
+ <html>
2
+ <body>
3
+ <div id='flash' style="background-color: #DDFFDD;">
4
+ <%= flash[:notice] %>
5
+ </div>
6
+ <%= yield %>
7
+ </body>
8
+ </html>
@@ -0,0 +1,4 @@
1
+ # This file is used by Rack-based servers to start the application.
2
+
3
+ require ::File.expand_path('../config/environment', __FILE__)
4
+ run Example30::Application
@@ -0,0 +1,42 @@
1
+ require File.expand_path('../boot', __FILE__)
2
+
3
+ require 'rails/all'
4
+
5
+ # If you have a Gemfile, require the gems listed there, including any gems
6
+ # you've limited to :test, :development, or :production.
7
+ Bundler.require(:default, Rails.env) if defined?(Bundler)
8
+
9
+ module Example30
10
+ class Application < Rails::Application
11
+ # Settings in config/environments/* take precedence over those specified here.
12
+ # Application configuration should go into files in config/initializers
13
+ # -- all .rb files in that directory are automatically loaded.
14
+
15
+ # Custom directories with classes and modules you want to be autoloadable.
16
+ # config.autoload_paths += %W(#{config.root}/extras)
17
+
18
+ # Only load the plugins named here, in the order given (default is alphabetical).
19
+ # :all can be used as a placeholder for all plugins not explicitly named.
20
+ # config.plugins = [ :exception_notification, :ssl_requirement, :all ]
21
+
22
+ # Activate observers that should always be running.
23
+ # config.active_record.observers = :cacher, :garbage_collector, :forum_observer
24
+
25
+ # Set Time.zone default to the specified zone and make Active Record auto-convert to this zone.
26
+ # Run "rake -D time" for a list of tasks for finding time zone names. Default is UTC.
27
+ # config.time_zone = 'Central Time (US & Canada)'
28
+
29
+ # The default locale is :en and all translations from config/locales/*.rb,yml are auto loaded.
30
+ # config.i18n.load_path += Dir[Rails.root.join('my', 'locales', '*.{rb,yml}').to_s]
31
+ # config.i18n.default_locale = :de
32
+
33
+ # JavaScript files you want as :defaults (application.js is always included).
34
+ # config.action_view.javascript_expansions[:defaults] = %w(jquery rails)
35
+
36
+ # Configure the default encoding used in templates for Ruby 1.9.
37
+ config.encoding = "utf-8"
38
+
39
+ # Configure sensitive parameters which will be filtered from the log file.
40
+ config.filter_parameters += [:password]
41
+ end
42
+ end
@@ -0,0 +1,13 @@
1
+ require 'rubygems'
2
+
3
+ # Set up gems listed in the Gemfile.
4
+ gemfile = File.expand_path('../../Gemfile', __FILE__)
5
+ begin
6
+ ENV['BUNDLE_GEMFILE'] = gemfile
7
+ require 'bundler'
8
+ Bundler.setup
9
+ rescue Bundler::GemNotFound => e
10
+ STDERR.puts e.message
11
+ STDERR.puts "Try running `bundle install`."
12
+ exit!
13
+ end if File.exist?(gemfile)
@@ -0,0 +1,21 @@
1
+ # SQLite version 3.x
2
+ # gem install sqlite3-ruby (not necessary on OS X Leopard)
3
+
4
+ <% %w(development test production).each do |env| %>
5
+
6
+ <%= env %>:
7
+ adapter: sqlite3
8
+ database: <%= "db/#{env}.sqlite3" %>
9
+ timeout: 5000
10
+
11
+ <%= "shard_0_#{env}" %>:
12
+ adapter: sqlite3
13
+ database: <%= "db/s0_#{env}.sqlite3" %>
14
+ timeout: 5000
15
+
16
+ <%= "shard_1_#{env}" %>:
17
+ adapter: sqlite3
18
+ database: <%= "db/s1_#{env}.sqlite3" %>
19
+ timeout: 5000
20
+
21
+ <% end %>
@@ -0,0 +1,5 @@
1
+ # Load the rails application
2
+ require File.expand_path('../application', __FILE__)
3
+
4
+ # Initialize the rails application
5
+ Example30::Application.initialize!
@@ -0,0 +1,26 @@
1
+ Example30::Application.configure do
2
+ # Settings specified here will take precedence over those in config/application.rb
3
+
4
+ # In the development environment your application's code is reloaded on
5
+ # every request. This slows down response time but is perfect for development
6
+ # since you don't have to restart the webserver when you make code changes.
7
+ config.cache_classes = false
8
+
9
+ # Log error messages when you accidentally call methods on nil.
10
+ config.whiny_nils = true
11
+
12
+ # Show full error reports and disable caching
13
+ config.consider_all_requests_local = true
14
+ config.action_view.debug_rjs = true
15
+ config.action_controller.perform_caching = false
16
+
17
+ # Don't care if the mailer can't send
18
+ config.action_mailer.raise_delivery_errors = false
19
+
20
+ # Print deprecation notices to the Rails logger
21
+ config.active_support.deprecation = :log
22
+
23
+ # Only use best-standards-support built into browsers
24
+ config.action_dispatch.best_standards_support = :builtin
25
+ end
26
+
@@ -0,0 +1,49 @@
1
+ Example30::Application.configure do
2
+ # Settings specified here will take precedence over those in config/application.rb
3
+
4
+ # The production environment is meant for finished, "live" apps.
5
+ # Code is not reloaded between requests
6
+ config.cache_classes = true
7
+
8
+ # Full error reports are disabled and caching is turned on
9
+ config.consider_all_requests_local = false
10
+ config.action_controller.perform_caching = true
11
+
12
+ # Specifies the header that your server uses for sending files
13
+ config.action_dispatch.x_sendfile_header = "X-Sendfile"
14
+
15
+ # For nginx:
16
+ # config.action_dispatch.x_sendfile_header = 'X-Accel-Redirect'
17
+
18
+ # If you have no front-end server that supports something like X-Sendfile,
19
+ # just comment this out and Rails will serve the files
20
+
21
+ # See everything in the log (default is :info)
22
+ # config.log_level = :debug
23
+
24
+ # Use a different logger for distributed setups
25
+ # config.logger = SyslogLogger.new
26
+
27
+ # Use a different cache store in production
28
+ # config.cache_store = :mem_cache_store
29
+
30
+ # Disable Rails's static asset server
31
+ # In production, Apache or nginx will already do this
32
+ config.serve_static_assets = false
33
+
34
+ # Enable serving of images, stylesheets, and javascripts from an asset server
35
+ # config.action_controller.asset_host = "http://assets.example.com"
36
+
37
+ # Disable delivery errors, bad email addresses will be ignored
38
+ # config.action_mailer.raise_delivery_errors = false
39
+
40
+ # Enable threaded mode
41
+ # config.threadsafe!
42
+
43
+ # Enable locale fallbacks for I18n (makes lookups for any locale fall back to
44
+ # the I18n.default_locale when a translation can not be found)
45
+ config.i18n.fallbacks = true
46
+
47
+ # Send deprecation notices to registered listeners
48
+ config.active_support.deprecation = :notify
49
+ end
@@ -0,0 +1,35 @@
1
+ Example30::Application.configure do
2
+ # Settings specified here will take precedence over those in config/application.rb
3
+
4
+ # The test environment is used exclusively to run your application's
5
+ # test suite. You never need to work with it otherwise. Remember that
6
+ # your test database is "scratch space" for the test suite and is wiped
7
+ # and recreated between test runs. Don't rely on the data there!
8
+ config.cache_classes = true
9
+
10
+ # Log error messages when you accidentally call methods on nil.
11
+ config.whiny_nils = true
12
+
13
+ # Show full error reports and disable caching
14
+ config.consider_all_requests_local = true
15
+ config.action_controller.perform_caching = false
16
+
17
+ # Raise exceptions instead of rendering exception templates
18
+ config.action_dispatch.show_exceptions = false
19
+
20
+ # Disable request forgery protection in test environment
21
+ config.action_controller.allow_forgery_protection = false
22
+
23
+ # Tell Action Mailer not to deliver emails to the real world.
24
+ # The :test delivery method accumulates sent emails in the
25
+ # ActionMailer::Base.deliveries array.
26
+ config.action_mailer.delivery_method = :test
27
+
28
+ # Use SQL instead of Active Record's schema dumper when creating the test database.
29
+ # This is necessary if your schema can't be completely dumped by the schema dumper,
30
+ # like if you have constraints or database-specific column types
31
+ # config.active_record.schema_format = :sql
32
+
33
+ # Print deprecation notices to the stderr
34
+ config.active_support.deprecation = :stderr
35
+ end
@@ -0,0 +1,7 @@
1
+ # Be sure to restart your server when you modify this file.
2
+
3
+ # You can add backtrace silencers for libraries that you're using but don't wish to see in your backtraces.
4
+ # Rails.backtrace_cleaner.add_silencer { |line| line =~ /my_noisy_library/ }
5
+
6
+ # You can also remove all the silencers if you're trying to debug a problem that might stem from framework code.
7
+ # Rails.backtrace_cleaner.remove_silencers!
@@ -0,0 +1,10 @@
1
+ # Be sure to restart your server when you modify this file.
2
+
3
+ # Add new inflection rules using the following format
4
+ # (all these examples are active by default):
5
+ # ActiveSupport::Inflector.inflections do |inflect|
6
+ # inflect.plural /^(ox)$/i, '\1en'
7
+ # inflect.singular /^(ox)en/i, '\1'
8
+ # inflect.irregular 'person', 'people'
9
+ # inflect.uncountable %w( fish sheep )
10
+ # end
@@ -0,0 +1,5 @@
1
+ # Be sure to restart your server when you modify this file.
2
+
3
+ # Add new mime types for use in respond_to blocks:
4
+ # Mime::Type.register "text/richtext", :rtf
5
+ # Mime::Type.register_alias "text/html", :iphone
@@ -0,0 +1,7 @@
1
+ # Be sure to restart your server when you modify this file.
2
+
3
+ # Your secret key for verifying the integrity of signed cookies.
4
+ # If you change this key, all old signed cookies will become invalid!
5
+ # Make sure the secret is at least 30 characters and all random,
6
+ # no regular words or you'll be exposed to dictionary attacks.
7
+ Example30::Application.config.secret_token = 'a60db9b42489c36026c6307910c07803fbca1475341a2080a7ccb708fa02d7ffed8bcfb92482fa9bed95899f6e11989dc8fc37fbd92e7b0331b3046717346622'
@@ -0,0 +1,8 @@
1
+ # Be sure to restart your server when you modify this file.
2
+
3
+ Example30::Application.config.session_store :cookie_store, :key => '_example30_session'
4
+
5
+ # Use the database for sessions instead of the cookie-based default,
6
+ # which shouldn't be used to store highly confidential information
7
+ # (create the session table with "rails generate session_migration")
8
+ # Example30::Application.config.session_store :active_record_store