Class: Xapian::Indexer::Spider

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

Constructor Details

#initialize(database, generator, controller, options = {}) ⇒ Spider



25
26
27
28
29
30
31
32
33
34
# File 'lib/xapian/indexer/spider.rb', line 25

def initialize(database, generator, controller, options = {})
	@database = database
	@generator = generator
	@controller = controller
	
	@links = []
	@touched = Set.new
	
	@logger = options[:logger] || Logger.new($stdout)
end

Instance Attribute Details

#resourcesObject (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(options = {}, &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 options[:count] && count > options[: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 options[:depth] && depth > options[: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