From acd90042fbc0f2374db30e80b01c43e550c88a63 Mon Sep 17 00:00:00 2001 From: Dane Springmeyer Date: Fri, 2 Sep 2016 15:51:58 -0700 Subject: [PATCH] geojson.input: minor code adjustments - Avoids making copy of inline string to save memory - Moves duplicated code out of #ifdef to be more readible --- plugins/input/geojson/geojson_datasource.cpp | 31 +++++++------------- plugins/input/geojson/geojson_datasource.hpp | 2 +- 2 files changed, 11 insertions(+), 22 deletions(-) diff --git a/plugins/input/geojson/geojson_datasource.cpp b/plugins/input/geojson/geojson_datasource.cpp index 90270873d..2709c532d 100644 --- a/plugins/input/geojson/geojson_datasource.cpp +++ b/plugins/input/geojson/geojson_datasource.cpp @@ -113,18 +113,14 @@ geojson_datasource::geojson_datasource(parameters const& params) desc_(geojson_datasource::name(), *params.get("encoding","utf-8")), filename_(), - inline_string_(), + from_inline_string_(false), extent_(), features_(), tree_(nullptr), num_features_to_query_(*params.get("num_features_to_query",5)) { boost::optional inline_string = params.get("inline"); - if (inline_string) - { - inline_string_ = *inline_string; - } - else + if (!inline_string) { boost::optional file = params.get("file"); if (!file) throw mapnik::datasource_exception("GeoJSON Plugin: missing parameter"); @@ -137,10 +133,11 @@ geojson_datasource::geojson_datasource(parameters const& params) has_disk_index_ = mapnik::util::exists(filename_ + ".index"); } - if (!inline_string_.empty()) + if (inline_string) { - char const* start = inline_string_.c_str(); - char const* end = start + inline_string_.size(); + from_inline_string_ = true; + char const* start = inline_string->c_str(); + char const* end = start + inline_string->size(); parse_geojson(start, end); } else if (has_disk_index_) @@ -162,14 +159,6 @@ geojson_datasource::geojson_datasource(parameters const& params) std::fread(&file_buffer[0], file.size(), 1, file.get()); char const* start = file_buffer.c_str(); char const* end = start + file_buffer.length(); - if (cache_features_) - { - parse_geojson(start, end); - } - else - { - initialise_index(start, end); - } #else boost::optional mapped_region = mapnik::mapped_memory_cache::instance().find(filename_, false); @@ -180,6 +169,7 @@ geojson_datasource::geojson_datasource(parameters const& params) char const* start = reinterpret_cast((*mapped_region)->get_address()); char const* end = start + (*mapped_region)->get_size(); +#endif if (cache_features_) { parse_geojson(start, end); @@ -188,7 +178,6 @@ geojson_datasource::geojson_datasource(parameters const& params) { initialise_index(start, end); } -#endif } } @@ -276,7 +265,7 @@ void geojson_datasource::initialise_index(Iterator start, Iterator end) space); if (!result || itr != end) { - if (!inline_string_.empty()) throw mapnik::datasource_exception("geojson_datasource: Failed to parse GeoJSON file from in-memory string"); + if (from_inline_string_) throw mapnik::datasource_exception("geojson_datasource: Failed to parse GeoJSON file from in-memory string"); else throw mapnik::datasource_exception("geojson_datasource: Failed to parse GeoJSON file '" + filename_ + "'"); } @@ -368,7 +357,7 @@ void geojson_datasource::parse_geojson(Iterator start, Iterator end) space); if (!result || itr != end) { - if (!inline_string_.empty()) throw mapnik::datasource_exception("geojson_datasource: Failed parse GeoJSON file from in-memory string"); + if (from_inline_string_) throw mapnik::datasource_exception("geojson_datasource: Failed parse GeoJSON file from in-memory string"); else throw mapnik::datasource_exception("geojson_datasource: Failed parse GeoJSON file '" + filename_ + "'"); } } @@ -382,7 +371,7 @@ void geojson_datasource::parse_geojson(Iterator start, Iterator end) space); if (!result || itr != end) { - if (!inline_string_.empty()) throw mapnik::datasource_exception("geojson_datasource: Failed parse GeoJSON file from in-memory string"); + if (from_inline_string_) throw mapnik::datasource_exception("geojson_datasource: Failed parse GeoJSON file from in-memory string"); else throw mapnik::datasource_exception("geojson_datasource: Failed parse GeoJSON file '" + filename_ + "'"); } } diff --git a/plugins/input/geojson/geojson_datasource.hpp b/plugins/input/geojson/geojson_datasource.hpp index 5ba36a66b..e295cb1d6 100644 --- a/plugins/input/geojson/geojson_datasource.hpp +++ b/plugins/input/geojson/geojson_datasource.hpp @@ -98,7 +98,7 @@ private: mapnik::datasource::datasource_t type_; mapnik::layer_descriptor desc_; std::string filename_; - std::string inline_string_; + bool from_inline_string_; mapnik::box2d extent_; std::vector features_; std::unique_ptr tree_;