Class: HbaseAdapter::Row

Inherits:
Object
  • Object
show all
Defined in:
lib/hbase_adapter/row.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(connection, table, key) ⇒ Row

Returns a new instance of Row.



8
9
10
11
12
# File 'lib/hbase_adapter/row.rb', line 8

def initialize(connection, table, key)
  @connection = connection
  @table = table
  @key = key
end

Instance Attribute Details

#connectionObject (readonly)

Returns the value of attribute connection.



6
7
8
# File 'lib/hbase_adapter/row.rb', line 6

def connection
  @connection
end

#keyObject (readonly)

Returns the value of attribute key.



6
7
8
# File 'lib/hbase_adapter/row.rb', line 6

def key
  @key
end

#tableObject (readonly)

Returns the value of attribute table.



6
7
8
# File 'lib/hbase_adapter/row.rb', line 6

def table
  @table
end

Instance Method Details

#[](column, options = {}) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/hbase_adapter/row.rb', line 14

def [](column, options = {})
  num_versions = options[:num_versions]
  timestamp = options[:timestamp]

  if num_versions.nil? && timestamp.nil?
    # One column, no options.  Simple get.  Return a single cell
    tcell = connection.client.get(table.name, key, column).first
    HbaseAdapter::Cell.new(connection, self, column, tcell) if tcell
  else
    if timestamp.nil?
      # One column, versions specified.  getVer
      tcells = connection.client.getVer(table.name, key, column, num_versions)
    else
      # One column, versions specified, max time specified.  getVerTs
      tcells = connection.client.getVerTs(table.name, key, column, timestamp.to_i64, num_versions)
    end
    
    tcells.compact.map {|tcell| HbaseAdapter::Cell.new(connection, self, column, tcell)}
  end
end

#cells(*columns) ⇒ Object



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_adapter/row.rb', line 35

def cells(*columns)
  options = columns.pop if columns.last.is_a?(Hash)
  options ||= {}
  timestamp = options[:timestamp]
  
  if columns.empty?
    if timestamp.nil?
      # getRow
      trow_results = connection.client.getRow(table.name, key)
    else
      # getRowTs
      trow_results = connection.client.getRowTs(table.name, key, timestamp.to_i64)
    end
  else
    if timestamp.nil?
      # getRowWithColumns
      trow_results = connection.client.getRowWithColumns(table.name, key, columns)
    else
      # getRowWithColumnsTs
      trow_results = connection.client.getRowWithColumnsTs(table.name, key, columns, timestamp.to_i64)
    end
  end

  unless trow_results.empty?
    trow_results.first.columns.inject({}) do |hash, (col_name, tcell)|
      hash[col_name.to_sym] = HbaseAdapter::Cell.new(connection, self, col_name, tcell) if tcell
      hash
    end
  end
end

#delete!(col, options = {}) ⇒ Object



66
67
68
69
70
71
72
73
74
# File 'lib/hbase_adapter/row.rb', line 66

def delete!(col, options = {})
  timestamp = options[:timestamp]
  
  if timestamp.nil?
    connection.client.deleteAll(table.name, key.to_s, col.to_s)
  else
    connection.client.deleteAllTs(table.name, key.to_s, col.to_s, timestamp.to_i64)
  end
end

#mutate!(options = {}, &blk) ⇒ Object



76
77
78
79
80
81
82
83
84
85
86
# File 'lib/hbase_adapter/row.rb', line 76

def mutate!(options = {}, &blk)
  timestamp = options[:timestamp]
  
  ml = HbaseAdapter::MutationList.new(&blk)
  
  if timestamp.nil?
    connection.client.mutateRow(table.name, key, ml.to_thrift)
  else
    connection.client.mutateRowTs(table.name, key, ml.to_thrift, timestamp.to_i64)
  end
end