LCOV - code coverage report
Current view: top level - ogr/ogrsf_frmts/generic - ogrmutexeddatasource.cpp (source / functions) Hit Total Coverage
Test: gdal_filtered.info Lines: 77 133 57.9 %
Date: 2025-09-10 17:48:50 Functions: 19 33 57.6 %

          Line data    Source code
       1             : /******************************************************************************
       2             :  *
       3             :  * Project:  OpenGIS Simple Features Reference Implementation
       4             :  * Purpose:  Implements OGRMutexedDataSource class
       5             :  * Author:   Even Rouault, even dot rouault at spatialys.com
       6             :  *
       7             :  ******************************************************************************
       8             :  * Copyright (c) 2013, Even Rouault <even dot rouault at spatialys.com>
       9             :  *
      10             :  * SPDX-License-Identifier: MIT
      11             :  ****************************************************************************/
      12             : 
      13             : #ifndef DOXYGEN_SKIP
      14             : 
      15             : #include "ogrmutexeddatasource.h"
      16             : #include "cpl_multiproc.h"
      17             : 
      18          36 : OGRMutexedDataSource::OGRMutexedDataSource(GDALDataset *poBaseDataSource,
      19             :                                            int bTakeOwnership,
      20             :                                            CPLMutex *hMutexIn,
      21          36 :                                            int bWrapLayersInMutexedLayer)
      22             :     : m_poBaseDataSource(poBaseDataSource), m_bHasOwnership(bTakeOwnership),
      23             :       m_hGlobalMutex(hMutexIn),
      24          36 :       m_bWrapLayersInMutexedLayer(bWrapLayersInMutexedLayer)
      25             : {
      26          36 :     SetDescription(poBaseDataSource->GetDescription());
      27          36 :     poDriver = poBaseDataSource->GetDriver();
      28          36 : }
      29             : 
      30          72 : OGRMutexedDataSource::~OGRMutexedDataSource()
      31             : {
      32             :     std::map<OGRLayer *, OGRMutexedLayer *>::iterator oIter =
      33          36 :         m_oMapLayers.begin();
      34          99 :     for (; oIter != m_oMapLayers.end(); ++oIter)
      35          63 :         delete oIter->second;
      36             : 
      37          36 :     if (m_bHasOwnership)
      38          36 :         delete m_poBaseDataSource;
      39          72 : }
      40             : 
      41          25 : int OGRMutexedDataSource::GetLayerCount() const
      42             : {
      43          50 :     CPLMutexHolderOptionalLockD(m_hGlobalMutex);
      44          50 :     return m_poBaseDataSource->GetLayerCount();
      45             : }
      46             : 
      47         191 : OGRLayer *OGRMutexedDataSource::WrapLayerIfNecessary(OGRLayer *poLayer)
      48             : {
      49         191 :     if (poLayer && m_bWrapLayersInMutexedLayer)
      50             :     {
      51         183 :         OGRLayer *poWrappedLayer = m_oMapLayers[poLayer];
      52         183 :         if (poWrappedLayer)
      53           0 :             poLayer = poWrappedLayer;
      54             :         else
      55             :         {
      56             :             OGRMutexedLayer *poMutexedLayer =
      57         183 :                 new OGRMutexedLayer(poLayer, FALSE, m_hGlobalMutex);
      58         183 :             m_oMapLayers[poLayer] = poMutexedLayer;
      59         183 :             m_oReverseMapLayers[poMutexedLayer] = poLayer;
      60         183 :             poLayer = poMutexedLayer;
      61             :         }
      62             :     }
      63         191 :     return poLayer;
      64             : }
      65             : 
      66          42 : const OGRLayer *OGRMutexedDataSource::GetLayer(int iIndex) const
      67             : {
      68          84 :     CPLMutexHolderOptionalLockD(m_hGlobalMutex);
      69          42 :     return const_cast<OGRMutexedDataSource *>(this)->WrapLayerIfNecessary(
      70         126 :         m_poBaseDataSource->GetLayer(iIndex));
      71             : }
      72             : 
      73          24 : OGRLayer *OGRMutexedDataSource::GetLayerByName(const char *pszName)
      74             : {
      75          48 :     CPLMutexHolderOptionalLockD(m_hGlobalMutex);
      76          48 :     return WrapLayerIfNecessary(m_poBaseDataSource->GetLayerByName(pszName));
      77             : }
      78             : 
      79           0 : OGRErr OGRMutexedDataSource::DeleteLayer(int iIndex)
      80             : {
      81           0 :     CPLMutexHolderOptionalLockD(m_hGlobalMutex);
      82             :     OGRLayer *poLayer =
      83           0 :         m_bWrapLayersInMutexedLayer ? GetLayer(iIndex) : nullptr;
      84           0 :     OGRErr eErr = m_poBaseDataSource->DeleteLayer(iIndex);
      85           0 :     if (eErr == OGRERR_NONE && poLayer)
      86             :     {
      87             :         std::map<OGRLayer *, OGRMutexedLayer *>::iterator oIter =
      88           0 :             m_oMapLayers.find(poLayer);
      89           0 :         if (oIter != m_oMapLayers.end())
      90             :         {
      91           0 :             delete oIter->second;
      92           0 :             m_oReverseMapLayers.erase(oIter->second);
      93           0 :             m_oMapLayers.erase(oIter);
      94             :         }
      95             :     }
      96           0 :     return eErr;
      97             : }
      98             : 
      99           1 : bool OGRMutexedDataSource::IsLayerPrivate(int iLayer) const
     100             : {
     101           2 :     CPLMutexHolderOptionalLockD(m_hGlobalMutex);
     102           2 :     return m_poBaseDataSource->IsLayerPrivate(iLayer);
     103             : }
     104             : 
     105          89 : int OGRMutexedDataSource::TestCapability(const char *pszCap) const
     106             : {
     107         178 :     CPLMutexHolderOptionalLockD(m_hGlobalMutex);
     108         178 :     return m_poBaseDataSource->TestCapability(pszCap);
     109             : }
     110             : 
     111             : OGRLayer *
     112           0 : OGRMutexedDataSource::ICreateLayer(const char *pszName,
     113             :                                    const OGRGeomFieldDefn *poGeomFieldDefn,
     114             :                                    CSLConstList papszOptions)
     115             : {
     116           0 :     CPLMutexHolderOptionalLockD(m_hGlobalMutex);
     117           0 :     return WrapLayerIfNecessary(m_poBaseDataSource->CreateLayer(
     118           0 :         pszName, poGeomFieldDefn, papszOptions));
     119             : }
     120             : 
     121           0 : OGRLayer *OGRMutexedDataSource::CopyLayer(OGRLayer *poSrcLayer,
     122             :                                           const char *pszNewName,
     123             :                                           char **papszOptions)
     124             : {
     125           0 :     CPLMutexHolderOptionalLockD(m_hGlobalMutex);
     126           0 :     return WrapLayerIfNecessary(
     127           0 :         m_poBaseDataSource->CopyLayer(poSrcLayer, pszNewName, papszOptions));
     128             : }
     129             : 
     130           1 : OGRStyleTable *OGRMutexedDataSource::GetStyleTable()
     131             : {
     132           2 :     CPLMutexHolderOptionalLockD(m_hGlobalMutex);
     133           2 :     return m_poBaseDataSource->GetStyleTable();
     134             : }
     135             : 
     136           0 : void OGRMutexedDataSource::SetStyleTableDirectly(OGRStyleTable *poStyleTable)
     137             : {
     138           0 :     CPLMutexHolderOptionalLockD(m_hGlobalMutex);
     139           0 :     m_poBaseDataSource->SetStyleTableDirectly(poStyleTable);
     140           0 : }
     141             : 
     142           0 : void OGRMutexedDataSource::SetStyleTable(OGRStyleTable *poStyleTable)
     143             : {
     144           0 :     CPLMutexHolderOptionalLockD(m_hGlobalMutex);
     145           0 :     m_poBaseDataSource->SetStyleTable(poStyleTable);
     146           0 : }
     147             : 
     148         125 : OGRLayer *OGRMutexedDataSource::ExecuteSQL(const char *pszStatement,
     149             :                                            OGRGeometry *poSpatialFilter,
     150             :                                            const char *pszDialect)
     151             : {
     152         250 :     CPLMutexHolderOptionalLockD(m_hGlobalMutex);
     153         125 :     return WrapLayerIfNecessary(m_poBaseDataSource->ExecuteSQL(
     154         375 :         pszStatement, poSpatialFilter, pszDialect));
     155             : }
     156             : 
     157         120 : void OGRMutexedDataSource::ReleaseResultSet(OGRLayer *poResultsSet)
     158             : {
     159         240 :     CPLMutexHolderOptionalLockD(m_hGlobalMutex);
     160         120 :     if (poResultsSet && m_bWrapLayersInMutexedLayer)
     161             :     {
     162             :         std::map<OGRMutexedLayer *, OGRLayer *>::iterator oIter =
     163             :             m_oReverseMapLayers.find(
     164         120 :                 dynamic_cast<OGRMutexedLayer *>(poResultsSet));
     165         120 :         CPLAssert(oIter != m_oReverseMapLayers.end());
     166         120 :         delete poResultsSet;
     167         120 :         poResultsSet = oIter->second;
     168         120 :         m_oMapLayers.erase(poResultsSet);
     169         120 :         m_oReverseMapLayers.erase(oIter);
     170             :     }
     171             : 
     172         120 :     m_poBaseDataSource->ReleaseResultSet(poResultsSet);
     173         120 : }
     174             : 
     175           0 : CPLErr OGRMutexedDataSource::FlushCache(bool bAtClosing)
     176             : {
     177           0 :     CPLMutexHolderOptionalLockD(m_hGlobalMutex);
     178           0 :     return m_poBaseDataSource->FlushCache(bAtClosing);
     179             : }
     180             : 
     181           0 : OGRErr OGRMutexedDataSource::StartTransaction(int bForce)
     182             : {
     183           0 :     CPLMutexHolderOptionalLockD(m_hGlobalMutex);
     184           0 :     return m_poBaseDataSource->StartTransaction(bForce);
     185             : }
     186             : 
     187           0 : OGRErr OGRMutexedDataSource::CommitTransaction()
     188             : {
     189           0 :     CPLMutexHolderOptionalLockD(m_hGlobalMutex);
     190           0 :     return m_poBaseDataSource->CommitTransaction();
     191             : }
     192             : 
     193           0 : OGRErr OGRMutexedDataSource::RollbackTransaction()
     194             : {
     195           0 :     CPLMutexHolderOptionalLockD(m_hGlobalMutex);
     196           0 :     return m_poBaseDataSource->RollbackTransaction();
     197             : }
     198             : 
     199           1 : char **OGRMutexedDataSource::GetMetadata(const char *pszDomain)
     200             : {
     201           2 :     CPLMutexHolderOptionalLockD(m_hGlobalMutex);
     202           2 :     return m_poBaseDataSource->GetMetadata(pszDomain);
     203             : }
     204             : 
     205           0 : CPLErr OGRMutexedDataSource::SetMetadata(char **papszMetadata,
     206             :                                          const char *pszDomain)
     207             : {
     208           0 :     CPLMutexHolderOptionalLockD(m_hGlobalMutex);
     209           0 :     return m_poBaseDataSource->SetMetadata(papszMetadata, pszDomain);
     210             : }
     211             : 
     212           1 : const char *OGRMutexedDataSource::GetMetadataItem(const char *pszName,
     213             :                                                   const char *pszDomain)
     214             : {
     215           2 :     CPLMutexHolderOptionalLockD(m_hGlobalMutex);
     216           2 :     return m_poBaseDataSource->GetMetadataItem(pszName, pszDomain);
     217             : }
     218             : 
     219           0 : CPLErr OGRMutexedDataSource::SetMetadataItem(const char *pszName,
     220             :                                              const char *pszValue,
     221             :                                              const char *pszDomain)
     222             : {
     223           0 :     CPLMutexHolderOptionalLockD(m_hGlobalMutex);
     224           0 :     return m_poBaseDataSource->SetMetadataItem(pszName, pszValue, pszDomain);
     225             : }
     226             : 
     227             : std::vector<std::string>
     228           1 : OGRMutexedDataSource::GetFieldDomainNames(CSLConstList papszOptions) const
     229             : {
     230           2 :     CPLMutexHolderOptionalLockD(m_hGlobalMutex);
     231           2 :     return m_poBaseDataSource->GetFieldDomainNames(papszOptions);
     232             : }
     233             : 
     234             : const OGRFieldDomain *
     235           3 : OGRMutexedDataSource::GetFieldDomain(const std::string &name) const
     236             : {
     237           6 :     CPLMutexHolderOptionalLockD(m_hGlobalMutex);
     238           6 :     return m_poBaseDataSource->GetFieldDomain(name);
     239             : }
     240             : 
     241           0 : bool OGRMutexedDataSource::AddFieldDomain(
     242             :     std::unique_ptr<OGRFieldDomain> &&domain, std::string &failureReason)
     243             : {
     244           0 :     CPLMutexHolderOptionalLockD(m_hGlobalMutex);
     245           0 :     return m_poBaseDataSource->AddFieldDomain(std::move(domain), failureReason);
     246             : }
     247             : 
     248           0 : bool OGRMutexedDataSource::DeleteFieldDomain(const std::string &name,
     249             :                                              std::string &failureReason)
     250             : {
     251           0 :     CPLMutexHolderOptionalLockD(m_hGlobalMutex);
     252           0 :     return m_poBaseDataSource->DeleteFieldDomain(name, failureReason);
     253             : }
     254             : 
     255           0 : bool OGRMutexedDataSource::UpdateFieldDomain(
     256             :     std::unique_ptr<OGRFieldDomain> &&domain, std::string &failureReason)
     257             : {
     258           0 :     CPLMutexHolderOptionalLockD(m_hGlobalMutex);
     259           0 :     return m_poBaseDataSource->UpdateFieldDomain(std::move(domain),
     260           0 :                                                  failureReason);
     261             : }
     262             : 
     263             : std::vector<std::string>
     264           3 : OGRMutexedDataSource::GetRelationshipNames(CSLConstList papszOptions) const
     265             : {
     266           6 :     CPLMutexHolderOptionalLockD(m_hGlobalMutex);
     267           6 :     return m_poBaseDataSource->GetRelationshipNames(papszOptions);
     268             : }
     269             : 
     270             : const GDALRelationship *
     271           8 : OGRMutexedDataSource::GetRelationship(const std::string &name) const
     272             : {
     273          16 :     CPLMutexHolderOptionalLockD(m_hGlobalMutex);
     274          16 :     return m_poBaseDataSource->GetRelationship(name);
     275             : }
     276             : 
     277           1 : std::shared_ptr<GDALGroup> OGRMutexedDataSource::GetRootGroup() const
     278             : {
     279           2 :     CPLMutexHolderOptionalLockD(m_hGlobalMutex);
     280           2 :     return m_poBaseDataSource->GetRootGroup();
     281             : }
     282             : 
     283             : #endif /* #ifndef DOXYGEN_SKIP */

Generated by: LCOV version 1.14