bigindex 0.1.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 (54) hide show
  1. data/MIT-LICENSE +20 -0
  2. data/README.rdoc +58 -0
  3. data/Rakefile +14 -0
  4. data/VERSION +1 -0
  5. data/examples/bigindex.yml +9 -0
  6. data/generators/bigindex/bigindex_generator.rb +17 -0
  7. data/generators/bigindex/templates/bigindex.rake +3 -0
  8. data/init.rb +27 -0
  9. data/install.rb +15 -0
  10. data/lib/big_index/adapters/abstract_adapter.rb +70 -0
  11. data/lib/big_index/adapters/solr_adapter.rb +180 -0
  12. data/lib/big_index/adapters.rb +11 -0
  13. data/lib/big_index/index_field.rb +41 -0
  14. data/lib/big_index/repository.rb +77 -0
  15. data/lib/big_index/resource.rb +462 -0
  16. data/lib/big_index/support/assertions.rb +8 -0
  17. data/lib/big_index/support.rb +3 -0
  18. data/lib/big_index.rb +108 -0
  19. data/lib/bigindex.rb +1 -0
  20. data/rails/init.rb +27 -0
  21. data/spec/connections/activerecord/activerecord.yml +7 -0
  22. data/spec/connections/activerecord/connection.rb +19 -0
  23. data/spec/connections/bigindex.yml +7 -0
  24. data/spec/connections/bigrecord/bigrecord.yml +13 -0
  25. data/spec/connections/bigrecord/connection.rb +29 -0
  26. data/spec/connections/bigrecord/migrations/20090706182535_add_animals_table.rb +13 -0
  27. data/spec/connections/bigrecord/migrations/20090706190623_add_books_table.rb +15 -0
  28. data/spec/connections/bigrecord/migrations/20090706193019_add_companies_table.rb +14 -0
  29. data/spec/connections/bigrecord/migrations/20090706194512_add_employees_table.rb +13 -0
  30. data/spec/connections/bigrecord/migrations/20090706195741_add_zoos_table.rb +13 -0
  31. data/spec/lib/activerecord/animal.rb +14 -0
  32. data/spec/lib/activerecord/book.rb +26 -0
  33. data/spec/lib/activerecord/novel.rb +10 -0
  34. data/spec/lib/bigrecord/animal.rb +11 -0
  35. data/spec/lib/bigrecord/book.rb +27 -0
  36. data/spec/lib/bigrecord/novel.rb +7 -0
  37. data/spec/spec.opts +4 -0
  38. data/spec/spec_helper.rb +28 -0
  39. data/spec/unit/adapters/abstract_adapter_spec.rb +48 -0
  40. data/spec/unit/adapters/adapter_shared_spec.rb +10 -0
  41. data/spec/unit/adapters/solr_adapter_spec.rb +16 -0
  42. data/spec/unit/bigindex_setup_spec.rb +70 -0
  43. data/spec/unit/index_shared_spec.rb +59 -0
  44. data/spec/unit/index_spec.rb +225 -0
  45. data/spec/unit/inherited_class_spec.rb +42 -0
  46. data/tasks/gem.rb +20 -0
  47. data/tasks/rdoc.rb +8 -0
  48. data/tasks/spec.rb +38 -0
  49. data/vendor/solr/adapter_methods/search_results.rb +53 -0
  50. data/vendor/solr/adapter_methods/solr_result.rb +137 -0
  51. data/vendor/solr/adapter_methods.rb +360 -0
  52. data/vendor/solr/base.rb +159 -0
  53. data/vendor/solr.rb +20 -0
  54. metadata +147 -0
