Class: Xapian::Indexer::Spider
- Inherits:
-
Object
- Object
- Xapian::Indexer::Spider
- Defined in:
- lib/xapian/indexer/spider.rb
Overview
Represents a process which consumes resources into the database and follows links to related resources
Defined Under Namespace
Classes: Fetch
Instance Attribute Summary collapse
-
#resources ⇒ Object
readonly
Returns the value of attribute resources.
Instance Method Summary collapse
- #add(root) ⇒ Object
-
#initialize(database, generator, controller, options = {}) ⇒ Spider
constructor
database = Xapian::Database.new(ARGV).
- #process(options = {}, &block) ⇒ Object
- #remove_old! ⇒ Object
Constructor Details
#initialize(database, generator, controller, options = {}) ⇒ Spider
database = Xapian::Database.new(ARGV)
25 26 27 28 29 30 31 32 33 34 |
# File 'lib/xapian/indexer/spider.rb', line 25 def initialize(database, generator, controller, = {}) @database = database @generator = generator @controller = controller @links = [] @touched = Set.new @logger = [:logger] || Logger.new($stdout) end |
Instance Attribute Details
#resources ⇒ Object (readonly)
Returns the value of attribute resources.
36 37 38 |
# File 'lib/xapian/indexer/spider.rb', line 36 def resources @resources end |
Instance Method Details
#add(root) ⇒ Object
38 39 40 41 42 43 44 45 46 47 |
# File 'lib/xapian/indexer/spider.rb', line 38 def add(root) case root when String @links << root when Array @links += root else @logger.error "Could not add roots #{root.inspect}!" end end |
#process(options = {}, &block) ⇒ Object
99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 |
# File 'lib/xapian/indexer/spider.rb', line 99 def process( = {}, &block) count = 0 depth = 0 until @links.empty? new_links = [] @links.each do |link| # Mark and sweep - don't review the same resource twice! next if @touched.include?(link) @touched << link # Create a new fetch from the database... fetch = Fetch.new(@database, @controller, link) resource = fetch.current_resource # Does it already exist in the current database (and fresh?) unless fetch.archived_resource && fetch.archived_resource.fresh? # Fetch the resource and add it to the index begin @logger.info "Indexing #{resource.name}..." resource.fetch! rescue @logger.error "Could not fetch resource #{resource.name}: #{$!}!" $!.backtrace.each{|line| @logger.error(line)} end # Did we fetch a resource and was it indexable? if resource.fetched? if resource.content? doc = Xapian::Document.new doc.data = @controller.save(resource) doc.add_term(resource.name_digest) @generator.document = doc @generator.index_text(resource.content) @database.replace_document(resource.name_digest, doc) else @logger.warn "Resource was not indexable #{resource.name}!" end else @logger.warn "Could not fetch resource #{resource.name}!" end else @logger.info "Still fresh #{resource.name}..." end new_links += (fetch.links || []).map(&block).compact count += 1 if [:count] && count > [:count] # If we have to leave before finishing this breadth... @links += new_links return count end end @links = new_links depth += 1 return count if [:depth] && depth > [:depth] end end |
#remove_old! ⇒ Object
165 166 167 168 169 170 171 172 173 174 175 176 177 |
# File 'lib/xapian/indexer/spider.rb', line 165 def remove_old! postlist = @database.postlist("") postlist.each do |post| document = @database.document(post.docid) resource = @controller.recreate(document.data) unless resource.fresh? @logger.info "Removing expired index for #{resource.name}." @database.delete_document(post.docid) end end end |