~ubuntu-branches/ubuntu/karmic/chef/karmic

« back to all changes in this revision

Viewing changes to chef/lib/chef/search.rb

  • Committer: Bazaar Package Importer
  • Author(s): Joshua Timberman
  • Date: 2009-08-12 12:18:48 UTC
  • Revision ID: james.westby@ubuntu.com-20090812121848-yl38hpd7nfsl51xz
Tags: upstream-0.7.8
ImportĀ upstreamĀ versionĀ 0.7.8

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#
 
2
# Author:: Adam Jacob (<adam@opscode.com>)
 
3
# Copyright:: Copyright (c) 2008 Opscode, Inc.
 
4
# License:: Apache License, Version 2.0
 
5
#
 
6
# Licensed under the Apache License, Version 2.0 (the "License");
 
7
# you may not use this file except in compliance with the License.
 
8
# You may obtain a copy of the License at
 
9
 
10
#     http://www.apache.org/licenses/LICENSE-2.0
 
11
 
12
# Unless required by applicable law or agreed to in writing, software
 
13
# distributed under the License is distributed on an "AS IS" BASIS,
 
14
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 
15
# See the License for the specific language governing permissions and
 
16
# limitations under the License.
 
17
#
 
18
 
 
19
require 'chef/search/result'
 
20
require 'ferret'
 
21
 
 
22
class Chef
 
23
  class Search
 
24
    
 
25
    attr_reader :index
 
26
    
 
27
    def initialize
 
28
      @index = Ferret::Index::Index.new(:path => Chef::Config[:search_index_path])
 
29
    end
 
30
    
 
31
    def search(type, query="*", attributes=[], &block)
 
32
      search_query = build_search_query(type, query)
 
33
      start_time = Time.now
 
34
      results = []
 
35
      block ||= lambda { |b| b }
 
36
      
 
37
      @index.search_each(search_query, :limit => :all) do |id, score|
 
38
        results << block.call(build_hash(@index.doc(id)))
 
39
      end
 
40
      
 
41
      Chef::Log.debug("Search #{search_query} complete in #{Time.now - start_time} seconds")
 
42
      
 
43
      attributes.empty? ? results : filter_by_attributes(results,attributes)
 
44
    end
 
45
    
 
46
    def filter_by_attributes(results, attributes)
 
47
      results.collect do |r|
 
48
        nr = Hash.new
 
49
        nr[:index_name] = r[:index_name]
 
50
        nr[:id] = r[:id]
 
51
        attributes.each do |attrib|
 
52
          if r.has_key?(attrib)
 
53
            nr[attrib] = r[attrib]
 
54
          end
 
55
        end
 
56
        nr
 
57
      end
 
58
    end
 
59
    
 
60
    private :filter_by_attributes
 
61
    
 
62
    def list_indexes
 
63
      indexes = Hash.new
 
64
      @index.search_each("index_name:*", :limit => :all) do |id, score|
 
65
        indexes[@index.doc(id)["index_name"]] = true
 
66
      end
 
67
      indexes.keys
 
68
    end
 
69
    
 
70
    def has_index?(index)
 
71
      list_indexes.detect { |i| i == index }
 
72
    end
 
73
    
 
74
    private
 
75
      def build_search_query(type, query)
 
76
        query = "id:*" if query == '*'
 
77
        "index_name:#{type} AND (#{query})"
 
78
      end
 
79
    
 
80
      def build_hash(doc)
 
81
        result = Chef::Search::Result.new
 
82
        doc.fields.each do |f|
 
83
          result[f] = doc[f]
 
84
        end
 
85
        result
 
86
      end
 
87
  end
 
88
end