+ move 'bind' logic from datasource level to layer

+ store and copy mapnik::parameters only in mapnik::layer
+ make datasource_ptr mutable
This commit is contained in:
Artem Pavlenko 2012-03-05 13:59:12 +00:00
parent ad86e9aebc
commit 9a0248559f
6 changed files with 50 additions and 60 deletions

View file

@ -81,47 +81,25 @@ public:
Collection = 4 Collection = 4
}; };
datasource (parameters const& params) datasource (parameters const& params) {}
: params_(params),
is_bound_(false)
{}
/*!
* @brief Get the configuration parameters of the data source.
*
* These vary depending on the type of data source.
*
* @return The configuration parameters of the data source.
*/
parameters const& params() const
{
return params_;
}
/*! /*!
* @brief Get the type of the datasource * @brief Get the type of the datasource
* @return The type of the datasource (Vector or Raster) * @return The type of the datasource (Vector or Raster)
*/ */
virtual datasource_t type() const=0; virtual datasource_t type() const=0;
virtual featureset_ptr features(query const& q) const=0;
/*!
* @brief Connect to the datasource
*/
virtual void bind() const {};
virtual featureset_ptr features(const query& q) const=0;
virtual featureset_ptr features_at_point(coord2d const& pt) const=0; virtual featureset_ptr features_at_point(coord2d const& pt) const=0;
virtual box2d<double> envelope() const=0; virtual box2d<double> envelope() const=0;
virtual boost::optional<geometry_t> get_geometry_type() const=0; virtual boost::optional<geometry_t> get_geometry_type() const=0;
virtual layer_descriptor get_descriptor() const=0; virtual layer_descriptor get_descriptor() const=0;
virtual ~datasource() {}; virtual ~datasource() {};
protected:
parameters params_;
mutable bool is_bound_;
}; };
typedef boost::shared_ptr<datasource> datasource_ptr;
typedef std::string datasource_name(); typedef std::string datasource_name();
typedef datasource* create_ds(const parameters& params, bool bind); typedef datasource* create_ds(parameters const& params);
typedef void destroy_ds(datasource *ds); typedef void destroy_ds(datasource *ds);
@ -134,17 +112,14 @@ public:
} }
}; };
typedef boost::shared_ptr<datasource> datasource_ptr;
#define DATASOURCE_PLUGIN(classname) \ #define DATASOURCE_PLUGIN(classname) \
extern "C" MAPNIK_EXP std::string datasource_name() \ extern "C" MAPNIK_EXP std::string datasource_name() \
{ \ { \
return classname::name(); \ return classname::name(); \
} \ } \
extern "C" MAPNIK_EXP datasource* create(const parameters &params, bool bind) \ extern "C" MAPNIK_EXP datasource* create(parameters const& params) \
{ \ { \
return new classname(params, bind); \ return new classname(params); \
} \ } \
extern "C" MAPNIK_EXP void destroy(datasource *ds) \ extern "C" MAPNIK_EXP void destroy(datasource *ds) \
{ \ { \

View file

@ -37,6 +37,9 @@
#include <map> #include <map>
namespace mapnik { namespace mapnik {
class datasource;
class MAPNIK_DECL datasource_cache : class MAPNIK_DECL datasource_cache :
public singleton <datasource_cache,CreateStatic>, public singleton <datasource_cache,CreateStatic>,
private boost::noncopyable private boost::noncopyable
@ -55,7 +58,8 @@ public:
static std::vector<std::string> plugin_names(); static std::vector<std::string> plugin_names();
static std::string plugin_directories(); static std::string plugin_directories();
static void register_datasources(const std::string& path); static void register_datasources(const std::string& path);
static boost::shared_ptr<datasource> create(parameters const& params, bool bind=true);
static boost::shared_ptr<datasource> create(parameters const& params);
}; };
} }

View file

@ -176,13 +176,15 @@ public:
* *
* @param ds The datasource to attach. * @param ds The datasource to attach.
*/ */
void set_datasource(datasource_ptr const& ds); void set_datasource_parameters(parameters const& p);
parameters const& datasource_parameters() const;
/*! /*!
* @return the datasource attached to this layer. * @return the datasource attached to this layer.
*/ */
datasource_ptr datasource() const; datasource_ptr datasource() const;
/*! /*!
* @return the geographic envelope/bounding box of the data in the layer. * @return the geographic envelope/bounding box of the data in the layer.
*/ */
@ -203,7 +205,8 @@ private:
bool cache_features_; bool cache_features_;
std::string group_by_; std::string group_by_;
std::vector<std::string> styles_; std::vector<std::string> styles_;
datasource_ptr ds_; parameters datasource_params_;
mutable datasource_ptr ds_;
}; };
} }

View file

