Module: HBase::Util

Included in:
Scoped, Table, Table::Mutation
Defined in:
lib/hbase-jruby/util.rb

Constant Summary collapse

JAVA_BYTE_ARRAY_EMPTY =
[].to_java(Java::byte)
JAVA_BYTE_ARRAY_CLASS =
JAVA_BYTE_ARRAY_EMPTY.java_class

Class Method Summary collapse

Class Method Details

.append_0(v) ⇒ byte[]

Returns a byte array with a trailing ‘0’ byte

Parameters:

  • v (byte[])

Returns:

  • (byte[])


141
142
143
144
145
146
# File 'lib/hbase-jruby/util.rb', line 141

def append_0 v
  baos = java.io.ByteArrayOutputStream.new
  baos.write v, 0, v.length
  baos.write 0
  baos.toByteArray
end

.from_bytes(type, val) ⇒ Object

Returns Ruby object decoded from the byte array according to the given type

Parameters:

  • type (Symbol, Class)

    Type to convert to

  • val (byte[])

    Java byte array

Returns:

  • (Object)


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
# File 'lib/hbase-jruby/util.rb', line 108

def from_bytes type, val
  return nil if val.nil?

  import_java_classes!
  case type
  when :string, :str
    Bytes.to_string val
  when :fixnum, :long
    Bytes.to_long val
  when :byte
    val.first
  when :int
    Bytes.to_int val
  when :short
    Bytes.to_short val
  when :symbol, :sym
    Bytes.to_string(val).to_sym
  when :bigdecimal
    BigDecimal.new(Bytes.to_big_decimal(val).to_s)
  when :float, :double
    Bytes.to_double val
  when :boolean, :bool
    Bytes.to_boolean val
  when :raw, nil
    val
  else
    raise ArgumentError, "Invalid type: #{type}"
  end
end

.java_bytes?(v) ⇒ Boolean

Returns:

  • (Boolean)


9
10
11
# File 'lib/hbase-jruby/util.rb', line 9

def java_bytes? v
  v.respond_to?(:java_class) && v.java_class == JAVA_BYTE_ARRAY_CLASS
end

.parse_column_name(col) ⇒ Object

Extracts a byte array pair of column family and column qualifier from the given object

Parameters:

  • col (Object, Array, KeyValue)


150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
# File 'lib/hbase-jruby/util.rb', line 150

def parse_column_name col
  case col
  when KeyValue
    return col.getFamily, col.getQualifier
  when Array
    return to_bytes(col[0]), to_bytes(col[1])
  when '', nil
    raise ArgumentError, "Column family not specified"
  else
    col = col.to_s
    cf, cq = KeyValue.parseColumn(col.to_java_bytes)
    cq = JAVA_BYTE_ARRAY_EMPTY if cq.nil? && col[-1, 1] == ':'
    return cf, cq
  end
end

.to_bytes(v) ⇒ byte[]

Returns byte array representation of the Ruby object

Parameters:

  • v (byte[])

Returns:

  • (byte[])


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
# File 'lib/hbase-jruby/util.rb', line 16

def to_bytes v
  import_java_classes!

  case v
  when Array
    v.to_java(Java::byte)
  when String, ByteArray
    v.to_java_bytes
  when Fixnum
    Bytes.java_send :toBytes, [Java::long], v
  when Symbol
    v.to_s.to_java_bytes
  when Float
    Bytes.java_send :toBytes, [Java::double], v
  when true, false, ByteBuffer
    Bytes.to_bytes v
  when nil
    ''.to_java_bytes
  when Bignum
    raise ArgumentError, "Integer too large. Consider storing it as a BigDecimal."
  when BigDecimal
    Bytes.java_send :toBytes, [java.math.BigDecimal], v.to_java
  when java.math.BigDecimal
    Bytes.java_send :toBytes, [java.math.BigDecimal], v
  when Hash
    len = v.length
    raise ArgumentError, "Unknown value format" unless len == 1

    val = v.values.first
    raise ArgumentError, "Unknown value format" unless val.is_a?(Fixnum)

    case v.keys.first
    when :byte
      [val].to_java(Java::byte)
    when :int
      Bytes.java_send :toBytes, [Java::int], val
    when :short
      Bytes.java_send :toBytes, [Java::short], val
    when :long, :fixnum
      Bytes.java_send :toBytes, [Java::long], val
    else
      raise ArgumentError, "Invalid value format"
    end
  else
    if java_bytes?(v)
      v
    else
      raise ArgumentError.new("Don't know how to convert #{v.class} into Java bytes")
    end
  end
end

.to_typed_bytes(type, val) ⇒ Object



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
# File 'lib/hbase-jruby/util.rb', line 68

def to_typed_bytes type, val
  return nil               if val.nil?
  return Util.to_bytes val if type.nil?

  import_java_classes!
  case type
  when :string, :str, :symbol, :sym
    val.to_s.to_java_bytes
  when :byte
    [val].to_java(Java::byte)
  when :boolean, :bool
    Bytes.java_send :toBytes, [Java::boolean], val
  when :int
    Bytes.java_send :toBytes, [Java::int], val
  when :short
    Bytes.java_send :toBytes, [Java::short], val
  when :long, :fixnum
    Bytes.java_send :toBytes, [Java::long], val
  when :float, :double
    Bytes.java_send :toBytes, [Java::double], val
  when :bigdecimal
    case val
    when BigDecimal
      Bytes.java_send :toBytes, [java.math.BigDecimal], val.to_java
    when java.math.BigDecimal
      Bytes.java_send :toBytes, [java.math.BigDecimal], val
    else
      raise ArgumentError, "not BigDecimal"
    end
  when :raw
    val
  else
    raise ArgumentError, "invalid type: #{type}"
  end
end