Module: HBase::Util
- Included in:
- HBase, 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
-
.append_0(v) ⇒ byte[]
Returns a byte array with a trailing ‘0’ byte.
-
.from_bytes(type, val) ⇒ Object
Returns Ruby object decoded from the byte array according to the given type.
- .java_bytes?(v) ⇒ Boolean
-
.parse_column_name(col) ⇒ Object
Extracts a byte array pair of column family and column qualifier from the given object.
-
.to_bytes(v) ⇒ byte[]
Returns byte array representation of the Ruby object.
- .to_typed_bytes(type, val) ⇒ Object
Class Method Details
.append_0(v) ⇒ byte[]
Returns a byte array with a trailing ‘0’ byte
151 152 153 154 155 156 |
# File 'lib/hbase-jruby/util.rb', line 151 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
114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 |
# File 'lib/hbase-jruby/util.rb', line 114 def from_bytes type, val return nil if val.nil? HBase.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 Bytes.to_float val when :double Bytes.to_double val when :boolean, :bool Bytes.to_boolean val when :byte_array ByteArray[val] when :raw, nil val else raise ArgumentError, "Invalid type: #{type}" end end |
.java_bytes?(v) ⇒ 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
160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 |
# File 'lib/hbase-jruby/util.rb', line 160 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 cfcq = KeyValue.parseColumn(col.to_java_bytes) cf = cfcq[0] cq = if cfcq.length == 2 cfcq[1] elsif col[-1, 1] == ':' JAVA_BYTE_ARRAY_EMPTY end return cf, cq end end |
.to_bytes(v) ⇒ byte[]
Returns byte array representation of the Ruby object
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 67 68 69 70 |
# File 'lib/hbase-jruby/util.rb', line 16 def to_bytes v HBase.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?(Numeric) 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 when :float Bytes.java_send :toBytes, [Java::float], val when :double Bytes.java_send :toBytes, [Java::double], 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
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 103 104 105 106 107 108 |
# File 'lib/hbase-jruby/util.rb', line 72 def to_typed_bytes type, val return nil if val.nil? return Util.to_bytes val if type.nil? HBase.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 Bytes.java_send :toBytes, [Java::float], val when :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 |