Skip to content
Permalink
Browse files

Add ref counting to QgsOgrConnectionPool

  • Loading branch information
manisandro committed Jul 8, 2015
1 parent 7f5b5fe commit 42137e4adc7ef15ddabe8a8a185fdefadc7d8e6a
@@ -61,7 +61,13 @@ class QgsOgrConnPoolGroup : public QObject, public QgsConnectionPoolGroup<QgsOgr
Q_OBJECT

public:
QgsOgrConnPoolGroup( QString name ) : QgsConnectionPoolGroup<QgsOgrConn*>( name ) { initTimer( this ); }
QgsOgrConnPoolGroup( QString name ) : QgsConnectionPoolGroup<QgsOgrConn*>( name ), mRefCount( 0 ) { initTimer( this ); }
void ref() { ++mRefCount; }
bool unref()
{
Q_ASSERT( mRefCount > 0 );
return --mRefCount == 0;
}

protected slots:
void handleConnectionExpired() { onConnectionExpired(); }
@@ -71,6 +77,9 @@ class QgsOgrConnPoolGroup : public QObject, public QgsConnectionPoolGroup<QgsOgr
protected:
Q_DISABLE_COPY( QgsOgrConnPoolGroup )

private:
int mRefCount;

};

/** Ogr connection pool - singleton */
@@ -79,6 +88,29 @@ class QgsOgrConnPool : public QgsConnectionPool<QgsOgrConn*, QgsOgrConnPoolGroup
public:
static QgsOgrConnPool* instance();

void ref( const QString& connInfo )
{
mMutex.lock();
T_Groups::iterator it = mGroups.find( connInfo );
if ( it == mGroups.end() )
it = mGroups.insert( connInfo, new QgsOgrConnPoolGroup( connInfo ) );
it.value()->ref();
mMutex.unlock();
}

void unref( const QString& connInfo )
{
mMutex.lock();
T_Groups::iterator it = mGroups.find( connInfo );
Q_ASSERT( it != mGroups.end() );
if ( it.value()->unref() )
{
delete it.value();
mGroups.erase( it );
}
mMutex.unlock();
}

protected:
Q_DISABLE_COPY( QgsOgrConnPool )

@@ -342,6 +342,12 @@ QgsOgrFeatureSource::QgsOgrFeatureSource( const QgsOgrProvider* p )
mFields = p->mAttributeFields;
mDriverName = p->ogrDriverName;
mOgrGeometryTypeFilter = wkbFlatten( p->mOgrGeometryTypeFilter );
QgsOgrConnPool::instance()->ref( mFilePath );
}

QgsOgrFeatureSource::~QgsOgrFeatureSource()
{
QgsOgrConnPool::instance()->unref( mFilePath );
}

QgsFeatureIterator QgsOgrFeatureSource::getFeatures( const QgsFeatureRequest& request )
@@ -28,6 +28,7 @@ class QgsOgrFeatureSource : public QgsAbstractFeatureSource
{
public:
QgsOgrFeatureSource( const QgsOgrProvider* p );
~QgsOgrFeatureSource();

virtual QgsFeatureIterator getFeatures( const QgsFeatureRequest& request ) override;

@@ -450,6 +450,8 @@ QgsOgrProvider::QgsOgrProvider( QString const & uri )
mNativeTypes
<< QgsVectorDataProvider::NativeType( tr( "Date & Time" ), "datetime", QVariant::DateTime );
}

QgsOgrConnPool::instance()->ref( mFilePath );
}

QgsOgrProvider::~QgsOgrProvider()
@@ -470,6 +472,8 @@ QgsOgrProvider::~QgsOgrProvider()
free( extent_ );
extent_ = 0;
}

QgsOgrConnPool::instance()->unref( mFilePath );
}

QgsAbstractFeatureSource* QgsOgrProvider::featureSource() const

0 comments on commit 42137e4

Please sign in to comment.
You can’t perform that action at this time.