diff --git a/btreeidx.hh b/btreeidx.hh index 30817a96..8aaaa652 100644 --- a/btreeidx.hh +++ b/btreeidx.hh @@ -189,6 +189,12 @@ public: virtual uint32_t getFtsIndexVersion() { return 0; } + // Sort articles offsets for full-text search in dictionary-specific order + // to increase of articles retrieving speed + // Default - simple sorting in increase order + virtual void sortArticlesOffsetsForFTS( QVector< uint32_t > & offsets ) + { qSort( offsets ); } + /// Called before each matching operation to ensure that any child init /// has completed. Mainly used for deferred init. The default implementation /// does nothing. diff --git a/ftshelpers.cc b/ftshelpers.cc index 965cdf95..f8ef1f8a 100644 --- a/ftshelpers.cc +++ b/ftshelpers.cc @@ -340,7 +340,7 @@ void makeFTSIndex( BtreeIndexing::BtreeDictionary * dict, QAtomicInt & isCancell if( Qt4x5::AtomicInt::loadAcquire( isCancelled ) ) throw exUserAbort(); - qSort( offsets ); + dict->sortArticlesOffsetsForFTS( offsets ); QMap< QString, QVector< uint32_t > > ftsWords; @@ -851,7 +851,7 @@ void FTSResultsRequest::indexSearch( BtreeIndexing::BtreeIndex & ftsIndex, setOfOffsets.clear(); - qSort( offsets ); + dict.sortArticlesOffsetsForFTS( offsets ); checkArticles( offsets, searchWords ); } @@ -1002,7 +1002,7 @@ void FTSResultsRequest::combinedIndexSearch( BtreeIndexing::BtreeIndex & ftsInde setOfOffsets.clear(); - qSort( offsets ); + dict.sortArticlesOffsetsForFTS( offsets ); checkArticles( offsets, searchWords, regexp ); } @@ -1086,7 +1086,7 @@ void FTSResultsRequest::fullIndexSearch( BtreeIndexing::BtreeIndex & ftsIndex, setOfOffsets.clear(); - qSort( offsets ); + dict.sortArticlesOffsetsForFTS( offsets ); checkArticles( offsets, searchWords, regexp ); } @@ -1121,7 +1121,7 @@ void FTSResultsRequest::fullSearch( QStringList & searchWords, QRegExp & regexp setOfOffsets.clear(); - qSort( offsets ); + dict.sortArticlesOffsetsForFTS( offsets ); if( Qt4x5::AtomicInt::loadAcquire( isCancelled ) ) return; diff --git a/zim.cc b/zim.cc index 17a40c47..2a983e6a 100644 --- a/zim.cc +++ b/zim.cc @@ -584,6 +584,8 @@ class ZimDictionary: public BtreeIndexing::BtreeDictionary && ( fts.maxDictionarySize == 0 || getArticleCount() <= fts.maxDictionarySize ); } + virtual void sortArticlesOffsetsForFTS( QVector< uint32_t > & offsets ); + protected: virtual void loadIcon() throw(); @@ -1083,6 +1085,21 @@ void ZimDictionary::makeFTSIndex( QAtomicInt & isCancelled, bool firstIteration } } +void ZimDictionary::sortArticlesOffsetsForFTS( QVector< uint32_t > & offsets ) +{ + QVector< QPair< quint32, uint32_t > > offsetsWithClusters; + offsetsWithClusters.reserve( offsets.size() ); + + for( QVector< uint32_t >::ConstIterator it = offsets.constBegin(); + it != offsets.constEnd(); ++it ) + offsetsWithClusters.append( QPair< uint32_t, quint32 >( getArticleCluster( df, *it ), *it ) ); + + qSort( offsetsWithClusters ); + + for( int i = 0; i < offsetsWithClusters.size(); i++ ) + offsets[ i ] = offsetsWithClusters.at( i ).second; +} + void ZimDictionary::getArticleText( uint32_t articleAddress, QString & headword, QString & text ) { try