Class: ActiveDataFrame::Row
Instance Attribute Summary collapse
#block_type, #block_type_name, #data_frame_type, #plural_df_name, #singular_df_name, #value_map
Class Method Summary
collapse
Instance Method Summary
collapse
#[], #[]=, #blocks_between, #clear, #column_map, #column_name_map, #database, #extract_ranges, #get_bounds, get_bounds, iterate_bounds, #iterate_bounds, #match_range, #method_missing, #range_size, #reverse_column_map, #reverse_value_map, suppress_logs, #unmap_ranges, #verify_and_cleanse_hash_values
Constructor Details
#initialize(block_type, data_frame_type, instance, value_map: nil, singular_df_name: '', plural_df_name: '') ⇒ Row
Returns a new instance of Row.
6
7
8
9
|
# File 'lib/active_data_frame/row.rb', line 6
def initialize(block_type, data_frame_type, instance, value_map: nil, singular_df_name: '', plural_df_name: '')
super(block_type, data_frame_type, value_map: value_map, singular_df_name: singular_df_name, plural_df_name: plural_df_name)
self.instance = instance
end
|
Instance Attribute Details
#instance ⇒ Object
Returns the value of attribute instance.
4
5
6
|
# File 'lib/active_data_frame/row.rb', line 4
def instance
@instance
end
|
Class Method Details
.set_all(scope, block_type, data_frame_type, from, values, trim: false) ⇒ Object
15
16
17
18
19
20
21
22
23
24
25
26
|
# File 'lib/active_data_frame/row.rb', line 15
def self.set_all(scope, block_type, data_frame_type, from, values, trim: false)
if trim || ActiveRecord::Base.connection_config[:adapter] === 'mysql2'
case values
when Hash then scope.where(id: values.keys)
.each{|instance| Row.new(block_type, data_frame_type, instance)
.patch(from, values[instance.id]) }
else scope.each{|instance| Row.new(block_type, data_frame_type, instance).patch(from, values) }
end
else
upsert_all(scope, block_type, data_frame_type, from, values)
end
end
|
.upsert_all(rows, block_type, data_frame_type, from, values) ⇒ Object
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
|
# File 'lib/active_data_frame/row.rb', line 28
def self.upsert_all(rows, block_type, data_frame_type, from, values)
length = values.kind_of?(Hash) ? values.values.first.length : values.length
to = from + length - 1
bounds = get_bounds(from, to, block_type)
scope = block_type.where(data_frame_type: data_frame_type.name, data_frame_id: rows.select(:id))
scope = scope.where(data_frame_id: values.keys) if values.kind_of?(Hash)
all_update_indices = scope.where(period_index: bounds.from.index..bounds.to.index).order(data_frame_id: :asc, period_index: :asc).pluck(:data_frame_id, :period_index)
grouped_update_indices = all_update_indices.group_by(&:first).transform_values{|value| Set.new(value.map!(&:last)) }
instance_ids = rows.pluck(:id)
instance_ids &= values.keys if values.kind_of?(Hash)
upserts = to_enum(:iterate_bounds, [bounds], block_type).flat_map do |index, left, right, cursor, size|
instance_ids.map do |instance_id|
slice = values.kind_of?(Hash) ? values[instance_id][cursor...cursor + size] : values[cursor...cursor + size]
[[:data_frame_id, instance_id], [:period_index, index], *(left.succ..right.succ).map{|v| :"t#{v}" }.zip(slice)].to_h
end
end
update, insert = upserts.partition{|upsert| grouped_update_indices[upsert[:data_frame_id]]&.include?(upsert[:period_index]) }
Database.for_types(block: block_type, df: data_frame_type).bulk_upsert(update, insert)
values
end
|
Instance Method Details
#get(ranges) ⇒ Object
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
|
# File 'lib/active_data_frame/row.rb', line 109
def get(ranges)
all_bounds = ranges.map.with_index do |range, index|
get_bounds(range.first, range.exclude_end? ? range.end - 1 : range.end, index)
end
existing = self.class.suppress_logs{
blocks_between(all_bounds).pluck(:period_index, *block_type::COLUMNS).map{|pi, *values| [pi, values]}.to_h
}
result = V.blank(typecode: block_type::TYPECODE, columns: all_bounds.map(&:length).sum)
iterate_bounds(all_bounds) do |index, left, right, cursor, size|
if block = existing[index]
chunk = block[left..right]
result.narray[cursor...cursor + size] = chunk.length == 1 ? chunk.first : chunk
end
end
if column_map && !column_map.default_proc
total = 0
range_sizes = ranges.map do |range, memo|
last_total = total
total += range.size
[range.first, range.size, last_total]
end
index_of = ->(column){
selected = range_sizes.find{|start, size| start <= column && start + size >= column}
if selected
start, _, total = selected
(column - start) + total
else
nil
end
}
result.column_map = column_map.map do |name, column|
[name, index_of[column_map[name]]]
end.to_h
end
result
end
|
#inspect ⇒ Object
11
12
13
|
# File 'lib/active_data_frame/row.rb', line 11
def inspect
"#{data_frame_type.name} Row(#{instance.id})"
end
|
#patch(from, values) ⇒ Object
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
|
# File 'lib/active_data_frame/row.rb', line 74
def patch(from, values)
to = (from + values.length) - 1
bounds = get_bounds(from, to)
new_blocks = Hash.new do |h, k|
h[k] = [[0] * block_type::BLOCK_SIZE, self.instance.id]
end
deleted_indices = []
existing = blocks_between([bounds]).pluck(:data_frame_id, :period_index, *block_type::COLUMNS).map do |id, period_index, *block_values|
[period_index, [block_values, id]]
end.to_h
iterate_bounds([bounds]) do |index, left, right, cursor, size|
chunk = values[cursor...cursor + size]
if existing[index]
block = existing[index]
block.first[left..right] = chunk.to_a
if block.first.all?(&:zero?)
deleted_indices << index
existing.delete(index)
end
elsif chunk.any?(&:nonzero?)
new_blocks[index].first[left..right] = chunk.to_a
end
end
database.bulk_delete(self.instance.id, deleted_indices) unless deleted_indices.size.zero?
database.bulk_update(existing) unless existing.size.zero?
database.bulk_insert(new_blocks) unless new_blocks.size.zero?
values
end
|
#set(from, values, trim: false) ⇒ Object
50
51
52
53
54
55
56
|
# File 'lib/active_data_frame/row.rb', line 50
def set(from, values, trim: false)
if trim || ActiveRecord::Base.connection_config[:adapter] === 'mysql2'
patch(from, values)
else
upsert(from, values)
end
end
|
#upsert(from, values) ⇒ Object
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
|
# File 'lib/active_data_frame/row.rb', line 58
def upsert(from, values)
to = (from + values.length) - 1
bounds = get_bounds(from, to)
update_indices = Set.new(scope.where(period_index: bounds.from.index..bounds.to.index).order(period_index: :asc).pluck(:period_index))
upserts = to_enum(:iterate_bounds, [bounds]).map do |index, left, right, cursor, size|
[[:data_frame_id, self.instance.id], [:period_index, index], *(left.succ..right.succ).map{|v| :"t#{v}" }.zip(values[cursor...cursor + size])].to_h
end
update, insert = upserts.partition{|upsert| update_indices.include?(upsert[:period_index]) }
database.bulk_upsert(update, insert)
values
end
|