From 7dabdc0a6822e38b07c090f2ad98b299c8719f94 Mon Sep 17 00:00:00 2001 From: Christian Muehlhaeuser Date: Thu, 7 Jun 2012 13:25:02 +0200 Subject: [PATCH] * Make FuzzyIndex robuster. --- src/libtomahawk/database/FuzzyIndex.cpp | 44 ++++++++++++++++++------- src/libtomahawk/database/FuzzyIndex.h | 7 +++- 2 files changed, 39 insertions(+), 12 deletions(-) diff --git a/src/libtomahawk/database/FuzzyIndex.cpp b/src/libtomahawk/database/FuzzyIndex.cpp index a6a117ffc..017e1bb4b 100644 --- a/src/libtomahawk/database/FuzzyIndex.cpp +++ b/src/libtomahawk/database/FuzzyIndex.cpp @@ -24,7 +24,9 @@ #include #include +#include "DatabaseCommand_UpdateSearchIndex.h" #include "DatabaseImpl.h" +#include "Database.h" #include "utils/TomahawkUtils.h" #include "utils/Logger.h" #include "Source.h" @@ -38,7 +40,7 @@ using namespace lucene::queryParser; using namespace lucene::search; -FuzzyIndex::FuzzyIndex( QObject* parent, bool wipeIndex ) +FuzzyIndex::FuzzyIndex( QObject* parent, bool wipe ) : QObject( parent ) , m_luceneReader( 0 ) , m_luceneSearcher( 0 ) @@ -56,15 +58,11 @@ FuzzyIndex::FuzzyIndex( QObject* parent, bool wipeIndex ) catch ( CLuceneError& error ) { tDebug() << "Caught CLucene error:" << error.what(); - Q_ASSERT( false ); + wipe = true; } - if ( wipeIndex ) - { - tLog( LOGVERBOSE ) << "Wiping fuzzy index..."; - beginIndexing(); - endIndexing(); - } + if ( wipe ) + wipeIndex(); } @@ -77,6 +75,27 @@ FuzzyIndex::~FuzzyIndex() } +bool +FuzzyIndex::wipeIndex() +{ + tLog( LOGVERBOSE ) << "Wiping fuzzy index..."; + beginIndexing(); + endIndexing(); + + QTimer::singleShot( 0, this, SLOT( updateIndex() ) ); + + return true; // FIXME +} + + +void +FuzzyIndex::updateIndex() +{ + DatabaseCommand* cmd = new DatabaseCommand_UpdateSearchIndex(); + Database::instance()->enqueue( QSharedPointer( cmd ) ); +} + + void FuzzyIndex::beginIndexing() { @@ -170,7 +189,8 @@ FuzzyIndex::appendFields( const QMap< unsigned int, QMap< QString, QString > >& catch( CLuceneError& error ) { tDebug() << "Caught CLucene error:" << error.what(); - Q_ASSERT( false ); + + wipeIndex(); } } @@ -263,7 +283,8 @@ FuzzyIndex::search( const Tomahawk::query_ptr& query ) catch( CLuceneError& error ) { tDebug() << "Caught CLucene error:" << error.what(); - Q_ASSERT( false ); + + wipeIndex(); } return resultsmap; @@ -317,7 +338,8 @@ FuzzyIndex::searchAlbum( const Tomahawk::query_ptr& query ) catch( CLuceneError& error ) { tDebug() << "Caught CLucene error:" << error.what(); - Q_ASSERT( false ); + + wipeIndex(); } return resultsmap; diff --git a/src/libtomahawk/database/FuzzyIndex.h b/src/libtomahawk/database/FuzzyIndex.h index f6da1aa76..e1f0be693 100644 --- a/src/libtomahawk/database/FuzzyIndex.h +++ b/src/libtomahawk/database/FuzzyIndex.h @@ -55,7 +55,7 @@ class FuzzyIndex : public QObject Q_OBJECT public: - explicit FuzzyIndex( QObject* parent, bool wipeIndex = false ); + explicit FuzzyIndex( QObject* parent, bool wipe = false ); ~FuzzyIndex(); void beginIndexing(); @@ -71,7 +71,12 @@ public slots: QMap< int, float > search( const Tomahawk::query_ptr& query ); QMap< int, float > searchAlbum( const Tomahawk::query_ptr& query ); +private slots: + void updateIndex(); + private: + bool wipeIndex(); + QMutex m_mutex; QString m_lucenePath;