Module: Spark::Helper::Parser::Methods
- Defined in:
- lib/spark/helper/parser.rb
Instance Method Summary collapse
- #convert_to_java_int(data) ⇒ Object
-
#memory_multiplier_based_kb(type) ⇒ Object
Based to KB.
- #to_java_array_list(array) ⇒ Object
- #to_java_hash(hash) ⇒ Object
-
#to_memory_size(memory, result_unit = "KB") ⇒ Object
Parse and convert memory size.
Instance Method Details
#convert_to_java_int(data) ⇒ Object
23 24 25 26 27 28 29 |
# File 'lib/spark/helper/parser.rb', line 23 def convert_to_java_int(data) if data.is_a?(Array) data.map{|x| JInteger.new(x)} else JInteger.new(data) end end |
#memory_multiplier_based_kb(type) ⇒ Object
Based to KB
66 67 68 69 70 71 72 73 74 75 76 77 |
# File 'lib/spark/helper/parser.rb', line 66 def memory_multiplier_based_kb(type) case type.to_s.upcase when "G", "GB" 1048576 when "M", "MB" 1024 when "K", "KB" 1 else raise Spark::ParseError, "Unsupported type #{type}" end end |
#to_java_array_list(array) ⇒ Object
31 32 33 34 35 36 37 |
# File 'lib/spark/helper/parser.rb', line 31 def to_java_array_list(array) array_list = ArrayList.new array.each do |item| array_list.add(item) end array_list end |
#to_java_hash(hash) ⇒ Object
11 12 13 14 15 16 17 18 19 20 21 |
# File 'lib/spark/helper/parser.rb', line 11 def to_java_hash(hash) hash_map = HashMap.new hash.each_pair do |key, value| begin # RJB raise Object is NULL (but new record is put correctly) hash_map.put(key, value) rescue RuntimeError end end hash_map end |
#to_memory_size(memory, result_unit = "KB") ⇒ Object
Parse and convert memory size. Shifting be better but Float doesn’t support it.
Examples:
to_memory_size("512mb")
# => 524288
to_memory_size("512 MB")
# => 524288
to_memory_size("512mb", "GB")
# => 0.5
51 52 53 54 55 56 57 58 59 60 61 62 63 |
# File 'lib/spark/helper/parser.rb', line 51 def to_memory_size(memory, result_unit="KB") match = memory.match(/([\d]+)[\s]*([\w]*)/) if match.nil? raise Spark::ParseError, "Memory has wrong format. Use: 'SIZE UNIT'" end size = match[1].to_f unit = match[2] size *= memory_multiplier_based_kb(unit) size /= memory_multiplier_based_kb(result_unit) size.round(2) end |