module Prick module Build class NodePool def schemas() @schemas.keys end def before_schema(s) schemas.take_while { |schema| schema != s } end def after_schema(s) schemas.reverse.take_while { |schema| schema != s }.reverse end attr_reader :nodes # All nodes including nodes deleted later on attr_reader :all_nodes attr_reader :init_nodes attr_reader :decl_nodes attr_reader :seed_nodes attr_reader :term_nodes # attr_reader :setup_nodes # attr_reader :teardown_nodes def fox_seed_nodes() seed_nodes.select { |node| node.kind == :fox } end def sql_seed_nodes() seed_nodes.select { |node| node.kind == :sql } end # List of BuildNode nodes def build_nodes() nodes.select { |node| node.is_a?(BuildNode) } end # List of schemas to ignore when building PgGraph objects. This is used # to exclude foreign schemes that doesn't follow the PgGraph naming # conventions def pg_graph_ignore_schemas() all_nodes.select { |node| node.is_a?(BuildNode) && node.pg_graph_ignore_schema }.map(&:schema).uniq end def refresh_schemas() build_nodes.select(&:refresh_schema).map(&:schema).uniq end def keep_schemas() build_nodes.reject(&:refresh_schema).map(&:schema).uniq end def initialize() self.clear end def add(*nodes) nodes = Array(nodes).flatten @nodes.concat(nodes) @all_nodes.concat(nodes) nodes.each { |node| @schemas[node.schema] += 1 @kind_nodes[node.phase]&.append(node) } self end def delete(*nodes) # puts "#delete(*nodes)" nodes = Array(nodes).flatten nodes.each { |node| delete_node(node) kind_nodes = @kind_nodes[node.phase] and kind_nodes.delete_at(kind_nodes.index(node)) } nodes.last end def delete_if(phase = nil, &block) candidates = @kind_nodes[phase] || @nodes delete(candidates.select { |node| yield(node) }) end def delete_schema(*schemas, exclude: []) schemas = Array(schemas).flatten delete_if { |node| schemas.include?(node.schema) && !exclude.include?(node.phase) && !exclude.include?(node.kind) } end def clear(*phases) phases = Array(phases).flatten if !phases.empty? for phase in phases nodes = @kind_nodes[phase] nodes.each { |node| delete_node(node) } @kind_nodes[phase].clear end else @schemas = Hash.new(0) # map from schema name to number of nodes @nodes = [] @all_nodes = [] @init_nodes = [] @decl_nodes = [] @seed_nodes = [] @term_nodes = [] @kind_nodes = { decl: @decl_nodes, init: @init_nodes, seed: @seed_nodes, term: @term_nodes, yml: nil } end end def dump puts "NodePool, #{nodes.size} nodes" indent { puts "init_nodes:" indent { @init_nodes.each &:dump } puts "decl_nodes:" indent { @decl_nodes.each &:dump } puts "seed_nodes:" indent { @seed_nodes.each &:dump } puts "term_nodes:" indent { @term_nodes.each &:dump } } end private def delete_node(node) # puts "#delete_node" @nodes.delete_at(@nodes.index(node)) @schemas[node.schema] -= 1 @schemas.delete(node.schema) if @schemas[node.schema] == 0 end end end end