data_fabric 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (81) hide show
  1. data/CHANGELOG +1 -0
  2. data/Manifest +79 -0
  3. data/README.rdoc +108 -0
  4. data/Rakefile +69 -0
  5. data/TESTING.rdoc +34 -0
  6. data/TODO +1 -0
  7. data/data_fabric.gemspec +166 -0
  8. data/example/Rakefile +58 -0
  9. data/example/app/controllers/accounts_controller.rb +22 -0
  10. data/example/app/controllers/application.rb +39 -0
  11. data/example/app/controllers/figments_controller.rb +8 -0
  12. data/example/app/helpers/accounts_helper.rb +2 -0
  13. data/example/app/helpers/application_helper.rb +3 -0
  14. data/example/app/helpers/figments_helper.rb +2 -0
  15. data/example/app/models/account.rb +3 -0
  16. data/example/app/models/figment.rb +4 -0
  17. data/example/app/views/accounts/index.html.erb +47 -0
  18. data/example/app/views/layouts/application.html.erb +8 -0
  19. data/example/config/boot.rb +109 -0
  20. data/example/config/database.yml +21 -0
  21. data/example/config/environment.rb +67 -0
  22. data/example/config/environments/development.rb +17 -0
  23. data/example/config/environments/production.rb +22 -0
  24. data/example/config/environments/test.rb +22 -0
  25. data/example/config/initializers/inflections.rb +10 -0
  26. data/example/config/initializers/mime_types.rb +5 -0
  27. data/example/config/initializers/new_rails_defaults.rb +15 -0
  28. data/example/config/routes.rb +45 -0
  29. data/example/db/development.sqlite3 +0 -0
  30. data/example/db/migrate/20080702154628_create_accounts.rb +14 -0
  31. data/example/db/migrate/20080702154820_create_figments.rb +14 -0
  32. data/example/db/s0_development.sqlite3 +0 -0
  33. data/example/db/s0_test.sqlite3 +0 -0
  34. data/example/db/s1_development.sqlite3 +0 -0
  35. data/example/db/s1_test.sqlite3 +0 -0
  36. data/example/db/schema.rb +28 -0
  37. data/example/db/test.sqlite3 +0 -0
  38. data/example/public/404.html +30 -0
  39. data/example/public/422.html +30 -0
  40. data/example/public/500.html +30 -0
  41. data/example/public/dispatch.cgi +10 -0
  42. data/example/public/dispatch.fcgi +24 -0
  43. data/example/public/dispatch.rb +10 -0
  44. data/example/public/favicon.ico +0 -0
  45. data/example/public/images/rails.png +0 -0
  46. data/example/public/javascripts/application.js +2 -0
  47. data/example/public/javascripts/controls.js +963 -0
  48. data/example/public/javascripts/dragdrop.js +972 -0
  49. data/example/public/javascripts/effects.js +1120 -0
  50. data/example/public/javascripts/prototype.js +4225 -0
  51. data/example/public/robots.txt +5 -0
  52. data/example/script/about +3 -0
  53. data/example/script/console +3 -0
  54. data/example/script/dbconsole +3 -0
  55. data/example/script/destroy +3 -0
  56. data/example/script/generate +3 -0
  57. data/example/script/performance/benchmarker +3 -0
  58. data/example/script/performance/profiler +3 -0
  59. data/example/script/performance/request +3 -0
  60. data/example/script/plugin +3 -0
  61. data/example/script/process/inspector +3 -0
  62. data/example/script/process/reaper +3 -0
  63. data/example/script/process/spawner +3 -0
  64. data/example/script/runner +3 -0
  65. data/example/script/server +3 -0
  66. data/example/test/fixtures/accounts.yml +7 -0
  67. data/example/test/functional/accounts_controller_test.rb +12 -0
  68. data/example/test/integration/account_figments_test.rb +95 -0
  69. data/example/test/test_helper.rb +41 -0
  70. data/example/vendor/plugins/data_fabric/init.rb +1 -0
  71. data/example/vendor/plugins/data_fabric/lib/data_fabric.rb +231 -0
  72. data/init.rb +1 -0
  73. data/lib/data_fabric/version.rb +5 -0
  74. data/lib/data_fabric.rb +231 -0
  75. data/test/connection_test.rb +103 -0
  76. data/test/database.yml +27 -0
  77. data/test/database_test.rb +39 -0
  78. data/test/shard_test.rb +24 -0
  79. data/test/test_helper.rb +17 -0
  80. data/test/thread_test.rb +91 -0
  81. metadata +164 -0
