From a4715aa8df145592d3f97a4549089a1cdbeadbaf Mon Sep 17 00:00:00 2001 From: Dane Springmeyer Date: Sun, 13 Nov 2011 16:02:27 -0800 Subject: [PATCH] sqlite: disassociate extent calculation from rtree auto-index creation, and grab immediate lock on rtree table --- plugins/input/sqlite/sqlite_datasource.cpp | 18 +++--- plugins/input/sqlite/sqlite_utils.hpp | 64 +++++++++------------- 2 files changed, 37 insertions(+), 45 deletions(-) diff --git a/plugins/input/sqlite/sqlite_datasource.cpp b/plugins/input/sqlite/sqlite_datasource.cpp index 38517dd65..5516fc5ee 100644 --- a/plugins/input/sqlite/sqlite_datasource.cpp +++ b/plugins/input/sqlite/sqlite_datasource.cpp @@ -245,13 +245,9 @@ void sqlite_datasource::bind() const dataset_->execute("attach database '" + index_db + "' as " + index_table_); } has_spatial_index_ = sqlite_utils::has_rtree(index_table_,dataset_); - } - - if (! extent_initialized_ - && !has_spatial_index_ - && auto_index) - { + if (!has_spatial_index_ && auto_index) + { if (! key_field_.empty()) { std::ostringstream query; @@ -261,9 +257,14 @@ void sqlite_datasource::bind() const << " FROM (" << geometry_table_ << ")"; boost::shared_ptr rs = dataset_->execute_query(query.str()); - if (sqlite_utils::create_spatial_index(index_db,index_table_,rs,extent_)) + if (sqlite_utils::create_spatial_index(index_db,index_table_,rs)) { - extent_initialized_ = true; + //extent_initialized_ = true; + has_spatial_index_ = true; + if (boost::filesystem::exists(index_db)) + { + dataset_->execute("attach database '" + index_db + "' as " + index_table_); + } } } else @@ -275,6 +276,7 @@ void sqlite_datasource::bind() const << geometry_table_; throw datasource_exception(s.str()); } + } } if (! extent_initialized_) diff --git a/plugins/input/sqlite/sqlite_utils.hpp b/plugins/input/sqlite/sqlite_utils.hpp index e3bf4d601..8d289c528 100644 --- a/plugins/input/sqlite/sqlite_utils.hpp +++ b/plugins/input/sqlite/sqlite_utils.hpp @@ -182,8 +182,7 @@ public: static bool create_spatial_index(std::string const& index_db, std::string const& index_table, - boost::shared_ptr rs, - mapnik::box2d& extent) + boost::shared_ptr rs) { /* TODO - speedups @@ -203,34 +202,33 @@ public: bool existed = boost::filesystem::exists(index_db); boost::shared_ptr ds = boost::make_shared(index_db,flags); - ds->execute("PRAGMA synchronous=OFF"); - ds->execute("BEGIN TRANSACTION"); - - // first drop the index if it already exists - std::ostringstream spatial_index_drop_sql; - spatial_index_drop_sql << "DROP TABLE IF EXISTS " << index_table; - ds->execute(spatial_index_drop_sql.str()); - - // create the spatial index - std::ostringstream create_idx; - create_idx << "create virtual table " - << index_table - << " using rtree(pkid, xmin, xmax, ymin, ymax)"; - - // insert for prepared statement - std::ostringstream insert_idx; - insert_idx << "insert into " - << index_table - << " values (?,?,?,?,?)"; - - ds->execute(create_idx.str()); - - prepared_index_statement ps(ds,insert_idx.str()); - bool one_success = false; try { - bool first = true; + ds->execute("PRAGMA synchronous=OFF"); + ds->execute("BEGIN IMMEDIATE TRANSACTION"); + + // first drop the index if it already exists + std::ostringstream spatial_index_drop_sql; + spatial_index_drop_sql << "DROP TABLE IF EXISTS " << index_table; + ds->execute(spatial_index_drop_sql.str()); + + // create the spatial index + std::ostringstream create_idx; + create_idx << "create virtual table " + << index_table + << " using rtree(pkid, xmin, xmax, ymin, ymax)"; + + // insert for prepared statement + std::ostringstream insert_idx; + insert_idx << "insert into " + << index_table + << " values (?,?,?,?,?)"; + + ds->execute(create_idx.str()); + + prepared_index_statement ps(ds,insert_idx.str()); + while (rs->is_valid() && rs->step_next()) { int size; @@ -246,15 +244,6 @@ public: mapnik::box2d const& bbox = paths[i].envelope(); if (bbox.valid()) { - if (first) - { - first = false; - extent = bbox; - } - else - { - extent.expand_to_include(bbox); - } ps.bind(bbox); @@ -397,8 +386,9 @@ public: return true; } } - catch (...) + catch (std::exception const& ex) { + //std::clog << "no: " << ex.what() << "\n"; return false; } return false;