@@ -0,0 +1,159 @@
1
+ module Solr
2
+
3
+ class Base
4
+ attr_reader :configurations
5
+ attr_accessor :logger
6
+
7
+ # URL and Shard methods ====================================
8
+
9
+ def solr_url
10
+ @solr_url ||= @configurations[:solr_url]
11
+ end
12
+
13
+ def solr_shards_urls
14
+ @solr_shards_url ||= (@configurations[:shards] || [])
15
+ end
16
+
17
+ def solr_shards
18
+ @solr_shards ||= solr_shards_urls.each{|url| url.gsub("http://", "")}
19
+ end
20
+
21
+ def solr_using_shards?
22
+ !solr_shards.empty?
23
+ end
24
+
25
+
26
+ # Solr execute and find methods ====================================
27
+
28
+ def solr_execute(request, shard_url = nil)
29
+ if solr_using_shards?
30
+ # Handle batch update
31
+ if request.is_a?(Solr::Request::AddDocument) and request.docs.size > 1 and !shard_url
32
+ # Split the request into several ones if it must be sent to different shards
33
+ sub_requests = {}
34
+ request.docs.each do |doc|
35
+ url = shard_url_for(doc['pk_s'])
36
+ if sub_requests.has_key?(url)
37
+ sub_requests[url].add_doc(doc)
38
+ else
39
+ sub_requests[url] = Solr::Request::AddDocument.new(doc)
40
+ end
41
+ end
42
+ # Recursive execution of the sub queries
43
+ logger.debug "Splitted batch update to send it to the following shards: #{sub_requests.keys.inspect}" if sub_requests.size > 1
44
+ sub_requests.each{|url, sub_request| solr_execute(sub_request, url)} and return
45
+
46
+ # Handle delete by query by sending the request to all the shards
47
+ elsif request.is_a?(Solr::Request::Delete) and request.query and !shard_url
48
+ solr_shards_urls.each do |url|
49
+ solr_execute(request, url)
50
+ end
51
+ return
52
+
53
+ # Handle commit by sending the request to all the shards
54
+ elsif request.is_a?(Solr::Request::Commit) and !shard_url
55
+ solr_shards_urls.each do |url|
56
+ solr_execute(request, url)
57
+ end
58
+ return
59
+
60
+ # Handle optimize by sending the request to all the shards
61
+ elsif request.is_a?(Solr::Request::Optimize) and !shard_url
62
+ solr_shards_urls.each do |url|
63
+ solr_execute(request, url)
64
+ end
65
+ return
66
+ end
67
+
68
+ url = shard_url ? shard_url : random_shard_url
69
+ request.shards = solr_shards if request.is_a?(Solr::Request::Select)
70
+
71
+ logger.debug "#{request.class} using shard #{url}"
72
+
73
+ else
74
+ url = solr_url
75
+ end
76
+
77
+ connection = Solr::Connection.new(url)
78
+ result = nil
79
+
80
+ message = request.respond_to?(:to_hash) ? "/#{request.handler} #{request.to_hash.inspect}" : request.to_s
81
+
82
+ solr_log message do
83
+ result = connection.send(request)
84
+ end
85
+ result
86
+ end
87
+
88
+
89
+ private
90
+
91
+ def initialize(options, logger = nil)
92
+ raise ArgumentError, "Adapter: #{options[:adapter]} is not for Solr" unless options[:adapter] == "solr"
93
+
94
+ @configurations = options
95
+ @logger = logger
96
+ end
97
+
98
+ protected
99
+
100
+ # Logging related methods. Only works if a logger was defined in initialize()
101
+
102
+ def solr_log(str, name = nil)
103
+ if block_given?
104
+ if @logger and @logger.level <= Logger::INFO
105
+ result = nil
106
+ seconds = Benchmark.realtime { result = yield }
107
+ solr_log_info(str, name, seconds)
108
+ result
109
+ else
110
+ yield
111
+ end
112
+ else
113
+ solr_log_info(str, name, 0)
114
+ nil
115
+ end
116
+ rescue Exception => e
117
+ # Log message and raise exception.
118
+ # Set last_verfication to 0, so that connection gets verified
119
+ # upon reentering the request loop
120
+ @last_verification = 0
121
+ message = "#{e.class.name}: #{e.message}: #{str}"
122
+ solr_log_info(message, name, 0)
123
+ raise message
124
+ end
125
+
126
+ def solr_log_info(str, name, runtime)
127
+ return unless @logger
128
+
129
+ @logger.debug(
130
+ solr_format_log_entry(
131
+ "#{name.nil? ? "Solr" : name} (#{sprintf("%f", runtime)})",
132
+ str.gsub(/ +/, " ")
133
+ )
134
+ )
135
+ end
136
+
137
+ @@row_even = true
138
+
139
+ def solr_format_log_entry(message, dump = nil)
140
+ if ActiveRecord::Base.colorize_logging
141
+ if @@row_even
142
+ @@row_even = false
143
+ message_color, dump_color = "4;36;1", "0;1"
144
+ else
145
+ @@row_even = true
146
+ message_color, dump_color = "4;35;1", "0"
147
+ end
148
+
149
+ log_entry = " \e[#{message_color}m#{message}\e[0m "
150
+ log_entry << "\e[#{dump_color}m%#{String === dump ? 's' : 'p'}\e[0m" % dump if dump
151
+ log_entry
152
+ else
153
+ "%s %s" % [message, dump]
154
+ end
155
+ end
156
+
157
+ end # class Base
158
+
159
+ end # module Solr
data/vendor/solr.rb ADDED
@@ -0,0 +1,20 @@
1
+ # The ASF licenses this file to You under the Apache License, Version 2.0
2
+ # (the "License"); you may not use this file except in compliance with
3
+ # the License. You may obtain a copy of the License at
4
+ #
5
+ # http://www.apache.org/licenses/LICENSE-2.0
6
+ #
7
+ # Unless required by applicable law or agreed to in writing, software
8
+ # distributed under the License is distributed on an "AS IS" BASIS,
9
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10
+ # See the License for the specific language governing permissions and
11
+ # limitations under the License.
12
+
13
+ begin
14
+ require "solr"
15
+ rescue LoadError
16
+ raise "Using Bigindex with Solr requires the solr-ruby libraries. Please install them with: gem install solr-ruby"
17
+ end
18
+
19
+ require File.dirname(__FILE__) + '/solr/base'
20
+ require File.dirname(__FILE__) + '/solr/adapter_methods'
metadata ADDED
@@ -0,0 +1,147 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: bigindex
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - openplaces.org
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-10-12 00:00:00 -04:00
13
+ default_executable:
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: rspec
17
+ type: :development
18
+ version_requirement:
19
+ version_requirements: !ruby/object:Gem::Requirement
20
+ requirements:
21
+ - - ">="
22
+ - !ruby/object:Gem::Version
23
+ version: "0"
24
+ version:
25
+ - !ruby/object:Gem::Dependency
26
+ name: solr-ruby
27
+ type: :runtime
28
+ version_requirement:
29
+ version_requirements: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: 0.0.7
34
+ version:
35
+ description: A Rails plugin that drops into models and provides indexing functionality.
36
+ email: bigrecord@openplaces.org
37
+ executables: []
38
+
39
+ extensions: []
40
+
41
+ extra_rdoc_files:
42
+ - MIT-LICENSE
43
+ - README.rdoc
44
+ files:
45
+ - Rakefile
46
+ - VERSION
47
+ - examples/bigindex.yml
48
+ - generators/bigindex/bigindex_generator.rb
49
+ - generators/bigindex/templates/bigindex.rake
50
+ - init.rb
51
+ - install.rb
52
+ - lib/big_index.rb
53
+ - lib/big_index/adapters.rb
54
+ - lib/big_index/adapters/abstract_adapter.rb
55
+ - lib/big_index/adapters/solr_adapter.rb
56
+ - lib/big_index/index_field.rb
57
+ - lib/big_index/repository.rb
58
+ - lib/big_index/resource.rb
59
+ - lib/big_index/support.rb
60
+ - lib/big_index/support/assertions.rb
61
+ - lib/bigindex.rb
62
+ - rails/init.rb
63
+ - spec/connections/activerecord/activerecord.yml
64
+ - spec/connections/activerecord/connection.rb
65
+ - spec/connections/bigindex.yml
66
+ - spec/connections/bigrecord/bigrecord.yml
67
+ - spec/connections/bigrecord/connection.rb
68
+ - spec/connections/bigrecord/migrations/20090706182535_add_animals_table.rb
69
+ - spec/connections/bigrecord/migrations/20090706190623_add_books_table.rb
70
+ - spec/connections/bigrecord/migrations/20090706193019_add_companies_table.rb
71
+ - spec/connections/bigrecord/migrations/20090706194512_add_employees_table.rb
72
+ - spec/connections/bigrecord/migrations/20090706195741_add_zoos_table.rb
73
+ - spec/lib/activerecord/animal.rb
74
+ - spec/lib/activerecord/book.rb
75
+ - spec/lib/activerecord/novel.rb
76
+ - spec/lib/bigrecord/animal.rb
77
+ - spec/lib/bigrecord/book.rb
78
+ - spec/lib/bigrecord/novel.rb
79
+ - spec/spec.opts
80
+ - spec/spec_helper.rb
81
+ - spec/unit/adapters/abstract_adapter_spec.rb
82
+ - spec/unit/adapters/adapter_shared_spec.rb
83
+ - spec/unit/adapters/solr_adapter_spec.rb
84
+ - spec/unit/bigindex_setup_spec.rb
85
+ - spec/unit/index_shared_spec.rb
86
+ - spec/unit/index_spec.rb
87
+ - spec/unit/inherited_class_spec.rb
88
+ - tasks/gem.rb
89
+ - tasks/rdoc.rb
90
+ - tasks/spec.rb
91
+ - vendor/solr.rb
92
+ - vendor/solr/adapter_methods.rb
93
+ - vendor/solr/adapter_methods/search_results.rb
94
+ - vendor/solr/adapter_methods/solr_result.rb
95
+ - vendor/solr/base.rb
96
+ - MIT-LICENSE
97
+ - README.rdoc
98
+ has_rdoc: true
99
+ homepage: http://www.bigrecord.org
100
+ licenses: []
101
+
102
+ post_install_message:
103
+ rdoc_options:
104
+ - --charset=UTF-8
105
+ require_paths:
106
+ - lib
107
+ required_ruby_version: !ruby/object:Gem::Requirement
108
+ requirements:
109
+ - - ">="
110
+ - !ruby/object:Gem::Version
111
+ version: "0"
112
+ version:
113
+ required_rubygems_version: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - ">="
116
+ - !ruby/object:Gem::Version
117
+ version: "0"
118
+ version:
119
+ requirements: []
120
+
121
+ rubyforge_project:
122
+ rubygems_version: 1.3.5
123
+ signing_key:
124
+ specification_version: 3
125
+ summary: A Rails plugin that drops into models and provides indexing functionality. Uses an adapter/repository pattern inspired by Datamapper to abstract the actual indexer used in the background, and exposes the model to a simple indexing API.
126
+ test_files:
127
+ - spec/lib/bigrecord/book.rb
128
+ - spec/lib/bigrecord/animal.rb
129
+ - spec/lib/bigrecord/novel.rb
130
+ - spec/lib/activerecord/book.rb
131
+ - spec/lib/activerecord/animal.rb
132
+ - spec/lib/activerecord/novel.rb
133
+ - spec/connections/bigrecord/migrations/20090706194512_add_employees_table.rb
134
+ - spec/connections/bigrecord/migrations/20090706182535_add_animals_table.rb
135
+ - spec/connections/bigrecord/migrations/20090706190623_add_books_table.rb
136
+ - spec/connections/bigrecord/migrations/20090706195741_add_zoos_table.rb
137
+ - spec/connections/bigrecord/migrations/20090706193019_add_companies_table.rb
138
+ - spec/connections/bigrecord/connection.rb
139
+ - spec/connections/activerecord/connection.rb
140
+ - spec/spec_helper.rb
141
+ - spec/unit/index_spec.rb
142
+ - spec/unit/index_shared_spec.rb
143
+ - spec/unit/bigindex_setup_spec.rb
144
+ - spec/unit/inherited_class_spec.rb
145
+ - spec/unit/adapters/solr_adapter_spec.rb
146
+ - spec/unit/adapters/abstract_adapter_spec.rb
147
+ - spec/unit/adapters/adapter_shared_spec.rb