Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Lint] PG: modernize SRS cache #9119

Merged
merged 1 commit into from
Feb 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions ogr/ogrsf_frmts/pg/ogr_pg.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
#include "ogrpgutility.h"
#include "ogr_pgdump.h"

#include <map>
rouault marked this conversation as resolved.
Show resolved Hide resolved
#include <optional>
#include <vector>

Expand Down Expand Up @@ -595,9 +596,9 @@ class OGRPGDataSource final : public OGRDataSource

// We maintain a list of known SRID to reduce the number of trips to
// the database to get SRSes.
int nKnownSRID = 0;
int *panSRID = nullptr;
OGRSpatialReference **papoSRS = nullptr;
std::map<int,
std::unique_ptr<OGRSpatialReference, OGRSpatialReferenceReleaser>>
m_oSRSCache{};

OGRPGTableLayer *poLayerInCopyMode = nullptr;

Expand Down Expand Up @@ -656,7 +657,7 @@ class OGRPGDataSource final : public OGRDataSource
}

int FetchSRSId(const OGRSpatialReference *poSRS);
OGRSpatialReference *FetchSRS(int nSRSId);
const OGRSpatialReference *FetchSRS(int nSRSId);
static OGRErr InitializeMetadataTables();

int Open(const char *, int bUpdate, int bTestOpen, char **papszOpenOptions);
Expand Down
34 changes: 9 additions & 25 deletions ogr/ogrsf_frmts/pg/ogrpgdatasource.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -90,14 +90,6 @@ OGRPGDataSource::~OGRPGDataSource()
PQfinish(hPGConn);
hPGConn = nullptr;
}

for (int i = 0; i < nKnownSRID; i++)
{
if (papoSRS[i] != nullptr)
papoSRS[i]->Release();
}
CPLFree(panSRID);
CPLFree(papoSRS);
}

/************************************************************************/
Expand Down Expand Up @@ -2574,7 +2566,7 @@ OGRErr OGRPGDataSource::InitializeMetadataTables()
/* OGRSpatialReference, as handles may be cached. */
/************************************************************************/

OGRSpatialReference *OGRPGDataSource::FetchSRS(int nId)
const OGRSpatialReference *OGRPGDataSource::FetchSRS(int nId)

{
if (nId < 0 || !m_bHasSpatialRefSys)
Expand All @@ -2583,10 +2575,10 @@ OGRSpatialReference *OGRPGDataSource::FetchSRS(int nId)
/* -------------------------------------------------------------------- */
/* First, we look through our SRID cache, is it there? */
/* -------------------------------------------------------------------- */
for (int i = 0; i < nKnownSRID; i++)
auto oIter = m_oSRSCache.find(nId);
if (oIter != m_oSRSCache.end())
{
if (panSRID[i] == nId)
return papoSRS[i];
return oIter->second.get();
}

EndCopy();
Expand All @@ -2595,7 +2587,7 @@ OGRSpatialReference *OGRPGDataSource::FetchSRS(int nId)
/* Try looking up in spatial_ref_sys table. */
/* -------------------------------------------------------------------- */
CPLString osCommand;
OGRSpatialReference *poSRS = nullptr;
std::unique_ptr<OGRSpatialReference, OGRSpatialReferenceReleaser> poSRS;

osCommand.Printf("SELECT srtext, auth_name, auth_srid FROM spatial_ref_sys "
"WHERE srid = %d",
Expand All @@ -2608,7 +2600,7 @@ OGRSpatialReference *OGRPGDataSource::FetchSRS(int nId)
const char *pszWKT = PQgetvalue(hResult, 0, 0);
const char *pszAuthName = PQgetvalue(hResult, 0, 1);
const char *pszAuthSRID = PQgetvalue(hResult, 0, 2);
poSRS = new OGRSpatialReference();
poSRS.reset(new OGRSpatialReference());
poSRS->SetAxisMappingStrategy(OAMS_TRADITIONAL_GIS_ORDER);

// Try to import first from EPSG code, and then from WKT
Expand All @@ -2620,8 +2612,7 @@ OGRSpatialReference *OGRPGDataSource::FetchSRS(int nId)
}
else if (poSRS->importFromWkt(pszWKT) != OGRERR_NONE)
{
delete poSRS;
poSRS = nullptr;
poSRS.reset();
}
}
else
Expand All @@ -2638,15 +2629,8 @@ OGRSpatialReference *OGRPGDataSource::FetchSRS(int nId)
/* -------------------------------------------------------------------- */
/* Add to the cache. */
/* -------------------------------------------------------------------- */
panSRID =
static_cast<int *>(CPLRealloc(panSRID, sizeof(int) * (nKnownSRID + 1)));
papoSRS = static_cast<OGRSpatialReference **>(
CPLRealloc(papoSRS, sizeof(OGRSpatialReference *) * (nKnownSRID + 1)));
panSRID[nKnownSRID] = nId;
papoSRS[nKnownSRID] = poSRS;
nKnownSRID++;

return poSRS;
oIter = m_oSRSCache.emplace(nId, std::move(poSRS)).first;
return oIter->second.get();
}

/************************************************************************/
Expand Down
Loading