Module: Taps::Utils

Extended by:
Utils
Included in:
Utils
Defined in:
lib/taps/utils.rb

Instance Method Summary collapse

Instance Method Details

#base64decode(data) ⇒ Object



35
36
37
# File 'lib/taps/utils.rb', line 35

def base64decode(data)
  data.unpack("m").first
end

#base64encode(data) ⇒ Object



31
32
33
# File 'lib/taps/utils.rb', line 31

def base64encode(data)
  [data].pack("m")
end

#bin(cmd) ⇒ Object



18
19
20
21
# File 'lib/taps/utils.rb', line 18

def bin(cmd)
  cmd = "#{cmd}.cmd" if windows?
  cmd
end

#blobs_to_string(row, columns) ⇒ Object



87
88
89
90
91
92
93
# File 'lib/taps/utils.rb', line 87

def blobs_to_string(row, columns)
  return row if columns.size == 0
  columns.each do |c|
    row[c] = row[c].to_s if row[c].kind_of?(Sequel::SQL::Blob)
  end
  row
end

#calculate_chunksize(old_chunksize) ⇒ Object



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
# File 'lib/taps/utils.rb', line 95

def calculate_chunksize(old_chunksize)
  chunksize = old_chunksize

  retries = 0
  time_in_db = 0
  begin
    t1 = Time.now
    time_in_db = yield chunksize
    time_in_db = time_in_db.to_f rescue 0
  rescue Errno::EPIPE, RestClient::RequestFailed, RestClient::RequestTimeout
    retries += 1
    raise if retries > 2

    # we got disconnected, the chunksize could be too large
    # on first retry change to 10, on successive retries go down to 1
    chunksize = (retries == 1) ? 10 : 1

    retry
  end

  t2 = Time.now

  diff = t2 - t1 - time_in_db

  new_chunksize = if retries > 0
    chunksize
  elsif diff > 3.0
    (chunksize / 3).ceil
  elsif diff > 1.1
    chunksize - 100
  elsif diff < 0.8
    chunksize * 2
  else
    chunksize + 100
  end
  new_chunksize = 1 if new_chunksize < 1
  new_chunksize
end

#checksum(data) ⇒ Object



23
24
25
# File 'lib/taps/utils.rb', line 23

def checksum(data)
  Zlib.crc32(data)
end

#format_data(data, opts = {}) ⇒ Object



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
# File 'lib/taps/utils.rb', line 39

def format_data(data, opts={})
  return {} if data.size == 0
  string_columns = opts[:string_columns] || []
  schema = opts[:schema] || []
  table  = opts[:table]

  max_lengths = schema.inject({}) do |hash, (column, meta)|
    if meta[:db_type] =~ /^\w+\((\d+)\)/
      hash.update(column => $1.to_i)
    end
    hash
  end

  header = data[0].keys
  only_data = data.collect do |row|
    row = blobs_to_string(row, string_columns)
    row.each do |column, data|
      if data.to_s.length > (max_lengths[column] || data.to_s.length)
        raise Taps::InvalidData.new(<<-ERROR)
Detected data that exceeds the length limitation of its column. This is
generally due to the fact that SQLite does not enforce length restrictions.

Table  : #{table}
Column : #{column}
Type   : #{schema.detect{|s| s.first == column}.last[:db_type]}
Data   : #{data}
        ERROR
      end
    end
    header.collect { |h| row[h] }
  end
  { :header => header, :data => only_data }
end

#incorrect_blobs(db, table) ⇒ Object

mysql text and blobs fields are handled the same way internally this is not true for other databases so we must check if the field is actually text and manually convert it back to a string



76
77
78
79
80
81
82
83
84
85
# File 'lib/taps/utils.rb', line 76

def incorrect_blobs(db, table)
  return [] if (db.url =~ /mysql:\/\//).nil?

  columns = []
  db.schema(table).each do |data|
    column, cdata = data
    columns << column if cdata[:db_type] =~ /text/
  end
  columns
end

#load_indexes(database_url, index_data) ⇒ Object



141
142
143
144
145
146
# File 'lib/taps/utils.rb', line 141

def load_indexes(database_url, index_data)
  Tempfile.open('taps') do |tmp|
    File.open(tmp.path, 'w') { |f| f.write(index_data) }
    schema_bin(:load_indexes, database_url, tmp.path)
  end
end

#load_schema(database_url, schema_data) ⇒ Object



134
135
136
137
138
139
# File 'lib/taps/utils.rb', line 134

def load_schema(database_url, schema_data)
  Tempfile.open('taps') do |tmp|
    File.open(tmp.path, 'w') { |f| f.write(schema_data) }
    schema_bin(:load, database_url, tmp.path)
  end
end

#order_by(db, table) ⇒ Object



163
164
165
166
167
168
169
170
171
# File 'lib/taps/utils.rb', line 163

def order_by(db, table)
  pkey = primary_key(db, table)
  if pkey
    pkey.kind_of?(Array) ? pkey : [pkey.to_sym]
  else
    table = table.to_sym.identifier unless table.kind_of?(Sequel::SQL::Identifier)
    db[table].columns
  end
end

#primary_key(db, table) ⇒ Object



153
154
155
# File 'lib/taps/utils.rb', line 153

def primary_key(db, table)
  db.schema(table).select { |c| c[1][:primary_key] }.map { |c| c[0] }
end

#reraise_server_exception(e) ⇒ Object



188
189
190
191
192
193
194
195
196
197
# File 'lib/taps/utils.rb', line 188

def reraise_server_exception(e)
  if e.kind_of?(RestClient::Exception)
    if e.respond_to?(:response) && e.response.headers[:content_type] == 'application/json'
      json = JSON.parse(e.response.to_s)
      klass = eval(json['error_class']) rescue nil
      raise klass.new(json['error_message'], :backtrace => json['error_backtrace']) if klass
    end
  end
  raise e
end

#schema_bin(*args) ⇒ Object



148
149
150
151
# File 'lib/taps/utils.rb', line 148

def schema_bin(*args)
  bin_path = File.expand_path("#{File.dirname(__FILE__)}/../../bin/#{bin('schema')}")
  `"#{bin_path}" #{args.map { |a| "'#{a}'" }.join(' ')}`
end

#server_error_handling(&blk) ⇒ Object

try to detect server side errors to give the client a more useful error message



176
177
178
179
180
181
182
183
184
185
186
# File 'lib/taps/utils.rb', line 176

def server_error_handling(&blk)
  begin
    blk.call
  rescue Sequel::DatabaseError => e
    if e.message =~ /duplicate key value/i
      raise Taps::DuplicatePrimaryKeyError, e.message
    else
      raise
    end
  end
end

#single_integer_primary_key(db, table) ⇒ Object



157
158
159
160
161
# File 'lib/taps/utils.rb', line 157

def single_integer_primary_key(db, table)
  table = table.to_sym.identifier unless table.kind_of?(Sequel::SQL::Identifier)
  keys = db.schema(table).select { |c| c[1][:primary_key] and c[1][:type] == :integer }
  not keys.nil? and keys.size == 1
end

#valid_data?(data, crc32) ⇒ Boolean

Returns:

  • (Boolean)


27
28
29
# File 'lib/taps/utils.rb', line 27

def valid_data?(data, crc32)
  Zlib.crc32(data) == crc32.to_i
end

#windows?Boolean

Returns:

  • (Boolean)


12
13
14
15
16
# File 'lib/taps/utils.rb', line 12

def windows?
  return @windows if defined?(@windows)
  require 'rbconfig'
  @windows = !!(::Config::CONFIG['host_os'] =~ /mswin|mingw/)
end