Module: MassiveRecord::ORM::Persistence::Operations::TableOperationHelpers

Included in:
AtomicOperation, Destroy, Insert, Reload, Update
Defined in:
lib/massive_record/orm/persistence/operations/table_operation_helpers.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.calculate_missing_family_names(klass) ⇒ Object

Calculate which column families are missing in the database in context of what the schema instructs.



20
21
22
23
24
25
# File 'lib/massive_record/orm/persistence/operations/table_operation_helpers.rb', line 20

def self.calculate_missing_family_names(klass)
  existing_family_names = klass.table.fetch_column_families.collect(&:name) rescue []
  expected_family_names = klass.column_families ? klass.column_families.collect(&:name) : []

  expected_family_names.collect(&:to_s) - existing_family_names.collect(&:to_s)
end

.hbase_create_table!(klass) ⇒ Object

Creates table for ORM classes



30
31
32
33
34
# File 'lib/massive_record/orm/persistence/operations/table_operation_helpers.rb', line 30

def self.hbase_create_table!(klass)
  missing_family_names = calculate_missing_family_names(klass)
  klass.table.create_column_families(missing_family_names) unless missing_family_names.empty?
  klass.table.save
end

.included(base) ⇒ Object



9
10
11
12
13
# File 'lib/massive_record/orm/persistence/operations/table_operation_helpers.rb', line 9

def self.included(base)
  base.class_eval do
    include MassiveRecord::ORM::QueryInstrumentation::Operations
  end
end

Instance Method Details

#attributes_to_row_values_hash(only_attr_names = []) ⇒ Object

Returns attributes on a form which Wrapper::Row expects



77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/massive_record/orm/persistence/operations/table_operation_helpers.rb', line 77

def attributes_to_row_values_hash(only_attr_names = [])
  values = Hash.new { |hash, key| hash[key] = Hash.new }

  record.attributes_schema.each do |attr_name, orm_field|
    next unless only_attr_names.empty? || only_attr_names.include?(attr_name)
    values[orm_field.column_family.name][orm_field.column] = orm_field.encode(record[attr_name])
  end

  record.relation_proxies_for_embedded.select { |p| p.proxy_targets_update_hash.any? }.each do |proxy_with_changes|
    values[proxy_with_changes..store_in].merge!(proxy_with_changes.proxy_targets_update_hash)
  end

  values
end

#calculate_missing_family_namesObject



53
54
55
# File 'lib/massive_record/orm/persistence/operations/table_operation_helpers.rb', line 53

def calculate_missing_family_names
  TableOperationHelpers.calculate_missing_family_names(klass)
end

#ensure_that_we_have_table_and_column_families!Object

Iterates over tables and column families and ensure that we have what we need



40
41
42
43
44
45
46
# File 'lib/massive_record/orm/persistence/operations/table_operation_helpers.rb', line 40

def ensure_that_we_have_table_and_column_families! # :nodoc:
  #
  # TODO: Can we skip checking if it exists at all, and instead, rescue it if it does not?
  #
  hbase_create_table! unless klass.table.exists?
  raise ColumnFamiliesMissingError.new(klass, calculate_missing_family_names) if calculate_missing_family_names.any?
end

#hbase_create_table!Object



48
49
50
# File 'lib/massive_record/orm/persistence/operations/table_operation_helpers.rb', line 48

def hbase_create_table!
  TableOperationHelpers.hbase_create_table!(klass)
end

#row_for_recordObject

Returns a Wrapper::Row class which we can manipulate this record in the database with

Raises:



65
66
67
68
69
70
71
72
# File 'lib/massive_record/orm/persistence/operations/table_operation_helpers.rb', line 65

def row_for_record
  raise IdMissing.new("You must set an ID before save.") if record.id.blank?

  MassiveRecord::Wrapper::Row.new({
    :id => record.id,
    :table => klass.table
  })
end

#store_record_to_database(action, attribute_names_to_update = []) ⇒ Object

Takes care of the actual storing of the record to the database Both update and create is using this



97
98
99
100
101
# File 'lib/massive_record/orm/persistence/operations/table_operation_helpers.rb', line 97

def store_record_to_database(action, attribute_names_to_update = [])
  row = row_for_record
  row.values = attributes_to_row_values_hash(attribute_names_to_update)
  row.save
end