Skip to content

Commit ce7b1d3

Browse files
committed
Merge pull request #2060 from manisandro/ogrpool
Add connection pool for OGR provider (followup 0b9d1dc)
2 parents f337d41 + a64ffc4 commit ce7b1d3

File tree

5 files changed

+125
-11
lines changed

5 files changed

+125
-11
lines changed

src/providers/ogr/CMakeLists.txt

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11

2-
SET (OGR_SRCS qgsogrprovider.cpp qgsogrdataitems.cpp qgsogrfeatureiterator.cpp qgsogrgeometrysimplifier.cpp)
2+
SET (OGR_SRCS qgsogrprovider.cpp qgsogrdataitems.cpp qgsogrfeatureiterator.cpp qgsogrgeometrysimplifier.cpp qgsogrconnpool.cpp)
33

4-
SET(OGR_MOC_HDRS qgsogrprovider.h qgsogrdataitems.h)
4+
SET(OGR_MOC_HDRS qgsogrprovider.h qgsogrdataitems.h qgsogrconnpool.h)
55

66
########################################################
77
# Build

src/providers/ogr/qgsogrconnpool.cpp

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/***************************************************************************
2+
qgsogrconnpool.cpp
3+
---------------------
4+
begin : May 2015
5+
copyright : (C) 2015 by Sandro Mani
6+
email : smani at sourcepole dot ch
7+
***************************************************************************
8+
* *
9+
* This program is free software; you can redistribute it and/or modify *
10+
* it under the terms of the GNU General Public License as published by *
11+
* the Free Software Foundation; either version 2 of the License, or *
12+
* (at your option) any later version. *
13+
* *
14+
***************************************************************************/
15+
16+
#include "qgsogrconnpool.h"
17+
18+
19+
QgsOgrConnPool* QgsOgrConnPool::instance()
20+
{
21+
static QgsOgrConnPool sInstance;
22+
return &sInstance;
23+
}
24+
25+
QgsOgrConnPool::QgsOgrConnPool() : QgsConnectionPool<QgsOgrConn*, QgsOgrConnPoolGroup>()
26+
{
27+
QgsDebugCall;
28+
}
29+
30+
QgsOgrConnPool::~QgsOgrConnPool()
31+
{
32+
QgsDebugCall;
33+
}
34+

src/providers/ogr/qgsogrconnpool.h

+80
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
/***************************************************************************
2+
qgsogrconnpool.h
3+
---------------------
4+
begin : May 2015
5+
copyright : (C) 2015 by Sandro Mani
6+
email : smani at sourcepole dot ch
7+
***************************************************************************
8+
* *
9+
* This program is free software; you can redistribute it and/or modify *
10+
* it under the terms of the GNU General Public License as published by *
11+
* the Free Software Foundation; either version 2 of the License, or *
12+
* (at your option) any later version. *
13+
* *
14+
***************************************************************************/
15+
16+
#ifndef QGSOGRCONNPOOL_H
17+
#define QGSOGRCONNPOOL_H
18+
19+
#include "qgsconnectionpool.h"
20+
#include <ogr_api.h>
21+
22+
23+
struct QgsOgrConn
24+
{
25+
QString path;
26+
OGRDataSourceH ds;
27+
};
28+
29+
inline QString qgsConnectionPool_ConnectionToName( QgsOgrConn* c )
30+
{
31+
return c->path;
32+
}
33+
34+
inline void qgsConnectionPool_ConnectionCreate( QString connInfo, QgsOgrConn*& c )
35+
{
36+
c = new QgsOgrConn;
37+
c->ds = OGROpen( connInfo.toUtf8().constData(), false, NULL );
38+
c->path = connInfo;
39+
}
40+
41+
inline void qgsConnectionPool_ConnectionDestroy( QgsOgrConn* c )
42+
{
43+
OGR_DS_Destroy( c->ds );
44+
delete c;
45+
}
46+
47+
48+
class QgsOgrConnPoolGroup : public QObject, public QgsConnectionPoolGroup<QgsOgrConn*>
49+
{
50+
Q_OBJECT
51+
52+
public:
53+
QgsOgrConnPoolGroup( QString name ) : QgsConnectionPoolGroup<QgsOgrConn*>( name ) { initTimer( this ); }
54+
55+
protected slots:
56+
void handleConnectionExpired() { onConnectionExpired(); }
57+
void startExpirationTimer() { expirationTimer->start(); }
58+
void stopExpirationTimer() { expirationTimer->stop(); }
59+
60+
protected:
61+
Q_DISABLE_COPY( QgsOgrConnPoolGroup )
62+
63+
};
64+
65+
/** Ogr connection pool - singleton */
66+
class QgsOgrConnPool : public QgsConnectionPool<QgsOgrConn*, QgsOgrConnPoolGroup>
67+
{
68+
public:
69+
static QgsOgrConnPool* instance();
70+
71+
protected:
72+
Q_DISABLE_COPY( QgsOgrConnPool )
73+
74+
private:
75+
QgsOgrConnPool();
76+
~QgsOgrConnPool();
77+
};
78+
79+
80+
#endif // QGSOGRCONNPOOL_H

