From 4ab26f687cbfc78d850e180cb3e9147b28a295ae Mon Sep 17 00:00:00 2001 From: Xiao YiFang Date: Sun, 2 Oct 2022 21:52:11 +0800 Subject: [PATCH] mdx normal index check logic --- dictionary.cc | 53 +++++++++++++++++++++++++++++++++++++++++++++++++-- dictionary.hh | 3 +++ mdx.cc | 2 +- 3 files changed, 55 insertions(+), 3 deletions(-) diff --git a/dictionary.cc b/dictionary.cc index a00de8e3..c028c214 100644 --- a/dictionary.cc +++ b/dictionary.cc @@ -579,14 +579,63 @@ bool needToRebuildIndex( vector< string > const & dictionaryFiles, return fileInfo.lastModified().toSecsSinceEpoch() < lastModified; #else - QDir d(FsEncoding::decode( indexFile.c_str() )); - if(!d.exists()){ + QFileInfo fileInfo( FsEncoding::decode( indexFile.c_str() ) ); + + if ( fileInfo.exists()&&fileInfo.isFile() ) + { + QFile::remove(FsEncoding::decode( indexFile.c_str() )); return true; } + return false; #endif } +bool needToRebuildBTreeIndex( vector< string > const & dictionaryFiles, + string const & indexFile ) noexcept +{ + unsigned long lastModified = 0; + + for( std::vector< string >::const_iterator i = dictionaryFiles.begin(); + i != dictionaryFiles.end(); ++i ) + { + QString name = FsEncoding::decode( i->c_str() ); + QFileInfo fileInfo( name ); + unsigned long ts; + + if( fileInfo.isDir() ) + continue; + + if( name.toLower().endsWith( ".zip" ) ) + { + ZipFile::SplitZipFile zf( name ); + if( !zf.exists() ) + return true; + ts = zf.lastModified().toSecsSinceEpoch(); + } + else + { + if ( !fileInfo.exists() ) + return true; + ts = fileInfo.lastModified().toSecsSinceEpoch(); + } + + if ( ts > lastModified ) + lastModified = ts; + } + + QDir d(FsEncoding::decode( indexFile.c_str() )); + if(d.exists()){ + d.removeRecursively(); + } + QFileInfo fileInfo( FsEncoding::decode( indexFile.c_str() ) ); + + if ( !fileInfo.exists() ) + return true; + + return fileInfo.lastModified().toSecsSinceEpoch() < lastModified; +} + QString generateRandomDictionaryId() { return QString( diff --git a/dictionary.hh b/dictionary.hh index 369f8300..493f158b 100644 --- a/dictionary.hh +++ b/dictionary.hh @@ -483,6 +483,9 @@ string makeDictionaryId( vector< string > const & dictionaryFiles ) noexcept; bool needToRebuildIndex( vector< string > const & dictionaryFiles, string const & indexFile ) noexcept; +bool needToRebuildBTreeIndex( vector< string > const & dictionaryFiles, + string const & indexFile ) noexcept; + /// Returns a random dictionary id useful for interactively created /// dictionaries. QString generateRandomDictionaryId(); diff --git a/mdx.cc b/mdx.cc index efe3d4dc..2c135c12 100644 --- a/mdx.cc +++ b/mdx.cc @@ -1401,7 +1401,7 @@ vector< sptr< Dictionary::Class > > makeDictionaries( vector< string > const & f string dictId = Dictionary::makeDictionaryId( dictFiles ); string indexFile = indicesDir + dictId; - if ( Dictionary::needToRebuildIndex( dictFiles, indexFile ) || + if ( Dictionary::needToRebuildBTreeIndex( dictFiles, indexFile ) || indexIsOldOrBad( dictFiles, indexFile ) ) { // Building the index