Permalink
Browse files

initial commit

  • Loading branch information...
0 parents commit fc17e02609058d3f74230ebf4d60f84741081794 @Deradon committed Oct 7, 2011
Showing with 866 additions and 0 deletions.
  1. +6 −0 .gitignore
  2. +17 −0 Gemfile
  3. +97 −0 Gemfile.lock
  4. +21 −0 MIT-LICENSE
  5. +3 −0 README.rdoc
  6. +37 −0 Rakefile
  7. +180 −0 lib/look_up_table.rb
  8. +3 −0 lib/look_up_table/version.rb
  9. +4 −0 lib/tasks/look_up_table_tasks.rake
  10. +23 −0 look_up_table.gemspec
  11. +7 −0 test/dummy/Rakefile
  12. +9 −0 test/dummy/app/assets/javascripts/application.js
  13. +7 −0 test/dummy/app/assets/stylesheets/application.css
  14. +3 −0 test/dummy/app/controllers/application_controller.rb
  15. +2 −0 test/dummy/app/helpers/application_helper.rb
  16. 0 test/dummy/app/mailers/.gitkeep
  17. 0 test/dummy/app/models/.gitkeep
  18. +14 −0 test/dummy/app/views/layouts/application.html.erb
  19. +4 −0 test/dummy/config.ru
  20. +45 −0 test/dummy/config/application.rb
  21. +10 −0 test/dummy/config/boot.rb
  22. +25 −0 test/dummy/config/database.yml
  23. +5 −0 test/dummy/config/environment.rb
  24. +30 −0 test/dummy/config/environments/development.rb
  25. +60 −0 test/dummy/config/environments/production.rb
  26. +39 −0 test/dummy/config/environments/test.rb
  27. +7 −0 test/dummy/config/initializers/backtrace_silencers.rb
  28. +10 −0 test/dummy/config/initializers/inflections.rb
  29. +5 −0 test/dummy/config/initializers/mime_types.rb
  30. +7 −0 test/dummy/config/initializers/secret_token.rb
  31. +8 −0 test/dummy/config/initializers/session_store.rb
  32. +14 −0 test/dummy/config/initializers/wrap_parameters.rb
  33. +5 −0 test/dummy/config/locales/en.yml
  34. +58 −0 test/dummy/config/routes.rb
  35. 0 test/dummy/lib/assets/.gitkeep
  36. 0 test/dummy/log/.gitkeep
  37. +26 −0 test/dummy/public/404.html
  38. +26 −0 test/dummy/public/422.html
  39. +26 −0 test/dummy/public/500.html
  40. 0 test/dummy/public/favicon.ico
  41. +6 −0 test/dummy/script/rails
  42. +7 −0 test/look_up_table_test.rb
  43. +10 −0 test/test_helper.rb
