Skip to content

Commit

Permalink
Add GDALMDArray::IsCacheable() so that drivers can disable attempts a…
Browse files Browse the repository at this point in the history
…t opening cache file on main dataset
  • Loading branch information
rouault committed May 14, 2021
1 parent 415a34d commit bebb93b
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 30 deletions.
1 change: 1 addition & 0 deletions gdal/gcore/gdal_priv.h
Expand Up @@ -2388,6 +2388,7 @@ class CPL_DLL GDALMDArray: virtual public GDALAbstractMDArray, public GDALIHasAt
virtual bool IAdviseRead(const GUInt64* arrayStartIdx,
const size_t* count) const;

virtual bool IsCacheable() const { return true; }
//! @endcond

public:
Expand Down
64 changes: 34 additions & 30 deletions gdal/gcore/gdalmultidim.cpp
Expand Up @@ -3567,42 +3567,46 @@ bool GDALMDArray::Read(const GUInt64* arrayStartIdx,
const void* pDstBufferAllocStart,
size_t nDstBufferAllocSize) const
{
const auto& osFilename = GetFilename();
if( !m_bHasTriedCachedArray && !osFilename.empty() )
if( !m_bHasTriedCachedArray )
{
m_bHasTriedCachedArray = true;
if( !EQUAL(CPLGetExtension(osFilename.c_str()), "gmac") )
if( IsCacheable() )
{
const auto osCacheFilename = osFilename + ".gmac";
std::unique_ptr<GDALDataset> poDS(GDALDataset::Open(
osCacheFilename.c_str(), GDAL_OF_MULTIDIM_RASTER));
if( poDS )
const auto& osFilename = GetFilename();
if( !osFilename.empty() &&
!EQUAL(CPLGetExtension(osFilename.c_str()), "gmac") )
{
auto poRG = poDS->GetRootGroup();
assert( poRG );

const std::string osCachedArrayName(MassageName(GetFullName()));
m_poCachedArray = poRG->OpenMDArray(osCachedArrayName);
if( m_poCachedArray )
const auto osCacheFilename = osFilename + ".gmac";
std::unique_ptr<GDALDataset> poDS(GDALDataset::Open(
osCacheFilename.c_str(), GDAL_OF_MULTIDIM_RASTER));
if( poDS )
{
const auto& dims = GetDimensions();
const auto& cachedDims = m_poCachedArray->GetDimensions();
const size_t nDims = dims.size();
bool ok =
m_poCachedArray->GetDataType() == GetDataType() &&
cachedDims.size() == nDims;
for( size_t i = 0; ok && i < nDims; ++i )
{
ok = dims[i]->GetSize() == cachedDims[i]->GetSize();
}
if( !ok )
auto poRG = poDS->GetRootGroup();
assert( poRG );

const std::string osCachedArrayName(MassageName(GetFullName()));
m_poCachedArray = poRG->OpenMDArray(osCachedArrayName);
if( m_poCachedArray )
{
CPLError(CE_Warning, CPLE_AppDefined,
"Cached array %s in %s has incompatible "
"characteristics with current array.",
osCachedArrayName.c_str(),
osCacheFilename.c_str());
m_poCachedArray.reset();
const auto& dims = GetDimensions();
const auto& cachedDims = m_poCachedArray->GetDimensions();
const size_t nDims = dims.size();
bool ok =
m_poCachedArray->GetDataType() == GetDataType() &&
cachedDims.size() == nDims;
for( size_t i = 0; ok && i < nDims; ++i )
{
ok = dims[i]->GetSize() == cachedDims[i]->GetSize();
}
if( !ok )
{
CPLError(CE_Warning, CPLE_AppDefined,
"Cached array %s in %s has incompatible "
"characteristics with current array.",
osCachedArrayName.c_str(),
osCacheFilename.c_str());
m_poCachedArray.reset();
}
}
}
}
Expand Down

0 comments on commit bebb93b

Please sign in to comment.