module Shameless::Model
Attributes
store[R]
Public Instance Methods
attach_to(store, name)
click to toggle source
# File lib/shameless/model.rb, line 9 def attach_to(store, name) @store = store @name = name || self.name.downcase # TODO use activesupport? @cell_names = [] cell(Cell::BASE) include(InstanceMethods) end
cell(name)
click to toggle source
# File lib/shameless/model.rb, line 26 def cell(name) name = name.to_s @cell_names << name define_method(name) { @cells[name] } end
create_tables!()
click to toggle source
# File lib/shameless/model.rb, line 89 def create_tables! @store.create_table!(table_name) do |t, sharded_table_name| t.primary_key :id t.varchar :uuid, size: 36 t.varchar :column_name, null: false t.integer :ref_key, null: false t.mediumblob :body created_at_type = @store.configuration.legacy_created_at_is_bigint ? :bigint : :datetime t.column :created_at, created_at_type, null: false t.index %i[uuid column_name ref_key], name: "#{sharded_table_name}_model", unique: true end @indices.each(&:create_tables!) end
fetch_cell(shardable_value, uuid, cell_name, ref_key)
click to toggle source
# File lib/shameless/model.rb, line 61 def fetch_cell(shardable_value, uuid, cell_name, ref_key) query = {uuid: uuid, column_name: cell_name} query[:ref_key] = ref_key if ref_key @store.where(table_name, shardable_value, query).order(:ref_key).last end
fetch_latest_cells(shard:, cursor:, limit:)
click to toggle source
# File lib/shameless/model.rb, line 68 def fetch_latest_cells(shard:, cursor:, limit:) query = Sequel.lit("id > ?", cursor) @store.where(table_name, shard, query).limit(limit).map do |cell_values| model = new(cell_values[:uuid]) name = cell_values[:column_name].to_sym Cell.new(model, name, cell_values) end end
index(name = nil, &block)
click to toggle source
# File lib/shameless/model.rb, line 18 def index(name = nil, &block) @indices ||= [] index = Index.new(name, self, &block) @indices << index define_singleton_method(index.full_name) { index } end
initialize_cells(instance, base_body)
click to toggle source
# File lib/shameless/model.rb, line 33 def initialize_cells(instance, base_body) Hash[@cell_names.map do |name| cell = name == Cell::BASE ? Cell.base(instance, base_body) : Cell.new(instance, name) [name, cell] end] end
max_id_on_shard(shard)
click to toggle source
# File lib/shameless/model.rb, line 77 def max_id_on_shard(shard) @store.find_table(table_name, shard).max(:id) end
prevent_readonly_attribute_mutation!(key)
click to toggle source
# File lib/shameless/model.rb, line 114 def prevent_readonly_attribute_mutation!(key) @indices.each {|i| i.prevent_readonly_attribute_mutation!(key) } end
put(values)
click to toggle source
# File lib/shameless/model.rb, line 40 def put(values) if model = where(values).first model_values = reject_index_values(values) model.update(model_values) model else uuid = SecureRandom.uuid new(uuid, values).tap do |m| m.save index_values = values.merge(uuid: uuid) @indices.each {|i| i.put(index_values) } end end end
put_cell(shardable_value, cell_values)
click to toggle source
# File lib/shameless/model.rb, line 57 def put_cell(shardable_value, cell_values) @store.put(table_name, shardable_value, cell_values) end
reject_index_values(values)
click to toggle source
# File lib/shameless/model.rb, line 110 def reject_index_values(values) values.reject {|k, _| @indices.any? {|i| i.column?(k) } } end
table_name()
click to toggle source
# File lib/shameless/model.rb, line 81 def table_name [@store.name, @name].compact.join('_') end
table_names()
click to toggle source
# File lib/shameless/model.rb, line 85 def table_names [table_name, *@indices.map(&:table_name)] end
where(query, &block)
click to toggle source
# File lib/shameless/model.rb, line 106 def where(query, &block) primary_index.where(query, &block) end