Skip to content
Permalink
Browse files
Add connection pool for OGR provider
  • Loading branch information
manisandro committed May 22, 2015
1 parent 5910adf commit a64ffc495e4099759dcfce292154b15f51814ba9
@@ -1,7 +1,7 @@

SET (OGR_SRCS qgsogrprovider.cpp qgsogrdataitems.cpp qgsogrfeatureiterator.cpp qgsogrgeometrysimplifier.cpp)
SET (OGR_SRCS qgsogrprovider.cpp qgsogrdataitems.cpp qgsogrfeatureiterator.cpp qgsogrgeometrysimplifier.cpp qgsogrconnpool.cpp)

SET(OGR_MOC_HDRS qgsogrprovider.h qgsogrdataitems.h)
SET(OGR_MOC_HDRS qgsogrprovider.h qgsogrdataitems.h qgsogrconnpool.h)

########################################################
# Build
@@ -0,0 +1,34 @@
/***************************************************************************
qgsogrconnpool.cpp
---------------------
begin : May 2015
copyright : (C) 2015 by Sandro Mani
email : smani at sourcepole dot ch
***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/

#include "qgsogrconnpool.h"


QgsOgrConnPool* QgsOgrConnPool::instance()
{
static QgsOgrConnPool sInstance;
return &sInstance;
}

QgsOgrConnPool::QgsOgrConnPool() : QgsConnectionPool<QgsOgrConn*, QgsOgrConnPoolGroup>()
{
QgsDebugCall;
}

QgsOgrConnPool::~QgsOgrConnPool()
{
QgsDebugCall;
}

@@ -0,0 +1,80 @@
/***************************************************************************
qgsogrconnpool.h
---------------------
begin : May 2015
copyright : (C) 2015 by Sandro Mani
email : smani at sourcepole dot ch
***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/

#ifndef QGSOGRCONNPOOL_H
#define QGSOGRCONNPOOL_H

#include "qgsconnectionpool.h"
#include <ogr_api.h>


struct QgsOgrConn
{
QString path;
OGRDataSourceH ds;
};

inline QString qgsConnectionPool_ConnectionToName( QgsOgrConn* c )
{
return c->path;
}

inline void qgsConnectionPool_ConnectionCreate( QString connInfo, QgsOgrConn*& c )
{
c = new QgsOgrConn;
c->ds = OGROpen( connInfo.toUtf8().constData(), false, NULL );
c->path = connInfo;
}

inline void qgsConnectionPool_ConnectionDestroy( QgsOgrConn* c )
{
OGR_DS_Destroy( c->ds );
delete c;
}


class QgsOgrConnPoolGroup : public QObject, public QgsConnectionPoolGroup<QgsOgrConn*>
{
Q_OBJECT

public:
QgsOgrConnPoolGroup( QString name ) : QgsConnectionPoolGroup<QgsOgrConn*>( name ) { initTimer( this ); }

protected slots:
void handleConnectionExpired() { onConnectionExpired(); }
void startExpirationTimer() { expirationTimer->start(); }
void stopExpirationTimer() { expirationTimer->stop(); }

protected:
Q_DISABLE_COPY( QgsOgrConnPoolGroup )

};

/** Ogr connection pool - singleton */
class QgsOgrConnPool : public QgsConnectionPool<QgsOgrConn*, QgsOgrConnPoolGroup>
{
public:
static QgsOgrConnPool* instance();

protected:
Q_DISABLE_COPY( QgsOgrConnPool )

private:
QgsOgrConnPool();
~QgsOgrConnPool();
};


#endif // QGSOGRCONNPOOL_H
@@ -35,27 +35,26 @@

QgsOgrFeatureIterator::QgsOgrFeatureIterator( QgsOgrFeatureSource* source, bool ownSource, const QgsFeatureRequest& request )
: QgsAbstractFeatureIteratorFromSource<QgsOgrFeatureSource>( source, ownSource, request )
, ogrDataSource( 0 )
, ogrLayer( 0 )
, mSubsetStringSet( false )
, mGeometrySimplifier( NULL )
{
mFeatureFetched = false;

ogrDataSource = OGROpen( TO8F( mSource->mFilePath ), false, NULL );
mConn = QgsOgrConnPool::instance()->acquireConnection( mSource->mFilePath );

if ( mSource->mLayerName.isNull() )
{
ogrLayer = OGR_DS_GetLayer( ogrDataSource, mSource->mLayerIndex );
ogrLayer = OGR_DS_GetLayer( mConn->ds, mSource->mLayerIndex );
}
else
{
ogrLayer = OGR_DS_GetLayerByName( ogrDataSource, TO8( mSource->mLayerName ) );
ogrLayer = OGR_DS_GetLayerByName( mConn->ds, TO8( mSource->mLayerName ) );
}

if ( !mSource->mSubsetString.isEmpty() )
{
ogrLayer = QgsOgrUtils::setSubsetString( ogrLayer, ogrDataSource, mSource->mEncoding, mSource->mSubsetString );
ogrLayer = QgsOgrUtils::setSubsetString( ogrLayer, mConn->ds, mSource->mEncoding, mSource->mSubsetString );
mSubsetStringSet = true;
}

@@ -218,13 +217,13 @@ bool QgsOgrFeatureIterator::close()

if ( mSubsetStringSet )
{
OGR_DS_ReleaseResultSet( ogrDataSource, ogrLayer );
OGR_DS_ReleaseResultSet( mConn->ds, ogrLayer );
}

OGR_DS_Destroy( ogrDataSource );
QgsOgrConnPool::instance()->releaseConnection( mConn );
mConn = 0;

mClosed = true;
ogrDataSource = 0;
return true;
}

@@ -16,6 +16,7 @@
#define QGSOGRFEATUREITERATOR_H

#include "qgsfeatureiterator.h"
#include "qgsogrconnpool.h"

#include <ogr_api.h>

@@ -71,7 +72,7 @@ class QgsOgrFeatureIterator : public QgsAbstractFeatureIteratorFromSource<QgsOgr

bool mFeatureFetched;

OGRDataSourceH ogrDataSource;
QgsOgrConn* mConn;
OGRLayerH ogrLayer;

bool mSubsetStringSet;

0 comments on commit a64ffc4

Please sign in to comment.