taps-jruby 0.3.14
Sign up to get free protection for your applications and to get access to all the features.
- data/LICENSE +20 -0
- data/README.rdoc +51 -0
- data/Rakefile +70 -0
- data/TODO +1 -0
- data/VERSION.yml +5 -0
- data/bin/schema +54 -0
- data/bin/schema.cmd +6 -0
- data/bin/taps +6 -0
- data/lib/taps/cli.rb +193 -0
- data/lib/taps/config.rb +47 -0
- data/lib/taps/data_stream.rb +333 -0
- data/lib/taps/db_session.rb +20 -0
- data/lib/taps/errors.rb +15 -0
- data/lib/taps/log.rb +15 -0
- data/lib/taps/monkey.rb +21 -0
- data/lib/taps/multipart.rb +73 -0
- data/lib/taps/operation.rb +557 -0
- data/lib/taps/progress_bar.rb +236 -0
- data/lib/taps/schema.rb +83 -0
- data/lib/taps/server.rb +188 -0
- data/lib/taps/utils.rb +199 -0
- data/spec/base.rb +26 -0
- data/spec/cli_spec.rb +10 -0
- data/spec/data_stream_spec.rb +23 -0
- data/spec/operation_spec.rb +32 -0
- data/spec/server_spec.rb +35 -0
- data/spec/utils_spec.rb +61 -0
- metadata +194 -0
@@ -0,0 +1,236 @@
|
|
1
|
+
#
|
2
|
+
# Ruby/ProgressBar - a text progress bar library
|
3
|
+
#
|
4
|
+
# Copyright (C) 2001-2005 Satoru Takabayashi <satoru@namazu.org>
|
5
|
+
# All rights reserved.
|
6
|
+
# This is free software with ABSOLUTELY NO WARRANTY.
|
7
|
+
#
|
8
|
+
# You can redistribute it and/or modify it under the terms
|
9
|
+
# of Ruby's license.
|
10
|
+
#
|
11
|
+
|
12
|
+
class ProgressBar
|
13
|
+
VERSION = "0.9"
|
14
|
+
|
15
|
+
def initialize (title, total, out = STDERR)
|
16
|
+
@title = title
|
17
|
+
@total = total
|
18
|
+
@out = out
|
19
|
+
@terminal_width = 80
|
20
|
+
@bar_mark = "="
|
21
|
+
@current = 0
|
22
|
+
@previous = 0
|
23
|
+
@finished_p = false
|
24
|
+
@start_time = Time.now
|
25
|
+
@previous_time = @start_time
|
26
|
+
@title_width = 14
|
27
|
+
@format = "%-#{@title_width}s %3d%% %s %s"
|
28
|
+
@format_arguments = [:title, :percentage, :bar, :stat]
|
29
|
+
clear
|
30
|
+
show
|
31
|
+
end
|
32
|
+
attr_reader :title
|
33
|
+
attr_reader :current
|
34
|
+
attr_reader :total
|
35
|
+
attr_accessor :start_time
|
36
|
+
|
37
|
+
private
|
38
|
+
def fmt_bar
|
39
|
+
bar_width = do_percentage * @terminal_width / 100
|
40
|
+
sprintf("|%s%s|",
|
41
|
+
@bar_mark * bar_width,
|
42
|
+
" " * (@terminal_width - bar_width))
|
43
|
+
end
|
44
|
+
|
45
|
+
def fmt_percentage
|
46
|
+
do_percentage
|
47
|
+
end
|
48
|
+
|
49
|
+
def fmt_stat
|
50
|
+
if @finished_p then elapsed else eta end
|
51
|
+
end
|
52
|
+
|
53
|
+
def fmt_stat_for_file_transfer
|
54
|
+
if @finished_p then
|
55
|
+
sprintf("%s %s %s", bytes, transfer_rate, elapsed)
|
56
|
+
else
|
57
|
+
sprintf("%s %s %s", bytes, transfer_rate, eta)
|
58
|
+
end
|
59
|
+
end
|
60
|
+
|
61
|
+
def fmt_title
|
62
|
+
@title[0,(@title_width - 1)] + ":"
|
63
|
+
end
|
64
|
+
|
65
|
+
def convert_bytes (bytes)
|
66
|
+
if bytes < 1024
|
67
|
+
sprintf("%6dB", bytes)
|
68
|
+
elsif bytes < 1024 * 1000 # 1000kb
|
69
|
+
sprintf("%5.1fKB", bytes.to_f / 1024)
|
70
|
+
elsif bytes < 1024 * 1024 * 1000 # 1000mb
|
71
|
+
sprintf("%5.1fMB", bytes.to_f / 1024 / 1024)
|
72
|
+
else
|
73
|
+
sprintf("%5.1fGB", bytes.to_f / 1024 / 1024 / 1024)
|
74
|
+
end
|
75
|
+
end
|
76
|
+
|
77
|
+
def transfer_rate
|
78
|
+
bytes_per_second = @current.to_f / (Time.now - @start_time)
|
79
|
+
sprintf("%s/s", convert_bytes(bytes_per_second))
|
80
|
+
end
|
81
|
+
|
82
|
+
def bytes
|
83
|
+
convert_bytes(@current)
|
84
|
+
end
|
85
|
+
|
86
|
+
def format_time (t)
|
87
|
+
t = t.to_i
|
88
|
+
sec = t % 60
|
89
|
+
min = (t / 60) % 60
|
90
|
+
hour = t / 3600
|
91
|
+
sprintf("%02d:%02d:%02d", hour, min, sec);
|
92
|
+
end
|
93
|
+
|
94
|
+
# ETA stands for Estimated Time of Arrival.
|
95
|
+
def eta
|
96
|
+
if @current == 0
|
97
|
+
"ETA: --:--:--"
|
98
|
+
else
|
99
|
+
elapsed = Time.now - @start_time
|
100
|
+
eta = elapsed * @total / @current - elapsed;
|
101
|
+
sprintf("ETA: %s", format_time(eta))
|
102
|
+
end
|
103
|
+
end
|
104
|
+
|
105
|
+
def elapsed
|
106
|
+
elapsed = Time.now - @start_time
|
107
|
+
sprintf("Time: %s", format_time(elapsed))
|
108
|
+
end
|
109
|
+
|
110
|
+
def eol
|
111
|
+
if @finished_p then "\n" else "\r" end
|
112
|
+
end
|
113
|
+
|
114
|
+
def do_percentage
|
115
|
+
if @total.zero?
|
116
|
+
100
|
117
|
+
else
|
118
|
+
@current * 100 / @total
|
119
|
+
end
|
120
|
+
end
|
121
|
+
|
122
|
+
def get_width
|
123
|
+
# FIXME: I don't know how portable it is.
|
124
|
+
default_width = 80
|
125
|
+
begin
|
126
|
+
tiocgwinsz = 0x5413
|
127
|
+
data = [0, 0, 0, 0].pack("SSSS")
|
128
|
+
if @out.ioctl(tiocgwinsz, data) >= 0 then
|
129
|
+
rows, cols, xpixels, ypixels = data.unpack("SSSS")
|
130
|
+
if cols > 0 then cols else default_width end
|
131
|
+
else
|
132
|
+
default_width
|
133
|
+
end
|
134
|
+
rescue Exception
|
135
|
+
default_width
|
136
|
+
end
|
137
|
+
end
|
138
|
+
|
139
|
+
def show
|
140
|
+
arguments = @format_arguments.map {|method|
|
141
|
+
method = sprintf("fmt_%s", method)
|
142
|
+
send(method)
|
143
|
+
}
|
144
|
+
line = sprintf(@format, *arguments)
|
145
|
+
|
146
|
+
width = get_width
|
147
|
+
if line.length == width - 1
|
148
|
+
@out.print(line + eol)
|
149
|
+
@out.flush
|
150
|
+
elsif line.length >= width
|
151
|
+
@terminal_width = [@terminal_width - (line.length - width + 1), 0].max
|
152
|
+
if @terminal_width == 0 then @out.print(line + eol) else show end
|
153
|
+
else # line.length < width - 1
|
154
|
+
@terminal_width += width - line.length + 1
|
155
|
+
show
|
156
|
+
end
|
157
|
+
@previous_time = Time.now
|
158
|
+
end
|
159
|
+
|
160
|
+
def show_if_needed
|
161
|
+
if @total.zero?
|
162
|
+
cur_percentage = 100
|
163
|
+
prev_percentage = 0
|
164
|
+
else
|
165
|
+
cur_percentage = (@current * 100 / @total).to_i
|
166
|
+
prev_percentage = (@previous * 100 / @total).to_i
|
167
|
+
end
|
168
|
+
|
169
|
+
# Use "!=" instead of ">" to support negative changes
|
170
|
+
if cur_percentage != prev_percentage ||
|
171
|
+
Time.now - @previous_time >= 1 || @finished_p
|
172
|
+
show
|
173
|
+
end
|
174
|
+
end
|
175
|
+
|
176
|
+
public
|
177
|
+
def clear
|
178
|
+
@out.print "\r"
|
179
|
+
@out.print(" " * (get_width - 1))
|
180
|
+
@out.print "\r"
|
181
|
+
end
|
182
|
+
|
183
|
+
def finish
|
184
|
+
@current = @total
|
185
|
+
@finished_p = true
|
186
|
+
show
|
187
|
+
end
|
188
|
+
|
189
|
+
def finished?
|
190
|
+
@finished_p
|
191
|
+
end
|
192
|
+
|
193
|
+
def file_transfer_mode
|
194
|
+
@format_arguments = [:title, :percentage, :bar, :stat_for_file_transfer]
|
195
|
+
end
|
196
|
+
|
197
|
+
def format= (format)
|
198
|
+
@format = format
|
199
|
+
end
|
200
|
+
|
201
|
+
def format_arguments= (arguments)
|
202
|
+
@format_arguments = arguments
|
203
|
+
end
|
204
|
+
|
205
|
+
def halt
|
206
|
+
@finished_p = true
|
207
|
+
show
|
208
|
+
end
|
209
|
+
|
210
|
+
def inc (step = 1)
|
211
|
+
@current += step
|
212
|
+
@current = @total if @current > @total
|
213
|
+
show_if_needed
|
214
|
+
@previous = @current
|
215
|
+
end
|
216
|
+
|
217
|
+
def set (count)
|
218
|
+
if count < 0 || count > @total
|
219
|
+
raise "invalid count: #{count} (total: #{@total})"
|
220
|
+
end
|
221
|
+
@current = count
|
222
|
+
show_if_needed
|
223
|
+
@previous = @current
|
224
|
+
end
|
225
|
+
|
226
|
+
def inspect
|
227
|
+
"#<ProgressBar:#{@current}/#{@total}>"
|
228
|
+
end
|
229
|
+
end
|
230
|
+
|
231
|
+
class ReversedProgressBar < ProgressBar
|
232
|
+
def do_percentage
|
233
|
+
100 - super
|
234
|
+
end
|
235
|
+
end
|
236
|
+
|
data/lib/taps/schema.rb
ADDED
@@ -0,0 +1,83 @@
|
|
1
|
+
require 'sequel'
|
2
|
+
require 'sequel/extensions/schema_dumper'
|
3
|
+
require 'sequel/extensions/migration'
|
4
|
+
|
5
|
+
require 'json/pure'
|
6
|
+
|
7
|
+
module Taps
|
8
|
+
module Schema
|
9
|
+
extend self
|
10
|
+
|
11
|
+
def dump(database_url)
|
12
|
+
db = Sequel.connect(database_url)
|
13
|
+
db.dump_schema_migration(:indexes => false)
|
14
|
+
end
|
15
|
+
|
16
|
+
def dump_table(database_url, table)
|
17
|
+
table = table.to_sym
|
18
|
+
Sequel.connect(database_url) do |db|
|
19
|
+
<<END_MIG
|
20
|
+
Class.new(Sequel::Migration) do
|
21
|
+
def up
|
22
|
+
#{db.dump_table_schema(table.identifier, :indexes => false)}
|
23
|
+
end
|
24
|
+
|
25
|
+
def down
|
26
|
+
drop_table("#{table}") if @db.table_exists?("#{table}")
|
27
|
+
end
|
28
|
+
end
|
29
|
+
END_MIG
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
def indexes(database_url)
|
34
|
+
db = Sequel.connect(database_url)
|
35
|
+
db.dump_indexes_migration
|
36
|
+
end
|
37
|
+
|
38
|
+
def indexes_individual(database_url)
|
39
|
+
idxs = {}
|
40
|
+
Sequel.connect(database_url) do |db|
|
41
|
+
tables = db.tables
|
42
|
+
tables.each do |table|
|
43
|
+
idxs[table] = db.send(:dump_table_indexes, table, :add_index, {}).split("\n")
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
idxs.each do |table, indexes|
|
48
|
+
idxs[table] = indexes.map do |idx|
|
49
|
+
<<END_MIG
|
50
|
+
Class.new(Sequel::Migration) do
|
51
|
+
def up
|
52
|
+
#{idx}
|
53
|
+
end
|
54
|
+
end
|
55
|
+
END_MIG
|
56
|
+
end
|
57
|
+
end
|
58
|
+
idxs.to_json
|
59
|
+
end
|
60
|
+
|
61
|
+
def load(database_url, schema)
|
62
|
+
Sequel.connect(database_url) do |db|
|
63
|
+
klass = eval(schema)
|
64
|
+
klass.apply(db, :down)
|
65
|
+
klass.apply(db, :up)
|
66
|
+
end
|
67
|
+
end
|
68
|
+
|
69
|
+
def load_indexes(database_url, indexes)
|
70
|
+
Sequel.connect(database_url) do |db|
|
71
|
+
eval(indexes).apply(db, :up)
|
72
|
+
end
|
73
|
+
end
|
74
|
+
|
75
|
+
def reset_db_sequences(database_url)
|
76
|
+
db = Sequel.connect(database_url)
|
77
|
+
return unless db.respond_to?(:reset_primary_key_sequence)
|
78
|
+
db.tables.each do |table|
|
79
|
+
db.reset_primary_key_sequence(table)
|
80
|
+
end
|
81
|
+
end
|
82
|
+
end
|
83
|
+
end
|
data/lib/taps/server.rb
ADDED
@@ -0,0 +1,188 @@
|
|
1
|
+
require 'sinatra/base'
|
2
|
+
require 'taps/config'
|
3
|
+
require 'taps/utils'
|
4
|
+
require 'taps/db_session'
|
5
|
+
require 'taps/data_stream'
|
6
|
+
|
7
|
+
module Taps
|
8
|
+
class Server < Sinatra::Base
|
9
|
+
use Rack::Auth::Basic do |login, password|
|
10
|
+
login == Taps::Config.login && password == Taps::Config.password
|
11
|
+
end
|
12
|
+
|
13
|
+
use Rack::Deflater unless ENV['NO_DEFLATE']
|
14
|
+
|
15
|
+
error do
|
16
|
+
e = request.env['sinatra.error']
|
17
|
+
if e.kind_of?(Taps::BaseError)
|
18
|
+
content_type "application/json"
|
19
|
+
halt 412, { 'error_class' => e.class.to_s, 'error_message' => e.message, 'error_backtrace' => e.backtrace.join("\n") }.to_json
|
20
|
+
else
|
21
|
+
"Taps Server Error: #{e}\n#{e.backtrace}"
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
before do
|
26
|
+
major, minor, patch = request.env['HTTP_TAPS_VERSION'].split('.') rescue []
|
27
|
+
unless "#{major}.#{minor}" == Taps.compatible_version && patch.to_i >= 14
|
28
|
+
halt 417, "Taps >= v#{Taps.compatible_version}.14 is required for this server"
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
get '/' do
|
33
|
+
"hello"
|
34
|
+
end
|
35
|
+
|
36
|
+
post '/sessions' do
|
37
|
+
key = rand(9999999999).to_s
|
38
|
+
|
39
|
+
if ENV['NO_DEFAULT_DATABASE_URL']
|
40
|
+
database_url = request.body.string
|
41
|
+
else
|
42
|
+
database_url = Taps::Config.database_url || request.body.string
|
43
|
+
end
|
44
|
+
|
45
|
+
DbSession.create(:key => key, :database_url => database_url, :started_at => Time.now, :last_access => Time.now)
|
46
|
+
|
47
|
+
"/sessions/#{key}"
|
48
|
+
end
|
49
|
+
|
50
|
+
post '/sessions/:key/push/verify_stream' do
|
51
|
+
session = DbSession.filter(:key => params[:key]).first
|
52
|
+
halt 404 unless session
|
53
|
+
|
54
|
+
state = DataStream.parse_json(params[:state])
|
55
|
+
stream = nil
|
56
|
+
|
57
|
+
size = 0
|
58
|
+
session.conn do |db|
|
59
|
+
Taps::Utils.server_error_handling do
|
60
|
+
stream = DataStream.factory(db, state)
|
61
|
+
stream.verify_stream
|
62
|
+
end
|
63
|
+
end
|
64
|
+
|
65
|
+
content_type 'application/json'
|
66
|
+
{ :state => stream.to_hash }.to_json
|
67
|
+
end
|
68
|
+
|
69
|
+
post '/sessions/:key/push/table' do
|
70
|
+
session = DbSession.filter(:key => params[:key]).first
|
71
|
+
halt 404 unless session
|
72
|
+
|
73
|
+
json = DataStream.parse_json(params[:json])
|
74
|
+
|
75
|
+
size = 0
|
76
|
+
session.conn do |db|
|
77
|
+
Taps::Utils.server_error_handling do
|
78
|
+
stream = DataStream.factory(db, json[:state])
|
79
|
+
size = stream.fetch_remote_in_server(params)
|
80
|
+
end
|
81
|
+
end
|
82
|
+
|
83
|
+
# TODO: return the stream's state with the size
|
84
|
+
size.to_s
|
85
|
+
end
|
86
|
+
|
87
|
+
post '/sessions/:key/push/reset_sequences' do
|
88
|
+
session = DbSession.filter(:key => params[:key]).first
|
89
|
+
halt 404 unless session
|
90
|
+
|
91
|
+
Taps::Utils.schema_bin(:reset_db_sequences, session.database_url)
|
92
|
+
end
|
93
|
+
|
94
|
+
post '/sessions/:key/push/schema' do
|
95
|
+
session = DbSession.filter(:key => params[:key]).first
|
96
|
+
halt 404 unless session
|
97
|
+
|
98
|
+
schema_data = request.body.read
|
99
|
+
Taps::Utils.load_schema(session.database_url, schema_data)
|
100
|
+
end
|
101
|
+
|
102
|
+
post '/sessions/:key/push/indexes' do
|
103
|
+
session = DbSession.filter(:key => params[:key]).first
|
104
|
+
halt 404 unless session
|
105
|
+
|
106
|
+
index_data = request.body.read
|
107
|
+
Taps::Utils.load_indexes(session.database_url, index_data)
|
108
|
+
end
|
109
|
+
|
110
|
+
post '/sessions/:key/pull/schema' do
|
111
|
+
session = DbSession.filter(:key => params[:key]).first
|
112
|
+
halt 404 unless session
|
113
|
+
|
114
|
+
Taps::Utils.schema_bin(:dump_table, session.database_url, params[:table_name])
|
115
|
+
end
|
116
|
+
|
117
|
+
get '/sessions/:key/pull/indexes' do
|
118
|
+
session = DbSession.filter(:key => params[:key]).first
|
119
|
+
halt 404 unless session
|
120
|
+
|
121
|
+
content_type 'application/json'
|
122
|
+
Taps::Utils.schema_bin(:indexes_individual, session.database_url)
|
123
|
+
end
|
124
|
+
|
125
|
+
get '/sessions/:key/pull/table_names' do
|
126
|
+
session = DbSession.filter(:key => params[:key]).first
|
127
|
+
halt 404 unless session
|
128
|
+
|
129
|
+
tables = []
|
130
|
+
session.conn do |db|
|
131
|
+
tables = db.tables
|
132
|
+
end
|
133
|
+
|
134
|
+
content_type 'application/json'
|
135
|
+
tables.to_json
|
136
|
+
end
|
137
|
+
|
138
|
+
post '/sessions/:key/pull/table_count' do
|
139
|
+
session = DbSession.filter(:key => params[:key]).first
|
140
|
+
halt 404 unless session
|
141
|
+
|
142
|
+
count = 0
|
143
|
+
session.conn do |db|
|
144
|
+
count = db[ params[:table].to_sym.identifier ].count
|
145
|
+
end
|
146
|
+
count.to_s
|
147
|
+
end
|
148
|
+
|
149
|
+
post '/sessions/:key/pull/table' do
|
150
|
+
session = DbSession.filter(:key => params[:key]).first
|
151
|
+
halt 404 unless session
|
152
|
+
|
153
|
+
encoded_data = nil
|
154
|
+
stream = nil
|
155
|
+
|
156
|
+
session.conn do |db|
|
157
|
+
state = JSON.parse(params[:state]).symbolize_keys
|
158
|
+
stream = Taps::DataStream.factory(db, state)
|
159
|
+
encoded_data = stream.fetch.first
|
160
|
+
end
|
161
|
+
|
162
|
+
checksum = Taps::Utils.checksum(encoded_data).to_s
|
163
|
+
json = { :checksum => checksum, :state => stream.to_hash }.to_json
|
164
|
+
|
165
|
+
content, content_type_value = Taps::Multipart.create do |r|
|
166
|
+
r.attach :name => :encoded_data,
|
167
|
+
:payload => encoded_data,
|
168
|
+
:content_type => 'application/octet-stream'
|
169
|
+
r.attach :name => :json,
|
170
|
+
:payload => json,
|
171
|
+
:content_type => 'application/json'
|
172
|
+
end
|
173
|
+
|
174
|
+
content_type content_type_value
|
175
|
+
content
|
176
|
+
end
|
177
|
+
|
178
|
+
delete '/sessions/:key' do
|
179
|
+
session = DbSession.filter(:key => params[:key]).first
|
180
|
+
halt 404 unless session
|
181
|
+
|
182
|
+
session.destroy
|
183
|
+
|
184
|
+
"ok"
|
185
|
+
end
|
186
|
+
|
187
|
+
end
|
188
|
+
end
|