Class: HBase::Table::Mutation

Inherits:
Object
  • Object
show all
Includes:
Util
Defined in:
lib/hbase-jruby/table/mutation.rb

Overview

Generate single-row mutation objects

Defined Under Namespace

Classes: Mutator

Constant Summary

Constants included from Util

Util::JAVA_BYTE_ARRAY_CLASS, Util::JAVA_BYTE_ARRAY_EMPTY

Instance Method Summary collapse

Methods included from Util

append_0, from_bytes, java_bytes?, parse_column_name, to_bytes, to_typed_bytes

Constructor Details

#initialize(table) ⇒ Mutation

Returns a new instance of Mutation.



7
8
9
# File 'lib/hbase-jruby/table/mutation.rb', line 7

def initialize table
  @table = table
end

Instance Method Details

#append(rowkey, spec) ⇒ Object



99
100
101
102
103
104
105
106
# File 'lib/hbase-jruby/table/mutation.rb', line 99

def append rowkey, spec
  Append.new(Util.to_bytes rowkey).tap { |apnd|
    spec.each do |col, val|
      cf, cq, _ = @table.lookup_and_parse col, true
      apnd.add(cf, cq, Util.to_bytes(val))
    end
  }
end

#delete(rowkey, *extra) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/hbase-jruby/table/mutation.rb', line 48

def delete rowkey, *extra
  Delete.new(Util.to_bytes rowkey).tap { |del|
    cf = cq = nil
    prcd = false

    prc = lambda do
      unless prcd
        if cq
          # Delete all versions
          del.deleteColumns cf, cq
        elsif cf
          del.deleteFamily cf
        end
      end
    end

    extra.each do |x|
      case x
      when Fixnum, Time
        if cq
          del.deleteColumn cf, cq, time_to_long(x)
          prcd = true
        else
          raise ArgumentError, 'qualifier not given'
        end
      else
        prc.call
        cf, cq, _ = @table.lookup_and_parse x, false
        prcd = false
      end
    end
    prc.call
  }
end

#increment(rowkey, *spec) ⇒ Object



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/hbase-jruby/table/mutation.rb', line 83

def increment rowkey, *spec
  if spec.first.is_a?(Hash)
    spec = spec.first
  else
    c, b = spec
    spec = { c => (b || 1) }
  end

  Increment.new(Util.to_bytes rowkey).tap { |inc|
    spec.each do |col, by|
      cf, cq, _ = @table.lookup_and_parse col, true
      inc.addColumn cf, cq, by
    end
  }
end

#mutate(rowkey) {|rm| ... } ⇒ Object

Yields:

  • (rm)


108
109
110
111
112
113
114
115
116
# File 'lib/hbase-jruby/table/mutation.rb', line 108

def mutate rowkey
  rm = Mutator.new(self, rowkey)
  yield rm
  org.apache.hadoop.hbase.client.RowMutations.new(Util.to_bytes rowkey).tap { |m|
    rm.mutations.each do |action|
      m.add action
    end
  } unless rm.empty?
end

#put(rowkey, props, timestamp = nil) ⇒ Object



11
12
13
14
15
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
# File 'lib/hbase-jruby/table/mutation.rb', line 11

def put rowkey, props, timestamp = nil
  Put.new(Util.to_bytes rowkey).tap { |put|
    props.each do |col, val|
      next if val.nil?

      cf, cq, type = @table.lookup_and_parse col, true

      case val
      when Hash
        val.each do |t, v|
          case t
          # Timestamp / Ruby Time
          when Time, Fixnum
            put.add cf, cq, time_to_long(t), Util.to_typed_bytes(type, v)
          # Types: :byte, :short, :int, ...
          else
            put.add cf, cq, Util.to_typed_bytes(t, v)
          end unless v.nil?
        end
      when String
        if timestamp
          put.add cf, cq, time_to_long(timestamp), val.to_java_bytes
        else
          put.add cf, cq, val.to_java_bytes
        end
      else
        if timestamp
          put.add cf, cq, time_to_long(timestamp), Util.to_typed_bytes(type, val)
        else
          put.add cf, cq, Util.to_typed_bytes(type, val)
        end
      end
    end
    raise ArgumentError, "no column to put" if put.empty?
  }
end