Class: ActiveRecord::Materialized::SummaryDelta Private

Inherits:
Object
  • Object
show all
Defined in:
lib/activerecord/materialized/summary_delta.rb

Overview

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

Accumulates signed per-partition, per-column numeric changes for a delta-maintainable view (partition key tuple => mv column => amount).

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(buckets = {}) ⇒ SummaryDelta

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns a new instance of SummaryDelta.



12
13
14
# File 'lib/activerecord/materialized/summary_delta.rb', line 12

def initialize(buckets = {})
  @buckets = buckets
end

Instance Attribute Details

#bucketsObject (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



10
11
12
# File 'lib/activerecord/materialized/summary_delta.rb', line 10

def buckets
  @buckets
end

Class Method Details

.deserialize(payload) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



50
51
52
53
54
55
56
57
# File 'lib/activerecord/materialized/summary_delta.rb', line 50

def self.deserialize(payload)
  rows = payload && payload["summary"]
  return nil if rows.nil?

  buckets = {}
  rows.each { |row| buckets[row.fetch("key")] = row.fetch("columns") }
  new(buckets)
end

Instance Method Details

#add(key_tuple, column, amount) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



16
17
18
19
# File 'lib/activerecord/materialized/summary_delta.rb', line 16

def add(key_tuple, column, amount)
  bucket = (@buckets[key_tuple] ||= {})
  bucket[column] = (bucket[column] || 0) + amount
end

#empty?Boolean

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns:

  • (Boolean)


21
22
23
# File 'lib/activerecord/materialized/summary_delta.rb', line 21

def empty?
  @buckets.empty?
end

#merge(other) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



37
38
39
40
41
42
43
# File 'lib/activerecord/materialized/summary_delta.rb', line 37

def merge(other)
  merged = SummaryDelta.new(@buckets.transform_values(&:dup))
  other.buckets.each do |key_tuple, columns|
    columns.each { |column, amount| merged.add(key_tuple, column, amount) }
  end
  merged
end

#prune!Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Drops net-zero columns (no-op changes) and the partitions left empty.



31
32
33
34
35
# File 'lib/activerecord/materialized/summary_delta.rb', line 31

def prune!
  @buckets.each_value { |columns| columns.reject! { |_column, amount| amount.zero? } }
  @buckets.reject! { |_key_tuple, columns| columns.empty? }
  self
end

#serializeObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

A list of "key" => tuple, "columns" => … entries so array keys survive JSON.



46
47
48
# File 'lib/activerecord/materialized/summary_delta.rb', line 46

def serialize
  { "summary" => @buckets.map { |key_tuple, columns| { "key" => key_tuple, "columns" => columns } } }
end

#tracked_partition_countObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Number of distinct partitions (buckets) accumulated so far.



26
27
28
# File 'lib/activerecord/materialized/summary_delta.rb', line 26

def tracked_partition_count
  @buckets.size
end