forked from goldendict/goldendict
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathftshelpers.cc
862 lines (674 loc) · 22.7 KB
/
ftshelpers.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
/* This file is (c) 2014 Abs62
* Part of GoldenDict. Licensed under GPLv3 or later, see the LICENSE file */
#include "fulltextsearch.hh"
#include "ftshelpers.hh"
#include "wstring_qt.hh"
#include "file.hh"
#include "gddebug.hh"
#include "qt4x5.hh"
#include <vector>
#include <string>
using std::vector;
using std::string;
DEF_EX( exUserAbort, "User abort", Dictionary::Ex )
namespace FtsHelpers
{
bool ftsIndexIsOldOrBad( string const & indexFile,
BtreeIndexing::BtreeDictionary * dict )
{
File::Class idx( indexFile, "rb" );
FtsIdxHeader header;
return idx.readRecords( &header, sizeof( header ), 1 ) != 1 ||
header.signature != FtsSignature ||
header.formatVersion != CurrentFtsFormatVersion + dict->getFtsIndexVersion();
}
bool parseSearchString( QString const & str, QStringList & indexWords,
QStringList & searchWords,
QRegExp & searchRegExp, int searchMode,
bool matchCase,
int distanceBetweenWords,
bool & hasCJK )
{
searchWords.clear();
indexWords.clear();
QRegExp spacesRegExp( "\\W+" );
QRegExp wordRegExp( QString( "\\w{" ) + QString::number( FTS::MinimumWordSize ) + ",}" );
QRegExp setsRegExp( "\\[[^\\]]+\\]", Qt::CaseInsensitive, QRegExp::RegExp2 );
QRegExp regexRegExp( "\\\\[afnrtvdDwWsSbB]|\\\\x([0-9A-Fa-f]{4})|\\\\0([0-7]{3})", Qt::CaseSensitive, QRegExp::RegExp2 );
hasCJK = false;
for( int x = 0; x < str.size(); x++ )
if( isCJKChar( str.at( x ).unicode() ) )
{
hasCJK = true;
break;
}
if( searchMode == FTS::WholeWords || searchMode == FTS::PlainText )
{
if( hasCJK )
return false;
// Make words list for search in article text
searchWords = str.normalized( QString::NormalizationForm_C )
.split( spacesRegExp, QString::SkipEmptyParts );
// Make words list for index search
QStringList list = str.normalized( QString::NormalizationForm_C )
.toLower().split( spacesRegExp, QString::SkipEmptyParts );
indexWords = list.filter( wordRegExp );
indexWords.removeDuplicates();
// Make regexp for results hilite
QStringList allWords = str.split( spacesRegExp, QString::SkipEmptyParts );
QString searchString( "(" );
QString stripWords( "(?:\\W+\\w+){0," );
if( distanceBetweenWords >= 0 )
stripWords += QString::number( distanceBetweenWords );
stripWords += "}\\W+";
QString boundWord( searchMode == FTS::WholeWords ? "\\b" : "(?:\\w*)");
for( int x = 0; x < allWords.size(); x++ )
{
if( x )
searchString += stripWords;
searchString += boundWord + allWords[ x ] + boundWord;
}
searchString += ")";
searchRegExp = QRegExp( searchString, matchCase ? Qt::CaseSensitive : Qt::CaseInsensitive,
QRegExp::RegExp2 );
searchRegExp.setMinimal( true );
return !indexWords.isEmpty();
}
else
{
// Make words list for index search
QString tmp = str;
// Remove RegExp commands
if( searchMode == FTS::RegExp )
tmp.replace( regexRegExp, " " );
// Remove all symbol sets
tmp.replace( setsRegExp, " " );
QStringList list = tmp.normalized( QString::NormalizationForm_C )
.toLower().split( spacesRegExp, QString::SkipEmptyParts );
if( hasCJK )
{
QStringList wordList, hieroglyphList;
for( int i = 0; i < list.size(); i ++ )
{
QString word = list.at( i );
// Check for CJK symbols in word
bool parsed = false;
QString hieroglyph;
for( int x = 0; x < word.size(); x++ )
if( isCJKChar( word.at( x ).unicode() ) )
{
parsed = true;
hieroglyph.append( word[ x ] );
if( QChar( word.at( x ) ).isHighSurrogate()
&& QChar( word[ x + 1 ] ).isLowSurrogate() )
hieroglyph.append( word[ ++x ] );
hieroglyphList.append( hieroglyph );
hieroglyph.clear();
}
// If word don't contains CJK symbols put it in list as is
if( !parsed )
wordList.append( word );
}
indexWords = wordList.filter( wordRegExp );
indexWords.removeDuplicates();
hieroglyphList.removeDuplicates();
indexWords += hieroglyphList;
}
else
{
indexWords = list.filter( wordRegExp );
indexWords.removeDuplicates();
}
searchRegExp = QRegExp( str, matchCase ? Qt::CaseSensitive : Qt::CaseInsensitive,
searchMode == FTS::Wildcards ? QRegExp::WildcardUnix : QRegExp::RegExp2 );
searchRegExp.setMinimal( true );
}
return true;
}
void parseArticleForFts( uint32_t articleAddress, QString & articleText,
QMap< QString, QVector< uint32_t > > & words )
{
if( articleText.isEmpty() )
return;
QStringList articleWords = articleText.normalized( QString::NormalizationForm_C )
.split( QRegExp( "\\W+" ), QString::SkipEmptyParts );
QSet< QString > setOfWords;
for( int x = 0; x < articleWords.size(); x++ )
{
QString word = articleWords.at( x ).toLower();
bool hasCJK = false;
QString hieroglyph;
// If word contains CJK symbols we add to index only these symbols separately
for( int y = 0; y < word.size(); y++ )
if( isCJKChar( word.at( y ).unicode() ) )
{
hasCJK = true;
hieroglyph.append( word[ y ] );
if( QChar( word.at( y ) ).isHighSurrogate()
&& QChar( word[ y + 1 ] ).isLowSurrogate() )
hieroglyph.append( word[ ++y ] );
if( !setOfWords.contains( hieroglyph ) )
{
setOfWords.insert( hieroglyph );
words[ hieroglyph ].push_back( articleAddress );
}
hieroglyph.clear();
}
if( !hasCJK )
{
// Else we add word to index as is
if( word.size() < FTS::MinimumWordSize )
continue;
if( !setOfWords.contains( word ) )
{
setOfWords.insert( word );
words[ word ].push_back( articleAddress );
}
}
}
}
void makeFTSIndex( BtreeIndexing::BtreeDictionary * dict, QAtomicInt & isCancelled )
{
Mutex::Lock _( dict->getFtsMutex() );
if( Qt4x5::AtomicInt::loadAcquire( isCancelled ) )
throw exUserAbort();
File::Class ftsIdx( dict->ftsIndexName(), "wb" );
FtsIdxHeader ftsIdxHeader;
memset( &ftsIdxHeader, 0, sizeof( ftsIdxHeader ) );
// We write a dummy header first. At the end of the process the header
// will be rewritten with the right values.
ftsIdx.write( ftsIdxHeader );
ChunkedStorage::Writer chunks( ftsIdx );
BtreeIndexing::IndexedWords indexedWords;
QSet< uint32_t > setOfOffsets;
dict->findArticleLinks( 0, &setOfOffsets, 0, &isCancelled );
if( Qt4x5::AtomicInt::loadAcquire( isCancelled ) )
throw exUserAbort();
QVector< uint32_t > offsets;
offsets.resize( setOfOffsets.size() );
uint32_t * ptr = &offsets.front();
for( QSet< uint32_t >::ConstIterator it = setOfOffsets.constBegin();
it != setOfOffsets.constEnd(); ++it )
{
*ptr = *it;
ptr++;
}
// Free memory
setOfOffsets.clear();
if( Qt4x5::AtomicInt::loadAcquire( isCancelled ) )
throw exUserAbort();
qSort( offsets );
QMap< QString, QVector< uint32_t > > ftsWords;
// index articles for full-text search
for( int i = 0; i < offsets.size(); i++ )
{
if( Qt4x5::AtomicInt::loadAcquire( isCancelled ) )
throw exUserAbort();
QString headword, articleStr;
dict->getArticleText( offsets.at( i ), headword, articleStr );
parseArticleForFts( offsets.at( i ), articleStr, ftsWords );
}
// Free memory
offsets.clear();
QMap< QString, QVector< uint32_t > >::iterator it = ftsWords.begin();
while( it != ftsWords.end() )
{
if( Qt4x5::AtomicInt::loadAcquire( isCancelled ) )
throw exUserAbort();
uint32_t offset = chunks.startNewBlock();
uint32_t size = it.value().size();
chunks.addToBlock( &size, sizeof(uint32_t) );
chunks.addToBlock( it.value().data(), size * sizeof(uint32_t) );
indexedWords.addSingleWord( gd::toWString( it.key() ), offset );
it = ftsWords.erase( it );
}
ftsIdxHeader.chunksOffset = chunks.finish();
ftsIdxHeader.wordCount = indexedWords.size();
if( Qt4x5::AtomicInt::loadAcquire( isCancelled ) )
throw exUserAbort();
BtreeIndexing::IndexInfo ftsIdxInfo = BtreeIndexing::buildIndex( indexedWords, ftsIdx );
// Free memory
indexedWords.clear();
ftsIdxHeader.indexBtreeMaxElements = ftsIdxInfo.btreeMaxElements;
ftsIdxHeader.indexRootOffset = ftsIdxInfo.rootOffset;
ftsIdxHeader.signature = FtsHelpers::FtsSignature;
ftsIdxHeader.formatVersion = FtsHelpers::CurrentFtsFormatVersion + dict->getFtsIndexVersion();
ftsIdx.rewind();
ftsIdx.writeRecords( &ftsIdxHeader, sizeof(ftsIdxHeader), 1 );
}
bool isCJKChar( ushort ch )
{
if( ( ch >= 0x3400 && ch <= 0x9FFF )
|| ( ch >= 0xF900 && ch <= 0xFAFF )
|| ( ch >= 0xD800 && ch <= 0xDFFF ) )
return true;
return false;
}
void FTSResultsRequestRunnable::run()
{
r.run();
}
void FTSResultsRequest::checkArticles( QVector< uint32_t > const & offsets,
QStringList const & words,
QRegExp const & searchRegexp )
{
int results = 0;
QString headword, articleText;
QList< uint32_t > offsetsForHeadwords;
QString id = QString::fromUtf8( dict.getId().c_str() );
if( searchMode == FTS::Wildcards || searchMode == FTS::RegExp )
{
// RegExp mode
for( int i = 0; i < offsets.size(); i++ )
{
if( Qt4x5::AtomicInt::loadAcquire( isCancelled ) )
break;
dict.getArticleText( offsets.at( i ), headword, articleText );
if( articleText.contains( searchRegexp ) )
{
if( headword.isEmpty() )
offsetsForHeadwords.append( offsets.at( i ) );
else
foundHeadwords->append( FTS::FtsHeadword( headword, id ) );
results++;
if( maxResults > 0 && results >= maxResults )
break;
}
}
}
else
{
// Words mode
QRegExp regex( "\\b\\w+\\b" );
Qt::CaseSensitivity cs = matchCase ? Qt::CaseSensitive : Qt::CaseInsensitive;
for( int i = 0; i < offsets.size(); i++ )
{
if( Qt4x5::AtomicInt::loadAcquire( isCancelled ) )
break;
int pos = 0;
int matchWordNom = 0;
int unmatchWordNom = 0;
dict.getArticleText( offsets.at( i ), headword, articleText );
int articleLength = articleText.length();
while ( pos >= 0 && pos < articleLength )
{
pos = articleText.indexOf( regex, pos );
if( pos >= 0 )
{
QString s = regex.cap().normalized( QString::NormalizationForm_C );
if( ( searchMode == FTS::WholeWords && s.compare( words.at( matchWordNom ), cs ) == 0 )
|| ( searchMode == FTS::PlainText && s.contains( words.at( matchWordNom ), cs ) ) )
{
matchWordNom += 1;
if( matchWordNom >= words.size() )
break; // All words are found
unmatchWordNom = 0;
}
else
if( matchWordNom > 0 )
{
unmatchWordNom += 1;
if( distanceBetweenWords >= 0 && unmatchWordNom > distanceBetweenWords )
{
matchWordNom = 0;
unmatchWordNom = 0;
}
}
pos += s.isEmpty() ? 1 : s.length();
}
}
if( matchWordNom >= words.size() )
{
if( headword.isEmpty() )
offsetsForHeadwords.append( offsets.at( i ) );
else
foundHeadwords->append( FTS::FtsHeadword( headword, id ) );
results++;
if( maxResults > 0 && results >= maxResults )
break;
}
}
}
if( !offsetsForHeadwords.isEmpty() )
{
QVector< QString > headwords;
dict.getHeadwordsFromOffsets( offsetsForHeadwords, headwords, &isCancelled );
for( int x = 0; x < headwords.size(); x++ )
foundHeadwords->append( FTS::FtsHeadword( headwords.at( x ), id ) );
}
}
void FTSResultsRequest::indexSearch( BtreeIndexing::BtreeIndex & ftsIndex,
sptr< ChunkedStorage::Reader > chunks,
QStringList & indexWords,
QStringList & searchWords )
{
// Find articles which contains all requested words
vector< BtreeIndexing::WordArticleLink > links;
QSet< uint32_t > setOfOffsets, tmp;
uint32_t size;
if( indexWords.isEmpty() )
return;
int n = indexWords.length();
for( int i = 0; i < n; i++ )
{
if( Qt4x5::AtomicInt::loadAcquire( isCancelled ) )
return;
tmp.clear();
links = ftsIndex.findArticles( gd::toWString( indexWords.at( i ) ) );
for( unsigned x = 0; x < links.size(); x++ )
{
if( Qt4x5::AtomicInt::loadAcquire( isCancelled ) )
return;
vector< char > chunk;
char * linksPtr;
{
Mutex::Lock _( dict.getFtsMutex() );
linksPtr = chunks->getBlock( links[ x ].articleOffset, chunk );
}
memcpy( &size, linksPtr, sizeof(uint32_t) );
linksPtr += sizeof(uint32_t);
for( uint32_t y = 0; y < size; y++ )
{
tmp.insert( *( reinterpret_cast< uint32_t * >( linksPtr ) ) );
linksPtr += sizeof(uint32_t);
}
}
links.clear();
if( i == 0 )
setOfOffsets = tmp;
else
setOfOffsets = setOfOffsets.intersect( tmp );
}
tmp.clear();
if( setOfOffsets.isEmpty() )
return;
QVector< uint32_t > offsets;
offsets.resize( setOfOffsets.size() );
uint32_t * ptr = &offsets.front();
for( QSet< uint32_t >::ConstIterator it = setOfOffsets.constBegin();
it != setOfOffsets.constEnd(); ++it )
{
*ptr = *it;
ptr++;
}
setOfOffsets.clear();
qSort( offsets );
checkArticles( offsets, searchWords );
}
void FTSResultsRequest::combinedIndexSearch( BtreeIndexing::BtreeIndex & ftsIndex,
sptr< ChunkedStorage::Reader > chunks,
QStringList & indexWords,
QStringList & searchWords,
QRegExp & regexp )
{
// Special case - combination of index search for hieroglyphs
// and full index search for other words
QSet< uint32_t > setOfOffsets;
uint32_t size;
if( Qt4x5::AtomicInt::loadAcquire( isCancelled ) )
return;
if( indexWords.isEmpty() )
return;
QStringList wordsList, hieroglyphsList;
for( int x = 0; x < indexWords.size(); x++ )
{
QString const & word = indexWords.at( x );
if( isCJKChar( word[ 0 ].unicode() ) )
hieroglyphsList.append( word );
else
wordsList.append( word );
}
QVector< QSet< uint32_t > > allWordsLinks;
int n = wordsList.size();
if( !hieroglyphsList.isEmpty() )
n += 1;
allWordsLinks.resize( n );
int wordNom = 0;
if( !hieroglyphsList.empty() )
{
QSet< uint32_t > tmp;
vector< BtreeIndexing::WordArticleLink > links;
for( int i = 0; i < hieroglyphsList.size(); i++ )
{
links = ftsIndex.findArticles( gd::toWString( hieroglyphsList.at( i ) ) );
for( unsigned x = 0; x < links.size(); x++ )
{
if( Qt4x5::AtomicInt::loadAcquire( isCancelled ) )
return;
vector< char > chunk;
char * linksPtr;
{
Mutex::Lock _( dict.getFtsMutex() );
linksPtr = chunks->getBlock( links[ x ].articleOffset, chunk );
}
memcpy( &size, linksPtr, sizeof(uint32_t) );
linksPtr += sizeof(uint32_t);
for( uint32_t y = 0; y < size; y++ )
{
tmp.insert( *( reinterpret_cast< uint32_t * >( linksPtr ) ) );
linksPtr += sizeof(uint32_t);
}
}
links.clear();
if( i == 0 )
setOfOffsets = tmp;
else
setOfOffsets = setOfOffsets.intersect( tmp );
}
allWordsLinks[ wordNom ] = setOfOffsets;
setOfOffsets.clear();
wordNom += 1;
}
if( !wordsList.isEmpty() )
{
QVector< BtreeIndexing::WordArticleLink > links;
ftsIndex.findArticleLinks( &links, 0, 0 );
for( int x = 0; x < links.size(); x++ )
{
if( Qt4x5::AtomicInt::loadAcquire( isCancelled ) )
return;
QString word = QString::fromUtf8( links[ x ].word.data(), links[ x ].word.size() );
for( int i = 0; i < wordsList.size(); i++ )
{
if( word.length() >= wordsList.at( i ).length() && word.contains( wordsList.at( i ) ) )
{
vector< char > chunk;
char * linksPtr;
{
Mutex::Lock _( dict.getFtsMutex() );
linksPtr = chunks->getBlock( links[ x ].articleOffset, chunk );
}
memcpy( &size, linksPtr, sizeof(uint32_t) );
linksPtr += sizeof(uint32_t);
for( uint32_t y = 0; y < size; y++ )
{
allWordsLinks[ wordNom ].insert( *( reinterpret_cast< uint32_t * >( linksPtr ) ) );
linksPtr += sizeof(uint32_t);
}
wordNom += 1;
break;
}
}
}
links.clear();
}
for( int i = 0; i < allWordsLinks.size(); i++ )
{
if( i == 0 )
setOfOffsets = allWordsLinks.at( i );
else
setOfOffsets = setOfOffsets.intersect( allWordsLinks.at( i ) );
}
if( setOfOffsets.isEmpty() )
return;
allWordsLinks.clear();
QVector< uint32_t > offsets;
offsets.resize( setOfOffsets.size() );
uint32_t * ptr = &offsets.front();
for( QSet< uint32_t >::ConstIterator it = setOfOffsets.constBegin();
it != setOfOffsets.constEnd(); ++it )
{
*ptr = *it;
ptr++;
}
setOfOffsets.clear();
qSort( offsets );
checkArticles( offsets, searchWords, regexp );
}
void FTSResultsRequest::fullIndexSearch( BtreeIndexing::BtreeIndex & ftsIndex,
sptr< ChunkedStorage::Reader > chunks,
QStringList & indexWords,
QStringList & searchWords,
QRegExp & regexp )
{
QSet< uint32_t > setOfOffsets;
uint32_t size;
QVector< BtreeIndexing::WordArticleLink > links;
if( Qt4x5::AtomicInt::loadAcquire( isCancelled ) )
return;
if( indexWords.isEmpty() )
return;
ftsIndex.findArticleLinks( &links, 0, 0 );
QVector< QSet< uint32_t > > allWordsLinks;
allWordsLinks.resize( indexWords.size() );
for( int x = 0; x < links.size(); x++ )
{
if( Qt4x5::AtomicInt::loadAcquire( isCancelled ) )
return;
QString word = QString::fromUtf8( links[ x ].word.data(), links[ x ].word.size() );
for( int i = 0; i < indexWords.size(); i++ )
{
if( word.length() >= indexWords.at( i ).length() && word.contains( indexWords.at( i ) ) )
{
vector< char > chunk;
char * linksPtr;
{
Mutex::Lock _( dict.getFtsMutex() );
linksPtr = chunks->getBlock( links[ x ].articleOffset, chunk );
}
memcpy( &size, linksPtr, sizeof(uint32_t) );
linksPtr += sizeof(uint32_t);
for( uint32_t y = 0; y < size; y++ )
{
allWordsLinks[ i ].insert( *( reinterpret_cast< uint32_t * >( linksPtr ) ) );
linksPtr += sizeof(uint32_t);
}
break;
}
}
}
links.clear();
for( int i = 0; i < allWordsLinks.size(); i++ )
{
if( i == 0 )
setOfOffsets = allWordsLinks.at( i );
else
setOfOffsets = setOfOffsets.intersect( allWordsLinks.at( i ) );
}
if( setOfOffsets.isEmpty() )
return;
allWordsLinks.clear();
QVector< uint32_t > offsets;
offsets.resize( setOfOffsets.size() );
uint32_t * ptr = &offsets.front();
for( QSet< uint32_t >::ConstIterator it = setOfOffsets.constBegin();
it != setOfOffsets.constEnd(); ++it )
{
*ptr = *it;
ptr++;
}
setOfOffsets.clear();
qSort( offsets );
checkArticles( offsets, searchWords, regexp );
}
void FTSResultsRequest::fullSearch( QStringList & searchWords, QRegExp & regexp )
{
// Whole file survey
if( Qt4x5::AtomicInt::loadAcquire( isCancelled ) )
return;
QSet< uint32_t > setOfOffsets;
setOfOffsets.reserve( dict.getArticleCount() );
dict.findArticleLinks( 0, &setOfOffsets, 0 );
if( Qt4x5::AtomicInt::loadAcquire( isCancelled ) )
return;
QVector< uint32_t > offsets;
offsets.resize( setOfOffsets.size() );
uint32_t * ptr = &offsets.front();
for( QSet< uint32_t >::ConstIterator it = setOfOffsets.constBegin();
it != setOfOffsets.constEnd(); ++it )
{
*ptr = *it;
ptr++;
}
if( Qt4x5::AtomicInt::loadAcquire( isCancelled ) )
return;
setOfOffsets.clear();
qSort( offsets );
if( Qt4x5::AtomicInt::loadAcquire( isCancelled ) )
return;
checkArticles( offsets, searchWords, regexp );
}
void FTSResultsRequest::run()
{
if ( dict.ensureInitDone().size() )
{
setErrorString( QString::fromUtf8( dict.ensureInitDone().c_str() ) );
finish();
return;
}
try
{
QStringList indexWords, searchWords;
QRegExp searchRegExp;
if( !FtsHelpers::parseSearchString( searchString, indexWords, searchWords, searchRegExp,
searchMode, matchCase, distanceBetweenWords, hasCJK ) )
{
finish();
return;
}
if( dict.haveFTSIndex() && !indexWords.isEmpty() )
{
FtsIdxHeader ftsIdxHeader;
BtreeIndexing::BtreeIndex ftsIndex;
sptr< ChunkedStorage::Reader > chunks;
File::Class ftsIdx( dict.ftsIndexName(), "rb" );
{
Mutex::Lock _( dict.getFtsMutex() );
ftsIdxHeader = ftsIdx.read< FtsIdxHeader >();
ftsIndex.openIndex( BtreeIndexing::IndexInfo( ftsIdxHeader.indexBtreeMaxElements,
ftsIdxHeader.indexRootOffset ),
ftsIdx, dict.getFtsMutex() );
chunks = new ChunkedStorage::Reader( ftsIdx, ftsIdxHeader.chunksOffset );
}
if( hasCJK )
combinedIndexSearch( ftsIndex, chunks, indexWords, searchWords, searchRegExp );
else
{
if( searchMode == FTS::WholeWords )
indexSearch( ftsIndex, chunks, indexWords, searchWords );
else
fullIndexSearch( ftsIndex, chunks, indexWords, searchWords, searchRegExp );
}
}
else
{
fullSearch( searchWords, searchRegExp );
}
if( foundHeadwords && foundHeadwords->size() > 0 )
{
Mutex::Lock _( dataMutex );
data.resize( sizeof( foundHeadwords ) );
memcpy( &data.front(), &foundHeadwords, sizeof( foundHeadwords ) );
foundHeadwords = 0;
hasAnyData = true;
}
}
catch( std::exception &ex )
{
gdWarning( "FTS: Failed full-text search for \"%s\", reason: %s\n",
dict.getName().c_str(), ex.what() );
// Results not loaded -- we don't set the hasAnyData flag then
}
finish();
}
} // namespace