Class: Gonzui::Indexer
- Inherits:
-
Object
show all
- Includes:
- Util
- Defined in:
- lib/gonzui/indexer.rb
Constant Summary
collapse
- BinaryRegexp =
Regexp.new(pattern)
- @@performance_monitor =
PerformanceMonitor.new
Class Method Summary
collapse
Instance Method Summary
collapse
-
#add_binary_content ⇒ Object
-
#add_content ⇒ Object
-
#add_content_common(format_abbrev, format_name) ⇒ Object
-
#add_content_with_indexing ⇒ Object
-
#add_format(format_abbrev, format_name) ⇒ Object
-
#add_fragment(fragment) ⇒ Object
-
#add_license ⇒ Object
-
#add_package_if_necessary ⇒ Object
-
#add_path ⇒ Object
-
#add_property(abbrev, name, counter, make_key, pkgid_ids) ⇒ Object
-
#add_text(fragment, type_id) ⇒ Object
-
#add_word(word, byteno, type_id) ⇒ Object
-
#binary_content?(content) ⇒ Boolean
-
#convert_to_utf8(content) ⇒ Object
-
#flush_cache ⇒ Object
-
#get_fragments(scanner) ⇒ Object
-
#get_package_name ⇒ Object
-
#index ⇒ Object
-
#index_content(scanner) ⇒ Object
-
#indexable?(scanner) ⇒ Boolean
-
#initialize(config, dbm, source_uri, normalized_path, content, options = {}) ⇒ Indexer
constructor
A new instance of Indexer.
-
#initialize_profilers_if_necessary ⇒ Object
-
#make_content_info ⇒ Object
-
#make_scanner ⇒ Object
-
#normalize_content(content) ⇒ Object
-
#read_content ⇒ Object
Methods included from Util
assert, assert_equal, assert_equal_all, assert_non_nil, assert_not_reached, benchmark, command_exist?, commify, eprintf, format_bytes, program_name, protect_from_signals, require_command, set_verbosity, shell_escape, unix?, vprintf, windows?, wprintf
Constructor Details
#initialize(config, dbm, source_uri, normalized_path, content, options = {}) ⇒ Indexer
Returns a new instance of Indexer.
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
|
# File 'lib/gonzui/indexer.rb', line 41
def initialize(config, dbm, source_uri, normalized_path, content,
options = {})
@config = config
@dbm = dbm
@normalized_path = normalized_path
@source_uri = source_uri
@content = content
@content_hash = Digest::MD5.hexdigest(content.text)
@noindex_formats = (options[:noindex_formats] or @config.noindex_formats)
@package_name = get_package_name
@seqno = 0
@word_cache = {}
@wordinfo_cache = {}
@digest_cache = []
@format_id = nil
@license_id = nil
@license_abbrev = nil
@encoding = nil
@nlines = nil
@package_id = nil
@path_id = nil
@bols = [] @indexed_p = false
initialize_profilers_if_necessary
end
|
Class Method Details
.statistics ⇒ Object
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
|
# File 'lib/gonzui/indexer.rb', line 24
def self.statistics
return "" if @@performance_monitor.empty?
pm = @@performance_monitor
summary = "Performance statistics:\n"
summary << pm.heading
summary << pm.format([Indexer, :index],
[Indexer, :read_content],
[Indexer, :add_license],
[Indexer, :index_content])
labels = LangScan.modules.map {|m|
[m, :scan]
}.push([Indexer, :add_fragment],
[Indexer, :flush_cache])
summary << pm.format([Indexer, :index_content], *labels)
return summary
end
|
Instance Method Details
#add_binary_content ⇒ Object
276
277
278
|
# File 'lib/gonzui/indexer.rb', line 276
def add_binary_content
add_content_common("binary", "Binary")
end
|
#add_content ⇒ Object
300
301
302
303
304
305
306
|
# File 'lib/gonzui/indexer.rb', line 300
def add_content
if @encoding == "binary"
add_binary_content
else
add_content_with_indexing
end
end
|
#add_content_common(format_abbrev, format_name) ⇒ Object
265
266
267
268
269
270
271
272
273
274
|
# File 'lib/gonzui/indexer.rb', line 265
def add_content_common(format_abbrev, format_name)
add_format(format_abbrev, format_name)
add_license
@dbm.pathid_pkgid[@path_id] = @package_id
@dbm.pathid_content[@path_id] = @content.text
@dbm.pathid_info[@path_id] = make_content_info
@dbm.pathid_hash[@path_id] = @content_hash
vprintf("added (%s): %s (%s)", format_abbrev,
@normalized_path, @license_abbrev)
end
|
#add_content_with_indexing ⇒ Object
290
291
292
293
294
295
296
297
298
|
# File 'lib/gonzui/indexer.rb', line 290
def add_content_with_indexing
scanner = make_scanner
if indexable?(scanner)
index_content(scanner)
else
vprintf("skip indexing: %s", @normalized_path)
end
add_content_common(scanner.abbrev, scanner.name)
end
|
184
185
186
187
188
189
190
|
# File 'lib/gonzui/indexer.rb', line 184
def add_format(format_abbrev, format_name)
@format_id = add_property(format_abbrev,
format_name,
:format_id_counter,
:make_ncontents_by_format_key,
:pkgid_fmtids)
end
|
#add_fragment(fragment) ⇒ Object
150
151
152
153
154
155
156
157
158
159
|
# File 'lib/gonzui/indexer.rb', line 150
def add_fragment(fragment)
type_id = @dbm.get_type_id(fragment.type)
if LangScan::Type.splittable?(fragment.type)
add_text(fragment, type_id)
else
add_word(fragment.text, fragment.byteno, type_id)
end
@digest_cache.push(fragment.byteno, fragment.text.length, type_id)
end
|
#add_license ⇒ Object
192
193
194
195
196
197
198
199
200
201
|
# File 'lib/gonzui/indexer.rb', line 192
def add_license
detector = LicenseDetector.new(@content.text)
license = detector.detect
@license_id = add_property(license.abbrev,
license.name,
:license_id_counter,
:make_ncontents_by_license_key,
:pkgid_lcsids)
@license_abbrev = license.abbrev
end
|
#add_package_if_necessary ⇒ Object
228
229
230
231
232
233
234
235
236
237
238
|
# File 'lib/gonzui/indexer.rb', line 228
def add_package_if_necessary
if @dbm.has_package?(@package_name)
@package_id = @dbm.get_package_id(@package_name)
else
@package_id = @dbm.package_id_counter.make_new_id
@dbm.pkg_pkgid[@package_name] = @package_id
@dbm.pkgid_pkg[@package_id] = @package_name
@dbm.pkgid_src[@package_id] = @source_uri.to_s
@dbm.put_package_options(@package_id)
end
end
|
#add_path ⇒ Object
203
204
205
206
207
208
209
|
# File 'lib/gonzui/indexer.rb', line 203
def add_path
assert_equal(false, @dbm.path_pathid.include?(@normalized_path))
@path_id = @dbm.path_id_counter.make_new_id
@dbm.path_pathid[@normalized_path] = @path_id
@dbm.pathid_path[@path_id] = @normalized_path
@dbm.pkgid_pathids[@package_id] = @path_id
end
|
#add_property(abbrev, name, counter, make_key, pkgid_ids) ⇒ Object
177
178
179
180
181
182
|
# File 'lib/gonzui/indexer.rb', line 177
def add_property(abbrev, name, counter, make_key, pkgid_ids)
id = @dbm.send(counter).get_id2(abbrev, name)
@dbm.send(pkgid_ids)[@package_id] = id
@dbm.increase_counter(@dbm.send(make_key, abbrev))
return id
end
|
#add_text(fragment, type_id) ⇒ Object
142
143
144
145
146
147
148
|
# File 'lib/gonzui/indexer.rb', line 142
def add_text(fragment, type_id)
text = fragment.text
byteno = fragment.byteno
TextTokenizer.each_word(text) {|word, pos|
add_word(word, byteno + pos, type_id)
}
end
|
#add_word(word, byteno, type_id) ⇒ Object
221
222
223
224
225
226
|
# File 'lib/gonzui/indexer.rb', line 221
def add_word(word, byteno, type_id)
word_id = @dbm.word_id_counter.get_id(word)
array = (@wordinfo_cache[word_id] ||= [])
array.push(@seqno, byteno, type_id)
@seqno += 1
end
|
#binary_content?(content) ⇒ Boolean
105
106
107
|
# File 'lib/gonzui/indexer.rb', line 105
def binary_content?(content)
BinaryRegexp.match(content)
end
|
#convert_to_utf8(content) ⇒ Object
109
110
111
112
113
114
115
116
117
118
119
|
# File 'lib/gonzui/indexer.rb', line 109
def convert_to_utf8(content)
encoding = "ascii"
if binary_content?(content)
encoding = "binary"
else
if @config.utf8
content, encoding = UTF8.to_utf8(content)
end
end
return content, encoding
end
|
#flush_cache ⇒ Object
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
|
# File 'lib/gonzui/indexer.rb', line 161
def flush_cache
all_word_ids = @wordinfo_cache.keys.sort!
all_word_ids.each {|word_id|
path_word_id = AutoPack.pack_id2(@path_id, word_id)
@dbm.pathwordid_info[path_word_id] =
DeltaDumper.dump_tuples(WordInfo, @wordinfo_cache[word_id])
}
@dbm.put_pathid_wordids(@package_id, @path_id, all_word_ids)
@dbm.pathid_wordids[@path_id] = DeltaDumper.dump_ids(all_word_ids)
@dbm.pathid_digest[@path_id] =
DeltaDumper.dump_tuples(DigestInfo, @digest_cache)
@dbm.pathid_bols[@path_id] = DeltaDumper.dump_fixnums(@bols)
@wordinfo_cache.clear
@dbm.word_id_counter.flush
end
|
#get_fragments(scanner) ⇒ Object
211
212
213
214
215
216
217
218
219
|
# File 'lib/gonzui/indexer.rb', line 211
def get_fragments(scanner)
@@performance_monitor.profile(scanner, :scan) if @config.verbose
fragments = []
scanner.scan(@content.text) {|fragment|
fragments.push(fragment) if LangScan::Type.include?(fragment.type)
}
fragments = fragments.sort_by {|fragment| fragment.byteno }
return fragments
end
|
#get_package_name ⇒ Object
130
131
132
133
134
135
136
137
138
139
140
|
# File 'lib/gonzui/indexer.rb', line 130
def get_package_name
parts = @normalized_path.split("/")
if parts.length < 2
raise IndexerError.new("normalized path should not be flat")
end
package_name = parts.first
if package_name.size == 0 || package_name == "." || package_name == ".."
package_name = File.basename(@source_uri.path)
end
return package_name
end
|
#index ⇒ Object
309
310
311
312
313
314
|
# File 'lib/gonzui/indexer.rb', line 309
def index
read_content
add_package_if_necessary
add_path
add_content
end
|
#index_content(scanner) ⇒ Object
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
|
# File 'lib/gonzui/indexer.rb', line 246
def index_content(scanner)
fragments = []
begin
fragments = get_fragments(scanner)
rescue
unless scanner == LangScan::Text
vprintf("#{@normalized_path}: fallback to LangScan::Text")
scanner = LangScan::Text
retry
end
end
fragments.each {|fragment| add_fragment(fragment) }
flush_cache
@dbm.increase_counter(:ncontents_indexed)
@dbm.increase_counter(:nlines_indexed, @nlines)
@indexed_p = true
end
|
#indexable?(scanner) ⇒ Boolean
286
287
288
|
# File 'lib/gonzui/indexer.rb', line 286
def indexable?(scanner)
not @noindex_formats.include?(scanner.abbrev)
end
|
#initialize_profilers_if_necessary ⇒ Object
72
73
74
75
76
77
78
79
80
81
82
|
# File 'lib/gonzui/indexer.rb', line 72
def initialize_profilers_if_necessary
if @config.verbose
@@performance_monitor.profile(Indexer, :index)
@@performance_monitor.profile(Indexer, :read_content)
@@performance_monitor.profile(Indexer, :index_content)
@@performance_monitor.profile(Indexer, :add_fragment)
@@performance_monitor.profile(Indexer, :add_license)
@@performance_monitor.profile(Indexer, :flush_cache)
end
end
|
#make_content_info ⇒ Object
240
241
242
243
244
|
# File 'lib/gonzui/indexer.rb', line 240
def make_content_info
ContentInfo.dump(@content.length, @content.mtime.to_i,
Time.now.to_i, @format_id, @license_id,
@nlines, @indexed_p)
end
|
#make_scanner ⇒ Object
280
281
282
283
284
|
# File 'lib/gonzui/indexer.rb', line 280
def make_scanner
scanner = LangScan.choose(@normalized_path, @content.text)
scanner = LangScan::Text if scanner.nil?
return scanner
end
|
#normalize_content(content) ⇒ Object
121
122
123
124
125
126
127
128
|
# File 'lib/gonzui/indexer.rb', line 121
def normalize_content(content)
content, encoding = convert_to_utf8(content)
unless encoding == "binary"
content = content.untabify
content.gsub!(/\r\n?/, "\n")
end
return content, encoding
end
|
#read_content ⇒ Object
84
85
86
87
88
89
90
91
92
93
94
|
# File 'lib/gonzui/indexer.rb', line 84
def read_content
content, @encoding = normalize_content(@content.text)
@content.text = content
@nlines = 0
pos = 0
@content.text.each_line {|line|
@bols.push(pos)
@nlines += 1
pos += line.length
}
end
|