/
dump.rb
165 lines (142 loc) · 4.27 KB
/
dump.rb
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
# Copyright (C) 2014 Droonga Project
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License version 2.1 as published by the Free Software Foundation.
#
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this library; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
require "groonga"
require "droonga/plugin"
require "droonga/error_messages"
module Droonga
module Plugins
module Dump
extend Plugin
register("dump")
class Handler < Droonga::Handler
def handle(message)
request = Request.new(message)
if request.need_dump?
dumper = Dumper.new(@context, loop, messenger, request)
dumper.start_dump
true
else
false
end
end
end
class Request
def initialize(message)
@message = message
end
def need_dump?
reply_to
end
def id
@message["id"]
end
def dataset
@message.raw["dataset"]
end
def reply_to
(@message.raw["replyTo"] || {})["to"]
end
def messages_per_seconds
request = (@message.request || {})
minimum_messages_per_seconds = 10
[
minimum_messages_per_seconds,
(request["messagesPerSecond"] || 10000).to_i,
].max
end
end
class Dumper
def initialize(context, loop, messenger, request)
@context = context
@loop = loop
@messenger = messenger
@request = request
end
def start_dump
setup_forward_data
forward("dump.start")
dumper = Fiber.new do
dump_records
forward("dump.end")
end
timer = Coolio::TimerWatcher.new(0.1, true)
timer.on_timer do
begin
dumper.resume
rescue FiberError
timer.detach
end
end
@loop.attach(timer)
end
private
def setup_forward_data
@base_forward_message = {
"inReplyTo" => @request.id,
"dataset" => @request.dataset,
}
@forward_to = @request.reply_to
@n_forwarded_messages = 0
@messages_per_100msec = @request.messages_per_seconds / 10
end
def forward(type, body=nil)
forward_message = @base_forward_message
if body
forward_message = forward_message.merge("body" => body)
end
@messenger.forward(forward_message,
"to" => @forward_to,
"type" => type)
@n_forwarded_messages += 1
@n_forwarded_messages %= @messages_per_100msec
Fiber.yield if @n_forwarded_messages.zero?
end
def dump_records
each_table do |table|
table.each do |record|
values = {}
record.attributes.each do |key, value|
values[key] = value unless key.start_with?("_")
end
body = {
"table" => table.name,
"key" => record.key,
"values" => values,
}
forward("dump.record", body)
end
end
end
def each_table
@context.database.each(:ignore_missing_object => true) do |object|
next unless object.is_a?(::Groonga::Table)
next if index_only_table?(object)
yield(object)
end
end
def index_only_table?(table)
table.columns.all? do |column|
column.is_a?(::Groonga::IndexColumn)
end
end
end
define_single_step do |step|
step.name = "dump"
step.handler = Handler
step.collector = Collectors::And
end
end
end
end