Class: MassiveRecord::Adapters::Thrift::Row

Inherits:
Object
  • Object
show all
Defined in:
lib/massive_record/adapters/thrift/row.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ Row

Returns a new instance of Row.



8
9
10
11
12
13
14
15
# File 'lib/massive_record/adapters/thrift/row.rb', line 8

def initialize(opts = {})
  @id              = opts[:id]
  self.values      = opts[:values] || {}
  @table           = opts[:table]
  @column_families = opts[:column_families] || []
  @columns         = opts[:columns] || {}
  @new_record      = true
end

Instance Attribute Details

#column_familiesObject

Returns the value of attribute column_families.



6
7
8
# File 'lib/massive_record/adapters/thrift/row.rb', line 6

def column_families
  @column_families
end

#columnsObject

Returns the value of attribute columns.



6
7
8
# File 'lib/massive_record/adapters/thrift/row.rb', line 6

def columns
  @columns
end

#idObject

Returns the value of attribute id.



6
7
8
# File 'lib/massive_record/adapters/thrift/row.rb', line 6

def id
  @id
end

#new_recordObject

Returns the value of attribute new_record.



6
7
8
# File 'lib/massive_record/adapters/thrift/row.rb', line 6

def new_record
  @new_record
end

#tableObject

Returns the value of attribute table.



6
7
8
# File 'lib/massive_record/adapters/thrift/row.rb', line 6

def table
  @table
end

Class Method Details

.populate_from_trow_result(result, connection, table_name, column_families = []) ⇒ Object



128
129
130
131
132
133
134
135
136
137
138
139
140
# File 'lib/massive_record/adapters/thrift/row.rb', line 128

def self.populate_from_trow_result(result, connection, table_name, column_families = [])
  row                 = self.new
  row.id              = result.row
  row.new_record      = false
  row.table           = Table.new(connection, table_name)
  row.column_families = column_families

  result.columns.each do |name, value|
    row.columns[name] =  MassiveRecord::Wrapper::Cell.populate_from_tcell(value)
  end
      
  row
end

Instance Method Details

#atomic_decrement(column_name, by = 1) ⇒ Object



120
121
122
# File 'lib/massive_record/adapters/thrift/row.rb', line 120

def atomic_decrement(column_name, by = 1)
  atomic_increment(column_name, -by)
end

#atomic_increment(column_name, by = 1) ⇒ Object



116
117
118
# File 'lib/massive_record/adapters/thrift/row.rb', line 116

def atomic_increment(column_name, by = 1)
  @table.client.atomicIncrement(@table.name, id.to_s, column_name, by) 
end

#column_namesObject



17
18
19
# File 'lib/massive_record/adapters/thrift/row.rb', line 17

def column_names
  columns.keys
end

#destroyObject



142
143
144
# File 'lib/massive_record/adapters/thrift/row.rb', line 142

def destroy
  @table.client.deleteAllRow(@table.name, @id).nil?
end

#fetch_all_column_familiesObject



21
22
23
24
# File 'lib/massive_record/adapters/thrift/row.rb', line 21

def fetch_all_column_families
  @table.fetch_column_family
  fetch_column_families(@table.column_family_names)
end

#fetch_column_families(list) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/massive_record/adapters/thrift/row.rb', line 26

def fetch_column_families(list)
  @column_families = table.column_families.collect do |column_name, description|
     MassiveRecord::Wrapper::ColumnFamily.new(column_name.split(":").first, {
      :row          => self,
      :name         => description.name,
      :max_versions => description.maxVersions,
      :compression  => description.compression,
      :in_memory    => description.inMemory
      # bloomFilterType, bloomFilterVectorSize, bloomFilterNbHashes, blockCacheEnabled, timeToLive
    })
  end
end

#new_record?Boolean

Returns:

  • (Boolean)


146
147
148
# File 'lib/massive_record/adapters/thrift/row.rb', line 146

def new_record?
  @new_record
end

#prevObject



150
151
152
# File 'lib/massive_record/adapters/thrift/row.rb', line 150

def prev
  self
end

#read_atomic_integer_value(column_name) ⇒ Object



124
125
126
# File 'lib/massive_record/adapters/thrift/row.rb', line 124

def read_atomic_integer_value(column_name)
  atomic_increment(column_name, 0)
end

#saveObject

Parse columns cells and save them



98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/massive_record/adapters/thrift/row.rb', line 98

def save
  mutations = []
      
  @columns.each do |column_name, cell|
    mutations << Apache::Hadoop::Hbase::Thrift::Mutation.new(:column => column_name).tap do |mutation|
      if new_value = cell.value_to_thrift
        mutation.value = new_value
      else
        mutation.isDelete = true
      end
    end
  end

  @table.client.mutateRow(@table.name, id.to_s.dup.force_encoding(Encoding::BINARY), mutations).nil?
end

#update_column(column_family_name, column_name, value) ⇒ Object



87
88
89
90
91
92
93
94
95
# File 'lib/massive_record/adapters/thrift/row.rb', line 87

def update_column(column_family_name, column_name, value)
  column = "#{column_family_name}:#{column_name}"
      
  if @columns[column].nil?
    @columns[column] =  MassiveRecord::Wrapper::Cell.new({:value =>  value, :created_at => Time.now})
  else
    @columns[column].value = value
  end
end

#update_columns(data = {}) ⇒ Object



79
80
81
82
83
84
85
# File 'lib/massive_record/adapters/thrift/row.rb', line 79

def update_columns(data = {})
  data.each do |column_family_name, columns|
    columns.each do |column_name, values|
      update_column(column_family_name, column_name, values)
    end
  end
end

#updated_atObject



154
155
156
# File 'lib/massive_record/adapters/thrift/row.rb', line 154

def updated_at
  columns.values.collect(&:created_at).max
end

#valuesObject

Parse columns / cells and create a Hash from them



40
41
42
# File 'lib/massive_record/adapters/thrift/row.rb', line 40

def values
  @columns.inject({"id" => id}) {|h, (column_name, cell)| h[column_name] = cell.value; h}
end

#values=(data) ⇒ Object



74
75
76
77
# File 'lib/massive_record/adapters/thrift/row.rb', line 74

def values=(data)
  @values = {}
  update_columns(data)
end

#values_hashObject

Returns values as a nested hash.

{

'family' => {
  'attr1' => 'value'
  'attr2' => 'value'
},
...

}

I think maybe that values should return this instead, as it is what the values= expects to receive.



56
57
58
59
60
61
62
63
# File 'lib/massive_record/adapters/thrift/row.rb', line 56

def values_hash
  Hash.new { |hash, key| hash[key] = {} }.tap do |hash|
    @columns.each do |key, column|
      column_family, name = key.split(':')
      hash[column_family][name] = column.value
    end
  end
end

#values_raw_data_hashObject



65
66
67
68
69
70
71
72
# File 'lib/massive_record/adapters/thrift/row.rb', line 65

def values_raw_data_hash
  Hash.new { |hash, key| hash[key] = {} }.tap do |hash|
    @columns.each do |key, column|
      column_family, name = key.split(':')
      hash[column_family][name] = MassiveRecord::ORM::RawData.new_with_data_from column
    end
  end
end