tem_mr_search 0.2 → 0.2.1
Sign up to get free protection for your applications and to get access to all the features.
- data/CHANGELOG +2 -0
- data/lib/tem_mr_search/client.rb +10 -2
- data/lib/tem_mr_search/server.rb +19 -3
- data/tem_mr_search.gemspec +1 -1
- data/test/test_client_server.rb +17 -4
- data/test/test_map_reduce_executor.rb +4 -0
- metadata +1 -1
data/CHANGELOG
CHANGED
data/lib/tem_mr_search/client.rb
CHANGED
@@ -9,7 +9,7 @@ class Client
|
|
9
9
|
def self.search(server_addr, client_query)
|
10
10
|
output = issue_request server_addr, :type => :search, :root_tem => 0,
|
11
11
|
:map_reduce => client_query.to_hash
|
12
|
-
client_query.unpack_output
|
12
|
+
output ? client_query.unpack_output(output) : nil
|
13
13
|
end
|
14
14
|
|
15
15
|
# Asks for an item in the server's database.
|
@@ -26,9 +26,17 @@ class Client
|
|
26
26
|
issue_request server_addr, :type => :shutdown
|
27
27
|
end
|
28
28
|
|
29
|
+
# Dumps the server database.
|
30
|
+
#
|
31
|
+
# In production, normal clients wouldn't have access to this.
|
32
|
+
def self.dump_database(server_addr)
|
33
|
+
issue_request server_addr, :type => :db_dump
|
34
|
+
end
|
35
|
+
|
29
36
|
# Issues a request against a Map-Reduce server and returns the response.
|
30
37
|
def self.issue_request(server_addr, request)
|
31
|
-
socket = Zerg::Support::SocketFactory.socket :out_addr => server_addr
|
38
|
+
socket = Zerg::Support::SocketFactory.socket :out_addr => server_addr,
|
39
|
+
:out_port => Server::DEFAULT_PORT
|
32
40
|
socket.extend OPAdapter
|
33
41
|
begin
|
34
42
|
socket.send_object request
|
data/lib/tem_mr_search/server.rb
CHANGED
@@ -5,6 +5,8 @@ require 'yaml'
|
|
5
5
|
module Tem::Mr::Search
|
6
6
|
|
7
7
|
class Server
|
8
|
+
DEFAULT_PORT = 9052
|
9
|
+
|
8
10
|
OP = Zerg::Support::Protocols::ObjectProtocol
|
9
11
|
OPAdapter = Zerg::Support::Sockets::ProtocolAdapter.adapter_module OP
|
10
12
|
|
@@ -12,8 +14,19 @@ class Server
|
|
12
14
|
def initialize(db_file, cluster_file, port)
|
13
15
|
@logger = Logger.new STDERR
|
14
16
|
@db = Db.new db_file
|
15
|
-
@
|
16
|
-
@
|
17
|
+
@cluster_file = cluster_file
|
18
|
+
@tems = []
|
19
|
+
refresh_tems!
|
20
|
+
@port = port || DEFAULT_PORT
|
21
|
+
end
|
22
|
+
|
23
|
+
# Reinitializes the TEM cluster connections.
|
24
|
+
#
|
25
|
+
# This should be called reasonably often to be able to respond to cluster
|
26
|
+
# configuration changes.
|
27
|
+
def refresh_tems!
|
28
|
+
@tems.each { |tem| tem.disconnect }
|
29
|
+
@tems = Server.tems_from_cluster_file @cluster_file
|
17
30
|
end
|
18
31
|
|
19
32
|
# This server's loop.
|
@@ -37,7 +50,7 @@ class Server
|
|
37
50
|
rescue Exception => e
|
38
51
|
@logger.error e
|
39
52
|
end
|
40
|
-
client_socket.close
|
53
|
+
client_socket.close
|
41
54
|
end
|
42
55
|
listen_socket.close
|
43
56
|
end
|
@@ -46,6 +59,7 @@ class Server
|
|
46
59
|
def process_request(request)
|
47
60
|
case request[:type]
|
48
61
|
when :search
|
62
|
+
refresh_tems!
|
49
63
|
job = MapReduceJob.new request[:map_reduce]
|
50
64
|
root_tem = request[:root_tem]
|
51
65
|
executor = MapReduceExecutor.new job, @db, @tems, root_tem
|
@@ -54,6 +68,8 @@ class Server
|
|
54
68
|
return @db.item_by_id(request[:id]) || :not_found
|
55
69
|
when :shutdown
|
56
70
|
return :shutdown
|
71
|
+
when :db_dump
|
72
|
+
return (0...@db.length).map { |i| @db.item(i) }
|
57
73
|
else
|
58
74
|
return :unknown
|
59
75
|
end
|
data/tem_mr_search.gemspec
CHANGED
data/test/test_client_server.rb
CHANGED
@@ -8,7 +8,7 @@ class ClientServerTest < MrTestCase
|
|
8
8
|
|
9
9
|
def setup
|
10
10
|
super
|
11
|
-
@server_port =
|
11
|
+
@server_port = 29550
|
12
12
|
end
|
13
13
|
|
14
14
|
def _test_request
|
@@ -27,11 +27,24 @@ class ClientServerTest < MrTestCase
|
|
27
27
|
end
|
28
28
|
end
|
29
29
|
|
30
|
+
def test_dump_database
|
31
|
+
@server_port = 29557
|
32
|
+
_test_request do |server_addr|
|
33
|
+
items = Client.dump_database server_addr
|
34
|
+
assert_equal @db.length, items.length, 'Wrong number of items'
|
35
|
+
items.each_with_index do |item, i|
|
36
|
+
assert_equal @db.item(i), item, "Discrepancy in item #{i}"
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
30
41
|
def test_query
|
31
|
-
|
32
|
-
@server_port = 29553
|
42
|
+
@server_port = 29551
|
33
43
|
flexmock(Server).should_receive(:tems_from_cluster_file).
|
34
|
-
with(@empty_cluster_file).and_return
|
44
|
+
with(@empty_cluster_file).and_return do |file|
|
45
|
+
Tem.auto_conf
|
46
|
+
[$tem]
|
47
|
+
end
|
35
48
|
_test_request do |server_addr|
|
36
49
|
result = Client.search server_addr, @client_query
|
37
50
|
gold_item = @db.item 5
|