remote_index.rb
1.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
require 'drb'
module ActsAsFerret
# This index implementation connects to a remote ferret server instance. It
# basically forwards all calls to the remote server.
class RemoteIndex < AbstractIndex
include RemoteFunctions
def initialize(config)
super
@server = DRbObject.new(nil, ActsAsFerret::remote)
end
# Cause model classes to be loaded (and indexes get declared) on the DRb
# side of things.
def register_class(clazz, options)
handle_drb_error { @server.register_class clazz.name }
end
def method_missing(method_name, *args)
args.unshift index_name
handle_drb_error { @server.send(method_name, *args) }
end
# Proxy any methods that require special return values in case of errors
{
:highlight => []
}.each do |method_name, default_result|
define_method method_name do |*args|
args.unshift index_name
handle_drb_error(default_result) { @server.send method_name, *args }
end
end
def find_ids(q, options = {}, &proc)
total_hits, results = handle_drb_error([0, []]) { @server.find_ids(index_name, q, options) }
block_given? ? yield_results(total_hits, results, &proc) : [ total_hits, results ]
end
# add record to index
def add(record)
handle_drb_error { @server.add index_name, record.to_doc }
end
alias << add
private
#def model_class_name
# index_definition[:class_name]
#end
end
end