2014-04-16 16:18:28 +00:00
|
|
|
/* 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"
|
2018-04-10 14:49:52 +00:00
|
|
|
#include "folding.hh"
|
2021-11-27 07:17:33 +00:00
|
|
|
#include "utils.hh"
|
2014-04-16 16:18:28 +00:00
|
|
|
|
|
|
|
#include <vector>
|
|
|
|
#include <string>
|
|
|
|
|
2017-07-25 15:28:29 +00:00
|
|
|
#include <QVector>
|
|
|
|
|
2018-02-21 14:43:35 +00:00
|
|
|
#include <QRegularExpression>
|
|
|
|
#include "wildcard.hh"
|
|
|
|
|
2014-04-16 16:18:28 +00:00
|
|
|
using std::vector;
|
|
|
|
using std::string;
|
|
|
|
|
|
|
|
DEF_EX( exUserAbort, "User abort", Dictionary::Ex )
|
|
|
|
|
|
|
|
namespace FtsHelpers
|
|
|
|
{
|
|
|
|
|
2014-11-22 14:22:04 +00:00
|
|
|
bool ftsIndexIsOldOrBad( string const & indexFile,
|
|
|
|
BtreeIndexing::BtreeDictionary * dict )
|
2014-04-16 16:18:28 +00:00
|
|
|
{
|
|
|
|
File::Class idx( indexFile, "rb" );
|
|
|
|
|
|
|
|
FtsIdxHeader header;
|
|
|
|
|
|
|
|
return idx.readRecords( &header, sizeof( header ), 1 ) != 1 ||
|
|
|
|
header.signature != FtsSignature ||
|
2014-11-22 14:22:04 +00:00
|
|
|
header.formatVersion != CurrentFtsFormatVersion + dict->getFtsIndexVersion();
|
2014-04-16 16:18:28 +00:00
|
|
|
}
|
|
|
|
|
2017-07-25 15:28:29 +00:00
|
|
|
static QString makeHiliteRegExpString( QStringList const & words,
|
|
|
|
int searchMode,
|
|
|
|
int distanceBetweenWords )
|
|
|
|
{
|
|
|
|
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 < words.size(); x++ )
|
|
|
|
{
|
|
|
|
if( x )
|
|
|
|
searchString += stripWords;
|
|
|
|
|
|
|
|
searchString += boundWord + words[ x ] + boundWord;
|
|
|
|
}
|
|
|
|
|
|
|
|
searchString += ")";
|
|
|
|
return searchString;
|
|
|
|
}
|
|
|
|
|
2014-04-16 16:18:28 +00:00
|
|
|
bool parseSearchString( QString const & str, QStringList & indexWords,
|
|
|
|
QStringList & searchWords,
|
|
|
|
QRegExp & searchRegExp, int searchMode,
|
2014-04-22 13:47:02 +00:00
|
|
|
bool matchCase,
|
2014-05-08 12:38:00 +00:00
|
|
|
int distanceBetweenWords,
|
|
|
|
bool & hasCJK )
|
2014-04-16 16:18:28 +00:00
|
|
|
{
|
|
|
|
searchWords.clear();
|
|
|
|
indexWords.clear();
|
2022-06-01 13:15:09 +00:00
|
|
|
QRegularExpression spacesRegExp( "\\W+", QRegularExpression::UseUnicodePropertiesOption );
|
|
|
|
QRegularExpression wordRegExp( QString( "\\w{" ) + QString::number( FTS::MinimumWordSize ) + ",}", QRegularExpression::UseUnicodePropertiesOption );
|
2022-02-27 05:17:37 +00:00
|
|
|
QRegularExpression setsRegExp( "\\[[^\\]]+\\]", QRegularExpression::CaseInsensitiveOption );
|
|
|
|
QRegularExpression regexRegExp( "\\\\[afnrtvdDwWsSbB]|\\\\x([0-9A-Fa-f]{4})|\\\\0([0-7]{3})", QRegularExpression::CaseInsensitiveOption);
|
2014-04-16 16:18:28 +00:00
|
|
|
|
2014-05-08 12:38:00 +00:00
|
|
|
hasCJK = false;
|
|
|
|
for( int x = 0; x < str.size(); x++ )
|
|
|
|
if( isCJKChar( str.at( x ).unicode() ) )
|
|
|
|
{
|
|
|
|
hasCJK = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2014-04-16 16:18:28 +00:00
|
|
|
if( searchMode == FTS::WholeWords || searchMode == FTS::PlainText )
|
|
|
|
{
|
2014-05-08 12:38:00 +00:00
|
|
|
if( hasCJK )
|
|
|
|
return false;
|
|
|
|
|
2014-04-16 16:18:28 +00:00
|
|
|
// Make words list for search in article text
|
|
|
|
searchWords = str.normalized( QString::NormalizationForm_C )
|
2021-12-29 14:29:06 +00:00
|
|
|
.split( spacesRegExp, Qt::SkipEmptyParts );
|
2014-04-16 16:18:28 +00:00
|
|
|
|
|
|
|
// Make words list for index search
|
|
|
|
QStringList list = str.normalized( QString::NormalizationForm_C )
|
2021-12-29 14:29:06 +00:00
|
|
|
.toLower().split( spacesRegExp, Qt::SkipEmptyParts );
|
2014-04-16 16:18:28 +00:00
|
|
|
indexWords = list.filter( wordRegExp );
|
|
|
|
indexWords.removeDuplicates();
|
|
|
|
|
2014-04-22 13:47:02 +00:00
|
|
|
// Make regexp for results hilite
|
|
|
|
|
2021-12-29 14:29:06 +00:00
|
|
|
QStringList allWords = str.split( spacesRegExp, Qt::SkipEmptyParts );
|
2017-07-25 15:28:29 +00:00
|
|
|
QString searchString = makeHiliteRegExpString( allWords, searchMode, distanceBetweenWords );
|
2014-04-22 13:47:02 +00:00
|
|
|
|
|
|
|
searchRegExp = QRegExp( searchString, matchCase ? Qt::CaseSensitive : Qt::CaseInsensitive,
|
|
|
|
QRegExp::RegExp2 );
|
|
|
|
searchRegExp.setMinimal( true );
|
2014-04-16 16:18:28 +00:00
|
|
|
|
|
|
|
return !indexWords.isEmpty();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// Make words list for index search
|
|
|
|
|
|
|
|
QString tmp = str;
|
|
|
|
|
2015-12-31 12:42:33 +00:00
|
|
|
// Remove RegExp commands
|
|
|
|
if( searchMode == FTS::RegExp )
|
|
|
|
tmp.replace( regexRegExp, " " );
|
|
|
|
|
2014-04-16 16:18:28 +00:00
|
|
|
// Remove all symbol sets
|
|
|
|
tmp.replace( setsRegExp, " " );
|
|
|
|
|
|
|
|
QStringList list = tmp.normalized( QString::NormalizationForm_C )
|
2021-12-29 14:29:06 +00:00
|
|
|
.toLower().split( spacesRegExp, Qt::SkipEmptyParts );
|
2014-05-08 12:38:00 +00:00
|
|
|
|
|
|
|
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();
|
|
|
|
}
|
2014-04-16 16:18:28 +00:00
|
|
|
|
|
|
|
searchRegExp = QRegExp( str, matchCase ? Qt::CaseSensitive : Qt::CaseInsensitive,
|
|
|
|
searchMode == FTS::Wildcards ? QRegExp::WildcardUnix : QRegExp::RegExp2 );
|
2014-04-22 13:47:02 +00:00
|
|
|
searchRegExp.setMinimal( true );
|
2014-04-16 16:18:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void parseArticleForFts( uint32_t articleAddress, QString & articleText,
|
2017-07-25 15:28:56 +00:00
|
|
|
QMap< QString, QVector< uint32_t > > & words,
|
|
|
|
bool handleRoundBrackets )
|
2014-04-16 16:18:28 +00:00
|
|
|
{
|
|
|
|
if( articleText.isEmpty() )
|
|
|
|
return;
|
|
|
|
|
2018-02-21 14:43:35 +00:00
|
|
|
QRegularExpression regBrackets( "(\\([\\w\\p{M}]+\\)){0,1}([\\w\\p{M}]+)(\\([\\w\\p{M}]+\\)){0,1}([\\w\\p{M}]+){0,1}(\\([\\w\\p{M}]+\\)){0,1}",
|
|
|
|
QRegularExpression::UseUnicodePropertiesOption);
|
|
|
|
QRegularExpression regSplit( "[^\\w\\p{M}]+", QRegularExpression::UseUnicodePropertiesOption );
|
|
|
|
|
|
|
|
QStringList articleWords = articleText.normalized( QString::NormalizationForm_C )
|
|
|
|
.split( QRegularExpression( handleRoundBrackets ? "[^\\w\\(\\)\\p{M}]+" : "[^\\w\\p{M}]+",
|
|
|
|
QRegularExpression::UseUnicodePropertiesOption ),
|
2021-12-29 14:29:06 +00:00
|
|
|
Qt::SkipEmptyParts );
|
2017-07-25 15:28:56 +00:00
|
|
|
|
2018-02-21 14:43:35 +00:00
|
|
|
|
2014-04-16 16:18:28 +00:00
|
|
|
QSet< QString > setOfWords;
|
2018-03-01 15:07:22 +00:00
|
|
|
setOfWords.reserve( articleWords.size() );
|
2014-04-16 16:18:28 +00:00
|
|
|
|
|
|
|
for( int x = 0; x < articleWords.size(); x++ )
|
|
|
|
{
|
|
|
|
QString word = articleWords.at( x ).toLower();
|
|
|
|
|
2014-05-08 12:38:00 +00:00
|
|
|
bool hasCJK = false;
|
|
|
|
QString hieroglyph;
|
2014-04-16 16:18:28 +00:00
|
|
|
|
2014-05-08 12:38:00 +00:00
|
|
|
// 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 )
|
2014-04-16 16:18:28 +00:00
|
|
|
{
|
2014-05-08 12:38:00 +00:00
|
|
|
// Else we add word to index as is
|
|
|
|
if( word.size() < FTS::MinimumWordSize )
|
|
|
|
continue;
|
|
|
|
|
2017-07-25 15:28:56 +00:00
|
|
|
if( handleRoundBrackets && ( word.indexOf( '(' ) >= 0 || word.indexOf( ')' ) >= 0 ) )
|
|
|
|
{
|
|
|
|
// Special handle for words with round brackets - DSL feature
|
|
|
|
QStringList list;
|
|
|
|
|
2021-12-29 14:29:06 +00:00
|
|
|
QStringList oldVariant = word.split( regSplit, Qt::SkipEmptyParts );
|
2017-07-25 15:28:56 +00:00
|
|
|
for( QStringList::iterator it = oldVariant.begin(); it != oldVariant.end(); ++it )
|
|
|
|
if( it->size() >= FTS::MinimumWordSize && !list.contains( *it ) )
|
|
|
|
list.append( *it );
|
|
|
|
|
2018-02-21 14:43:35 +00:00
|
|
|
QRegularExpressionMatch match = regBrackets.match( word );
|
|
|
|
if( match.hasMatch() )
|
|
|
|
{
|
|
|
|
QStringList parts = match.capturedTexts();
|
|
|
|
// Add empty strings for compatibility with QRegExp behaviour
|
|
|
|
for( int i = match.lastCapturedIndex() + 1; i < 6; i++ )
|
|
|
|
parts.append( QString() );
|
2017-07-25 15:28:56 +00:00
|
|
|
QString parsedWord = parts[ 2 ] + parts[ 4 ]; // Brackets removed
|
|
|
|
|
|
|
|
if( parsedWord.size() >= FTS::MinimumWordSize && !list.contains( parsedWord ) )
|
|
|
|
list.append( parsedWord );
|
|
|
|
|
|
|
|
parsedWord = parts[ 1 ].remove( '(' ).remove( ')' )
|
|
|
|
+ parts[ 2 ]
|
|
|
|
+ parts[ 3 ].remove( '(' ).remove( ')' )
|
|
|
|
+ parts[ 4 ]
|
|
|
|
+ parts[ 5 ].remove( '(' ).remove( ')' ); // Brackets expansed
|
|
|
|
|
|
|
|
if( parsedWord.size() >= FTS::MinimumWordSize && !list.contains( parsedWord ) )
|
|
|
|
list.append( parsedWord );
|
|
|
|
}
|
|
|
|
|
|
|
|
for( QStringList::iterator it = list.begin(); it != list.end(); ++it )
|
|
|
|
{
|
|
|
|
if( !setOfWords.contains( *it ) )
|
|
|
|
{
|
|
|
|
setOfWords.insert( *it );
|
|
|
|
words[ *it ].push_back( articleAddress );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
2014-05-08 12:38:00 +00:00
|
|
|
if( !setOfWords.contains( word ) )
|
|
|
|
{
|
|
|
|
setOfWords.insert( word );
|
|
|
|
words[ word ].push_back( articleAddress );
|
|
|
|
}
|
2014-04-16 16:18:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void makeFTSIndex( BtreeIndexing::BtreeDictionary * dict, QAtomicInt & isCancelled )
|
|
|
|
{
|
|
|
|
Mutex::Lock _( dict->getFtsMutex() );
|
|
|
|
|
2021-11-27 07:17:33 +00:00
|
|
|
if( Utils::AtomicInt::loadAcquire( isCancelled ) )
|
2014-04-17 14:18:15 +00:00
|
|
|
throw exUserAbort();
|
|
|
|
|
2014-04-16 16:18:28 +00:00
|
|
|
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;
|
2018-03-01 15:07:22 +00:00
|
|
|
setOfOffsets.reserve( dict->getArticleCount() );
|
2014-04-16 16:18:28 +00:00
|
|
|
|
2014-04-17 14:18:15 +00:00
|
|
|
dict->findArticleLinks( 0, &setOfOffsets, 0, &isCancelled );
|
|
|
|
|
2021-11-27 07:17:33 +00:00
|
|
|
if( Utils::AtomicInt::loadAcquire( isCancelled ) )
|
2014-04-17 14:18:15 +00:00
|
|
|
throw exUserAbort();
|
2014-04-16 16:18:28 +00:00
|
|
|
|
|
|
|
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();
|
|
|
|
|
2021-11-27 07:17:33 +00:00
|
|
|
if( Utils::AtomicInt::loadAcquire( isCancelled ) )
|
2014-04-17 14:18:15 +00:00
|
|
|
throw exUserAbort();
|
|
|
|
|
2018-03-08 08:46:19 +00:00
|
|
|
dict->sortArticlesOffsetsForFTS( offsets, isCancelled );
|
2014-04-16 16:18:28 +00:00
|
|
|
|
|
|
|
QMap< QString, QVector< uint32_t > > ftsWords;
|
|
|
|
|
2017-07-25 15:28:56 +00:00
|
|
|
bool needHandleBrackets;
|
|
|
|
{
|
|
|
|
QString name = QString::fromUtf8( dict->getDictionaryFilenames()[ 0 ].c_str() ).toLower();
|
|
|
|
needHandleBrackets = name.endsWith( ".dsl" ) || name.endsWith( "dsl.dz" );
|
|
|
|
}
|
|
|
|
|
2014-04-16 16:18:28 +00:00
|
|
|
// index articles for full-text search
|
|
|
|
for( int i = 0; i < offsets.size(); i++ )
|
|
|
|
{
|
2021-11-27 07:17:33 +00:00
|
|
|
if( Utils::AtomicInt::loadAcquire( isCancelled ) )
|
2014-04-16 16:18:28 +00:00
|
|
|
throw exUserAbort();
|
|
|
|
|
|
|
|
QString headword, articleStr;
|
|
|
|
|
|
|
|
dict->getArticleText( offsets.at( i ), headword, articleStr );
|
|
|
|
|
2017-07-25 15:28:56 +00:00
|
|
|
parseArticleForFts( offsets.at( i ), articleStr, ftsWords, needHandleBrackets );
|
2014-04-16 16:18:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Free memory
|
|
|
|
offsets.clear();
|
|
|
|
|
2014-04-17 20:35:51 +00:00
|
|
|
QMap< QString, QVector< uint32_t > >::iterator it = ftsWords.begin();
|
|
|
|
while( it != ftsWords.end() )
|
2014-04-16 16:18:28 +00:00
|
|
|
{
|
2021-11-27 07:17:33 +00:00
|
|
|
if( Utils::AtomicInt::loadAcquire( isCancelled ) )
|
2014-04-16 16:18:28 +00:00
|
|
|
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 );
|
|
|
|
|
2014-04-17 20:35:51 +00:00
|
|
|
it = ftsWords.erase( it );
|
|
|
|
}
|
2014-04-17 14:18:15 +00:00
|
|
|
|
2014-04-16 16:18:28 +00:00
|
|
|
ftsIdxHeader.chunksOffset = chunks.finish();
|
|
|
|
ftsIdxHeader.wordCount = indexedWords.size();
|
|
|
|
|
2021-11-27 07:17:33 +00:00
|
|
|
if( Utils::AtomicInt::loadAcquire( isCancelled ) )
|
2014-04-17 14:18:15 +00:00
|
|
|
throw exUserAbort();
|
|
|
|
|
2014-04-16 16:18:28 +00:00
|
|
|
BtreeIndexing::IndexInfo ftsIdxInfo = BtreeIndexing::buildIndex( indexedWords, ftsIdx );
|
|
|
|
|
|
|
|
// Free memory
|
|
|
|
indexedWords.clear();
|
|
|
|
|
|
|
|
ftsIdxHeader.indexBtreeMaxElements = ftsIdxInfo.btreeMaxElements;
|
|
|
|
ftsIdxHeader.indexRootOffset = ftsIdxInfo.rootOffset;
|
|
|
|
|
|
|
|
ftsIdxHeader.signature = FtsHelpers::FtsSignature;
|
2014-11-22 14:22:04 +00:00
|
|
|
ftsIdxHeader.formatVersion = FtsHelpers::CurrentFtsFormatVersion + dict->getFtsIndexVersion();
|
2014-04-16 16:18:28 +00:00
|
|
|
|
|
|
|
ftsIdx.rewind();
|
|
|
|
ftsIdx.writeRecords( &ftsIdxHeader, sizeof(ftsIdxHeader), 1 );
|
|
|
|
}
|
|
|
|
|
2014-05-08 12:38:00 +00:00
|
|
|
bool isCJKChar( ushort ch )
|
|
|
|
{
|
|
|
|
if( ( ch >= 0x3400 && ch <= 0x9FFF )
|
|
|
|
|| ( ch >= 0xF900 && ch <= 0xFAFF )
|
|
|
|
|| ( ch >= 0xD800 && ch <= 0xDFFF ) )
|
|
|
|
return true;
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-04-16 16:18:28 +00:00
|
|
|
void FTSResultsRequest::checkArticles( QVector< uint32_t > const & offsets,
|
|
|
|
QStringList const & words,
|
|
|
|
QRegExp const & searchRegexp )
|
|
|
|
{
|
|
|
|
int results = 0;
|
|
|
|
QString headword, articleText;
|
|
|
|
QList< uint32_t > offsetsForHeadwords;
|
2017-07-25 15:28:29 +00:00
|
|
|
QVector< QStringList > hiliteRegExps;
|
2014-04-16 16:18:28 +00:00
|
|
|
|
|
|
|
QString id = QString::fromUtf8( dict.getId().c_str() );
|
2017-07-25 15:28:56 +00:00
|
|
|
bool needHandleBrackets;
|
|
|
|
{
|
|
|
|
QString name = QString::fromUtf8( dict.getDictionaryFilenames()[ 0 ].c_str() ).toLower();
|
|
|
|
needHandleBrackets = name.endsWith( ".dsl" ) || name.endsWith( ".dsl.dz" );
|
|
|
|
}
|
|
|
|
|
2019-09-20 21:30:53 +00:00
|
|
|
QRegularExpression regBrackets( "(\\([\\w\\p{M}]+\\)){0,1}([\\w\\p{M}]+)(\\([\\w\\p{M}]+\\)){0,1}([\\w\\p{M}]+){0,1}(\\([\\w\\p{M}]+\\)){0,1}",
|
2018-02-21 14:43:35 +00:00
|
|
|
QRegularExpression::UseUnicodePropertiesOption);
|
|
|
|
QRegularExpression regSplit( "[^\\w\\p{M}]+", QRegularExpression::UseUnicodePropertiesOption );
|
2021-11-19 13:47:22 +00:00
|
|
|
|
2014-04-16 16:18:28 +00:00
|
|
|
|
|
|
|
if( searchMode == FTS::Wildcards || searchMode == FTS::RegExp )
|
|
|
|
{
|
|
|
|
// RegExp mode
|
|
|
|
|
2018-02-21 14:43:35 +00:00
|
|
|
QRegularExpression searchRegularExpression;
|
|
|
|
if( searchMode == FTS::Wildcards )
|
|
|
|
searchRegularExpression.setPattern( wildcardsToRegexp( searchRegexp.pattern() ) );
|
|
|
|
else
|
|
|
|
searchRegularExpression.setPattern( searchRegexp.pattern() );
|
|
|
|
QRegularExpression::PatternOptions patternOptions = QRegularExpression::DotMatchesEverythingOption
|
|
|
|
| QRegularExpression::UseUnicodePropertiesOption
|
|
|
|
| QRegularExpression::MultilineOption
|
|
|
|
| QRegularExpression::InvertedGreedinessOption;
|
|
|
|
if( searchRegexp.caseSensitivity() == Qt::CaseInsensitive )
|
|
|
|
patternOptions |= QRegularExpression::CaseInsensitiveOption;
|
|
|
|
searchRegularExpression.setPatternOptions( patternOptions );
|
|
|
|
if( !searchRegularExpression.isValid() )
|
|
|
|
searchRegularExpression.setPattern( "" );
|
2014-04-16 16:18:28 +00:00
|
|
|
for( int i = 0; i < offsets.size(); i++ )
|
|
|
|
{
|
2021-11-27 07:17:33 +00:00
|
|
|
if( Utils::AtomicInt::loadAcquire( isCancelled ) )
|
2014-04-16 16:18:28 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
dict.getArticleText( offsets.at( i ), headword, articleText );
|
2018-02-21 14:43:35 +00:00
|
|
|
articleText = articleText.normalized( QString::NormalizationForm_C );
|
2014-04-16 16:18:28 +00:00
|
|
|
|
2018-04-10 14:49:52 +00:00
|
|
|
if( ignoreDiacritics )
|
|
|
|
articleText = gd::toQString( Folding::applyDiacriticsOnly( gd::toWString( articleText ) ) );
|
|
|
|
|
2018-02-21 14:43:35 +00:00
|
|
|
if( articleText.contains( searchRegularExpression ) )
|
2014-04-16 16:18:28 +00:00
|
|
|
{
|
|
|
|
if( headword.isEmpty() )
|
|
|
|
offsetsForHeadwords.append( offsets.at( i ) );
|
|
|
|
else
|
2017-07-25 15:28:29 +00:00
|
|
|
foundHeadwords->append( FTS::FtsHeadword( headword, id, QStringList(), matchCase ) );
|
2014-04-16 16:18:28 +00:00
|
|
|
|
|
|
|
results++;
|
|
|
|
if( maxResults > 0 && results >= maxResults )
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// Words mode
|
|
|
|
|
2018-03-06 14:46:01 +00:00
|
|
|
QRegularExpression splitWithBrackets( "[^\\w\\(\\)\\p{M}]+", QRegularExpression::UseUnicodePropertiesOption );
|
|
|
|
QRegularExpression splitWithoutBrackets( "[^\\w\\p{M}]+", QRegularExpression::UseUnicodePropertiesOption );
|
|
|
|
|
2014-04-16 16:18:28 +00:00
|
|
|
Qt::CaseSensitivity cs = matchCase ? Qt::CaseSensitive : Qt::CaseInsensitive;
|
2017-07-25 15:28:29 +00:00
|
|
|
QVector< QPair< QString, bool > > wordsList;
|
|
|
|
if( ignoreWordsOrder )
|
|
|
|
{
|
|
|
|
for( QStringList::const_iterator it = words.begin(); it != words.end(); ++it )
|
|
|
|
wordsList.append( QPair< QString, bool >( *it, true ) );
|
|
|
|
}
|
2014-04-16 16:18:28 +00:00
|
|
|
|
|
|
|
for( int i = 0; i < offsets.size(); i++ )
|
|
|
|
{
|
2021-11-27 07:17:33 +00:00
|
|
|
if( Utils::AtomicInt::loadAcquire( isCancelled ) )
|
2014-04-16 16:18:28 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
int pos = 0;
|
|
|
|
int matchWordNom = 0;
|
|
|
|
int unmatchWordNom = 0;
|
2017-08-26 00:08:24 +00:00
|
|
|
int nextNotFoundPos = 0;
|
2014-04-16 16:18:28 +00:00
|
|
|
|
2017-07-25 15:28:29 +00:00
|
|
|
QVector< QStringList > allOrders;
|
|
|
|
QStringList order;
|
|
|
|
|
|
|
|
if( ignoreWordsOrder )
|
|
|
|
{
|
|
|
|
for( int i = 0; i < wordsList.size(); i++ )
|
|
|
|
wordsList[ i ].second = true;
|
|
|
|
}
|
|
|
|
|
2014-04-16 16:18:28 +00:00
|
|
|
dict.getArticleText( offsets.at( i ), headword, articleText );
|
2018-02-21 14:43:35 +00:00
|
|
|
|
2018-04-10 14:49:52 +00:00
|
|
|
articleText = articleText.normalized( QString::NormalizationForm_C );
|
|
|
|
|
|
|
|
if( ignoreDiacritics )
|
|
|
|
articleText = gd::toQString( Folding::applyDiacriticsOnly( gd::toWString( articleText ) ) );
|
|
|
|
|
|
|
|
QStringList articleWords = articleText.split( needHandleBrackets ? splitWithBrackets : splitWithoutBrackets,
|
2021-12-29 14:29:06 +00:00
|
|
|
Qt::SkipEmptyParts );
|
2018-02-21 14:43:35 +00:00
|
|
|
|
|
|
|
int wordsNum = articleWords.length();
|
|
|
|
while ( pos < wordsNum )
|
2014-04-16 16:18:28 +00:00
|
|
|
{
|
2018-02-21 14:43:35 +00:00
|
|
|
QString s = articleWords[ pos ];
|
2017-07-25 15:28:56 +00:00
|
|
|
bool breakSearch = false;
|
|
|
|
|
|
|
|
QStringList parsedWords;
|
|
|
|
if( needHandleBrackets && ( s.indexOf( '(' ) >= 0 || s.indexOf( ')' ) >= 0 ) )
|
2017-07-25 15:28:29 +00:00
|
|
|
{
|
2017-07-25 15:28:56 +00:00
|
|
|
// Handle brackets
|
2018-02-21 14:43:35 +00:00
|
|
|
QRegularExpressionMatch match_brackets = regBrackets.match( s );
|
|
|
|
if( match_brackets.hasMatch() )
|
|
|
|
{
|
|
|
|
QStringList parts = match_brackets.capturedTexts();
|
|
|
|
// Add empty strings for compatibility with QRegExp behaviour
|
|
|
|
for( int i = match_brackets.lastCapturedIndex() + 1; i < 6; i++ )
|
|
|
|
parts.append( QString() );
|
2021-11-26 09:24:59 +00:00
|
|
|
|
2017-07-25 15:28:56 +00:00
|
|
|
QString word = parts[ 2 ] + parts[ 4 ]; // Brackets removed
|
|
|
|
parsedWords.append( word );
|
|
|
|
|
|
|
|
word = parts[ 1 ].remove( '(' ).remove( ')' )
|
|
|
|
+ parts[ 2 ]
|
|
|
|
+ parts[ 3 ].remove( '(' ).remove( ')' )
|
|
|
|
+ parts[ 4 ]
|
|
|
|
+ parts[ 5 ].remove( '(' ).remove( ')' ); // Brackets expansed
|
|
|
|
parsedWords.append( word );
|
|
|
|
}
|
|
|
|
else
|
2021-12-29 14:29:06 +00:00
|
|
|
parsedWords = s.split( regSplit, Qt::SkipEmptyParts );
|
2017-07-25 15:28:56 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
parsedWords.append( s );
|
|
|
|
|
|
|
|
int n;
|
|
|
|
for( n = 0; n < parsedWords.size(); n++ )
|
|
|
|
{
|
|
|
|
if( ignoreWordsOrder )
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
for( i = 0; i < wordsList.size(); i++ )
|
2017-07-25 15:28:29 +00:00
|
|
|
{
|
2017-07-25 15:28:56 +00:00
|
|
|
if( wordsList.at( i ).second )
|
2017-07-25 15:28:29 +00:00
|
|
|
{
|
2017-07-25 15:28:56 +00:00
|
|
|
if( ( searchMode == FTS::WholeWords && parsedWords.at( n ).compare( wordsList.at( i ).first, cs ) == 0 )
|
|
|
|
|| ( searchMode == FTS::PlainText && parsedWords.at( n ).contains( wordsList.at( i ).first, cs ) ) )
|
|
|
|
{
|
|
|
|
wordsList[ i ].second = false;
|
|
|
|
|
|
|
|
if( parsedWords.size() > 1 )
|
|
|
|
{
|
|
|
|
QString wordToHilite = s;
|
|
|
|
while( !wordToHilite.isEmpty() && ( wordToHilite.at( 0 ) == '(' || wordToHilite.at( 0 ) == ')' ) )
|
|
|
|
wordToHilite.remove( 0, 1 );
|
|
|
|
while( !wordToHilite.isEmpty() && ( wordToHilite.endsWith( '(' ) || wordToHilite.endsWith( ')' ) ) )
|
|
|
|
wordToHilite.chop( 1 );
|
|
|
|
order.append( wordToHilite.replace( '(', "\\(" ).replace( ')', "\\)" ) );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
order.append( wordsList.at( i ).first );
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
2017-07-25 15:28:29 +00:00
|
|
|
}
|
|
|
|
}
|
2017-07-25 15:28:56 +00:00
|
|
|
if( i < wordsList.size() )
|
2017-07-25 15:28:29 +00:00
|
|
|
{
|
2017-07-25 15:28:56 +00:00
|
|
|
// Word found
|
2017-08-26 00:08:24 +00:00
|
|
|
|
2017-07-25 15:28:56 +00:00
|
|
|
matchWordNom += 1;
|
2017-07-25 15:28:29 +00:00
|
|
|
|
2017-08-26 00:08:24 +00:00
|
|
|
if( matchWordNom == 1 )
|
|
|
|
{
|
|
|
|
// Store position to remake search if sequence will not be found
|
2018-02-21 14:43:35 +00:00
|
|
|
nextNotFoundPos = pos + 1;
|
2017-08-26 00:08:24 +00:00
|
|
|
}
|
|
|
|
|
2017-07-25 15:28:56 +00:00
|
|
|
if( matchWordNom >= words.size() )
|
2017-07-25 15:28:29 +00:00
|
|
|
{
|
2017-07-25 15:28:56 +00:00
|
|
|
// All words are found
|
|
|
|
// Store found words sequence and continue search
|
|
|
|
// It's nesessary for hilite search results
|
|
|
|
|
|
|
|
// Check if such sequence already presented
|
|
|
|
int x;
|
|
|
|
for( x = 0; x < allOrders.size(); x++ )
|
|
|
|
{
|
|
|
|
if( allOrders[ x ] == order )
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if( x >= allOrders.size() )
|
|
|
|
allOrders.append( order );
|
|
|
|
|
|
|
|
order.clear();
|
|
|
|
|
|
|
|
matchWordNom = 0;
|
|
|
|
unmatchWordNom = 0;
|
|
|
|
for( int i = 0; i < wordsList.size(); i++ )
|
|
|
|
wordsList[ i ].second = true;
|
2017-08-26 00:08:24 +00:00
|
|
|
nextNotFoundPos = 0;
|
2017-07-25 15:28:29 +00:00
|
|
|
|
2017-07-25 15:28:56 +00:00
|
|
|
break;
|
|
|
|
}
|
2017-07-25 15:28:29 +00:00
|
|
|
|
|
|
|
unmatchWordNom = 0;
|
2017-07-25 15:28:56 +00:00
|
|
|
break;
|
2017-07-25 15:28:29 +00:00
|
|
|
}
|
2017-07-25 15:28:56 +00:00
|
|
|
else
|
|
|
|
if( matchWordNom > 0 && n >= parsedWords.size() - 1 )
|
2017-07-25 15:28:29 +00:00
|
|
|
{
|
2017-07-25 15:28:56 +00:00
|
|
|
unmatchWordNom += 1;
|
|
|
|
if( distanceBetweenWords >= 0 && unmatchWordNom > distanceBetweenWords )
|
|
|
|
{
|
|
|
|
// Sequence broken, clear all counters
|
|
|
|
matchWordNom = 0;
|
|
|
|
unmatchWordNom = 0;
|
|
|
|
for( int i = 0; i < wordsList.size(); i++ )
|
|
|
|
wordsList[ i ].second = true;
|
|
|
|
order.clear();
|
|
|
|
}
|
2017-07-25 15:28:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2017-07-25 15:28:56 +00:00
|
|
|
if( ( searchMode == FTS::WholeWords && parsedWords.at( n ).compare( words.at( matchWordNom ), cs ) == 0 )
|
|
|
|
|| ( searchMode == FTS::PlainText && parsedWords.at( n ).contains( words.at( matchWordNom ), cs ) ) )
|
2017-07-25 15:28:29 +00:00
|
|
|
{
|
2017-07-25 15:28:56 +00:00
|
|
|
matchWordNom += 1;
|
|
|
|
|
2017-08-26 00:08:24 +00:00
|
|
|
if( matchWordNom == 1 )
|
|
|
|
{
|
|
|
|
// Store position to remake search if sequence will not be found
|
2018-02-21 14:43:35 +00:00
|
|
|
nextNotFoundPos = pos + 1;
|
2017-08-26 00:08:24 +00:00
|
|
|
}
|
|
|
|
|
2017-07-25 15:28:56 +00:00
|
|
|
if( needHandleBrackets )
|
|
|
|
{
|
|
|
|
if( parsedWords.size() > 1 )
|
|
|
|
{
|
|
|
|
QString wordToHilite = s;
|
|
|
|
while( !wordToHilite.isEmpty() && ( wordToHilite.at( 0 ) == '(' || wordToHilite.at( 0 ) == ')' ) )
|
|
|
|
wordToHilite.remove( 0, 1 );
|
|
|
|
while( !wordToHilite.isEmpty() && ( wordToHilite.endsWith( '(' ) || wordToHilite.endsWith( ')' ) ) )
|
|
|
|
wordToHilite.chop( 1 );
|
|
|
|
order.append( wordToHilite.replace( '(', "\\(" ).replace( ')', "\\)" ) );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
order.append( words.at( matchWordNom - 1 ) );
|
|
|
|
}
|
|
|
|
|
|
|
|
if( matchWordNom >= words.size() )
|
|
|
|
{
|
|
|
|
// All words are found
|
|
|
|
if( needHandleBrackets )
|
|
|
|
{
|
|
|
|
if( allOrders.isEmpty() )
|
|
|
|
allOrders.append( words );
|
|
|
|
|
|
|
|
// Check if such sequence already presented
|
|
|
|
int x;
|
|
|
|
for( x = 0; x < allOrders.size(); x++ )
|
|
|
|
{
|
|
|
|
if( allOrders[ x ] == order )
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if( x >= allOrders.size() )
|
|
|
|
allOrders.append( order );
|
|
|
|
|
|
|
|
matchWordNom = 0;
|
|
|
|
unmatchWordNom = 0;
|
|
|
|
order.clear();
|
2017-08-26 00:08:24 +00:00
|
|
|
nextNotFoundPos = 0;
|
2017-07-25 15:28:56 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
breakSearch = true;
|
|
|
|
break;
|
|
|
|
}
|
2017-07-25 15:28:29 +00:00
|
|
|
unmatchWordNom = 0;
|
2017-07-25 15:28:56 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
if( matchWordNom > 0 && n >= parsedWords.size() - 1 )
|
|
|
|
{
|
|
|
|
unmatchWordNom += 1;
|
|
|
|
if( distanceBetweenWords >= 0 && unmatchWordNom > distanceBetweenWords )
|
|
|
|
{
|
|
|
|
matchWordNom = 0;
|
|
|
|
unmatchWordNom = 0;
|
|
|
|
if( needHandleBrackets )
|
|
|
|
order.clear();
|
|
|
|
}
|
2017-07-25 15:28:29 +00:00
|
|
|
}
|
|
|
|
}
|
2014-04-16 16:18:28 +00:00
|
|
|
}
|
2017-07-25 15:28:56 +00:00
|
|
|
if( breakSearch )
|
|
|
|
break;
|
2017-08-26 00:08:24 +00:00
|
|
|
if( nextNotFoundPos > 0 && matchWordNom == 0 )
|
|
|
|
{
|
|
|
|
pos = nextNotFoundPos;
|
|
|
|
nextNotFoundPos = 0;
|
|
|
|
}
|
|
|
|
else
|
2018-02-21 14:43:35 +00:00
|
|
|
pos += 1;
|
2014-04-16 16:18:28 +00:00
|
|
|
}
|
|
|
|
|
2017-07-25 15:28:29 +00:00
|
|
|
if( !allOrders.isEmpty() || matchWordNom >= words.size() )
|
2014-04-16 16:18:28 +00:00
|
|
|
{
|
2017-07-25 15:28:29 +00:00
|
|
|
QStringList hiliteReg;
|
|
|
|
if( !allOrders.isEmpty() )
|
|
|
|
{
|
|
|
|
for( int i = 0; i < allOrders.size(); i++ )
|
|
|
|
{
|
|
|
|
QString hiliteStr = makeHiliteRegExpString( allOrders.at( i ), searchMode, distanceBetweenWords );
|
|
|
|
hiliteReg.append( hiliteStr );
|
|
|
|
}
|
|
|
|
allOrders.clear();
|
|
|
|
}
|
2014-04-16 16:18:28 +00:00
|
|
|
if( headword.isEmpty() )
|
2017-07-25 15:28:29 +00:00
|
|
|
{
|
2014-04-16 16:18:28 +00:00
|
|
|
offsetsForHeadwords.append( offsets.at( i ) );
|
2017-07-25 15:28:29 +00:00
|
|
|
hiliteRegExps.append( hiliteReg );
|
|
|
|
}
|
2014-04-16 16:18:28 +00:00
|
|
|
else
|
2017-07-25 15:28:29 +00:00
|
|
|
foundHeadwords->append( FTS::FtsHeadword( headword, id, hiliteReg, matchCase ) );
|
2014-04-16 16:18:28 +00:00
|
|
|
|
|
|
|
results++;
|
|
|
|
if( maxResults > 0 && results >= maxResults )
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if( !offsetsForHeadwords.isEmpty() )
|
|
|
|
{
|
|
|
|
QVector< QString > headwords;
|
2014-04-17 14:18:15 +00:00
|
|
|
dict.getHeadwordsFromOffsets( offsetsForHeadwords, headwords, &isCancelled );
|
2014-04-16 16:18:28 +00:00
|
|
|
for( int x = 0; x < headwords.size(); x++ )
|
2017-07-25 15:28:29 +00:00
|
|
|
foundHeadwords->append( FTS::FtsHeadword( headwords.at( x ), id, x < hiliteRegExps.size() ? hiliteRegExps.at( x ) : QStringList(), matchCase ) );
|
2014-04-16 16:18:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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++ )
|
|
|
|
{
|
2021-11-27 07:17:33 +00:00
|
|
|
if( Utils::AtomicInt::loadAcquire( isCancelled ) )
|
2014-04-16 16:18:28 +00:00
|
|
|
return;
|
|
|
|
|
|
|
|
tmp.clear();
|
|
|
|
|
2018-04-10 14:49:52 +00:00
|
|
|
links = ftsIndex.findArticles( gd::toWString( indexWords.at( i ) ), ignoreDiacritics );
|
2014-04-16 16:18:28 +00:00
|
|
|
for( unsigned x = 0; x < links.size(); x++ )
|
|
|
|
{
|
|
|
|
|
2021-11-27 07:17:33 +00:00
|
|
|
if( Utils::AtomicInt::loadAcquire( isCancelled ) )
|
2014-04-16 16:18:28 +00:00
|
|
|
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();
|
|
|
|
|
2018-03-08 08:46:19 +00:00
|
|
|
dict.sortArticlesOffsetsForFTS( offsets, isCancelled );
|
2014-04-16 16:18:28 +00:00
|
|
|
|
|
|
|
checkArticles( offsets, searchWords );
|
|
|
|
}
|
|
|
|
|
2014-05-08 12:38:00 +00:00
|
|
|
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;
|
|
|
|
|
2021-11-27 07:17:33 +00:00
|
|
|
if( Utils::AtomicInt::loadAcquire( isCancelled ) )
|
2014-05-08 12:38:00 +00:00
|
|
|
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++ )
|
|
|
|
{
|
|
|
|
|
2021-11-27 07:17:33 +00:00
|
|
|
if( Utils::AtomicInt::loadAcquire( isCancelled ) )
|
2014-05-08 12:38:00 +00:00
|
|
|
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;
|
2018-03-01 15:07:22 +00:00
|
|
|
links.reserve( wordsInIndex );
|
2018-03-01 15:06:48 +00:00
|
|
|
ftsIndex.findArticleLinks( &links, 0, 0, &isCancelled );
|
2014-05-08 12:38:00 +00:00
|
|
|
|
|
|
|
for( int x = 0; x < links.size(); x++ )
|
|
|
|
{
|
2021-11-27 07:17:33 +00:00
|
|
|
if( Utils::AtomicInt::loadAcquire( isCancelled ) )
|
2014-05-08 12:38:00 +00:00
|
|
|
return;
|
|
|
|
|
|
|
|
QString word = QString::fromUtf8( links[ x ].word.data(), links[ x ].word.size() );
|
2018-04-10 14:49:52 +00:00
|
|
|
|
|
|
|
if( ignoreDiacritics )
|
|
|
|
word = gd::toQString( Folding::applyDiacriticsOnly( gd::toWString( word ) ) );
|
|
|
|
|
2014-05-08 12:38:00 +00:00
|
|
|
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;
|
2021-10-13 18:15:01 +00:00
|
|
|
if( searchMode == FTS::PlainText || searchMode == FTS::WholeWords )
|
|
|
|
break;
|
2014-05-08 12:38:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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();
|
|
|
|
|
2018-03-08 08:46:19 +00:00
|
|
|
dict.sortArticlesOffsetsForFTS( offsets, isCancelled );
|
2014-05-08 12:38:00 +00:00
|
|
|
|
|
|
|
checkArticles( offsets, searchWords, regexp );
|
|
|
|
}
|
|
|
|
|
2014-04-16 16:18:28 +00:00
|
|
|
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;
|
|
|
|
|
2021-11-27 07:17:33 +00:00
|
|
|
if( Utils::AtomicInt::loadAcquire( isCancelled ) )
|
2014-04-16 16:18:28 +00:00
|
|
|
return;
|
|
|
|
|
|
|
|
if( indexWords.isEmpty() )
|
|
|
|
return;
|
|
|
|
|
2018-03-01 15:07:22 +00:00
|
|
|
links.reserve( wordsInIndex );
|
2018-03-01 15:06:48 +00:00
|
|
|
ftsIndex.findArticleLinks( &links, 0, 0, &isCancelled );
|
2014-04-16 16:18:28 +00:00
|
|
|
|
|
|
|
QVector< QSet< uint32_t > > allWordsLinks;
|
|
|
|
allWordsLinks.resize( indexWords.size() );
|
|
|
|
|
|
|
|
for( int x = 0; x < links.size(); x++ )
|
|
|
|
{
|
2021-11-27 07:17:33 +00:00
|
|
|
if( Utils::AtomicInt::loadAcquire( isCancelled ) )
|
2014-04-16 16:18:28 +00:00
|
|
|
return;
|
|
|
|
|
|
|
|
QString word = QString::fromUtf8( links[ x ].word.data(), links[ x ].word.size() );
|
2018-04-10 14:49:52 +00:00
|
|
|
|
|
|
|
if( ignoreDiacritics )
|
|
|
|
word = gd::toQString( Folding::applyDiacriticsOnly( gd::toWString( word ) ) );
|
|
|
|
|
2014-04-16 16:18:28 +00:00
|
|
|
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);
|
|
|
|
}
|
2021-10-13 18:15:01 +00:00
|
|
|
if( searchMode == FTS::PlainText )
|
|
|
|
break;
|
2014-04-16 16:18:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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();
|
|
|
|
|
2018-03-08 08:46:19 +00:00
|
|
|
dict.sortArticlesOffsetsForFTS( offsets, isCancelled );
|
2014-04-16 16:18:28 +00:00
|
|
|
|
|
|
|
checkArticles( offsets, searchWords, regexp );
|
|
|
|
}
|
|
|
|
|
|
|
|
void FTSResultsRequest::fullSearch( QStringList & searchWords, QRegExp & regexp )
|
|
|
|
{
|
|
|
|
// Whole file survey
|
|
|
|
|
2021-11-27 07:17:33 +00:00
|
|
|
if( Utils::AtomicInt::loadAcquire( isCancelled ) )
|
2014-04-16 16:18:28 +00:00
|
|
|
return;
|
|
|
|
|
|
|
|
QSet< uint32_t > setOfOffsets;
|
|
|
|
setOfOffsets.reserve( dict.getArticleCount() );
|
2018-03-01 15:06:48 +00:00
|
|
|
dict.findArticleLinks( 0, &setOfOffsets, 0, &isCancelled );
|
2014-04-16 16:18:28 +00:00
|
|
|
|
2021-11-27 07:17:33 +00:00
|
|
|
if( Utils::AtomicInt::loadAcquire( isCancelled ) )
|
2014-04-16 16:18:28 +00:00
|
|
|
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++;
|
|
|
|
}
|
|
|
|
|
2021-11-27 07:17:33 +00:00
|
|
|
if( Utils::AtomicInt::loadAcquire( isCancelled ) )
|
2014-04-16 16:18:28 +00:00
|
|
|
return;
|
|
|
|
|
|
|
|
setOfOffsets.clear();
|
|
|
|
|
2018-03-08 08:46:19 +00:00
|
|
|
dict.sortArticlesOffsetsForFTS( offsets, isCancelled );
|
2014-04-16 16:18:28 +00:00
|
|
|
|
2021-11-27 07:17:33 +00:00
|
|
|
if( Utils::AtomicInt::loadAcquire( isCancelled ) )
|
2014-04-16 16:18:28 +00:00
|
|
|
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;
|
|
|
|
|
2014-04-22 13:47:02 +00:00
|
|
|
if( !FtsHelpers::parseSearchString( searchString, indexWords, searchWords, searchRegExp,
|
2014-05-08 12:38:00 +00:00
|
|
|
searchMode, matchCase, distanceBetweenWords, hasCJK ) )
|
2014-04-16 16:18:28 +00:00
|
|
|
{
|
|
|
|
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 >();
|
|
|
|
|
2018-03-01 15:07:22 +00:00
|
|
|
wordsInIndex = ftsIdxHeader.wordCount;
|
|
|
|
|
2014-04-16 16:18:28 +00:00
|
|
|
ftsIndex.openIndex( BtreeIndexing::IndexInfo( ftsIdxHeader.indexBtreeMaxElements,
|
|
|
|
ftsIdxHeader.indexRootOffset ),
|
|
|
|
ftsIdx, dict.getFtsMutex() );
|
|
|
|
|
|
|
|
chunks = new ChunkedStorage::Reader( ftsIdx, ftsIdxHeader.chunksOffset );
|
|
|
|
}
|
|
|
|
|
2014-05-08 12:38:00 +00:00
|
|
|
if( hasCJK )
|
|
|
|
combinedIndexSearch( ftsIndex, chunks, indexWords, searchWords, searchRegExp );
|
2014-04-16 16:18:28 +00:00
|
|
|
else
|
2014-05-08 12:38:00 +00:00
|
|
|
{
|
|
|
|
if( searchMode == FTS::WholeWords )
|
|
|
|
indexSearch( ftsIndex, chunks, indexWords, searchWords );
|
|
|
|
else
|
|
|
|
fullIndexSearch( ftsIndex, chunks, indexWords, searchWords, searchRegExp );
|
|
|
|
}
|
2014-04-16 16:18:28 +00:00
|
|
|
}
|
|
|
|
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
|
|
|
|
|