acts_as_ferret 0.4.3 → 0.4.4
Sign up to get free protection for your applications and to get access to all the features.
- data/README +32 -6
- data/acts_as_ferret.gemspec +260 -0
- data/config/ferret_server.yml +1 -0
- data/doc/demo/README +154 -0
- data/doc/demo/README_DEMO +23 -0
- data/doc/demo/Rakefile +10 -0
- data/doc/demo/app/controllers/admin/backend_controller.rb +14 -0
- data/doc/demo/app/controllers/admin_area_controller.rb +4 -0
- data/doc/demo/app/controllers/application.rb +5 -0
- data/doc/demo/app/controllers/contents_controller.rb +49 -0
- data/doc/demo/app/controllers/searches_controller.rb +8 -0
- data/doc/demo/app/helpers/admin/backend_helper.rb +2 -0
- data/doc/demo/app/helpers/application_helper.rb +3 -0
- data/doc/demo/app/helpers/content_helper.rb +2 -0
- data/doc/demo/app/helpers/search_helper.rb +2 -0
- data/doc/demo/app/models/comment.rb +48 -0
- data/doc/demo/app/models/content.rb +12 -0
- data/doc/demo/app/models/content_base.rb +28 -0
- data/doc/demo/app/models/search.rb +19 -0
- data/doc/demo/app/models/shared_index1.rb +3 -0
- data/doc/demo/app/models/shared_index2.rb +3 -0
- data/doc/demo/app/models/special_content.rb +3 -0
- data/doc/demo/app/models/stats.rb +20 -0
- data/doc/demo/app/views/admin/backend/search.rhtml +18 -0
- data/doc/demo/app/views/contents/_form.rhtml +10 -0
- data/doc/demo/app/views/contents/edit.rhtml +9 -0
- data/doc/demo/app/views/contents/index.rhtml +24 -0
- data/doc/demo/app/views/contents/new.rhtml +8 -0
- data/doc/demo/app/views/contents/show.rhtml +8 -0
- data/doc/demo/app/views/layouts/application.html.erb +17 -0
- data/doc/demo/app/views/searches/_content.html.erb +2 -0
- data/doc/demo/app/views/searches/search.html.erb +20 -0
- data/doc/demo/config/boot.rb +109 -0
- data/doc/demo/config/database.yml +38 -0
- data/doc/demo/config/environment.rb +69 -0
- data/doc/demo/config/environments/development.rb +16 -0
- data/doc/demo/config/environments/production.rb +19 -0
- data/doc/demo/config/environments/test.rb +21 -0
- data/doc/demo/config/ferret_server.yml +18 -0
- data/doc/demo/config/lighttpd.conf +40 -0
- data/doc/demo/config/routes.rb +9 -0
- data/doc/demo/db/development_structure.sql +15 -0
- data/doc/demo/db/migrate/001_initial_migration.rb +18 -0
- data/doc/demo/db/migrate/002_add_type_to_contents.rb +9 -0
- data/doc/demo/db/migrate/003_create_shared_index1s.rb +11 -0
- data/doc/demo/db/migrate/004_create_shared_index2s.rb +11 -0
- data/doc/demo/db/migrate/005_special_field.rb +9 -0
- data/doc/demo/db/migrate/006_create_stats.rb +15 -0
- data/doc/demo/db/schema.sql +18 -0
- data/doc/demo/doc/README_FOR_APP +2 -0
- data/doc/demo/doc/howto.txt +70 -0
- data/doc/demo/public/.htaccess +40 -0
- data/doc/demo/public/404.html +8 -0
- data/doc/demo/public/500.html +8 -0
- data/doc/demo/public/dispatch.cgi +10 -0
- data/doc/demo/public/dispatch.fcgi +24 -0
- data/doc/demo/public/dispatch.rb +10 -0
- data/doc/demo/public/favicon.ico +0 -0
- data/doc/demo/public/images/rails.png +0 -0
- data/doc/demo/public/index.html +277 -0
- data/doc/demo/public/robots.txt +1 -0
- data/doc/demo/public/stylesheets/scaffold.css +74 -0
- data/doc/demo/script/about +3 -0
- data/doc/demo/script/breakpointer +3 -0
- data/doc/demo/script/console +3 -0
- data/doc/demo/script/destroy +3 -0
- data/doc/demo/script/ferret_server +10 -0
- data/doc/demo/script/generate +3 -0
- data/doc/demo/script/performance/benchmarker +3 -0
- data/doc/demo/script/performance/profiler +3 -0
- data/doc/demo/script/plugin +3 -0
- data/doc/demo/script/process/inspector +3 -0
- data/doc/demo/script/process/reaper +3 -0
- data/doc/demo/script/process/spawner +3 -0
- data/doc/demo/script/process/spinner +3 -0
- data/doc/demo/script/runner +3 -0
- data/doc/demo/script/server +3 -0
- data/doc/demo/test/fixtures/comments.yml +12 -0
- data/doc/demo/test/fixtures/contents.yml +13 -0
- data/doc/demo/test/fixtures/remote_contents.yml +9 -0
- data/doc/demo/test/fixtures/shared_index1s.yml +7 -0
- data/doc/demo/test/fixtures/shared_index2s.yml +7 -0
- data/doc/demo/test/functional/admin/backend_controller_test.rb +35 -0
- data/doc/demo/test/functional/contents_controller_test.rb +81 -0
- data/doc/demo/test/functional/searches_controller_test.rb +71 -0
- data/doc/demo/test/smoke/drb_smoke_test.rb +321 -0
- data/doc/demo/test/smoke/process_stats.rb +21 -0
- data/doc/demo/test/test_helper.rb +30 -0
- data/doc/demo/test/unit/comment_test.rb +217 -0
- data/doc/demo/test/unit/content_test.rb +705 -0
- data/doc/demo/test/unit/ferret_result_test.rb +24 -0
- data/doc/demo/test/unit/multi_index_test.rb +329 -0
- data/doc/demo/test/unit/remote_index_test.rb +23 -0
- data/doc/demo/test/unit/shared_index1_test.rb +108 -0
- data/doc/demo/test/unit/shared_index2_test.rb +13 -0
- data/doc/demo/test/unit/sort_test.rb +21 -0
- data/doc/demo/test/unit/special_content_test.rb +25 -0
- data/doc/demo/vendor/plugins/will_paginate/LICENSE +18 -0
- data/doc/demo/vendor/plugins/will_paginate/README +108 -0
- data/doc/demo/vendor/plugins/will_paginate/Rakefile +23 -0
- data/doc/demo/vendor/plugins/will_paginate/init.rb +21 -0
- data/doc/demo/vendor/plugins/will_paginate/lib/will_paginate/collection.rb +45 -0
- data/doc/demo/vendor/plugins/will_paginate/lib/will_paginate/core_ext.rb +44 -0
- data/doc/demo/vendor/plugins/will_paginate/lib/will_paginate/finder.rb +159 -0
- data/doc/demo/vendor/plugins/will_paginate/lib/will_paginate/view_helpers.rb +95 -0
- data/doc/demo/vendor/plugins/will_paginate/test/array_pagination_test.rb +23 -0
- data/doc/demo/vendor/plugins/will_paginate/test/boot.rb +27 -0
- data/doc/demo/vendor/plugins/will_paginate/test/console +10 -0
- data/doc/demo/vendor/plugins/will_paginate/test/finder_test.rb +219 -0
- data/doc/demo/vendor/plugins/will_paginate/test/fixtures/admin.rb +3 -0
- data/doc/demo/vendor/plugins/will_paginate/test/fixtures/companies.yml +24 -0
- data/doc/demo/vendor/plugins/will_paginate/test/fixtures/company.rb +23 -0
- data/doc/demo/vendor/plugins/will_paginate/test/fixtures/developer.rb +11 -0
- data/doc/demo/vendor/plugins/will_paginate/test/fixtures/developers_projects.yml +13 -0
- data/doc/demo/vendor/plugins/will_paginate/test/fixtures/project.rb +4 -0
- data/doc/demo/vendor/plugins/will_paginate/test/fixtures/projects.yml +7 -0
- data/doc/demo/vendor/plugins/will_paginate/test/fixtures/replies.yml +20 -0
- data/doc/demo/vendor/plugins/will_paginate/test/fixtures/reply.rb +5 -0
- data/doc/demo/vendor/plugins/will_paginate/test/fixtures/schema.sql +44 -0
- data/doc/demo/vendor/plugins/will_paginate/test/fixtures/topic.rb +19 -0
- data/doc/demo/vendor/plugins/will_paginate/test/fixtures/topics.yml +30 -0
- data/doc/demo/vendor/plugins/will_paginate/test/fixtures/user.rb +2 -0
- data/doc/demo/vendor/plugins/will_paginate/test/fixtures/users.yml +35 -0
- data/doc/demo/vendor/plugins/will_paginate/test/helper.rb +42 -0
- data/doc/demo/vendor/plugins/will_paginate/test/lib/activerecord_test_connector.rb +64 -0
- data/doc/demo/vendor/plugins/will_paginate/test/lib/load_fixtures.rb +10 -0
- data/doc/demo/vendor/plugins/will_paginate/test/pagination_test.rb +136 -0
- data/init.rb +2 -0
- data/lib/act_methods.rb +50 -157
- data/lib/acts_as_ferret.rb +457 -23
- data/lib/ar_mysql_auto_reconnect_patch.rb +41 -0
- data/lib/blank_slate.rb +53 -0
- data/lib/bulk_indexer.rb +4 -1
- data/lib/class_methods.rb +106 -295
- data/lib/ferret_extensions.rb +78 -5
- data/lib/ferret_find_methods.rb +141 -0
- data/lib/ferret_result.rb +26 -9
- data/lib/ferret_server.rb +100 -65
- data/lib/index.rb +83 -15
- data/lib/instance_methods.rb +32 -17
- data/lib/local_index.rb +106 -112
- data/lib/more_like_this.rb +13 -13
- data/lib/multi_index.rb +115 -72
- data/lib/rdig_adapter.rb +148 -0
- data/lib/remote_functions.rb +43 -0
- data/lib/remote_index.rb +25 -21
- data/lib/remote_multi_index.rb +20 -0
- data/lib/search_results.rb +7 -10
- data/lib/server_manager.rb +16 -4
- data/lib/unix_daemon.rb +23 -0
- data/lib/without_ar.rb +52 -0
- data/rakefile +23 -16
- data/recipes/aaf_recipes.rb +114 -0
- data/tasks/ferret.rake +22 -0
- metadata +233 -54
- data/lib/ferret_cap_tasks.rb +0 -21
- data/lib/shared_index.rb +0 -14
- data/lib/shared_index_class_methods.rb +0 -90
data/lib/ferret_extensions.rb
CHANGED
@@ -71,7 +71,80 @@ module Ferret
|
|
71
71
|
end
|
72
72
|
@auto_flush = orig_flush
|
73
73
|
end
|
74
|
+
|
74
75
|
|
76
|
+
# bulk-inserts a number of ferret documents.
|
77
|
+
# The argument has to be an array of two-element arrays each holding the document data and the analyzer to
|
78
|
+
# use for this document (which may be nil).
|
79
|
+
def update_batch(document_analyzer_pairs)
|
80
|
+
ids = document_analyzer_pairs.collect {|da| da.first[@id_field] }
|
81
|
+
@dir.synchrolock do
|
82
|
+
batch_delete(ids)
|
83
|
+
ensure_writer_open()
|
84
|
+
document_analyzer_pairs.each do |doc, analyzer|
|
85
|
+
if analyzer
|
86
|
+
old_analyzer = @writer.analyzer
|
87
|
+
@writer.analyzer = analyzer
|
88
|
+
@writer.add_document(doc)
|
89
|
+
@writer.analyzer = old_analyzer
|
90
|
+
else
|
91
|
+
@writer.add_document(doc)
|
92
|
+
end
|
93
|
+
end
|
94
|
+
flush()
|
95
|
+
end
|
96
|
+
end
|
97
|
+
|
98
|
+
# If +docs+ is a Hash or an Array then a batch delete will be performed.
|
99
|
+
# If +docs+ is an Array then it will be considered an array of +id+'s. If
|
100
|
+
# it is a Hash, then its keys will be used instead as the Array of
|
101
|
+
# document +id+'s. If the +id+ is an Integers then it is considered a
|
102
|
+
# Ferret document number and the corresponding document will be deleted.
|
103
|
+
# If the +id+ is a String or a Symbol then the +id+ will be considered a
|
104
|
+
# term and the documents that contain that term in the +:id_field+ will
|
105
|
+
# be deleted.
|
106
|
+
#
|
107
|
+
# docs:: An Array of docs to be deleted, or a Hash (in which case the keys
|
108
|
+
# are used)
|
109
|
+
#
|
110
|
+
# ripped from Ferret trunk.
|
111
|
+
def batch_delete(docs)
|
112
|
+
docs = docs.keys if docs.is_a?(Hash)
|
113
|
+
raise ArgumentError, "must pass Array or Hash" unless docs.is_a? Array
|
114
|
+
ids = []
|
115
|
+
terms = []
|
116
|
+
docs.each do |doc|
|
117
|
+
case doc
|
118
|
+
when String then terms << doc
|
119
|
+
when Symbol then terms << doc.to_s
|
120
|
+
when Integer then ids << doc
|
121
|
+
else
|
122
|
+
raise ArgumentError, "Cannot delete for arg of type #{id.class}"
|
123
|
+
end
|
124
|
+
end
|
125
|
+
if ids.size > 0
|
126
|
+
ensure_reader_open
|
127
|
+
ids.each {|id| @reader.delete(id)}
|
128
|
+
end
|
129
|
+
if terms.size > 0
|
130
|
+
ensure_writer_open()
|
131
|
+
terms.each { |t| @writer.delete(@id_field, t) }
|
132
|
+
# TODO with Ferret trunk this would work:
|
133
|
+
# @writer.delete(@id_field, terms)
|
134
|
+
end
|
135
|
+
return self
|
136
|
+
end
|
137
|
+
|
138
|
+
# search for the first document with +arg+ in the +id+ field and return it's internal document number.
|
139
|
+
# The +id+ field is either :id or whatever you set
|
140
|
+
# :id_field parameter to when you create the Index object.
|
141
|
+
def doc_number(id)
|
142
|
+
@dir.synchronize do
|
143
|
+
ensure_reader_open()
|
144
|
+
term_doc_enum = @reader.term_docs_for(@id_field, id.to_s)
|
145
|
+
return term_doc_enum.next? ? term_doc_enum.doc : nil
|
146
|
+
end
|
147
|
+
end
|
75
148
|
end
|
76
149
|
|
77
150
|
# add marshalling support to SortFields
|
@@ -82,11 +155,11 @@ module Ferret
|
|
82
155
|
|
83
156
|
def self._load(string)
|
84
157
|
case string
|
85
|
-
when /<DOC(_ID)?>!/
|
86
|
-
when /<DOC(_ID)?>/
|
87
|
-
when '<SCORE>!'
|
88
|
-
when '<SCORE>'
|
89
|
-
when /^(\w+):<(\w+)>(!)?$/
|
158
|
+
when /<DOC(_ID)?>!/ then Ferret::Search::SortField::DOC_ID_REV
|
159
|
+
when /<DOC(_ID)?>/ then Ferret::Search::SortField::DOC_ID
|
160
|
+
when '<SCORE>!' then Ferret::Search::SortField::SCORE_REV
|
161
|
+
when '<SCORE>' then Ferret::Search::SortField::SCORE
|
162
|
+
when /^(\w+):<(\w+)>(!)?$/ then new($1.to_sym, :type => $2.to_sym, :reverse => !$3.nil?)
|
90
163
|
else raise "invalid value: #{string}"
|
91
164
|
end
|
92
165
|
end
|
@@ -0,0 +1,141 @@
|
|
1
|
+
module ActsAsFerret
|
2
|
+
# Ferret search logic common to single-class indexes, shared indexes and
|
3
|
+
# multi indexes.
|
4
|
+
module FerretFindMethods
|
5
|
+
|
6
|
+
def find_records(q, options = {}, ar_options = {})
|
7
|
+
late_pagination = options.delete :late_pagination
|
8
|
+
total_hits, result = if options[:lazy]
|
9
|
+
logger.warn "find_options #{ar_options} are ignored because :lazy => true" unless ar_options.empty?
|
10
|
+
lazy_find q, options
|
11
|
+
else
|
12
|
+
ar_find q, options, ar_options
|
13
|
+
end
|
14
|
+
if late_pagination
|
15
|
+
limit = late_pagination[:limit]
|
16
|
+
offset = late_pagination[:offset] || 0
|
17
|
+
end_index = limit == :all ? -1 : limit+offset-1
|
18
|
+
# puts "late pagination: #{offset} : #{end_index}"
|
19
|
+
result = result[offset..end_index]
|
20
|
+
end
|
21
|
+
return [total_hits, result]
|
22
|
+
end
|
23
|
+
|
24
|
+
def lazy_find(q, options = {})
|
25
|
+
logger.debug "lazy_find: #{q}"
|
26
|
+
result = []
|
27
|
+
rank = 0
|
28
|
+
total_hits = find_ids(q, options) do |model, id, score, data|
|
29
|
+
logger.debug "model: #{model}, id: #{id}, data: #{data}"
|
30
|
+
result << FerretResult.new(model, id, score, rank += 1, data)
|
31
|
+
end
|
32
|
+
[ total_hits, result ]
|
33
|
+
end
|
34
|
+
|
35
|
+
def ar_find(q, options = {}, ar_options = {})
|
36
|
+
ferret_options = options.dup
|
37
|
+
if ar_options[:conditions] or ar_options[:order]
|
38
|
+
ferret_options[:limit] = :all
|
39
|
+
ferret_options.delete :offset
|
40
|
+
end
|
41
|
+
total_hits, id_arrays = find_id_model_arrays q, ferret_options
|
42
|
+
logger.debug "now retrieving records from AR with options: #{ar_options.inspect}"
|
43
|
+
result = ActsAsFerret::retrieve_records(id_arrays, ar_options)
|
44
|
+
logger.debug "#{result.size} results from AR: #{result.inspect}"
|
45
|
+
|
46
|
+
# count total_hits via sql when using conditions, multiple models, or when we're called
|
47
|
+
# from an ActiveRecord association.
|
48
|
+
if id_arrays.size > 1 or ar_options[:conditions]
|
49
|
+
# chances are the ferret result count is not our total_hits value, so
|
50
|
+
# we correct this here.
|
51
|
+
if options[:limit] != :all || options[:page] || options[:offset] || ar_options[:limit] || ar_options[:offset]
|
52
|
+
# our ferret result has been limited, so we need to re-run that
|
53
|
+
# search to get the full result set from ferret.
|
54
|
+
new_th, id_arrays = find_id_model_arrays( q, options.merge(:limit => :all, :offset => 0) )
|
55
|
+
# Now ask the database for the total size of the final result set.
|
56
|
+
total_hits = count_records( id_arrays, ar_options )
|
57
|
+
else
|
58
|
+
# what we got from the database is our full result set, so take
|
59
|
+
# it's size
|
60
|
+
total_hits = result.length
|
61
|
+
end
|
62
|
+
end
|
63
|
+
[ total_hits, result ]
|
64
|
+
end
|
65
|
+
|
66
|
+
def count_records(id_arrays, ar_options = {})
|
67
|
+
count_options = ar_options.dup
|
68
|
+
count_options.delete :limit
|
69
|
+
count_options.delete :offset
|
70
|
+
count_options.delete :order
|
71
|
+
count = 0
|
72
|
+
id_arrays.each do |model, id_array|
|
73
|
+
next if id_array.empty?
|
74
|
+
model = model.constantize
|
75
|
+
# merge conditions
|
76
|
+
conditions = ActsAsFerret::conditions_for_model model, ar_options[:conditions]
|
77
|
+
count_options[:conditions] = ActsAsFerret::combine_conditions([ "#{model.table_name}.#{model.primary_key} in (?)", id_array.keys ], conditions)
|
78
|
+
count_options[:include] = ActsAsFerret::filter_include_list_for_model(model, ar_options[:include]) if ar_options[:include]
|
79
|
+
cnt = model.count count_options
|
80
|
+
if cnt.is_a?(ActiveSupport::OrderedHash) # fixes #227
|
81
|
+
count += cnt.size
|
82
|
+
else
|
83
|
+
count += cnt
|
84
|
+
end
|
85
|
+
end
|
86
|
+
count
|
87
|
+
end
|
88
|
+
|
89
|
+
def find_id_model_arrays(q, options)
|
90
|
+
id_arrays = {}
|
91
|
+
rank = 0
|
92
|
+
total_hits = find_ids(q, options) do |model, id, score, data|
|
93
|
+
id_arrays[model] ||= {}
|
94
|
+
id_arrays[model][id] = [ rank += 1, score ]
|
95
|
+
end
|
96
|
+
[total_hits, id_arrays]
|
97
|
+
end
|
98
|
+
|
99
|
+
# Queries the Ferret index to retrieve model class, id, score and the
|
100
|
+
# values of any fields stored in the index for each hit.
|
101
|
+
# If a block is given, these are yielded and the number of total hits is
|
102
|
+
# returned. Otherwise [total_hits, result_array] is returned.
|
103
|
+
def find_ids(query, options = {})
|
104
|
+
|
105
|
+
result = []
|
106
|
+
stored_fields = determine_stored_fields options
|
107
|
+
|
108
|
+
q = process_query(query, options)
|
109
|
+
q = scope_query_to_models q, options[:models] #if shared?
|
110
|
+
logger.debug "query: #{query}\n-->#{q}"
|
111
|
+
s = searcher
|
112
|
+
total_hits = s.search_each(q, options) do |hit, score|
|
113
|
+
doc = s[hit]
|
114
|
+
model = doc[:class_name]
|
115
|
+
# fetch stored fields if lazy loading
|
116
|
+
data = extract_stored_fields(doc, stored_fields)
|
117
|
+
if block_given?
|
118
|
+
yield model, doc[:id], score, data
|
119
|
+
else
|
120
|
+
result << { :model => model, :id => doc[:id], :score => score, :data => data }
|
121
|
+
end
|
122
|
+
end
|
123
|
+
#logger.debug "id_score_model array: #{result.inspect}"
|
124
|
+
return block_given? ? total_hits : [total_hits, result]
|
125
|
+
end
|
126
|
+
|
127
|
+
def scope_query_to_models(query, models)
|
128
|
+
return query if models.nil? or models == :all
|
129
|
+
models = [ models ] if Class === models
|
130
|
+
q = Ferret::Search::BooleanQuery.new
|
131
|
+
q.add_query(query, :must)
|
132
|
+
model_query = Ferret::Search::BooleanQuery.new
|
133
|
+
models.each do |model|
|
134
|
+
model_query.add_query(Ferret::Search::TermQuery.new(:class_name, model.name), :should)
|
135
|
+
end
|
136
|
+
q.add_query(model_query, :must)
|
137
|
+
return q
|
138
|
+
end
|
139
|
+
|
140
|
+
end
|
141
|
+
end
|
data/lib/ferret_result.rb
CHANGED
@@ -9,28 +9,45 @@ module ActsAsFerret
|
|
9
9
|
attr_accessor :ferret_rank
|
10
10
|
end
|
11
11
|
|
12
|
-
class FerretResult
|
12
|
+
class FerretResult < ActsAsFerret::BlankSlate
|
13
13
|
include ResultAttributes
|
14
14
|
attr_accessor :id
|
15
|
+
reveal :methods
|
15
16
|
|
16
|
-
def initialize(model, id, score, data = {})
|
17
|
+
def initialize(model, id, score, rank, data = {})
|
17
18
|
@model = model.constantize
|
18
19
|
@id = id
|
19
20
|
@ferret_score = score
|
21
|
+
@ferret_rank = rank
|
20
22
|
@data = data
|
23
|
+
@use_record = false
|
21
24
|
end
|
22
|
-
|
23
|
-
def
|
24
|
-
|
25
|
-
|
26
|
-
|
25
|
+
|
26
|
+
def inspect
|
27
|
+
"#<FerretResult wrapper for #{@model} with id #{@id}"
|
28
|
+
end
|
29
|
+
|
30
|
+
def method_missing(method, *args, &block)
|
31
|
+
if (@ar_record && @use_record) || !@data.has_key?(method)
|
32
|
+
to_record.send method, *args, &block
|
27
33
|
else
|
28
34
|
@data[method]
|
29
35
|
end
|
30
36
|
end
|
31
37
|
|
32
|
-
def
|
33
|
-
|
38
|
+
def respond_to?(name)
|
39
|
+
methods.include?(name.to_s) || @data.has_key?(name.to_sym) || to_record.respond_to?(name)
|
40
|
+
end
|
41
|
+
|
42
|
+
def to_record
|
43
|
+
unless @ar_record
|
44
|
+
@ar_record = @model.find(id)
|
45
|
+
@ar_record.ferret_rank = ferret_rank
|
46
|
+
@ar_record.ferret_score = ferret_score
|
47
|
+
# don't try to fetch attributes from RDig based records
|
48
|
+
@use_record = !@ar_record.class.included_modules.include?(ActsAsFerret::RdigAdapter)
|
49
|
+
end
|
50
|
+
@ar_record
|
34
51
|
end
|
35
52
|
end
|
36
53
|
end
|
data/lib/ferret_server.rb
CHANGED
@@ -18,6 +18,8 @@ module ActsAsFerret
|
|
18
18
|
'pid_file' => "#{RAILS_ROOT}/log/ferret_server.pid",
|
19
19
|
'log_file' => "#{RAILS_ROOT}/log/ferret_server.log",
|
20
20
|
'log_level' => 'debug',
|
21
|
+
'socket' => nil,
|
22
|
+
'script' => nil
|
21
23
|
}
|
22
24
|
|
23
25
|
################################################################################
|
@@ -26,7 +28,9 @@ module ActsAsFerret
|
|
26
28
|
@everything = YAML.load(ERB.new(IO.read(file)).result)
|
27
29
|
raise "malformed ferret server config" unless @everything.is_a?(Hash)
|
28
30
|
@config = DEFAULTS.merge(@everything[RAILS_ENV] || {})
|
29
|
-
|
31
|
+
if @everything[RAILS_ENV]
|
32
|
+
@config['uri'] = socket.nil? ? "druby://#{host}:#{port}" : "drbunix:#{socket}"
|
33
|
+
end
|
30
34
|
end
|
31
35
|
|
32
36
|
################################################################################
|
@@ -57,70 +61,108 @@ module ActsAsFerret
|
|
57
61
|
require 'unix_daemon'
|
58
62
|
include(ActsAsFerret::Remote::UnixDaemon)
|
59
63
|
|
64
|
+
|
60
65
|
################################################################################
|
61
66
|
cattr_accessor :running
|
62
67
|
|
63
68
|
################################################################################
|
64
69
|
def initialize
|
65
|
-
@cfg = ActsAsFerret::Remote::Config.new
|
66
70
|
ActiveRecord::Base.allow_concurrency = true
|
71
|
+
require 'ar_mysql_auto_reconnect_patch'
|
72
|
+
@cfg = ActsAsFerret::Remote::Config.new
|
67
73
|
ActiveRecord::Base.logger = @logger = Logger.new(@cfg.log_file)
|
68
74
|
ActiveRecord::Base.logger.level = Logger.const_get(@cfg.log_level.upcase) rescue Logger::DEBUG
|
75
|
+
if @cfg.script
|
76
|
+
path = File.join(RAILS_ROOT, @cfg.script)
|
77
|
+
load path
|
78
|
+
@logger.info "loaded custom startup script from #{path}"
|
79
|
+
end
|
69
80
|
end
|
70
81
|
|
71
82
|
################################################################################
|
72
|
-
# start the server
|
83
|
+
# start the server as a daemon process
|
73
84
|
def start
|
74
85
|
raise "ferret_server not configured for #{RAILS_ENV}" unless (@cfg.uri rescue nil)
|
75
|
-
|
86
|
+
platform_daemon { run_drb_service }
|
87
|
+
end
|
76
88
|
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
89
|
+
################################################################################
|
90
|
+
# run the server and block until it exits
|
91
|
+
def run
|
92
|
+
raise "ferret_server not configured for #{RAILS_ENV}" unless (@cfg.uri rescue nil)
|
93
|
+
run_drb_service
|
94
|
+
end
|
95
|
+
|
96
|
+
def run_drb_service
|
97
|
+
$stdout.puts("starting ferret server...")
|
98
|
+
self.class.running = true
|
99
|
+
DRb.start_service(@cfg.uri, self)
|
100
|
+
DRb.thread.join
|
82
101
|
rescue Exception => e
|
83
102
|
@logger.error(e.to_s)
|
84
103
|
raise
|
85
104
|
end
|
86
105
|
|
87
106
|
#################################################################################
|
88
|
-
# handles all incoming method calls, and sends them on to the
|
89
|
-
# instance
|
107
|
+
# handles all incoming method calls, and sends them on to the correct local index
|
108
|
+
# instance.
|
90
109
|
#
|
91
|
-
# Calls are not queued
|
110
|
+
# Calls are not queued, so this will block until the call returned.
|
92
111
|
#
|
93
112
|
def method_missing(name, *args)
|
94
113
|
@logger.debug "\#method_missing(#{name.inspect}, #{args.inspect})"
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
|
114
|
+
|
115
|
+
|
116
|
+
index_name = args.shift
|
117
|
+
index = if name.to_s =~ /^multi_(.+)/
|
118
|
+
name = $1
|
119
|
+
ActsAsFerret::multi_index(index_name)
|
120
|
+
else
|
121
|
+
ActsAsFerret::get_index(index_name)
|
122
|
+
end
|
123
|
+
|
124
|
+
if index.nil?
|
125
|
+
@logger.error "\#index with name #{index_name} not found in call to #{name} with args #{args.inspect}"
|
126
|
+
raise ActsAsFerret::IndexNotDefined.new(index_name)
|
127
|
+
end
|
128
|
+
|
129
|
+
|
130
|
+
# TODO find another way to implement the reconnection logic (maybe in
|
131
|
+
# local_index or class_methods)
|
132
|
+
# reconnect_when_needed(clazz) do
|
133
|
+
|
134
|
+
# using respond_to? here so we not have to catch NoMethodError
|
135
|
+
# which would silently catch those from deep inside the indexing
|
136
|
+
# code, too...
|
137
|
+
|
138
|
+
if index.respond_to?(name)
|
139
|
+
index.send name, *args
|
140
|
+
# TODO check where we need this:
|
141
|
+
#elsif clazz.respond_to?(name)
|
142
|
+
# @logger.debug "no luck, trying to call class method instead"
|
143
|
+
# clazz.send name, *args
|
144
|
+
else
|
145
|
+
raise NoMethodError.new("method #{name} not supported by DRb server")
|
110
146
|
end
|
111
147
|
rescue => e
|
112
148
|
@logger.error "ferret server error #{$!}\n#{$!.backtrace.join "\n"}"
|
113
149
|
raise e
|
114
150
|
end
|
115
151
|
|
152
|
+
def register_class(class_name)
|
153
|
+
@logger.debug "############ registerclass #{class_name}"
|
154
|
+
class_name.constantize
|
155
|
+
@logger.debug "index for class #{class_name}: #{ActsAsFerret::ferret_indexes[class_name.underscore.to_sym]}"
|
156
|
+
|
157
|
+
end
|
158
|
+
|
116
159
|
# make sure we have a versioned index in place, building one if necessary
|
117
|
-
def ensure_index_exists(
|
118
|
-
@logger.debug "DRb server: ensure_index_exists for
|
119
|
-
|
120
|
-
|
121
|
-
|
122
|
-
|
123
|
-
end
|
160
|
+
def ensure_index_exists(index_name)
|
161
|
+
@logger.debug "DRb server: ensure_index_exists for index #{index_name}"
|
162
|
+
definition = ActsAsFerret::get_index(index_name).index_definition
|
163
|
+
dir = definition[:index_dir]
|
164
|
+
unless File.directory?(dir) && File.file?(File.join(dir, 'segments')) && dir =~ %r{/\d+(_\d+)?$}
|
165
|
+
rebuild_index(index_name)
|
124
166
|
end
|
125
167
|
end
|
126
168
|
|
@@ -133,37 +175,31 @@ module ActsAsFerret
|
|
133
175
|
end
|
134
176
|
|
135
177
|
# hides LocalIndex#rebuild_index to implement index versioning
|
136
|
-
def rebuild_index(
|
137
|
-
|
138
|
-
|
139
|
-
|
140
|
-
|
141
|
-
|
142
|
-
|
143
|
-
|
144
|
-
|
145
|
-
|
146
|
-
|
147
|
-
|
148
|
-
|
149
|
-
|
150
|
-
|
151
|
-
|
152
|
-
end
|
153
|
-
|
154
|
-
File.rename index.options[:path], new_version
|
155
|
-
clazz.index_dir = new_version
|
178
|
+
def rebuild_index(index_name)
|
179
|
+
definition = ActsAsFerret::get_index(index_name).index_definition.dup
|
180
|
+
models = definition[:registered_models]
|
181
|
+
index = new_index_for(definition)
|
182
|
+
# TODO fix reconnection stuff
|
183
|
+
# reconnect_when_needed(clazz) do
|
184
|
+
# @logger.debug "DRb server: rebuild index for class(es) #{models.inspect} in #{index.options[:path]}"
|
185
|
+
index.index_models models
|
186
|
+
# end
|
187
|
+
new_version = File.join definition[:index_base_dir], Time.now.utc.strftime('%Y%m%d%H%M%S')
|
188
|
+
# create a unique directory name (needed for unit tests where
|
189
|
+
# multiple rebuilds per second may occur)
|
190
|
+
if File.exists?(new_version)
|
191
|
+
i = 0
|
192
|
+
i+=1 while File.exists?("#{new_version}_#{i}")
|
193
|
+
new_version << "_#{i}"
|
156
194
|
end
|
195
|
+
|
196
|
+
File.rename index.options[:path], new_version
|
197
|
+
ActsAsFerret::change_index_dir index_name, new_version
|
157
198
|
end
|
158
199
|
|
159
200
|
|
160
201
|
protected
|
161
202
|
|
162
|
-
def with_class(clazz, *args)
|
163
|
-
clazz = clazz.constantize if String === clazz
|
164
|
-
yield clazz, *args
|
165
|
-
end
|
166
|
-
|
167
203
|
def reconnect_when_needed(clazz)
|
168
204
|
retried = false
|
169
205
|
begin
|
@@ -185,15 +221,14 @@ module ActsAsFerret
|
|
185
221
|
end
|
186
222
|
end
|
187
223
|
|
188
|
-
def new_index_for(
|
189
|
-
|
190
|
-
ferret_cfg = aaf_configuration[:ferret].dup
|
224
|
+
def new_index_for(index_definition)
|
225
|
+
ferret_cfg = index_definition[:ferret].dup
|
191
226
|
ferret_cfg.update :auto_flush => false,
|
192
227
|
:create => true,
|
193
|
-
:field_infos => ActsAsFerret::field_infos(
|
194
|
-
:path => File.join(
|
228
|
+
:field_infos => ActsAsFerret::field_infos(index_definition),
|
229
|
+
:path => File.join(index_definition[:index_base_dir], 'rebuild')
|
195
230
|
returning Ferret::Index::Index.new(ferret_cfg) do |i|
|
196
|
-
i.batch_size =
|
231
|
+
i.batch_size = index_definition[:reindex_batch_size]
|
197
232
|
i.logger = @logger
|
198
233
|
end
|
199
234
|
end
|