Class: RedisMemo::MemoizeQuery::CachedSelect::BindParams::Plan

Inherits:
Object
  • Object
show all
Defined in:
lib/redis_memo/memoize_query/cached_select/bind_params.rb

Overview

Prior to actually extracting the bind parameters, we first quickly estimate if it makes sense to do so. If a query contains too many dependencies, or contains dependencies that have not been memoized, then the query itself cannot be cached correctly/efficiently, so there’s no point to actually extract.

The planning phase is similar to the extraction phase. Though in the planning phase, we can ignore all the actual attribute values and only look at the attribute names. This way, we can precompute the dependency size without populating their actual values.

For example, in the planning phase,

{a:nil} x {b: nil} => {a: nil, b: nil}
{a:nil, b:nil} x {a: nil: b: nil} => {a: nil, b: nil}

and in the extraction phase, that’s where the # of dependency can actually grow significantly:

{a: [1,2,3]} x {b: [1,2,3]} => [{a: 1, b: 1}, ....]
{a:[1,2], b:[1,2]} x {a: [1,2,3]: b: [1,2,3]} => [{a: 1, b: 1}, ...]

Defined Under Namespace

Classes: DependencySizeEstimation

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(bind_params) ⇒ Plan

Returns a new instance of Plan.



239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
# File 'lib/redis_memo/memoize_query/cached_select/bind_params.rb', line 239

def initialize(bind_params)
  @dependency_size_estimation = DependencySizeEstimation.new
  @model_attrs = Hash.new do |models, model|
    models[model] = Set.new
  end

  # An aggregated bind_params node can only obtain params by combining
  # its children nodes
  return if !bind_params.__send__(:operator).nil?

  bind_params.params.each do |model, attrs_set|
    @dependency_size_estimation[model] += attrs_set.size
    attrs_set.each do |attrs|
      # [k, nil]: Ignore the attr value and keep the name only
      @model_attrs[model] << attrs.keys.map { |k| [k, nil] }.to_h
    end
  end
end

Instance Attribute Details

#dependency_size_estimationObject

Returns the value of attribute dependency_size_estimation.



236
237
238
# File 'lib/redis_memo/memoize_query/cached_select/bind_params.rb', line 236

def dependency_size_estimation
  @dependency_size_estimation
end

#model_attrsObject

Returns the value of attribute model_attrs.



237
238
239
# File 'lib/redis_memo/memoize_query/cached_select/bind_params.rb', line 237

def model_attrs
  @model_attrs
end