@ -62,7 +62,7 @@ std::map<std::string,boost::shared_ptr<PluginInfo> > datasource_cache::plugins_;
bool datasource_cache::registered_=false; bool datasource_cache::registered_=false;
std::vector<std::string> datasource_cache::plugin_directories_; std::vector<std::string> datasource_cache::plugin_directories_;
datasource_ptr datasource_cache::create(const parameters& params, bool bind) datasource_ptr datasource_cache::create(const parameters& params)
{ {
boost::optional<std::string> type = params.get<std::string>("type"); boost::optional<std::string> type = params.get<std::string>("type");
if ( ! type) if ( ! type)
@ -106,7 +106,7 @@ datasource_ptr datasource_cache::create(const parameters& params, bool bind)
std::clog << i->first << "=" << i->second << "\n"; std::clog << i->first << "=" << i->second << "\n";
} }
#endif #endif
ds=datasource_ptr(create_datasource(params, bind), datasource_deleter()); ds=datasource_ptr(create_datasource(params), datasource_deleter());
#ifdef MAPNIK_DEBUG #ifdef MAPNIK_DEBUG
std::clog<<"datasource="<<ds<<" type="<<type<<std::endl; std::clog<<"datasource="<<ds<<" type="<<type<<std::endl;

View file

@ -96,18 +96,6 @@ namespace mapnik { namespace util {
BOOST_FOREACH ( layer const& lyr_in, map_in.layers()) BOOST_FOREACH ( layer const& lyr_in, map_in.layers())
{ {
layer lyr_out(lyr_in); layer lyr_out(lyr_in);
datasource_ptr ds_in = lyr_in.datasource();
if (ds_in)
{
parameters p(ds_in->params());
// TODO : re-use datasource extent if already set.
datasource_ptr ds_out = datasource_cache::create(p);
if (ds_out)
{
lyr_out.set_datasource(ds_out);
}
}
map_out.addLayer(lyr_out); map_out.addLayer(lyr_out);
} }
typedef std::map<std::string, feature_type_style> style_cont; typedef std::map<std::string, feature_type_style> style_cont;
@ -120,7 +108,6 @@ namespace mapnik { namespace util {
feature_type_style style_out(style_in,true); // deep copy feature_type_style style_out(style_in,true); // deep copy
map_out.insert_style(kv.first, style_out); map_out.insert_style(kv.first, style_out);
} }
} }
}} }}

View file

@ -45,7 +45,7 @@ layer::layer(std::string const& name, std::string const& srs)
clear_label_cache_(false), clear_label_cache_(false),
cache_features_(false), cache_features_(false),
group_by_(""), group_by_(""),
ds_() {} datasource_params_() {}
layer::layer(const layer& rhs) layer::layer(const layer& rhs)
: name_(rhs.name_), : name_(rhs.name_),
@ -58,7 +58,7 @@ layer::layer(const layer& rhs)
cache_features_(rhs.cache_features_), cache_features_(rhs.cache_features_),
group_by_(rhs.group_by_), group_by_(rhs.group_by_),
styles_(rhs.styles_), styles_(rhs.styles_),
ds_(rhs.ds_) {} datasource_params_(rhs.datasource_params_) {}
layer& layer::operator=(const layer& rhs) layer& layer::operator=(const layer& rhs)
{ {
@ -84,7 +84,7 @@ void layer::swap(const layer& rhs)
cache_features_ = rhs.cache_features_; cache_features_ = rhs.cache_features_;
group_by_ = rhs.group_by_; group_by_ = rhs.group_by_;
styles_=rhs.styles_; styles_=rhs.styles_;
ds_=rhs.ds_; datasource_params_ = rhs.datasource_params_;
} }
layer::~layer() {} layer::~layer() {}
@ -171,17 +171,38 @@ bool layer::isQueryable() const
datasource_ptr layer::datasource() const datasource_ptr layer::datasource() const
{ {
if (ds_) return ds_;
try
{
ds_ = datasource_cache::instance()->create(datasource_params_);
}
catch (const std::exception & ex )
{
throw config_error( ex.what() );
}
catch (...)
{
throw config_error("Unknown exception occured attempting to create datasoure for layer '" + name_ + "'");
}
return ds_; return ds_;
} }
void layer::set_datasource(datasource_ptr const& ds) void layer::set_datasource_parameters(parameters const& p)
{ {
ds_ = ds; datasource_params_ = p;
}
parameters const& layer::datasource_parameters() const
{
return datasource_params_;
} }
box2d<double> layer::envelope() const box2d<double> layer::envelope() const
{ {
if (ds_) return ds_->envelope(); datasource_ptr ds = this->datasource();
if (ds) return ds->envelope();
return box2d<double>(); return box2d<double>();
} }