data/example/Rakefile ADDED
@@ -0,0 +1,58 @@
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.join(File.dirname(__FILE__), 'config', 'boot'))
5
+
6
+ require 'rake'
7
+ require 'rake/testtask'
8
+ require 'rake/rdoctask'
9
+ require 'tasks/rails'
10
+
11
+ require 'fileutils'
12
+ include FileUtils::Verbose
13
+
14
+ namespace :db do
15
+ task :migrate do
16
+ require 'erb'
17
+ require 'logger'
18
+ require 'active_record'
19
+ reference = YAML::load(ERB.new(IO.read("config/database.yml")).result)
20
+ env = RAILS_ENV = ENV['RAILS_ENV'] || 'development'
21
+ ActiveRecord::Base.logger = Logger.new(STDOUT)
22
+ ActiveRecord::Base.logger.level = Logger::WARN
23
+ ActiveRecord::Base.configurations = reference.dup
24
+ old_config = reference[env]
25
+ reference.each_key do |name|
26
+ next unless name.include? env
27
+ next if name.include? 'slave' # Replicated databases should not be touched directly
28
+
29
+ puts "Migrating #{name}"
30
+ ActiveRecord::Base.clear_active_connections!
31
+ ActiveRecord::Base.configurations[env] = reference[name]
32
+ ActiveRecord::Base.establish_connection RAILS_ENV
33
+ ActiveRecord::Migration.verbose = ENV["VERBOSE"] ? ENV["VERBOSE"] == "true" : true
34
+ ActiveRecord::Migrator.migrate("db/migrate/", ENV["VERSION"] ? ENV["VERSION"].to_i : nil)
35
+ end
36
+ end
37
+ end
38
+
39
+ namespace :app do
40
+ task :prepare => [:clean, :copy_plugin, :migrate]
41
+
42
+ task :copy_plugin do
43
+ mkdir_p 'vendor/plugins/data_fabric'
44
+ cp_r '../lib', 'vendor/plugins/data_fabric'
45
+ cp '../init.rb', 'vendor/plugins/data_fabric'
46
+ end
47
+
48
+ task :clean do
49
+ rm_rf 'vendor/plugins/data_fabric'
50
+ rm_f 'db/*.sqlite3'
51
+ end
52
+
53
+ task :migrate do
54
+ sh "rake db:migrate"
55
+ sh "rake RAILS_ENV=test db:migrate"
56
+ end
57
+ end
58
+
@@ -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 AccountsHelper
2
+ end
@@ -0,0 +1,3 @@
1
+ # Methods added to this helper will be available to all templates in the application.
2
+ module ApplicationHelper
3
+ end
@@ -0,0 +1,2 @@
1
+ module FigmentsHelper
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
+ connection_topology :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,109 @@
1
+ # Don't change this file!
2
+ # Configure your app in config/environment.rb and config/environments/*.rb
3
+
4
+ RAILS_ROOT = "#{File.dirname(__FILE__)}/.." unless defined?(RAILS_ROOT)
5
+
6
+ module Rails
7
+ class << self
8
+ def boot!
9
+ unless booted?
10
+ preinitialize
11
+ pick_boot.run
12
+ end
13
+ end
14
+
15
+ def booted?
16
+ defined? Rails::Initializer
17
+ end
18
+
19
+ def pick_boot
20
+ (vendor_rails? ? VendorBoot : GemBoot).new
21
+ end
22
+
23
+ def vendor_rails?
24
+ File.exist?("#{RAILS_ROOT}/vendor/rails")
25
+ end
26
+
27
+ def preinitialize
28
+ load(preinitializer_path) if File.exist?(preinitializer_path)
29
+ end
30
+
31
+ def preinitializer_path
32
+ "#{RAILS_ROOT}/config/preinitializer.rb"
33
+ end
34
+ end
35
+
36
+ class Boot
37
+ def run
38
+ load_initializer
39
+ Rails::Initializer.run(:set_load_path)
40
+ end
41
+ end
42
+
43
+ class VendorBoot < Boot
44
+ def load_initializer
45
+ require "#{RAILS_ROOT}/vendor/rails/railties/lib/initializer"
46
+ Rails::Initializer.run(:install_gem_spec_stubs)
47
+ end
48
+ end
49
+
50
+ class GemBoot < Boot
51
+ def load_initializer
52
+ self.class.load_rubygems
53
+ load_rails_gem
54
+ require 'initializer'
55
+ end
56
+
57
+ def load_rails_gem
58
+ if version = self.class.gem_version
59
+ gem 'rails', version
60
+ else
61
+ gem 'rails'
62
+ end
63
+ rescue Gem::LoadError => load_error
64
+ $stderr.puts %(Missing the Rails #{version} gem. Please `gem install -v=#{version} rails`, update your RAILS_GEM_VERSION setting in config/environment.rb for the Rails version you do have installed, or comment out RAILS_GEM_VERSION to use the latest version installed.)
65
+ exit 1
66
+ end
67
+
68
+ class << self
69
+ def rubygems_version
70
+ Gem::RubyGemsVersion if defined? Gem::RubyGemsVersion
71
+ end
72
+
73
+ def gem_version
74
+ if defined? RAILS_GEM_VERSION
75
+ RAILS_GEM_VERSION
76
+ elsif ENV.include?('RAILS_GEM_VERSION')
77
+ ENV['RAILS_GEM_VERSION']
78
+ else
79
+ parse_gem_version(read_environment_rb)
80
+ end
81
+ end
82
+
83
+ def load_rubygems
84
+ require 'rubygems'
85
+
86
+ unless rubygems_version >= '0.9.4'
87
+ $stderr.puts %(Rails requires RubyGems >= 0.9.4 (you have #{rubygems_version}). Please `gem update --system` and try again.)
88
+ exit 1
89
+ end
90
+
91
+ rescue LoadError
92
+ $stderr.puts %(Rails requires RubyGems >= 0.9.4. Please install RubyGems and try again: http://rubygems.rubyforge.org)
93
+ exit 1
94
+ end
95
+
96
+ def parse_gem_version(text)
97
+ $1 if text =~ /^[^#]*RAILS_GEM_VERSION\s*=\s*["']([!~<>=]*\s*[\d.]+)["']/
98
+ end
99
+
100
+ private
101
+ def read_environment_rb
102
+ File.read("#{RAILS_ROOT}/config/environment.rb")
103
+ end
104
+ end
105
+ end
106
+ end
107
+
108
+ # All that for this:
109
+ Rails.boot!
@@ -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,67 @@
1
+ # Be sure to restart your server when you modify this file
2
+
3
+ # Uncomment below to force Rails into production mode when
4
+ # you don't control web/app server and can't set it the proper way
5
+ # ENV['RAILS_ENV'] ||= 'production'
6
+
7
+ # Specifies gem version of Rails to use when vendor/rails is not present
8
+ RAILS_GEM_VERSION = '2.1.0' unless defined? RAILS_GEM_VERSION
9
+
10
+ # Bootstrap the Rails environment, frameworks, and default configuration
11
+ require File.join(File.dirname(__FILE__), 'boot')
12
+
13
+ Rails::Initializer.run do |config|
14
+ # Settings in config/environments/* take precedence over those specified here.
15
+ # Application configuration should go into files in config/initializers
16
+ # -- all .rb files in that directory are automatically loaded.
17
+ # See Rails::Configuration for more options.
18
+
19
+ # Skip frameworks you're not going to use. To use Rails without a database
20
+ # you must remove the Active Record framework.
21
+ # config.frameworks -= [ :active_record, :active_resource, :action_mailer ]
22
+
23
+ # Specify gems that this application depends on.
24
+ # They can then be installed with "rake gems:install" on new installations.
25
+ # config.gem "bj"
26
+ # config.gem "hpricot", :version => '0.6', :source => "http://code.whytheluckystiff.net"
27
+ # config.gem "aws-s3", :lib => "aws/s3"
28
+
29
+ # Only load the plugins named here, in the order given. By default, all plugins
30
+ # in vendor/plugins are loaded in alphabetical order.
31
+ # :all can be used as a placeholder for all plugins not explicitly named
32
+ # config.plugins = [ :exception_notification, :ssl_requirement, :all ]
33
+
34
+ # Add additional load paths for your own custom dirs
35
+ # config.load_paths += %W( #{RAILS_ROOT}/extras )
36
+
37
+ # Force all environments to use the same logger level
38
+ # (by default production uses :info, the others :debug)
39
+ # config.log_level = :debug
40
+
41
+ # Make Time.zone default to the specified zone, and make Active Record store time values
42
+ # in the database in UTC, and return them converted to the specified local zone.
43
+ # Run "rake -D time" for a list of tasks for finding time zone names. Uncomment to use default local time.
44
+ config.time_zone = 'UTC'
45
+
46
+ # Your secret key for verifying cookie session data integrity.
47
+ # If you change this key, all old sessions will become invalid!
48
+ # Make sure the secret is at least 30 characters and all random,
49
+ # no regular words or you'll be exposed to dictionary attacks.
50
+ config.action_controller.session = {
51
+ :session_key => '_example_session',
52
+ :secret => 'fa331d8ca4b067b31008ae157b6785319b76f59e202d9e917a9aa79a1c5f8a87c1dbb1d20bd242a5dc598b66e255e02f8c8516b2d0b64f1f7e6e60ce90205f77'
53
+ }
54
+
55
+ # Use the database for sessions instead of the cookie-based default,
56
+ # which shouldn't be used to store highly confidential information
57
+ # (create the session table with "rake db:sessions:create")
58
+ # config.action_controller.session_store = :active_record_store
59
+
60
+ # Use SQL instead of Active Record's schema dumper when creating the test database.
61
+ # This is necessary if your schema can't be completely dumped by the schema dumper,
62
+ # like if you have constraints or database-specific column types
63
+ # config.active_record.schema_format = :sql
64
+
65
+ # Activate observers that should always be running
66
+ # config.active_record.observers = :cacher, :garbage_collector
67
+ end
@@ -0,0 +1,17 @@
1
+ # Settings specified here will take precedence over those in config/environment.rb
2
+
3
+ # In the development environment your application's code is reloaded on
4
+ # every request. This slows down response time but is perfect for development
5
+ # since you don't have to restart the webserver when you make code changes.
6
+ config.cache_classes = false
7
+
8
+ # Log error messages when you accidentally call methods on nil.
9
+ config.whiny_nils = true
10
+
11
+ # Show full error reports and disable caching
12
+ config.action_controller.consider_all_requests_local = true
13
+ config.action_view.debug_rjs = true
14
+ config.action_controller.perform_caching = false
15
+
16
+ # Don't care if the mailer can't send
17
+ config.action_mailer.raise_delivery_errors = false
@@ -0,0 +1,22 @@
1
+ # Settings specified here will take precedence over those in config/environment.rb
2
+
3
+ # The production environment is meant for finished, "live" apps.
4
+ # Code is not reloaded between requests
5
+ config.cache_classes = true
6
+
7
+ # Use a different logger for distributed setups
8
+ # config.logger = SyslogLogger.new
9
+
10
+ # Full error reports are disabled and caching is turned on
11
+ config.action_controller.consider_all_requests_local = false
12
+ config.action_controller.perform_caching = true
13
+ config.action_view.cache_template_loading = true
14
+
15
+ # Use a different cache store in production
16
+ # config.cache_store = :mem_cache_store
17
+
18
+ # Enable serving of images, stylesheets, and javascripts from an asset server
19
+ # config.action_controller.asset_host = "http://assets.example.com"
20
+
21
+ # Disable delivery errors, bad email addresses will be ignored
22
+ # config.action_mailer.raise_delivery_errors = false