source: trunk/tools/assistant/lib/qhelpsearchindexwriter_clucene.cpp@ 987

Last change on this file since 987 was 846, checked in by Dmitry A. Kuminov, 14 years ago

trunk: Merged in qt 4.7.2 sources from branches/vendor/nokia/qt.

File size: 26.7 KB
Line 
1/****************************************************************************
2**
3** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
4** All rights reserved.
5** Contact: Nokia Corporation ([email protected])
6**
7** This file is part of the Qt Assistant of the Qt Toolkit.
8**
9** $QT_BEGIN_LICENSE:LGPL$
10** Commercial Usage
11** Licensees holding valid Qt Commercial licenses may use this file in
12** accordance with the Qt Commercial License Agreement provided with the
13** Software or, alternatively, in accordance with the terms contained in
14** a written agreement between you and Nokia.
15**
16** GNU Lesser General Public License Usage
17** Alternatively, this file may be used under the terms of the GNU Lesser
18** General Public License version 2.1 as published by the Free Software
19** Foundation and appearing in the file LICENSE.LGPL included in the
20** packaging of this file. Please review the following information to
21** ensure the GNU Lesser General Public License version 2.1 requirements
22** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
23**
24** In addition, as a special exception, Nokia gives you certain additional
25** rights. These rights are described in the Nokia Qt LGPL Exception
26** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
27**
28** GNU General Public License Usage
29** Alternatively, this file may be used under the terms of the GNU
30** General Public License version 3.0 as published by the Free Software
31** Foundation and appearing in the file LICENSE.GPL included in the
32** packaging of this file. Please review the following information to
33** ensure the GNU General Public License version 3.0 requirements will be
34** met: http://www.gnu.org/copyleft/gpl.html.
35**
36** If you have questions regarding the use of this file, please contact
37** Nokia at [email protected].
38** $QT_END_LICENSE$
39**
40****************************************************************************/
41
42#include "qclucenefieldnames_p.h"
43#include "qhelpenginecore.h"
44#include "qhelp_global.h"
45#include "fulltextsearch/qhits_p.h"
46#include "fulltextsearch/qquery_p.h"
47#include "fulltextsearch/qanalyzer_p.h"
48#include "fulltextsearch/qdocument_p.h"
49#include "fulltextsearch/qsearchable_p.h"
50#include "fulltextsearch/qindexreader_p.h"
51#include "fulltextsearch/qindexwriter_p.h"
52#include "qhelpsearchindexwriter_clucene_p.h"
53
54#include <QtCore/QDir>
55#include <QtCore/QString>
56#include <QtCore/QFileInfo>
57#include <QtCore/QTextCodec>
58#include <QtCore/QTextStream>
59
60#include <QtNetwork/QLocalSocket>
61#include <QtNetwork/QLocalServer>
62
63#include "private/qfunctions_p.h"
64
65QT_BEGIN_NAMESPACE
66
67namespace fulltextsearch {
68namespace clucene {
69
70// taken from qtexthtmlparser
71static const struct QTextHtmlEntity
72{
73 const char *name;
74 quint16 code;
75} entities[] = {
76 { "AElig", 0x00c6 },
77 { "AMP", 38 },
78 { "Aacute", 0x00c1 },
79 { "Acirc", 0x00c2 },
80 { "Agrave", 0x00c0 },
81 { "Alpha", 0x0391 },
82 { "Aring", 0x00c5 },
83 { "Atilde", 0x00c3 },
84 { "Auml", 0x00c4 },
85 { "Beta", 0x0392 },
86 { "Ccedil", 0x00c7 },
87 { "Chi", 0x03a7 },
88 { "Dagger", 0x2021 },
89 { "Delta", 0x0394 },
90 { "ETH", 0x00d0 },
91 { "Eacute", 0x00c9 },
92 { "Ecirc", 0x00ca },
93 { "Egrave", 0x00c8 },
94 { "Epsilon", 0x0395 },
95 { "Eta", 0x0397 },
96 { "Euml", 0x00cb },
97 { "GT", 62 },
98 { "Gamma", 0x0393 },
99 { "Iacute", 0x00cd },
100 { "Icirc", 0x00ce },
101 { "Igrave", 0x00cc },
102 { "Iota", 0x0399 },
103 { "Iuml", 0x00cf },
104 { "Kappa", 0x039a },
105 { "LT", 60 },
106 { "Lambda", 0x039b },
107 { "Mu", 0x039c },
108 { "Ntilde", 0x00d1 },
109 { "Nu", 0x039d },
110 { "OElig", 0x0152 },
111 { "Oacute", 0x00d3 },
112 { "Ocirc", 0x00d4 },
113 { "Ograve", 0x00d2 },
114 { "Omega", 0x03a9 },
115 { "Omicron", 0x039f },
116 { "Oslash", 0x00d8 },
117 { "Otilde", 0x00d5 },
118 { "Ouml", 0x00d6 },
119 { "Phi", 0x03a6 },
120 { "Pi", 0x03a0 },
121 { "Prime", 0x2033 },
122 { "Psi", 0x03a8 },
123 { "QUOT", 34 },
124 { "Rho", 0x03a1 },
125 { "Scaron", 0x0160 },
126 { "Sigma", 0x03a3 },
127 { "THORN", 0x00de },
128 { "Tau", 0x03a4 },
129 { "Theta", 0x0398 },
130 { "Uacute", 0x00da },
131 { "Ucirc", 0x00db },
132 { "Ugrave", 0x00d9 },
133 { "Upsilon", 0x03a5 },
134 { "Uuml", 0x00dc },
135 { "Xi", 0x039e },
136 { "Yacute", 0x00dd },
137 { "Yuml", 0x0178 },
138 { "Zeta", 0x0396 },
139 { "aacute", 0x00e1 },
140 { "acirc", 0x00e2 },
141 { "acute", 0x00b4 },
142 { "aelig", 0x00e6 },
143 { "agrave", 0x00e0 },
144 { "alefsym", 0x2135 },
145 { "alpha", 0x03b1 },
146 { "amp", 38 },
147 { "and", 0x22a5 },
148 { "ang", 0x2220 },
149 { "apos", 0x0027 },
150 { "aring", 0x00e5 },
151 { "asymp", 0x2248 },
152 { "atilde", 0x00e3 },
153 { "auml", 0x00e4 },
154 { "bdquo", 0x201e },
155 { "beta", 0x03b2 },
156 { "brvbar", 0x00a6 },
157 { "bull", 0x2022 },
158 { "cap", 0x2229 },
159 { "ccedil", 0x00e7 },
160 { "cedil", 0x00b8 },
161 { "cent", 0x00a2 },
162 { "chi", 0x03c7 },
163 { "circ", 0x02c6 },
164 { "clubs", 0x2663 },
165 { "cong", 0x2245 },
166 { "copy", 0x00a9 },
167 { "crarr", 0x21b5 },
168 { "cup", 0x222a },
169 { "curren", 0x00a4 },
170 { "dArr", 0x21d3 },
171 { "dagger", 0x2020 },
172 { "darr", 0x2193 },
173 { "deg", 0x00b0 },
174 { "delta", 0x03b4 },
175 { "diams", 0x2666 },
176 { "divide", 0x00f7 },
177 { "eacute", 0x00e9 },
178 { "ecirc", 0x00ea },
179 { "egrave", 0x00e8 },
180 { "empty", 0x2205 },
181 { "emsp", 0x2003 },
182 { "ensp", 0x2002 },
183 { "epsilon", 0x03b5 },
184 { "equiv", 0x2261 },
185 { "eta", 0x03b7 },
186 { "eth", 0x00f0 },
187 { "euml", 0x00eb },
188 { "euro", 0x20ac },
189 { "exist", 0x2203 },
190 { "fnof", 0x0192 },
191 { "forall", 0x2200 },
192 { "frac12", 0x00bd },
193 { "frac14", 0x00bc },
194 { "frac34", 0x00be },
195 { "frasl", 0x2044 },
196 { "gamma", 0x03b3 },
197 { "ge", 0x2265 },
198 { "gt", 62 },
199 { "hArr", 0x21d4 },
200 { "harr", 0x2194 },
201 { "hearts", 0x2665 },
202 { "hellip", 0x2026 },
203 { "iacute", 0x00ed },
204 { "icirc", 0x00ee },
205 { "iexcl", 0x00a1 },
206 { "igrave", 0x00ec },
207 { "image", 0x2111 },
208 { "infin", 0x221e },
209 { "int", 0x222b },
210 { "iota", 0x03b9 },
211 { "iquest", 0x00bf },
212 { "isin", 0x2208 },
213 { "iuml", 0x00ef },
214 { "kappa", 0x03ba },
215 { "lArr", 0x21d0 },
216 { "lambda", 0x03bb },
217 { "lang", 0x2329 },
218 { "laquo", 0x00ab },
219 { "larr", 0x2190 },
220 { "lceil", 0x2308 },
221 { "ldquo", 0x201c },
222 { "le", 0x2264 },
223 { "lfloor", 0x230a },
224 { "lowast", 0x2217 },
225 { "loz", 0x25ca },
226 { "lrm", 0x200e },
227 { "lsaquo", 0x2039 },
228 { "lsquo", 0x2018 },
229 { "lt", 60 },
230 { "macr", 0x00af },
231 { "mdash", 0x2014 },
232 { "micro", 0x00b5 },
233 { "middot", 0x00b7 },
234 { "minus", 0x2212 },
235 { "mu", 0x03bc },
236 { "nabla", 0x2207 },
237 { "nbsp", 0x00a0 },
238 { "ndash", 0x2013 },
239 { "ne", 0x2260 },
240 { "ni", 0x220b },
241 { "not", 0x00ac },
242 { "notin", 0x2209 },
243 { "nsub", 0x2284 },
244 { "ntilde", 0x00f1 },
245 { "nu", 0x03bd },
246 { "oacute", 0x00f3 },
247 { "ocirc", 0x00f4 },
248 { "oelig", 0x0153 },
249 { "ograve", 0x00f2 },
250 { "oline", 0x203e },
251 { "omega", 0x03c9 },
252 { "omicron", 0x03bf },
253 { "oplus", 0x2295 },
254 { "or", 0x22a6 },
255 { "ordf", 0x00aa },
256 { "ordm", 0x00ba },
257 { "oslash", 0x00f8 },
258 { "otilde", 0x00f5 },
259 { "otimes", 0x2297 },
260 { "ouml", 0x00f6 },
261 { "para", 0x00b6 },
262 { "part", 0x2202 },
263 { "percnt", 0x0025 },
264 { "permil", 0x2030 },
265 { "perp", 0x22a5 },
266 { "phi", 0x03c6 },
267 { "pi", 0x03c0 },
268 { "piv", 0x03d6 },
269 { "plusmn", 0x00b1 },
270 { "pound", 0x00a3 },
271 { "prime", 0x2032 },
272 { "prod", 0x220f },
273 { "prop", 0x221d },
274 { "psi", 0x03c8 },
275 { "quot", 34 },
276 { "rArr", 0x21d2 },
277 { "radic", 0x221a },
278 { "rang", 0x232a },
279 { "raquo", 0x00bb },
280 { "rarr", 0x2192 },
281 { "rceil", 0x2309 },
282 { "rdquo", 0x201d },
283 { "real", 0x211c },
284 { "reg", 0x00ae },
285 { "rfloor", 0x230b },
286 { "rho", 0x03c1 },
287 { "rlm", 0x200f },
288 { "rsaquo", 0x203a },
289 { "rsquo", 0x2019 },
290 { "sbquo", 0x201a },
291 { "scaron", 0x0161 },
292 { "sdot", 0x22c5 },
293 { "sect", 0x00a7 },
294 { "shy", 0x00ad },
295 { "sigma", 0x03c3 },
296 { "sigmaf", 0x03c2 },
297 { "sim", 0x223c },
298 { "spades", 0x2660 },
299 { "sub", 0x2282 },
300 { "sube", 0x2286 },
301 { "sum", 0x2211 },
302 { "sup", 0x2283 },
303 { "sup1", 0x00b9 },
304 { "sup2", 0x00b2 },
305 { "sup3", 0x00b3 },
306 { "supe", 0x2287 },
307 { "szlig", 0x00df },
308 { "tau", 0x03c4 },
309 { "there4", 0x2234 },
310 { "theta", 0x03b8 },
311 { "thetasym", 0x03d1 },
312 { "thinsp", 0x2009 },
313 { "thorn", 0x00fe },
314 { "tilde", 0x02dc },
315 { "times", 0x00d7 },
316 { "trade", 0x2122 },
317 { "uArr", 0x21d1 },
318 { "uacute", 0x00fa },
319 { "uarr", 0x2191 },
320 { "ucirc", 0x00fb },
321 { "ugrave", 0x00f9 },
322 { "uml", 0x00a8 },
323 { "upsih", 0x03d2 },
324 { "upsilon", 0x03c5 },
325 { "uuml", 0x00fc },
326 { "weierp", 0x2118 },
327 { "xi", 0x03be },
328 { "yacute", 0x00fd },
329 { "yen", 0x00a5 },
330 { "yuml", 0x00ff },
331 { "zeta", 0x03b6 },
332 { "zwj", 0x200d },
333 { "zwnj", 0x200c }
334};
335
336Q_STATIC_GLOBAL_OPERATOR bool operator<(const QString &entityStr, const QTextHtmlEntity &entity)
337{
338 return entityStr < QLatin1String(entity.name);
339}
340
341Q_STATIC_GLOBAL_OPERATOR bool operator<(const QTextHtmlEntity &entity, const QString &entityStr)
342{
343 return QLatin1String(entity.name) < entityStr;
344}
345
346static QChar resolveEntity(const QString &entity)
347{
348 const QTextHtmlEntity *start = &entities[0];
349 const QTextHtmlEntity *end = &entities[(sizeof(entities) / sizeof(entities[0]))];
350 const QTextHtmlEntity *e = qBinaryFind(start, end, entity);
351 if (e == end)
352 return QChar();
353 return e->code;
354}
355
356static const uint latin1Extended[0xA0 - 0x80] = {
357 0x20ac, // 0x80
358 0x0081, // 0x81 direct mapping
359 0x201a, // 0x82
360 0x0192, // 0x83
361 0x201e, // 0x84
362 0x2026, // 0x85
363 0x2020, // 0x86
364 0x2021, // 0x87
365 0x02C6, // 0x88
366 0x2030, // 0x89
367 0x0160, // 0x8A
368 0x2039, // 0x8B
369 0x0152, // 0x8C
370 0x008D, // 0x8D direct mapping
371 0x017D, // 0x8E
372 0x008F, // 0x8F directmapping
373 0x0090, // 0x90 directmapping
374 0x2018, // 0x91
375 0x2019, // 0x92
376 0x201C, // 0x93
377 0X201D, // 0x94
378 0x2022, // 0x95
379 0x2013, // 0x96
380 0x2014, // 0x97
381 0x02DC, // 0x98
382 0x2122, // 0x99
383 0x0161, // 0x9A
384 0x203A, // 0x9B
385 0x0153, // 0x9C
386 0x009D, // 0x9D direct mapping
387 0x017E, // 0x9E
388 0x0178 // 0x9F
389};
390// end taken from qtexthtmlparser
391
392class DocumentHelper
393{
394public:
395 DocumentHelper(const QString &fileName, const QByteArray &data)
396 : fileName(fileName) , data(readData(data)) {}
397 ~DocumentHelper() {}
398
399 bool addFieldsToDocument(QCLuceneDocument *document,
400 const QString &namespaceName, const QString &attributes = QString())
401 {
402 if (!document)
403 return false;
404
405 if(!data.isEmpty()) {
406 QString parsedData = parseData();
407 QString parsedTitle = QHelpGlobal::documentTitle(data);
408
409 if(!parsedData.isEmpty()) {
410 document->add(new QCLuceneField(ContentField,
411 parsedData,QCLuceneField::INDEX_TOKENIZED));
412 document->add(new QCLuceneField(PathField, fileName,
413 QCLuceneField::STORE_YES | QCLuceneField::INDEX_UNTOKENIZED));
414 document->add(new QCLuceneField(TitleField, parsedTitle,
415 QCLuceneField::STORE_YES | QCLuceneField::INDEX_UNTOKENIZED));
416 document->add(new QCLuceneField(TitleTokenizedField, parsedTitle,
417 QCLuceneField::STORE_YES | QCLuceneField::INDEX_TOKENIZED));
418 document->add(new QCLuceneField(NamespaceField, namespaceName,
419 QCLuceneField::STORE_YES | QCLuceneField::INDEX_UNTOKENIZED));
420 document->add(new QCLuceneField(AttributeField, attributes,
421 QCLuceneField::STORE_YES | QCLuceneField::INDEX_TOKENIZED));
422 return true;
423 }
424 }
425
426 return false;
427 }
428
429private:
430 QString readData(const QByteArray &data)
431 {
432 QTextStream textStream(data);
433 const QByteArray &codec = QHelpGlobal::codecFromData(data).toLatin1();
434 textStream.setCodec(QTextCodec::codecForName(codec.constData()));
435
436 QString stream = textStream.readAll();
437 if (stream.isNull() || stream.isEmpty())
438 return QString();
439
440 return stream;
441 }
442
443 QString parseData() const
444 {
445 const int length = data.length();
446 const QChar *buf = data.unicode();
447
448 QString parsedContent;
449 parsedContent.reserve(length);
450
451 bool valid = true;
452 int j = 0, count = 0;
453
454 QChar c;
455 while (j < length) {
456 c = buf[j++];
457 if (c == QLatin1Char('<') || c == QLatin1Char('&')) {
458 if (count > 1 && c != QLatin1Char('&'))
459 parsedContent.append(QLatin1Char(' '));
460 else if (c == QLatin1Char('&')) {
461 // Note: this will modify the counter j, in case we sucessful parsed the entity
462 // we will have modified the counter to stay 1 before the closing ';', so
463 // the following if condition will be met with if (c == QLatin1Char(';'))
464 parsedContent.append(parseEntity(length, buf, j));
465 }
466
467 count = 0;
468 valid = false;
469 continue;
470 }
471 if ((c == QLatin1Char('>') || c == QLatin1Char(';')) && !valid) {
472 valid = true;
473 continue;
474 }
475 if (!valid)
476 continue;
477
478 if (c.isLetterOrNumber() || c.isPrint()) {
479 ++count;
480 parsedContent.append(c.toLower());
481 } else {
482 if (count > 1)
483 parsedContent.append(QLatin1Char(' '));
484 count = 0;
485 }
486 }
487
488 return parsedContent;
489 }
490
491 // taken from qtexthtmlparser
492 // parses an entity after "&", and returns it
493 QString parseEntity(int len, const QChar *buf, int &pos) const
494 {
495 int recover = pos;
496 QString entity;
497 while (pos < len) {
498 QChar c = buf[pos++];
499 if (c.isSpace() || pos - recover > 9) {
500 goto error;
501 }
502 if (c == QLatin1Char(';')) {
503 pos--;
504 break;
505 }
506 entity += c;
507 }
508 {
509 QChar resolved = resolveEntity(entity);
510 if (!resolved.isNull())
511 return QString(resolved);
512 }
513 if (entity.length() > 1 && entity.at(0) == QLatin1Char('#')) {
514 entity.remove(0, 1); // removing leading #
515
516 int base = 10;
517 bool ok = false;
518
519 if (entity.at(0).toLower() == QLatin1Char('x')) { // hex entity?
520 entity.remove(0, 1);
521 base = 16;
522 }
523
524 uint uc = entity.toUInt(&ok, base);
525 if (ok) {
526 if (uc >= 0x80 && uc < 0x80 + (sizeof(latin1Extended) / sizeof(latin1Extended[0])))
527 uc = latin1Extended[uc - 0x80]; // windows latin 1 extended
528 QString str;
529 if (uc > 0xffff) {
530 // surrogate pair
531 uc -= 0x10000;
532 ushort high = uc/0x400 + 0xd800;
533 ushort low = uc%0x400 + 0xdc00;
534 str.append(QChar(high));
535 str.append(QChar(low));
536 } else {
537 str.append(QChar(uc));
538 }
539 return str;
540 }
541 }
542 error:
543 pos = recover;
544 return QLatin1String(" ");
545 }
546 // end taken from qtexthtmlparser
547
548private:
549 QString fileName;
550 QString data;
551};
552
553
554QHelpSearchIndexWriter::QHelpSearchIndexWriter()
555 : QThread(0)
556 , m_cancel(false)
557{
558 // nothing todo
559}
560
561QHelpSearchIndexWriter::~QHelpSearchIndexWriter()
562{
563 mutex.lock();
564 this->m_cancel = true;
565 waitCondition.wakeOne();
566 mutex.unlock();
567
568 wait();
569}
570
571void QHelpSearchIndexWriter::cancelIndexing()
572{
573 mutex.lock();
574 this->m_cancel = true;
575 mutex.unlock();
576}
577
578void QHelpSearchIndexWriter::updateIndex(const QString &collectionFile,
579 const QString &indexFilesFolder, bool reindex)
580{
581 wait();
582 mutex.lock();
583 this->m_cancel = false;
584 this->m_reindex = reindex;
585 this->m_collectionFile = collectionFile;
586 this->m_indexFilesFolder = indexFilesFolder;
587 mutex.unlock();
588
589 start(QThread::LowestPriority);
590}
591
592void QHelpSearchIndexWriter::optimizeIndex()
593{
594#if !defined(QT_NO_EXCEPTIONS)
595 try {
596#endif
597 if (QCLuceneIndexReader::indexExists(m_indexFilesFolder)) {
598 if (QCLuceneIndexReader::isLocked(m_indexFilesFolder))
599 return;
600
601 QCLuceneStandardAnalyzer analyzer;
602 QCLuceneIndexWriter writer(m_indexFilesFolder, analyzer, false);
603 writer.optimize();
604 writer.close();
605 }
606#if !defined(QT_NO_EXCEPTIONS)
607 } catch (...) {
608 qWarning("Full Text Search, could not optimize index.");
609 return;
610 }
611#endif
612}
613
614void QHelpSearchIndexWriter::run()
615{
616#if !defined(QT_NO_EXCEPTIONS)
617 try {
618#endif
619 QMutexLocker mutexLocker(&mutex);
620
621 if (m_cancel)
622 return;
623
624 const bool reindex = this->m_reindex;
625 const QString collectionFile(this->m_collectionFile);
626
627 mutexLocker.unlock();
628
629 QHelpEngineCore engine(collectionFile, 0);
630 if (!engine.setupData())
631 return;
632
633 const QLatin1String key("CluceneIndexedNamespaces");
634 if (reindex)
635 engine.setCustomValue(key, QLatin1String(""));
636
637 QMap<QString, QDateTime> indexMap;
638 const QLatin1String oldKey("CluceneSearchNamespaces");
639 if (!engine.customValue(oldKey, QString()).isNull()) {
640 // old style qhc file < 4.4.2, need to convert...
641 const QStringList indexedNamespaces
642 = engine.customValue(oldKey).toString()
643 .split(QLatin1String("|"), QString::SkipEmptyParts);
644 foreach (const QString &nameSpace, indexedNamespaces)
645 indexMap.insert(nameSpace, QDateTime());
646 engine.removeCustomValue(oldKey);
647 } else {
648 QDataStream dataStream(engine.customValue(key).toByteArray());
649 dataStream >> indexMap;
650 }
651
652 QString indexPath = m_indexFilesFolder;
653
654 QFileInfo fInfo(indexPath);
655 if (fInfo.exists() && !fInfo.isWritable()) {
656 qWarning("Full Text Search, could not create index (missing permissions for '%s').",
657 qPrintable(indexPath));
658 return;
659 }
660
661 emit indexingStarted();
662
663 QCLuceneIndexWriter *writer = 0;
664 QCLuceneStandardAnalyzer analyzer;
665 const QStringList registeredDocs = engine.registeredDocumentations();
666
667 QLocalSocket localSocket;
668 localSocket.connectToServer(QString(QLatin1String("QtAssistant%1"))
669 .arg(QLatin1String(QT_VERSION_STR)));
670
671 QLocalServer localServer;
672 bool otherInstancesRunning = true;
673 if (!localSocket.waitForConnected()) {
674 otherInstancesRunning = false;
675 localServer.listen(QString(QLatin1String("QtAssistant%1"))
676 .arg(QLatin1String(QT_VERSION_STR)));
677 }
678
679 // check if it's locked, and if the other instance is running
680 if (!otherInstancesRunning && QCLuceneIndexReader::isLocked(indexPath))
681 QCLuceneIndexReader::unlock(indexPath);
682
683 if (QCLuceneIndexReader::isLocked(indexPath)) {
684 // poll unless indexing finished to fake progress
685 while (QCLuceneIndexReader::isLocked(indexPath)) {
686 mutexLocker.relock();
687 if (m_cancel)
688 break;
689 mutexLocker.unlock();
690 this->sleep(1);
691 }
692 emit indexingFinished();
693 return;
694 }
695
696 if (QCLuceneIndexReader::indexExists(indexPath) && !reindex) {
697 foreach(const QString &namespaceName, registeredDocs) {
698 mutexLocker.relock();
699 if (m_cancel) {
700 emit indexingFinished();
701 return;
702 }
703 mutexLocker.unlock();
704
705 if (!indexMap.contains(namespaceName)) {
706 // make sure we remove some partly indexed stuff
707 removeDocuments(indexPath, namespaceName);
708 } else {
709 QString path = engine.documentationFileName(namespaceName);
710 if (indexMap.value(namespaceName)
711 < QFileInfo(path).lastModified()) {
712 // make sure we remove some outdated indexed stuff
713 indexMap.remove(namespaceName);
714 removeDocuments(indexPath, namespaceName);
715 }
716
717 if (indexMap.contains(namespaceName)) {
718 // make sure we really have content indexed for namespace
719 QCLuceneTermQuery query(QCLuceneTerm(NamespaceField, namespaceName));
720 QCLuceneIndexSearcher indexSearcher(indexPath);
721 QCLuceneHits hits = indexSearcher.search(query);
722 if (hits.length() <= 0)
723 indexMap.remove(namespaceName);
724 }
725 }
726 }
727 writer = new QCLuceneIndexWriter(indexPath, analyzer, false);
728 } else {
729 indexMap.clear();
730 writer = new QCLuceneIndexWriter(indexPath, analyzer, true);
731 }
732
733 writer->setMergeFactor(100);
734 writer->setMinMergeDocs(1000);
735 writer->setMaxFieldLength(QCLuceneIndexWriter::DEFAULT_MAX_FIELD_LENGTH);
736
737 QStringList namespaces;
738 foreach(const QString &namespaceName, registeredDocs) {
739 mutexLocker.relock();
740 if (m_cancel) {
741 closeIndexWriter(writer);
742 emit indexingFinished();
743 return;
744 }
745 mutexLocker.unlock();
746
747 namespaces.append(namespaceName);
748 if (indexMap.contains(namespaceName))
749 continue;
750
751 const QList<QStringList> attributeSets =
752 engine.filterAttributeSets(namespaceName);
753
754 if (attributeSets.isEmpty()) {
755 const QList<QUrl> docFiles = indexableFiles(&engine, namespaceName,
756 QStringList());
757 if (!addDocuments(docFiles, engine, QStringList(), namespaceName,
758 writer, analyzer))
759 break;
760 } else {
761 bool bail = false;
762 foreach (const QStringList &attributes, attributeSets) {
763 const QList<QUrl> docFiles = indexableFiles(&engine,
764 namespaceName, attributes);
765 if (!addDocuments(docFiles, engine, attributes, namespaceName,
766 writer, analyzer)) {
767 bail = true;
768 break;
769 }
770 }
771 if (bail)
772 break;
773 }
774
775 mutexLocker.relock();
776 if (!m_cancel) {
777 QString path(engine.documentationFileName(namespaceName));
778 indexMap.insert(namespaceName, QFileInfo(path).lastModified());
779 writeIndexMap(engine, indexMap);
780 }
781 mutexLocker.unlock();
782 }
783
784 closeIndexWriter(writer);
785
786 mutexLocker.relock();
787 if (!m_cancel) {
788 mutexLocker.unlock();
789
790 QStringList indexedNamespaces = indexMap.keys();
791 foreach(const QString &namespaceName, indexedNamespaces) {
792 mutexLocker.relock();
793 if (m_cancel)
794 break;
795 mutexLocker.unlock();
796
797 if (!namespaces.contains(namespaceName)) {
798 indexMap.remove(namespaceName);
799 writeIndexMap(engine, indexMap);
800 removeDocuments(indexPath, namespaceName);
801 }
802 }
803 }
804
805#if !defined(QT_NO_EXCEPTIONS)
806 } catch (...) {
807 qWarning("%s: Failed because of CLucene exception.", Q_FUNC_INFO);
808 }
809#endif
810
811 emit indexingFinished();
812}
813
814bool QHelpSearchIndexWriter::addDocuments(const QList<QUrl> docFiles,
815 const QHelpEngineCore &engine, const QStringList &attributes,
816 const QString &namespaceName, QCLuceneIndexWriter *writer,
817 QCLuceneAnalyzer &analyzer)
818{
819 QMutexLocker locker(&mutex);
820 const QString attrList = attributes.join(QLatin1String(" "));
821
822 locker.unlock();
823 foreach(const QUrl &url, docFiles) {
824 QCLuceneDocument document;
825 DocumentHelper helper(url.toString(), engine.fileData(url));
826 if (helper.addFieldsToDocument(&document, namespaceName, attrList)) {
827#if !defined(QT_NO_EXCEPTIONS)
828 try {
829#endif
830 writer->addDocument(document, analyzer);
831#if !defined(QT_NO_EXCEPTIONS)
832 } catch (...) {
833 qWarning("Full Text Search, could not properly add documents.");
834 return false;
835 }
836#endif
837 }
838 locker.relock();
839 if (m_cancel)
840 return false;
841 locker.unlock();
842 }
843 return true;
844}
845
846void QHelpSearchIndexWriter::removeDocuments(const QString &indexPath,
847 const QString &namespaceName)
848{
849 if (namespaceName.isEmpty() || QCLuceneIndexReader::isLocked(indexPath))
850 return;
851
852 QCLuceneIndexReader reader = QCLuceneIndexReader::open(indexPath);
853 reader.deleteDocuments(QCLuceneTerm(NamespaceField, namespaceName));
854
855 reader.close();
856}
857
858bool QHelpSearchIndexWriter::writeIndexMap(QHelpEngineCore &engine,
859 const QMap<QString, QDateTime> &indexMap)
860{
861 QByteArray bArray;
862
863 QDataStream data(&bArray, QIODevice::ReadWrite);
864 data << indexMap;
865
866 return engine.setCustomValue(QLatin1String("CluceneIndexedNamespaces"),
867 bArray);
868}
869
870QList<QUrl> QHelpSearchIndexWriter::indexableFiles(QHelpEngineCore *helpEngine,
871 const QString &namespaceName, const QStringList &attributes) const
872{
873 QList<QUrl> docFiles = helpEngine->files(namespaceName, attributes,
874 QLatin1String("html"));
875 docFiles += helpEngine->files(namespaceName, attributes, QLatin1String("htm"));
876 docFiles += helpEngine->files(namespaceName, attributes, QLatin1String("txt"));
877
878 return docFiles;
879}
880
881void QHelpSearchIndexWriter::closeIndexWriter(QCLuceneIndexWriter *writer)
882{
883#if !defined(QT_NO_EXCEPTIONS)
884 try {
885#endif
886 writer->close();
887 delete writer;
888#if !defined(QT_NO_EXCEPTIONS)
889 } catch (...) {
890 qWarning("Full Text Search, could not properly close index writer.");
891 }
892#endif
893}
894
895} // namespace clucene
896} // namespace fulltextsearch
897
898QT_END_NAMESPACE
Note: See TracBrowser for help on using the repository browser.