Class: DagMe::Adapters::PostgresqlClosure

Inherits:
Base
  • Object
show all
Defined in:
lib/dag_me/adapters/postgresql_closure.rb

Overview

Answers reachability from the trigger-maintained closure table. Every query is a plain index join; no recursion at read time.

Instance Attribute Summary

Attributes inherited from Base

#config

Instance Method Summary collapse

Methods inherited from Base

#between, #initialize, #model

Constructor Details

This class inherits a constructor from DagMe::Adapters::Base

Instance Method Details

#ancestors(node) ⇒ Object



8
9
10
# File 'lib/dag_me/adapters/postgresql_closure.rb', line 8

def ancestors(node)
  self_and_ancestors(node).where.not(node_key(node))
end

#apply_topological_order(relation) ⇒ Object



35
36
37
38
39
40
41
42
# File 'lib/dag_me/adapters/postgresql_closure.rb', line 35

def apply_topological_order(relation)
  tp = paths.arel_table
  count = tp.project(Arel.star.count).where(
    config.paths_descendant_columns.zip(pk_columns)
          .map { |dc, pk| tp[dc].eq(model.arel_table[pk]) }.inject(:and)
  )
  ordered_by(relation, count)
end

#descendants(node) ⇒ Object



12
13
14
# File 'lib/dag_me/adapters/postgresql_closure.rb', line 12

def descendants(node)
  self_and_descendants(node).where.not(node_key(node))
end

#reachable?(ancestor, descendant) ⇒ Boolean

Returns:

  • (Boolean)


26
27
28
29
30
31
32
33
# File 'lib/dag_me/adapters/postgresql_closure.rb', line 26

def reachable?(ancestor, descendant)
  return false if same_node?(ancestor, descendant)

  key = config.paths_ancestor_columns.zip(node_values(ancestor))
              .concat(config.paths_descendant_columns.zip(node_values(descendant)))
              .to_h
  paths.exists?(key)
end

#self_and_ancestors(node) ⇒ Object



16
17
18
19
# File 'lib/dag_me/adapters/postgresql_closure.rb', line 16

def self_and_ancestors(node)
  reach(node, select_cols: config.paths_ancestor_columns,
              match_cols: config.paths_descendant_columns)
end

#self_and_descendants(node) ⇒ Object



21
22
23
24
# File 'lib/dag_me/adapters/postgresql_closure.rb', line 21

def self_and_descendants(node)
  reach(node, select_cols: config.paths_descendant_columns,
              match_cols: config.paths_ancestor_columns)
end