Class: ActiveRecord::Materialized::SummaryDelta Private

Inherits:
Object
  • Object
show all
Extended by:
T::Sig
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`).

Constant Summary collapse

KeyTuple =

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

T.type_alias { T::Array[T.untyped] }
Columns =

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

T.type_alias { T::Hash[String, Numeric] }
Buckets =

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

T.type_alias { T::Hash[KeyTuple, Columns] }

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.



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

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.



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

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.



66
67
68
69
70
71
72
73
# File 'lib/activerecord/materialized/summary_delta.rb', line 66

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

  buckets = T.let({}, 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.



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

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)


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

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.



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

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.



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

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.



61
62
63
# File 'lib/activerecord/materialized/summary_delta.rb', line 61

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.



38
39
40
# File 'lib/activerecord/materialized/summary_delta.rb', line 38

def tracked_partition_count
  @buckets.size
end