src/providers/ogr/qgsogrfeatureiterator.cpp

+7-8
Original file line numberDiff line numberDiff line change
@@ -35,27 +35,26 @@
3535

3636
QgsOgrFeatureIterator::QgsOgrFeatureIterator( QgsOgrFeatureSource* source, bool ownSource, const QgsFeatureRequest& request )
3737
: QgsAbstractFeatureIteratorFromSource<QgsOgrFeatureSource>( source, ownSource, request )
38-
, ogrDataSource( 0 )
3938
, ogrLayer( 0 )
4039
, mSubsetStringSet( false )
4140
, mGeometrySimplifier( NULL )
4241
{
4342
mFeatureFetched = false;
4443

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

4746
if ( mSource->mLayerName.isNull() )
4847
{
49-
ogrLayer = OGR_DS_GetLayer( ogrDataSource, mSource->mLayerIndex );
48+
ogrLayer = OGR_DS_GetLayer( mConn->ds, mSource->mLayerIndex );
5049
}
5150
else
5251
{
53-
ogrLayer = OGR_DS_GetLayerByName( ogrDataSource, TO8( mSource->mLayerName ) );
52+
ogrLayer = OGR_DS_GetLayerByName( mConn->ds, TO8( mSource->mLayerName ) );
5453
}
5554

5655
if ( !mSource->mSubsetString.isEmpty() )
5756
{
58-
ogrLayer = QgsOgrUtils::setSubsetString( ogrLayer, ogrDataSource, mSource->mEncoding, mSource->mSubsetString );
57+
ogrLayer = QgsOgrUtils::setSubsetString( ogrLayer, mConn->ds, mSource->mEncoding, mSource->mSubsetString );
5958
mSubsetStringSet = true;
6059
}
6160

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

219218
if ( mSubsetStringSet )
220219
{
221-
OGR_DS_ReleaseResultSet( ogrDataSource, ogrLayer );
220+
OGR_DS_ReleaseResultSet( mConn->ds, ogrLayer );
222221
}
223222

224-
OGR_DS_Destroy( ogrDataSource );
223+
QgsOgrConnPool::instance()->releaseConnection( mConn );
224+
mConn = 0;
225225

226226
mClosed = true;
227-
ogrDataSource = 0;
228227
return true;
229228
}
230229

src/providers/ogr/qgsogrfeatureiterator.h

+2-1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
#define QGSOGRFEATUREITERATOR_H
1717

1818
#include "qgsfeatureiterator.h"
19+
#include "qgsogrconnpool.h"
1920

2021
#include <ogr_api.h>
2122

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

7273
bool mFeatureFetched;
7374

74-
OGRDataSourceH ogrDataSource;
75+
QgsOgrConn* mConn;
7576
OGRLayerH ogrLayer;
7677

7778
bool mSubsetStringSet;

0 commit comments

Comments
 (0)