sqlite: disassociate extent calculation from rtree auto-index creation, and grab immediate lock on rtree table

This commit is contained in:
Dane Springmeyer 2011-11-13 16:02:27 -08:00
parent 3750171680
commit a4715aa8df
2 changed files with 37 additions and 45 deletions

View file

@ -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<sqlite_resultset> 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_)

View file

@ -182,8 +182,7 @@ public:
static bool create_spatial_index(std::string const& index_db,
std::string const& index_table,
boost::shared_ptr<sqlite_resultset> rs,
mapnik::box2d<double>& extent)
boost::shared_ptr<sqlite_resultset> rs)
{
/* TODO
- speedups
@ -203,34 +202,33 @@ public:
bool existed = boost::filesystem::exists(index_db);
boost::shared_ptr<sqlite_connection> ds = boost::make_shared<sqlite_connection>(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<double> 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;