Skip to content

Commit 7534b79

Browse files
m-kuhnjef-n
authored andcommittedMar 27, 2013
Add indexable vector layer cache
1 parent 4e0ae1b commit 7534b79

18 files changed

+970
-78
lines changed
 

‎src/core/CMakeLists.txt

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,9 @@ SET(QGIS_CORE_SRCS
4949
qgis.cpp
5050
qgsapplication.cpp
5151
qgsattributeaction.cpp
52+
qgscachedfeatureiterator.cpp
53+
qgscacheindex.cpp
54+
qgscacheindexfeatureid.cpp
5255
qgsbrowsermodel.cpp
5356
qgscentralpointpositionmanager.cpp
5457
qgsclipper.cpp
@@ -68,6 +71,7 @@ SET(QGIS_CORE_SRCS
6871
qgsfeaturestore.cpp
6972
qgsfield.cpp
7073
qgsgeometry.cpp
74+
qgsgeometrycache.cpp
7175
qgsgeometryvalidator.cpp
7276
qgsgml.cpp
7377
qgsgmlschema.cpp
@@ -113,6 +117,7 @@ SET(QGIS_CORE_SRCS
113117
qgscoordinatereferencesystem.cpp
114118
qgstolerance.cpp
115119
qgsvectordataprovider.cpp
120+
qgsvectorlayercache.cpp
116121
qgsvectorfilewriter.cpp
117122
qgsvectorlayer.cpp
118123
qgsvectorlayercache.cpp
@@ -318,6 +323,7 @@ SET(QGIS_CORE_MOC_HDRS
318323
qgsvectorlayereditbuffer.h
319324
qgsnetworkaccessmanager.h
320325
qgsvectordataprovider.h
326+
qgsvectorlayercache.h
321327
qgsgeometryvalidator.h
322328

323329
composer/qgsaddremoveitemcommand.h
@@ -375,6 +381,9 @@ SET(QGIS_CORE_HDRS
375381
qgis.h
376382
qgsapplication.h
377383
qgsattributeaction.h
384+
qgscachedfeatureiterator.h
385+
qgscacheindex.h
386+
qgscacheindexfeatureid.h
378387
qgscentralpointpositionmanager.h
379388
qgsclipper.h
380389
qgscontexthelp.h
@@ -394,6 +403,7 @@ SET(QGIS_CORE_HDRS
394403
qgsgeometry.h
395404
qgsgml.h
396405
qgsgmlschema.h
406+
qgsgeometrycache.h
397407
qgshttptransaction.h
398408
qgslabel.h
399409
qgslabelattributes.h
@@ -432,6 +442,7 @@ SET(QGIS_CORE_HDRS
432442
qgssnapper.h
433443
qgscoordinatereferencesystem.h
434444
qgsvectordataprovider.h
445+
qgsvectorlayercache.h
435446
qgsvectorfilewriter.h
436447
qgsvectorlayer.h
437448
qgsvectorlayercache.h

‎src/core/qgscachedfeatureiterator.cpp

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
/***************************************************************************
2+
qgscachedfeatureiterator.cpp
3+
--------------------------------------
4+
Date : 12.2.2013
5+
Copyright : (C) 2013 Matthias Kuhn
6+
Email : matthias dot kuhn at gmx 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 "qgscachedfeatureiterator.h"
17+
#include "qgsvectorlayercache.h"
18+
#include "qgsvectordataprovider.h"
19+
20+
QgsCachedFeatureIterator::QgsCachedFeatureIterator( QgsVectorLayerCache *vlCache, QgsFeatureRequest featureRequest, QgsFeatureIds featureIds )
21+
: QgsAbstractFeatureIterator( featureRequest ),
22+
mFeatureIds( featureIds ),
23+
mVectorLayerCache( vlCache )
24+
{
25+
mFeatureIdIterator = featureIds.begin();
26+
}
27+
28+
bool QgsCachedFeatureIterator::nextFeature( QgsFeature& f )
29+
{
30+
mFeatureIdIterator++;
31+
32+
if ( mFeatureIdIterator == mFeatureIds.end() )
33+
{
34+
return false;
35+
}
36+
else
37+
{
38+
f = QgsFeature( *mVectorLayerCache->mCache[*mFeatureIdIterator]->feature() );
39+
return true;
40+
}
41+
}
42+
43+
bool QgsCachedFeatureIterator::rewind()
44+
{
45+
mFeatureIdIterator = mFeatureIds.begin();
46+
return true;
47+
}
48+
49+
bool QgsCachedFeatureIterator::close()
50+
{
51+
// Nothing to clean...
52+
return true;
53+
}
54+
55+
QgsCachedFeatureWriterIterator::QgsCachedFeatureWriterIterator( QgsVectorLayerCache *vlCache, QgsFeatureRequest featureRequest )
56+
: QgsAbstractFeatureIterator( featureRequest ),
57+
mVectorLayerCache( vlCache )
58+
{
59+
mFeatIt = vlCache->layer()->dataProvider()->getFeatures( featureRequest );
60+
}
61+
62+
bool QgsCachedFeatureWriterIterator::nextFeature( QgsFeature& f )
63+
{
64+
if ( mFeatIt.nextFeature( f ) )
65+
{
66+
// As long as features can be fetched from the provider: Write them to cache
67+
mVectorLayerCache->cacheFeature( f );
68+
mFids.insert( f.id() );
69+
return true;
70+
}
71+
else
72+
{
73+
// Once no more features can be fetched: Inform the cache, that
74+
// the request has been completed
75+
mVectorLayerCache->requestCompleted( mRequest, mFids );
76+
return false;
77+
}
78+
}
79+
80+
bool QgsCachedFeatureWriterIterator::rewind()
81+
{
82+
mFids.clear();
83+
return mFeatIt.rewind();
84+
}
85+
86+
bool QgsCachedFeatureWriterIterator::close()
87+
{
88+
return mFeatIt.close();
89+
}

‎src/core/qgscachedfeatureiterator.h

Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
/***************************************************************************
2+
qgscachedfeatureiterator.h
3+
--------------------------------------
4+
Date : 12.2.2013
5+
Copyright : (C) 2013 Matthias Kuhn
6+
Email : matthias dot kuhn at gmx 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 QGSCACHEDFEATUREITERATOR_H
17+
#define QGSCACHEDFEATUREITERATOR_H
18+
19+
#include "qgsfeature.h"
20+
#include "qgsfeatureiterator.h"
21+
22+
class QgsVectorLayerCache;
23+
24+
/**
25+
* @brief
26+
* Delivers features from the cache
27+
*
28+
*/
29+
class CORE_EXPORT QgsCachedFeatureIterator : public QgsAbstractFeatureIterator
30+
{
31+
public:
32+
/**
33+
* @brief
34+
* This constructor creates a feature iterator, that delivers only cached information, based on the
35+
* {@link QgsFeatureIds}. No request is made to the backend.
36+
*
37+
* @param vlCache The vector layer cache to use
38+
* @param featureRequest The feature request to answer
39+
* @param featureIds The feature ids to return
40+
*/
41+
QgsCachedFeatureIterator( QgsVectorLayerCache* vlCache, QgsFeatureRequest featureRequest, QgsFeatureIds featureIds );
42+
43+
/**
44+
* @brief
45+
*
46+
* @param f
47+
* @return bool
48+
*/
49+
virtual bool nextFeature( QgsFeature& f );
50+
51+
/**
52+
* @brief
53+
*
54+
* @return bool
55+
*/
56+
virtual bool rewind();
57+
58+
/**
59+
* @brief
60+
*
61+
* @return bool
62+
*/
63+
virtual bool close();
64+
65+
private:
66+
QgsFeatureIds mFeatureIds;
67+
QgsVectorLayerCache* mVectorLayerCache;
68+
QgsFeatureIds::Iterator mFeatureIdIterator;
69+
};
70+
71+
/**
72+
* @brief
73+
* Uses another iterator as backend and writes features to the cache
74+
*
75+
*/
76+
class CORE_EXPORT QgsCachedFeatureWriterIterator : public QgsAbstractFeatureIterator
77+
{
78+
public:
79+
/**
80+
* @brief
81+
* This constructor creates a feature iterator, which queries the backend and caches retrieved features.
82+
*
83+
* @param vlCache The vector layer cache to use
84+
* @param featureRequest The feature request to answer
85+
*/
86+
QgsCachedFeatureWriterIterator( QgsVectorLayerCache* vlCache, QgsFeatureRequest featureRequest );
87+
88+
/**
89+
* @brief
90+
*
91+
* @param f
92+
* @return bool
93+
*/
94+
virtual bool nextFeature( QgsFeature& f );
95+
96+
/**
97+
* @brief
98+
*
99+
* @return bool
100+
*/
101+
virtual bool rewind();
102+
103+
/**
104+
* @brief
105+
*
106+
* @return bool
107+
*/
108+
virtual bool close();
109+
110+
private:
111+
QgsFeatureIterator mFeatIt;
112+
QgsVectorLayerCache* mVectorLayerCache;
113+
QgsFeatureIds mFids;
114+
};
115+
#endif // QGSCACHEDFEATUREITERATOR_H

‎src/core/qgscacheindex.cpp

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/***************************************************************************
2+
qgscacheindex.cpp
3+
--------------------------------------
4+
Date : 13.2.2013
5+
Copyright : (C) 2013 Matthias Kuhn
6+
Email : matthias dot kuhn at gmx 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 "qgscacheindex.h"
17+
#include "qgsfeaturerequest.h"
18+
19+
QgsAbstractCacheIndex::QgsAbstractCacheIndex()
20+
{
21+
}
22+
23+
QgsAbstractCacheIndex::~QgsAbstractCacheIndex()
24+
{
25+
}
26+
27+
void QgsAbstractCacheIndex::requestCompleted( QgsFeatureRequest featureRequest, QgsFeatureIds fids )
28+
{
29+
Q_UNUSED( featureRequest )
30+
Q_UNUSED( fids )
31+
}

‎src/core/qgscacheindex.h

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
/***************************************************************************
2+
qgscacheindex.h
3+
--------------------------------------
4+
Date : 13.2.2013
5+
Copyright : (C) 2013 Matthias Kuhn
6+
Email : matthias dot kuhn at gmx 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 QGSCACHEINDEX_H
17+
#define QGSCACHEINDEX_H
18+
19+
#include "qgsfeature.h" // QgsFeatureIds
20+
21+
class QgsFeatureRequest;
22+
23+
/**
24+
* @brief
25+
* Abstract base class for cache indices
26+
*/
27+
28+
class QgsAbstractCacheIndex
29+
{
30+
public:
31+
QgsAbstractCacheIndex();
32+
virtual ~QgsAbstractCacheIndex();
33+
34+
/**
35+
* Is called, whenever a feature is removed from the cache. You should update your indexes, so
36+
* they become invalid in case this feature was required to successfuly answer a request.
37+
*/
38+
virtual void flushFeature( const QgsFeatureId fid ) = 0;
39+
40+
/**
41+
* Sometimes, the whole cache changes its state and its easier to just withdraw everything.
42+
* In this case, this method is issued. Be sure to clear all cache information in here.
43+
*/
44+
virtual void flush() = 0;
45+
46+
/**
47+
* @brief
48+
* Implement this method to update the the indices, in case you need information contained by the request
49+
* to properly index. (E.g. spatial index)
50+
* Does nothing by default
51+
*
52+
* @param featureRequest The feature request that was answered
53+
* @param fids The feature ids that have been returned
54+
*/
55+
virtual void requestCompleted( QgsFeatureRequest featureRequest, QgsFeatureIds fids );
56+
57+
/**
58+
* Is called, when a feature request is issued on a cached layer.
59+
* If this cache index is able to completely answer the feature request, it will return true
60+
* and write the list of feature ids of cached features to cachedFeatures. If it is not able
61+
* it will return false and the cachedFeatures state is undefined.
62+
*
63+
* @param cachedFeatures A reference to {@link QgsFeatureIds}, where a list of ids is written to,
64+
* in case this index is able to answer the request.
65+
* @param featureRequest The feature request, for which this index is queried.
66+
*
67+
* @return True, if this index holds the information to answer the request.
68+
*
69+
*/
70+
virtual bool getCachedIds( QgsFeatureIds& cachedFeatures, const QgsFeatureRequest& featureRequest ) = 0;
71+
};
72+
73+
#endif // QGSCACHEINDEX_H

‎src/core/qgscacheindexfeatureid.cpp

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/***************************************************************************
2+
qgscacheindexfeatureid.cpp
3+
--------------------------------------
4+
Date : 13.2.2013
5+
Copyright : (C) 2013 Matthias Kuhn
6+
Email : matthias dot kuhn at gmx 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 "qgscacheindexfeatureid.h"
17+
18+
QgsCacheIndexFeatureId::QgsCacheIndexFeatureId( QgsCachedVectorLayer* cachedVectorLayer ) :
19+
QgsAbstractCacheIndex(),
20+
C( cachedVectorLayer )
21+
{
22+
23+
}
24+

0 commit comments

Comments
 (0)
Please sign in to comment.