diff options
| author | Nick Lewis <nick@puppetlabs.com> | 2011-03-11 15:22:23 -0800 |
|---|---|---|
| committer | Nick Lewis <nick@puppetlabs.com> | 2011-03-11 16:25:22 -0800 |
| commit | 8858e40839bd693420ddc791df6b51de79356d2a (patch) | |
| tree | 44f73bbd2f5c90665e545361010a79e188c662f6 /lib/puppet/indirector/facts | |
| parent | 8cfc8f195481bbca7c38a415ef8ba11bd20503a6 (diff) | |
| download | puppet-8858e40839bd693420ddc791df6b51de79356d2a.tar.gz puppet-8858e40839bd693420ddc791df6b51de79356d2a.tar.xz puppet-8858e40839bd693420ddc791df6b51de79356d2a.zip | |
(#6689) Make inventory_active_record terminus search quickly
This terminus behaves the same on all supported DB platforms, by performing a
limited portion of its query in SQL, and the rest of the comparison in Ruby.
Its results are consistent with the YAML terminus.
Paired-With: Jesse Wolfe
Diffstat (limited to 'lib/puppet/indirector/facts')
| -rw-r--r-- | lib/puppet/indirector/facts/inventory_active_record.rb | 14 |
1 files changed, 10 insertions, 4 deletions
diff --git a/lib/puppet/indirector/facts/inventory_active_record.rb b/lib/puppet/indirector/facts/inventory_active_record.rb index 89edaf332..5d130eae2 100644 --- a/lib/puppet/indirector/facts/inventory_active_record.rb +++ b/lib/puppet/indirector/facts/inventory_active_record.rb @@ -1,8 +1,10 @@ +require 'puppet/rails' require 'puppet/rails/inventory_node' require 'puppet/rails/inventory_fact' require 'puppet/indirector/active_record' class Puppet::Node::Facts::InventoryActiveRecord < Puppet::Indirector::ActiveRecord + include Puppet::Util def find(request) node = Puppet::Rails::InventoryNode.find_by_name(request.key) return nil unless node @@ -17,7 +19,7 @@ class Puppet::Node::Facts::InventoryActiveRecord < Puppet::Indirector::ActiveRec node.timestamp = facts.timestamp ActiveRecord::Base.transaction do - Puppet::Rails::InventoryFact.delete_all(:inventory_node_id => node.id) + Puppet::Rails::InventoryFact.delete_all(:node_id => node.id) # We don't want to save internal values as facts, because those are # metadata that belong on the node facts.values.each do |name,value| @@ -30,6 +32,7 @@ class Puppet::Node::Facts::InventoryActiveRecord < Puppet::Indirector::ActiveRec def search(request) return [] unless request.options + matching_nodes = [] fact_names = [] fact_filters = Hash.new {|h,k| h[k] = []} meta_filters = Hash.new {|h,k| h[k] = []} @@ -66,8 +69,11 @@ class Puppet::Node::Facts::InventoryActiveRecord < Puppet::Indirector::ActiveRec 'le' => '<=' }.each do |operator_name,operator| fact_filters[operator_name].each do |name,value| - nodes_with_fact = Puppet::Rails::InventoryNode.has_fact(name) - node_sets << nodes_with_fact.select {|h| h.value_for(name).to_f.send(operator, value.to_f)}.map {|node| node.name} + facts = Puppet::Rails::InventoryFact.find_by_sql(["SELECT inventory_facts.value, inventory_nodes.name AS node_name + FROM inventory_facts INNER JOIN inventory_nodes + ON inventory_facts.node_id = inventory_nodes.id + WHERE inventory_facts.name = ?", name]) + node_sets << facts.select {|fact| fact.value.to_f.send(operator, value.to_f)}.map {|fact| fact.node_name} end end node_sets @@ -84,7 +90,7 @@ class Puppet::Node::Facts::InventoryActiveRecord < Puppet::Indirector::ActiveRec 'le' => '<=' }.each do |operator_name,operator| meta_filters[operator_name].each do |name,value| - node_sets << Puppet::Rails::InventoryNode.find(:all, :conditions => ["timestamp #{operator} ?", value]).map {|node| node.name} + node_sets << Puppet::Rails::InventoryNode.find(:all, :select => "name", :conditions => ["timestamp #{operator} ?", value]).map {|node| node.name} end end node_sets |
