From b0c23a074099d5509a9136465af9614e63a6657c Mon Sep 17 00:00:00 2001 From: "Uwe L. Korn" Date: Mon, 2 Jun 2014 22:53:34 +0100 Subject: [PATCH] Add basic JS fuzzy resolve implementations --- .../database/fuzzyindex/FuzzyIndex.cpp | 19 +++++++ .../database/fuzzyindex/FuzzyIndex.h | 7 +++ .../resolvers/JSResolverHelper.cpp | 53 +++++++++++++++++++ src/libtomahawk/resolvers/JSResolverHelper.h | 11 ++++ src/libtomahawk/resolvers/JSResolver_p.h | 2 + 5 files changed, 92 insertions(+) diff --git a/src/libtomahawk/database/fuzzyindex/FuzzyIndex.cpp b/src/libtomahawk/database/fuzzyindex/FuzzyIndex.cpp index ad43e6ef0..977ff806e 100644 --- a/src/libtomahawk/database/fuzzyindex/FuzzyIndex.cpp +++ b/src/libtomahawk/database/fuzzyindex/FuzzyIndex.cpp @@ -195,6 +195,25 @@ FuzzyIndex::appendFields( const Tomahawk::IndexData& data ) } +void +FuzzyIndex::deleteIndex() +{ + if ( m_luceneReader != 0 ) + { + tDebug( LOGVERBOSE ) << "Deleting old lucene stuff."; + + m_luceneSearcher->close(); + m_luceneReader->close(); + delete m_luceneSearcher; + delete m_luceneReader; + m_luceneSearcher = 0; + m_luceneReader = 0; + } + + TomahawkUtils::removeDirectory( m_lucenePath ); +} + + void FuzzyIndex::loadLuceneIndex() { diff --git a/src/libtomahawk/database/fuzzyindex/FuzzyIndex.h b/src/libtomahawk/database/fuzzyindex/FuzzyIndex.h index 6db7e5b8d..d7351d59f 100644 --- a/src/libtomahawk/database/fuzzyindex/FuzzyIndex.h +++ b/src/libtomahawk/database/fuzzyindex/FuzzyIndex.h @@ -61,6 +61,13 @@ public: void endIndexing(); void appendFields( const Tomahawk::IndexData& data ); + /** + * Delete the index from the harddrive. + * + * You should no longer use this FuzzyIndex object after this call. + */ + void deleteIndex(); + virtual void updateIndex() = 0; signals: diff --git a/src/libtomahawk/resolvers/JSResolverHelper.cpp b/src/libtomahawk/resolvers/JSResolverHelper.cpp index 191dd28d3..8f4ad5f99 100644 --- a/src/libtomahawk/resolvers/JSResolverHelper.cpp +++ b/src/libtomahawk/resolvers/JSResolverHelper.cpp @@ -487,6 +487,59 @@ JSResolverHelper::reportStreamUrl( const QString& qid, } +bool +JSResolverHelper::hasFuzzyIndex() +{ + return !m_resolver->d_func()->fuzzyIndex.isNull(); +} + + +void +JSResolverHelper::createFuzzyIndex( const QVariantList& list ) +{ + // TODO +} + + +void +JSResolverHelper::addToFuzzyIndex( const QVariantList& list ) +{ + // TODO +} + + +QMap +JSResolverHelper::searchFuzzyIndex( const QString& query ) +{ + if ( m_resolver->d_func()->fuzzyIndex ) + { + return m_resolver->d_func()->fuzzyIndex->search( Query::get( query, QString() ) ); + } + return QMap(); +} + + +QMap +JSResolverHelper::resolveFromFuzzyIndex( const QString& artist, const QString& album, const QString& track ) +{ + if ( m_resolver->d_func()->fuzzyIndex ) + { + // Important: Do not autoresolve! + query_ptr query = Query::get( artist, album, track, QString(), false ); + return m_resolver->d_func()->fuzzyIndex->search( query ); + } + return QMap(); +} + + +void +JSResolverHelper::deleteFuzzyIndex() +{ + m_resolver->d_func()->fuzzyIndex->deleteIndex(); + m_resolver->d_func()->fuzzyIndex->deleteLater(); +} + + void JSResolverHelper::returnStreamUrl( const QString& streamUrl, const QMap& headers, boost::function< void( const QString&, QSharedPointer< QIODevice >& ) > callback ) diff --git a/src/libtomahawk/resolvers/JSResolverHelper.h b/src/libtomahawk/resolvers/JSResolverHelper.h index aed83b654..474c8daab 100644 --- a/src/libtomahawk/resolvers/JSResolverHelper.h +++ b/src/libtomahawk/resolvers/JSResolverHelper.h @@ -47,6 +47,17 @@ public: Q_INVOKABLE void reportStreamUrl( const QString& qid, const QString& streamUrl ); Q_INVOKABLE void reportStreamUrl( const QString& qid, const QString& streamUrl, const QVariantMap& headers ); + /** + * Clucene indices for JS resolvers + **/ + + Q_INVOKABLE bool hasFuzzyIndex(); + Q_INVOKABLE void createFuzzyIndex( const QVariantList& list ); + Q_INVOKABLE void addToFuzzyIndex( const QVariantList& list ); + Q_INVOKABLE QMap searchFuzzyIndex( const QString& query ); + Q_INVOKABLE QMap resolveFromFuzzyIndex( const QString& artist, const QString& album, const QString& tracks ); + Q_INVOKABLE void deleteFuzzyIndex(); + void customIODeviceFactory( const Tomahawk::result_ptr&, const QString& url, boost::function< void( const QString&, QSharedPointer< QIODevice >& ) > callback ); // async diff --git a/src/libtomahawk/resolvers/JSResolver_p.h b/src/libtomahawk/resolvers/JSResolver_p.h index 6c0d85af9..0850ac59d 100644 --- a/src/libtomahawk/resolvers/JSResolver_p.h +++ b/src/libtomahawk/resolvers/JSResolver_p.h @@ -25,6 +25,7 @@ #include "JSResolver.h" #include "JSResolverHelper.h" +#include "database/fuzzyindex/FuzzyIndex.h" class JSResolverPrivate { @@ -57,6 +58,7 @@ private: Tomahawk::ExternalResolver::ErrorState error; JSResolverHelper* resolverHelper; + QScopedPointer fuzzyIndex; QPointer< AccountConfigWidget > configWidget; QList< QVariant > dataWidgets; QStringList requiredScriptPaths;