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



89
90
91
92
93
94
95
96
# File 'lib/hbase-jruby/table/mutation.rb', line 89

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



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
71
# File 'lib/hbase-jruby/table/mutation.rb', line 38

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



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/hbase-jruby/table/mutation.rb', line 73

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)


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

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
  }
end

#put(rowkey, props) ⇒ 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
# File 'lib/hbase-jruby/table/mutation.rb', line 11

def put rowkey, props
  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
      else
        put.add cf, cq, Util.to_typed_bytes(type, val)
      end
    end
    raise ArgumentError, "no column to put" if put.empty?
  }
end