Skip to content

Commit

Permalink
MSSQLSpatial Fix BCP performance problem (OSGeo#9112)
Browse files Browse the repository at this point in the history
  • Loading branch information
szekerest committed Jan 21, 2024
1 parent fc0ec2b commit 0101453
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 6 deletions.
2 changes: 2 additions & 0 deletions ogr/ogrsf_frmts/mssqlspatial/ogr_mssqlspatial.h
Original file line number Diff line number Diff line change
Expand Up @@ -666,6 +666,8 @@ class OGRMSSQLSpatialDataSource final : public OGRDataSource
static char *LaunderName(const char *pszSrcName);
OGRErr InitializeMetadataTables();

void AddSRIDToCache(int nId, OGRSpatialReference *poSRS);

OGRSpatialReference *FetchSRS(int nId);
int FetchSRSId(const OGRSpatialReference *poSRS);

Expand Down
47 changes: 41 additions & 6 deletions ogr/ogrsf_frmts/mssqlspatial/ogrmssqlspatialdatasource.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1514,17 +1514,33 @@ OGRSpatialReference *OGRMSSQLSpatialDataSource::FetchSRS(int nId)
/* -------------------------------------------------------------------- */
if (poSRS)
{
panSRID = (int *)CPLRealloc(panSRID, sizeof(int) * (nKnownSRID + 1));
papoSRS = (OGRSpatialReference **)CPLRealloc(
papoSRS, sizeof(void *) * (nKnownSRID + 1));
panSRID[nKnownSRID] = nId;
papoSRS[nKnownSRID] = poSRS;
nKnownSRID++;
AddSRIDToCache(nId, poSRS);
}

return poSRS;
}

/************************************************************************/
/* AddSRIDToCache() */
/* */
/* Note: this will not add a reference on the poSRS object. Make */
/* sure it is freshly created, or add a reference yourself if not. */
/************************************************************************/

void OGRMSSQLSpatialDataSource::AddSRIDToCache(int nId,
OGRSpatialReference *poSRS)
{
/* -------------------------------------------------------------------- */
/* Add to the cache. */
/* -------------------------------------------------------------------- */
panSRID = (int *)CPLRealloc(panSRID, sizeof(int) * (nKnownSRID + 1));
papoSRS = (OGRSpatialReference **)CPLRealloc(papoSRS, sizeof(void *) *
(nKnownSRID + 1));
panSRID[nKnownSRID] = nId;
papoSRS[nKnownSRID] = poSRS;
nKnownSRID++;
}

/************************************************************************/
/* FetchSRSId() */
/* */
Expand All @@ -1541,6 +1557,19 @@ int OGRMSSQLSpatialDataSource::FetchSRSId(const OGRSpatialReference *poSRS)

if (poSRS == nullptr)
return 0;
/* -------------------------------------------------------------------- */
/* First, we look through our SRID cache, is it there? */
/* -------------------------------------------------------------------- */
for (int i = 0; i < nKnownSRID; i++)
{
if (papoSRS[i] == poSRS)
return panSRID[i];
}
for (int i = 0; i < nKnownSRID; i++)
{
if (papoSRS[i] != nullptr && papoSRS[i]->IsSame(poSRS))
return panSRID[i];
}

OGRSpatialReference oSRS(*poSRS);
// cppcheck-suppress uselessAssignmentPtrArg
Expand Down Expand Up @@ -1590,6 +1619,12 @@ int OGRMSSQLSpatialDataSource::FetchSRSId(const OGRSpatialReference *poSRS)
if (oStmt.ExecuteSQL() && oStmt.Fetch() && oStmt.GetColData(0))
{
nSRSId = atoi(oStmt.GetColData(0));
if (nSRSId != 0)
{
auto poCachedSRS = new OGRSpatialReference(oSRS);
poCachedSRS->SetAxisMappingStrategy(OAMS_TRADITIONAL_GIS_ORDER);
AddSRIDToCache(nSRSId, poCachedSRS);
}
return nSRSId;
}
}
Expand Down

0 comments on commit 0101453

Please sign in to comment.