Module: HBase::Util

Included in:
Scoped, Table
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[])


102
103
104
105
106
107
# File 'lib/hbase-jruby/util.rb', line 102

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)


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

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

  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
    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)


111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/hbase-jruby/util.rb', line 111

def parse_column_name col
  case col
  when ColumnKey
    return col.cf.to_java_bytes, col.cq(:raw)
  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
    cf, cq = KeyValue.parseColumn(col.to_s.to_java_bytes)
    cq = JAVA_BYTE_ARRAY_EMPTY if cq.nil? && col.to_s[-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
# File 'lib/hbase-jruby/util.rb', line 16

def to_bytes v
  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