Skip to content

Commit

Permalink
Add indexable vector layer cache
Browse files Browse the repository at this point in the history
  • Loading branch information
m-kuhn authored and jef-n committed Mar 27, 2013
1 parent 4e0ae1b commit 7534b79
Show file tree
Hide file tree
Showing 18 changed files with 970 additions and 78 deletions.
11 changes: 11 additions & 0 deletions src/core/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,9 @@ SET(QGIS_CORE_SRCS
qgis.cpp
qgsapplication.cpp
qgsattributeaction.cpp
qgscachedfeatureiterator.cpp
qgscacheindex.cpp
qgscacheindexfeatureid.cpp
qgsbrowsermodel.cpp
qgscentralpointpositionmanager.cpp
qgsclipper.cpp
Expand All @@ -68,6 +71,7 @@ SET(QGIS_CORE_SRCS
qgsfeaturestore.cpp
qgsfield.cpp
qgsgeometry.cpp
qgsgeometrycache.cpp
qgsgeometryvalidator.cpp
qgsgml.cpp
qgsgmlschema.cpp
Expand Down Expand Up @@ -113,6 +117,7 @@ SET(QGIS_CORE_SRCS
qgscoordinatereferencesystem.cpp
qgstolerance.cpp
qgsvectordataprovider.cpp
qgsvectorlayercache.cpp
qgsvectorfilewriter.cpp
qgsvectorlayer.cpp
qgsvectorlayercache.cpp
Expand Down Expand Up @@ -318,6 +323,7 @@ SET(QGIS_CORE_MOC_HDRS
qgsvectorlayereditbuffer.h
qgsnetworkaccessmanager.h
qgsvectordataprovider.h
qgsvectorlayercache.h
qgsgeometryvalidator.h

composer/qgsaddremoveitemcommand.h
Expand Down Expand Up @@ -375,6 +381,9 @@ SET(QGIS_CORE_HDRS
qgis.h
qgsapplication.h
qgsattributeaction.h
qgscachedfeatureiterator.h
qgscacheindex.h
qgscacheindexfeatureid.h
qgscentralpointpositionmanager.h
qgsclipper.h
qgscontexthelp.h
Expand All @@ -394,6 +403,7 @@ SET(QGIS_CORE_HDRS
qgsgeometry.h
qgsgml.h
qgsgmlschema.h
qgsgeometrycache.h
qgshttptransaction.h
qgslabel.h
qgslabelattributes.h
Expand Down Expand Up @@ -432,6 +442,7 @@ SET(QGIS_CORE_HDRS
qgssnapper.h
qgscoordinatereferencesystem.h
qgsvectordataprovider.h
qgsvectorlayercache.h
qgsvectorfilewriter.h
qgsvectorlayer.h
qgsvectorlayercache.h
Expand Down
89 changes: 89 additions & 0 deletions src/core/qgscachedfeatureiterator.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
/***************************************************************************
qgscachedfeatureiterator.cpp
--------------------------------------
Date : 12.2.2013
Copyright : (C) 2013 Matthias Kuhn
Email : matthias dot kuhn at gmx 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 "qgscachedfeatureiterator.h"
#include "qgsvectorlayercache.h"
#include "qgsvectordataprovider.h"

QgsCachedFeatureIterator::QgsCachedFeatureIterator( QgsVectorLayerCache *vlCache, QgsFeatureRequest featureRequest, QgsFeatureIds featureIds )
: QgsAbstractFeatureIterator( featureRequest ),
mFeatureIds( featureIds ),
mVectorLayerCache( vlCache )
{
mFeatureIdIterator = featureIds.begin();
}

bool QgsCachedFeatureIterator::nextFeature( QgsFeature& f )
{
mFeatureIdIterator++;

if ( mFeatureIdIterator == mFeatureIds.end() )
{
return false;
}
else
{
f = QgsFeature( *mVectorLayerCache->mCache[*mFeatureIdIterator]->feature() );
return true;
}
}

bool QgsCachedFeatureIterator::rewind()
{
mFeatureIdIterator = mFeatureIds.begin();
return true;
}

bool QgsCachedFeatureIterator::close()
{
// Nothing to clean...
return true;
}

QgsCachedFeatureWriterIterator::QgsCachedFeatureWriterIterator( QgsVectorLayerCache *vlCache, QgsFeatureRequest featureRequest )
: QgsAbstractFeatureIterator( featureRequest ),
mVectorLayerCache( vlCache )
{
mFeatIt = vlCache->layer()->dataProvider()->getFeatures( featureRequest );
}

bool QgsCachedFeatureWriterIterator::nextFeature( QgsFeature& f )
{
if ( mFeatIt.nextFeature( f ) )
{
// As long as features can be fetched from the provider: Write them to cache
mVectorLayerCache->cacheFeature( f );
mFids.insert( f.id() );
return true;
}
else
{
// Once no more features can be fetched: Inform the cache, that
// the request has been completed
mVectorLayerCache->requestCompleted( mRequest, mFids );
return false;
}
}

bool QgsCachedFeatureWriterIterator::rewind()
{
mFids.clear();
return mFeatIt.rewind();
}

bool QgsCachedFeatureWriterIterator::close()
{
return mFeatIt.close();
}
115 changes: 115 additions & 0 deletions src/core/qgscachedfeatureiterator.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
/***************************************************************************
qgscachedfeatureiterator.h
--------------------------------------
Date : 12.2.2013
Copyright : (C) 2013 Matthias Kuhn
Email : matthias dot kuhn at gmx 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 QGSCACHEDFEATUREITERATOR_H
#define QGSCACHEDFEATUREITERATOR_H

#include "qgsfeature.h"
#include "qgsfeatureiterator.h"

class QgsVectorLayerCache;

/**
* @brief
* Delivers features from the cache
*
*/
class CORE_EXPORT QgsCachedFeatureIterator : public QgsAbstractFeatureIterator
{
public:
/**
* @brief
* This constructor creates a feature iterator, that delivers only cached information, based on the
* {@link QgsFeatureIds}. No request is made to the backend.
*
* @param vlCache The vector layer cache to use
* @param featureRequest The feature request to answer
* @param featureIds The feature ids to return
*/
QgsCachedFeatureIterator( QgsVectorLayerCache* vlCache, QgsFeatureRequest featureRequest, QgsFeatureIds featureIds );

/**
* @brief
*
* @param f
* @return bool
*/
virtual bool nextFeature( QgsFeature& f );

/**
* @brief
*
* @return bool
*/
virtual bool rewind();

/**
* @brief
*
* @return bool
*/
virtual bool close();

private:
QgsFeatureIds mFeatureIds;
QgsVectorLayerCache* mVectorLayerCache;
QgsFeatureIds::Iterator mFeatureIdIterator;
};

/**
* @brief
* Uses another iterator as backend and writes features to the cache
*
*/
class CORE_EXPORT QgsCachedFeatureWriterIterator : public QgsAbstractFeatureIterator
{
public:
/**
* @brief
* This constructor creates a feature iterator, which queries the backend and caches retrieved features.
*
* @param vlCache The vector layer cache to use
* @param featureRequest The feature request to answer
*/
QgsCachedFeatureWriterIterator( QgsVectorLayerCache* vlCache, QgsFeatureRequest featureRequest );

/**
* @brief
*
* @param f
* @return bool
*/
virtual bool nextFeature( QgsFeature& f );

/**
* @brief
*
* @return bool
*/
virtual bool rewind();

/**
* @brief
*
* @return bool
*/
virtual bool close();

private:
QgsFeatureIterator mFeatIt;
QgsVectorLayerCache* mVectorLayerCache;
QgsFeatureIds mFids;
};
#endif // QGSCACHEDFEATUREITERATOR_H
31 changes: 31 additions & 0 deletions src/core/qgscacheindex.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/***************************************************************************
qgscacheindex.cpp
--------------------------------------
Date : 13.2.2013
Copyright : (C) 2013 Matthias Kuhn
Email : matthias dot kuhn at gmx 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 "qgscacheindex.h"
#include "qgsfeaturerequest.h"

QgsAbstractCacheIndex::QgsAbstractCacheIndex()
{
}

QgsAbstractCacheIndex::~QgsAbstractCacheIndex()
{
}

void QgsAbstractCacheIndex::requestCompleted( QgsFeatureRequest featureRequest, QgsFeatureIds fids )
{
Q_UNUSED( featureRequest )
Q_UNUSED( fids )
}
73 changes: 73 additions & 0 deletions src/core/qgscacheindex.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
/***************************************************************************
qgscacheindex.h
--------------------------------------
Date : 13.2.2013
Copyright : (C) 2013 Matthias Kuhn
Email : matthias dot kuhn at gmx 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 QGSCACHEINDEX_H
#define QGSCACHEINDEX_H

#include "qgsfeature.h" // QgsFeatureIds

class QgsFeatureRequest;

/**
* @brief
* Abstract base class for cache indices
*/

class QgsAbstractCacheIndex
{
public:
QgsAbstractCacheIndex();
virtual ~QgsAbstractCacheIndex();

/**
* Is called, whenever a feature is removed from the cache. You should update your indexes, so
* they become invalid in case this feature was required to successfuly answer a request.
*/
virtual void flushFeature( const QgsFeatureId fid ) = 0;

/**
* Sometimes, the whole cache changes its state and its easier to just withdraw everything.
* In this case, this method is issued. Be sure to clear all cache information in here.
*/
virtual void flush() = 0;

/**
* @brief
* Implement this method to update the the indices, in case you need information contained by the request
* to properly index. (E.g. spatial index)
* Does nothing by default
*
* @param featureRequest The feature request that was answered
* @param fids The feature ids that have been returned
*/
virtual void requestCompleted( QgsFeatureRequest featureRequest, QgsFeatureIds fids );

/**
* Is called, when a feature request is issued on a cached layer.
* If this cache index is able to completely answer the feature request, it will return true
* and write the list of feature ids of cached features to cachedFeatures. If it is not able
* it will return false and the cachedFeatures state is undefined.
*
* @param cachedFeatures A reference to {@link QgsFeatureIds}, where a list of ids is written to,
* in case this index is able to answer the request.
* @param featureRequest The feature request, for which this index is queried.
*
* @return True, if this index holds the information to answer the request.
*
*/
virtual bool getCachedIds( QgsFeatureIds& cachedFeatures, const QgsFeatureRequest& featureRequest ) = 0;
};

#endif // QGSCACHEINDEX_H
24 changes: 24 additions & 0 deletions src/core/qgscacheindexfeatureid.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/***************************************************************************
qgscacheindexfeatureid.cpp
--------------------------------------
Date : 13.2.2013
Copyright : (C) 2013 Matthias Kuhn
Email : matthias dot kuhn at gmx 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 "qgscacheindexfeatureid.h"

QgsCacheIndexFeatureId::QgsCacheIndexFeatureId( QgsCachedVectorLayer* cachedVectorLayer ) :
QgsAbstractCacheIndex(),
C( cachedVectorLayer )
{

}

Loading

0 comments on commit 7534b79

Please sign in to comment.