From a8c3207a765195e78bfd9a63a4365958e951073b Mon Sep 17 00:00:00 2001 From: YiFang Xiao Date: Sat, 5 Aug 2023 10:57:20 +0800 Subject: [PATCH] opt: full text index compact --- src/common/utils.cc | 19 +++++++++++++++++++ src/common/utils.hh | 2 ++ src/ftshelpers.cc | 6 +++++- 3 files changed, 26 insertions(+), 1 deletion(-) diff --git a/src/common/utils.cc b/src/common/utils.cc index 1aa945c7..865ed84b 100644 --- a/src/common/utils.cc +++ b/src/common/utils.cc @@ -68,4 +68,23 @@ std::string basename( std::string const & str ) return std::string( str, x + 1 ); } +void removeDirectory( QString const & directory ) +{ + QDir dir( directory ); + Q_FOREACH ( QFileInfo info, + dir.entryInfoList( QDir::NoDotAndDotDot | QDir::AllDirs | QDir::Files, QDir::DirsFirst ) ) { + if ( info.isDir() ) + removeDirectory( info.absoluteFilePath() ); + else + QFile::remove( info.absoluteFilePath() ); + } + + dir.rmdir( directory ); +} + +void removeDirectory( string const & directory ) +{ + removeDirectory( QString::fromStdString( directory ) ); +} + } // namespace Utils::Fs diff --git a/src/common/utils.hh b/src/common/utils.hh index baa3b854..79dc1730 100644 --- a/src/common/utils.hh +++ b/src/common/utils.hh @@ -332,7 +332,9 @@ char separator(); /// Returns the name part of the given filename. string basename( string const & ); +void removeDirectory( QString const & directory ); +void removeDirectory( string const & directory ); } // namespace Fs } // namespace Utils diff --git a/src/ftshelpers.cc b/src/ftshelpers.cc index 7f948f38..7519cfe5 100644 --- a/src/ftshelpers.cc +++ b/src/ftshelpers.cc @@ -105,7 +105,7 @@ void makeFTSIndex( BtreeIndexing::BtreeDictionary * dict, QAtomicInt & isCancell throw exUserAbort(); // Open the database for update, creating a new database if necessary. - Xapian::WritableDatabase db( dict->ftsIndexName(), Xapian::DB_CREATE_OR_OPEN ); + Xapian::WritableDatabase db( dict->ftsIndexName() + "_temp", Xapian::DB_CREATE_OR_OPEN ); Xapian::TermGenerator indexer; // Xapian::Stem stemmer("english"); @@ -206,6 +206,10 @@ void makeFTSIndex( BtreeIndexing::BtreeDictionary * dict, QAtomicInt & isCancell offsets.clear(); db.commit(); + + db.compact( dict->ftsIndexName() ); + + Utils::Fs::removeDirectory( dict->ftsIndexName() + "_temp" ); } catch ( Xapian::Error & e ) { qWarning() << "create xapian index:" << QString::fromStdString( e.get_description() );