@@ -0,0 +1,6 @@
+.bundle/
+log/*.log
+pkg/
+test/dummy/db/*.sqlite3
+test/dummy/log/*.log
+test/dummy/tmp/
@@ -0,0 +1,17 @@
+source "http://rubygems.org"
+
+# Declare your gem's dependencies in look_up_table.gemspec.
+# Bundler will treat runtime dependencies like base dependencies, and
+# development dependencies will be added by default to the :development group.
+gemspec
+
+# jquery-rails is used by the dummy application
+gem "jquery-rails"
+
+# Declare any dependencies that are still in development here instead of in
+# your gemspec. These might include edge Rails or gems from your path or
+# Git. Remember to move these dependencies to your gemspec before releasing
+# your gem to rubygems.org.
+
+# To use debugger
+# gem 'ruby-debug19', :require => 'ruby-debug'
@@ -0,0 +1,97 @@
+PATH
+ remote: .
+ specs:
+ look_up_table (0.0.1)
+ rails (~> 3.1.1)
+
+GEM
+ remote: http://rubygems.org/
+ specs:
+ actionmailer (3.1.1)
+ actionpack (= 3.1.1)
+ mail (~> 2.3.0)
+ actionpack (3.1.1)
+ activemodel (= 3.1.1)
+ activesupport (= 3.1.1)
+ builder (~> 3.0.0)
+ erubis (~> 2.7.0)
+ i18n (~> 0.6)
+ rack (~> 1.3.2)
+ rack-cache (~> 1.1)
+ rack-mount (~> 0.8.2)
+ rack-test (~> 0.6.1)
+ sprockets (~> 2.0.2)
+ activemodel (3.1.1)
+ activesupport (= 3.1.1)
+ builder (~> 3.0.0)
+ i18n (~> 0.6)
+ activerecord (3.1.1)
+ activemodel (= 3.1.1)
+ activesupport (= 3.1.1)
+ arel (~> 2.2.1)
+ tzinfo (~> 0.3.29)
+ activeresource (3.1.1)
+ activemodel (= 3.1.1)
+ activesupport (= 3.1.1)
+ activesupport (3.1.1)
+ multi_json (~> 1.0)
+ arel (2.2.1)
+ builder (3.0.0)
+ erubis (2.7.0)
+ hike (1.2.1)
+ i18n (0.6.0)
+ jquery-rails (1.0.14)
+ railties (~> 3.0)
+ thor (~> 0.14)
+ mail (2.3.0)
+ i18n (>= 0.4.0)
+ mime-types (~> 1.16)
+ treetop (~> 1.4.8)
+ mime-types (1.16)
+ multi_json (1.0.3)
+ polyglot (0.3.2)
+ rack (1.3.4)
+ rack-cache (1.1)
+ rack (>= 0.4)
+ rack-mount (0.8.3)
+ rack (>= 1.0.0)
+ rack-ssl (1.3.2)
+ rack
+ rack-test (0.6.1)
+ rack (>= 1.0)
+ rails (3.1.1)
+ actionmailer (= 3.1.1)
+ actionpack (= 3.1.1)
+ activerecord (= 3.1.1)
+ activeresource (= 3.1.1)
+ activesupport (= 3.1.1)
+ bundler (~> 1.0)
+ railties (= 3.1.1)
+ railties (3.1.1)
+ actionpack (= 3.1.1)
+ activesupport (= 3.1.1)
+ rack-ssl (~> 1.3.2)
+ rake (>= 0.8.7)
+ rdoc (~> 3.4)
+ thor (~> 0.14.6)
+ rake (0.9.2)
+ rdoc (3.9.4)
+ sprockets (2.0.2)
+ hike (~> 1.2)
+ rack (~> 1.0)
+ tilt (!= 1.3.0, ~> 1.1)
+ sqlite3 (1.3.4)
+ thor (0.14.6)
+ tilt (1.3.3)
+ treetop (1.4.10)
+ polyglot
+ polyglot (>= 0.3.1)
+ tzinfo (0.3.30)
+
+PLATFORMS
+ ruby
+
+DEPENDENCIES
+ jquery-rails
+ look_up_table!
+ sqlite3
@@ -0,0 +1,21 @@
+Copyright 2011 Patrick Helm
+
+Permission is hereby granted, free of charge, to any person obtaining
+a copy of this software and associated documentation files (the
+"Software"), to deal in the Software without restriction, including
+without limitation the rights to use, copy, modify, merge, publish,
+distribute, sublicense, and/or sell copies of the Software, and to
+permit persons to whom the Software is furnished to do so, subject to
+the following conditions:
+
+The above copyright notice and this permission notice shall be
+included in all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
+LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
+OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
+WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+
@@ -0,0 +1,3 @@
+= LookUpTable
+
+This project rocks and uses MIT-LICENSE.
@@ -0,0 +1,37 @@
+#!/usr/bin/env rake
+begin
+ require 'bundler/setup'
+rescue LoadError
+ puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
+end
+begin
+ require 'rdoc/task'
+rescue LoadError
+ require 'rdoc/rdoc'
+ require 'rake/rdoctask'
+ RDoc::Task = Rake::RDocTask
+end
+
+RDoc::Task.new(:rdoc) do |rdoc|
+ rdoc.rdoc_dir = 'rdoc'
+ rdoc.title = 'LookUpTable'
+ rdoc.options << '--line-numbers'
+ rdoc.rdoc_files.include('README.rdoc')
+ rdoc.rdoc_files.include('lib/**/*.rb')
+end
+
+
+
+Bundler::GemHelper.install_tasks
+
+require 'rake/testtask'
+
+Rake::TestTask.new(:test) do |t|
+ t.libs << 'lib'
+ t.libs << 'test'
+ t.pattern = 'test/**/*_test.rb'
+ t.verbose = false
+end
+
+
+task :default => :test
@@ -0,0 +1,180 @@
+# look_up_table :page_title, :batch_size => 10000 do |lut, page|
+# lut[page.page_title] = page.page_id
+# end
+
+# # Page.page_title_lut("Berlin") => 14954
+# look_up_table :page_title
+
+# # Page.lut(:page_title, "Berlin")
+
+
+module LookUpTable
+ extend ActiveSupport::Concern
+
+ # Class methods
+ module ClassMethods
+ # Defining a LookUpTable
+ # TODO: Usage
+ def look_up_table(lut_name, options={})
+ options = {
+ :batch_size => 10000,
+ :read_on_init => true,
+ :skip_memcached => false, #TODO
+ :sql_mode => true,
+ :write_on_init => true
+ }.merge(options)
+
+ if block_given?
+ lut_write_to_cache(options[:batch_size], lut_name, options[:sql_mode], &Proc.new) if options[:write_on_init]
+ else
+ lut_write_to_cache(options[:batch_size], lut_name, options[:sql_mode]) if options[:write_on_init]
+ end
+
+ self.lut(lut_name) if options[:read_on_init]
+ end
+
+ # Write a LookUpTable into Cache
+ def lut_write_to_cache(batch_size, lut_name, sql_mode)
+ if sql_mode
+ if block_given?
+ lut_write_to_cache_sql_mode(batch_size, lut_name, &Proc.new)
+ else
+ lut_write_to_cache_sql_mode(batch_size, lut_name)
+ end
+ else
+ if block_given?
+ lut_write_to_cache_no_sql_mode(batch_size, lut_name, &Proc.new)
+ else
+ lut_write_to_cache_no_sql_mode(batch_size, lut_name)
+ end
+ end
+ end
+
+
+ def lut_write_to_cache_sql_mode(batch_size, lut_name)
+ batch_count = 0
+
+ self.find_in_batches(:batch_size => batch_size) do |items|
+ lut = {}
+ items.each do |item|
+ if block_given?
+ yield(lut, item)
+ else
+ lut[item.send(lut_name)] = item.id
+ end
+ end
+
+ self.lut_write_cache_item(lut_name, batch_count, lut)
+ batch_count += 1
+ end
+ end
+
+
+ def lut_write_to_cache_no_sql_mode(batch_size, lut_name)
+ lut = {}
+ batch_count = 0
+
+ yield(lut)
+ keys = lut.keys
+
+ while
+ key_block = keys.slice!(0,batch_size)
+ break if key_block.empty?
+
+ lut_block = {}
+ key_block.each{|key| lut_block[key] = lut[key]}
+
+ self.lut_write_cache_item(lut_name, batch_count, lut_block)
+ batch_count += 1
+ end
+ end
+
+
+ # Write a single Item into LookUpTable-Cache
+ def lut_write_cache_item(lut_name, lut_item, lut_data)
+ status = Rails.cache.write("#{lut_name}/#{lut_item}", lut_data)
+ Raise "Cache::write returned false" unless status
+ end
+
+ # Reads a single LookUpTable from Cache
+ def lut_read_from_cache(lut_name)
+ i = 0
+ lut = {}
+
+ while res = lut_read_cache_item(lut_name, i)
+ lut.merge!(res) if res
+ i += 1
+ end
+
+ return lut
+ end
+
+ # Reads a single item of a LookUpTable from Cache
+ def lut_read_cache_item(lut_name, lut_item)
+ Rails.cache.read("#{lut_name}/#{lut_item}")
+ end
+
+ # Call to a LookUpTable, example Usage:
+ # * Tag.lut(:name, "Berlin")
+ # Returns nil or stored objects
+ def lut(lut_name, search_by=nil)
+ @@look_up_tables ||= {}
+ @@look_up_tables[lut_name.to_sym] ||= lut_read_from_cache(lut_name) || {}
+
+ return @@look_up_tables[lut_name.to_sym][search_by] if search_by
+ return @@look_up_tables[lut_name.to_sym]
+ end
+
+
+
+# # Write Keys of cached Items to cache
+# # TODO *key_batch_size
+# def lut_write_cache_keys(lut_name, keys)
+# key_batch_size = 10000
+# keys.uniq!
+# i = 0
+# while key_block = keys.slice!(0,key_batch_size)
+# lut_write_cache_key_block(lut_name, i, key_block)
+# i += 1
+# end
+# end
+
+# # Write a single KeyBlock into LookUpTable-Key-Cache
+# def lut_write_cache_key_block(lut_name, key_item, key_data)
+# status = Rails.cache.write("#{lut_name}/k/#{key_item}", key_data)
+# Raise "Cache::write returned false" unless status
+# end
+
+# # TODO desc
+# def lut_read_cache_keys(lut_name)
+# #TODO
+# end
+
+# # TODO desc
+# def lut_read_cache_key_block(lut_name, key_item)
+# Rails.cache.read("#{lut_name}/k/#{key_item}")
+# end
+
+
+
+ # Delegating <attribute>_lut(args) method calls
+ def method_missing(m, *args, &block)
+ method_name = m.to_s
+ if method_name.end_with?("_lut")
+ lut_name = method_name[0..-5]
+ self.lut(lut_name, args.first)
+ else
+ raise "MethodNotFound: #{m}"
+ end
+ end
+ end
+
+# Instance methods
+# module InstanceMethods
+# def foobar
+# end
+# end
+end
+
+ActiveRecord::Base.send :include, LookUpTable
+
@@ -0,0 +1,3 @@
+module LookUpTable
+ VERSION = "0.0.1"
+end
@@ -0,0 +1,4 @@
+# desc "Explaining what the task does"
+# task :look_up_table do
+# # Task goes here
+# end
Oops, something went wrong.

0 comments on commit fc17e02

Please sign in to comment.