Skip to content

Commit

Permalink
* Fixed race condition during resolving.
Browse files Browse the repository at this point in the history
  • Loading branch information
muesli committed Mar 17, 2012
1 parent d5aed7b commit b70114a
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 20 deletions.
40 changes: 22 additions & 18 deletions src/libtomahawk/database/databasecommand_resolve.cpp
Expand Up @@ -55,9 +55,6 @@ DatabaseCommand_Resolve::exec( DatabaseImpl* lib )
qDebug() << "Using result-hint to speed up resolving:" << m_query->resultHint();

Tomahawk::result_ptr result = lib->resultFromHint( m_query );
/* qDebug() << "Result null:" << result.isNull();
* qDebug() << "Collection null:" << result->collection().isNull();
* qDebug() << "Source null:" << result->collection()->source().isNull();*/
if ( !result.isNull() && !result->collection().isNull() && result->collection()->source()->isOnline() )
{
QList<Tomahawk::result_ptr> res;
Expand Down Expand Up @@ -137,7 +134,7 @@ DatabaseCommand_Resolve::resolve( DatabaseImpl* lib )
else
{
s = SourceList::instance()->get( files_query.value( 16 ).toUInt() );
if( s.isNull() )
if ( s.isNull() )
{
qDebug() << "Could not find source" << files_query.value( 16 ).toUInt();
continue;
Expand All @@ -147,12 +144,15 @@ DatabaseCommand_Resolve::resolve( DatabaseImpl* lib )
}

Tomahawk::result_ptr result = Tomahawk::Result::get( url );
Tomahawk::artist_ptr artist =
Tomahawk::Artist::get( files_query.value( 18 ).toUInt(), files_query.value( 12 ).toString() );
Tomahawk::album_ptr album =
Tomahawk::Album::get( files_query.value( 19 ).toUInt(), files_query.value( 13 ).toString(), artist );
Tomahawk::artist_ptr composer =
Tomahawk::Artist::get( files_query.value( 20 ).toUInt(), files_query.value( 15 ).toString() );
if ( result->isValid() )
{
qDebug() << "Result already cached:" << result->toString();
continue;
}

Tomahawk::artist_ptr artist = Tomahawk::Artist::get( files_query.value( 18 ).toUInt(), files_query.value( 12 ).toString() );
Tomahawk::album_ptr album = Tomahawk::Album::get( files_query.value( 19 ).toUInt(), files_query.value( 13 ).toString(), artist );
Tomahawk::artist_ptr composer = Tomahawk::Artist::get( files_query.value( 20 ).toUInt(), files_query.value( 15 ).toString() );

result->setModificationTime( files_query.value( 1 ).toUInt() );
result->setSize( files_query.value( 2 ).toUInt() );
Expand Down Expand Up @@ -181,6 +181,7 @@ DatabaseCommand_Resolve::resolve( DatabaseImpl* lib )

result->setAttributes( attr );
result->setCollection( s->collection() );

res << result;
}

Expand Down Expand Up @@ -270,7 +271,7 @@ DatabaseCommand_Resolve::fullTextResolve( DatabaseImpl* lib )
else
{
s = SourceList::instance()->get( files_query.value( 16 ).toUInt() );
if( s.isNull() )
if ( s.isNull() )
{
qDebug() << "Could not find source" << files_query.value( 16 ).toUInt();
continue;
Expand All @@ -280,12 +281,15 @@ DatabaseCommand_Resolve::fullTextResolve( DatabaseImpl* lib )
}

Tomahawk::result_ptr result = Tomahawk::Result::get( url );
Tomahawk::artist_ptr artist =
Tomahawk::Artist::get( files_query.value( 18 ).toUInt(), files_query.value( 12 ).toString() );
Tomahawk::album_ptr album =
Tomahawk::Album::get( files_query.value( 19 ).toUInt(), files_query.value( 13 ).toString(), artist );
Tomahawk::artist_ptr composer =
Tomahawk::Artist::get( files_query.value( 20 ).toUInt(), files_query.value( 15 ).toString() );
if ( result->isValid() )
{
qDebug() << "Result already cached:" << result->toString();
continue;
}

Tomahawk::artist_ptr artist = Tomahawk::Artist::get( files_query.value( 18 ).toUInt(), files_query.value( 12 ).toString() );
Tomahawk::album_ptr album = Tomahawk::Album::get( files_query.value( 19 ).toUInt(), files_query.value( 13 ).toString(), artist );
Tomahawk::artist_ptr composer = Tomahawk::Artist::get( files_query.value( 20 ).toUInt(), files_query.value( 15 ).toString() );

result->setModificationTime( files_query.value( 1 ).toUInt() );
result->setSize( files_query.value( 2 ).toUInt() );
Expand Down Expand Up @@ -322,8 +326,8 @@ DatabaseCommand_Resolve::fullTextResolve( DatabaseImpl* lib )
}

result->setAttributes( attr );

result->setCollection( s->collection() );

res << result;
}

Expand Down
18 changes: 17 additions & 1 deletion src/libtomahawk/result.cpp
Expand Up @@ -43,7 +43,7 @@ Result::get( const QString& url )
return s_results.value( url );
}

result_ptr r = result_ptr( new Result( url ), &QObject::deleteLater );
result_ptr r = result_ptr( new Result( url ), &Result::deleteLater );
s_results.insert( url, r );

return r;
Expand All @@ -68,12 +68,28 @@ Result::Result( const QString& url )


Result::~Result()
{
}


void
Result::deleteLater()
{
QMutexLocker lock( &s_mutex );

if ( s_results.contains( m_url ) )
{
s_results.remove( m_url );
}

QObject::deleteLater();
}


bool
Result::isValid() const
{
return !m_rid.isEmpty();
}


Expand Down
6 changes: 5 additions & 1 deletion src/libtomahawk/result.h
Expand Up @@ -58,6 +58,7 @@ friend class ::DatabaseCommand_LoadFile;
static Tomahawk::result_ptr get( const QString& url );
virtual ~Result();

bool isValid() const;
QVariant toVariant() const;
QString toString() const;
Tomahawk::query_ptr toQuery();
Expand Down Expand Up @@ -108,14 +109,17 @@ friend class ::DatabaseCommand_LoadFile;
unsigned int trackId() const { return m_trackId; }
unsigned int fileId() const { return m_fileId; }

public slots:
void deleteLater();

signals:
// emitted when the collection this result comes from is going offline/online:
void statusChanged();

private slots:
void onOffline();
void onOnline();

private:
// private constructor
explicit Result( const QString& url );
Expand Down

0 comments on commit b70114a

Please sign in to comment.