Skip to content

Commit

Permalink
Add DropCache method to GDALDataset (#8940) (fixes #8938)
Browse files Browse the repository at this point in the history
DropCache() release cache blocks but does not write cache to disk.
It is similar to FlushCache(true) but does not assume that the dataset will be closed.
  • Loading branch information
chacha21 authored Dec 11, 2023
1 parent 0f3e6e1 commit 5489059
Show file tree
Hide file tree
Showing 7 changed files with 174 additions and 5 deletions.
44 changes: 44 additions & 0 deletions autotest/cpp/test_gdal.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3491,4 +3491,48 @@ TEST_F(test_gdal, open_shared_open_options)
}
}

// Test DropCache() to check that no data is saved on disk
TEST_F(test_gdal, drop_cache)
{
CPLErrorReset();
{
GDALDriverManager *gdalDriverManager = GetGDALDriverManager();
GDALDriver *enviDriver =
!gdalDriverManager ? nullptr
: gdalDriverManager->GetDriverByName("ENVI");
const char *enviOptions[] = {"SUFFIX=ADD", "INTERLEAVE=BIL", nullptr};

const char *filename = GCORE_DATA_DIR "test_drop_cache.bil";

GDALDataset *poDS1 =
!enviDriver
? nullptr
: enviDriver->Create(filename, 1, 1, 1,
GDALDataType::GDT_Float32, enviOptions);
if (poDS1)
{
GDALRasterBand *rasterBand =
!poDS1 ? nullptr : poDS1->GetRasterBand(1);
if (rasterBand)
rasterBand->Fill(1);
poDS1->DropCache();
GDALClose(poDS1);
poDS1 = nullptr;
}

GDALDataset *poDS2 =
GDALDataset::Open(filename, GDAL_OF_SHARED, nullptr, nullptr);

if (poDS2)
{
GDALRasterBand *rasterBand =
!poDS2 ? nullptr : poDS2->GetRasterBand(1);
EXPECT_EQ(GDALChecksumImage(rasterBand, 0, 0, 1, 1), 0);
poDS2->MarkSuppressOnClose();
GDALClose(poDS2);
poDS2 = nullptr;
}
}
}

} // namespace
2 changes: 2 additions & 0 deletions gcore/gdal.h
Original file line number Diff line number Diff line change
Expand Up @@ -1191,6 +1191,7 @@ CPLErr CPL_DLL CPL_STDCALL GDALBuildOverviewsEx(
void CPL_DLL CPL_STDCALL GDALGetOpenDatasets(GDALDatasetH **hDS, int *pnCount);
int CPL_DLL CPL_STDCALL GDALGetAccess(GDALDatasetH hDS);
CPLErr CPL_DLL CPL_STDCALL GDALFlushCache(GDALDatasetH hDS);
CPLErr CPL_DLL CPL_STDCALL GDALDropCache(GDALDatasetH hDS);

CPLErr CPL_DLL CPL_STDCALL GDALCreateDatasetMaskBand(GDALDatasetH hDS,
int nFlags);
Expand Down Expand Up @@ -1545,6 +1546,7 @@ CPLErr CPL_DLL CPL_STDCALL GDALComputeRasterMinMax(GDALRasterBandH hBand,
int bApproxOK,
double adfMinMax[2]);
CPLErr CPL_DLL CPL_STDCALL GDALFlushRasterCache(GDALRasterBandH hBand);
CPLErr CPL_DLL CPL_STDCALL GDALDropRasterCache(GDALRasterBandH hBand);
CPLErr CPL_DLL CPL_STDCALL GDALGetRasterHistogram(
GDALRasterBandH hBand, double dfMin, double dfMax, int nBuckets,
int *panHistogram, int bIncludeOutOfRange, int bApproxOK,
Expand Down
10 changes: 8 additions & 2 deletions gcore/gdal_priv.h
Original file line number Diff line number Diff line change
Expand Up @@ -537,6 +537,7 @@ class CPL_DLL GDALDataset : public GDALMajorObject
Bands GetBands();

virtual CPLErr FlushCache(bool bAtClosing = false);
virtual CPLErr DropCache();

virtual GIntBig GetEstimatedRAMUsage();

Expand Down Expand Up @@ -1188,7 +1189,7 @@ class GDALAbstractBandBlockCache

int m_nInitialDirtyBlocksInFlushCache = 0;
int m_nLastTick = -1;
bool m_bWriteDirtyBlocks = true;
size_t m_nWriteDirtyBlocksDisabled = 0;

void FreeDanglingBlocks();
void UnreferenceBlockBase();
Expand All @@ -1205,9 +1206,13 @@ class GDALAbstractBandBlockCache
void AddBlockToFreeList(GDALRasterBlock *);
void IncDirtyBlocks(int nInc);
void WaitCompletionPendingTasks();
void EnableDirtyBlockWriting()
{
--m_nWriteDirtyBlocksDisabled;
}
void DisableDirtyBlockWriting()
{
m_bWriteDirtyBlocks = false;
++m_nWriteDirtyBlocksDisabled;
}
bool HasDirtyBlocks() const
{
Expand Down Expand Up @@ -1412,6 +1417,7 @@ class CPL_DLL GDALRasterBand : public GDALMajorObject
// New OpengIS CV_SampleDimension stuff.

virtual CPLErr FlushCache(bool bAtClosing = false);
virtual CPLErr DropCache();
virtual char **GetCategoryNames();
virtual double GetNoDataValue(int *pbSuccess = nullptr);
virtual int64_t GetNoDataValueAsInt64(int *pbSuccess = nullptr);
Expand Down
2 changes: 1 addition & 1 deletion gcore/gdalarraybandblockcache.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -442,7 +442,7 @@ CPLErr GDALArrayBandBlockCache::FlushBlock(int nXBlockOff, int nYBlockOff,

CPLErr eErr = CE_None;

if (m_bWriteDirtyBlocks && bWriteDirtyBlock && poBlock->GetDirty())
if (!m_nWriteDirtyBlocksDisabled && bWriteDirtyBlock && poBlock->GetDirty())
{
UpdateDirtyBlockFlushingLog();

Expand Down
53 changes: 53 additions & 0 deletions gcore/gdaldataset.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -613,6 +613,59 @@ CPLErr CPL_STDCALL GDALFlushCache(GDALDatasetH hDS)
return GDALDataset::FromHandle(hDS)->FlushCache(false);
}

/************************************************************************/
/* DropCache() */
/************************************************************************/

/**
* \brief Drop all write cached data
*
* This method is the same as the C function GDALDropCache().
*
* @return CE_None in case of success
* @since 3.9
*/

CPLErr GDALDataset::DropCache()

{
CPLErr eErr = CE_None;

if (papoBands)
{
for (int i = 0; i < nBands; ++i)
{
if (papoBands[i])
{
if (papoBands[i]->DropCache() != CE_None)
eErr = CE_Failure;
}
}
}

return eErr;
}

/************************************************************************/
/* GDALDropCache() */
/************************************************************************/

/**
* \brief Drop all write cached data
*
* @see GDALDataset::DropCache().
* @return CE_None in case of success
* @since 3.9
*/

CPLErr CPL_STDCALL GDALDropCache(GDALDatasetH hDS)

{
VALIDATE_POINTER1(hDS, "GDALDropCache", CE_Failure);

return GDALDataset::FromHandle(hDS)->DropCache();
}

/************************************************************************/
/* GetEstimatedRAMUsage() */
/************************************************************************/
Expand Down
4 changes: 2 additions & 2 deletions gcore/gdalhashsetbandblockcache.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ CPLErr GDALHashSetBandBlockCache::FlushCache()
{
CPLErr eErr = CE_None;

if (m_bWriteDirtyBlocks && eGlobalErr == CE_None &&
if (!m_nWriteDirtyBlocksDisabled && eGlobalErr == CE_None &&
poBlock->GetDirty())
{
UpdateDirtyBlockFlushingLog();
Expand Down Expand Up @@ -227,7 +227,7 @@ CPLErr GDALHashSetBandBlockCache::FlushBlock(int nXBlockOff, int nYBlockOff,

CPLErr eErr = CE_None;

if (m_bWriteDirtyBlocks && bWriteDirtyBlock && poBlock->GetDirty())
if (!m_nWriteDirtyBlocksDisabled && bWriteDirtyBlock && poBlock->GetDirty())
eErr = poBlock->Write();

delete poBlock;
Expand Down
64 changes: 64 additions & 0 deletions gcore/gdalrasterband.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1140,6 +1140,70 @@ CPLErr CPL_STDCALL GDALFlushRasterCache(GDALRasterBandH hBand)
return GDALRasterBand::FromHandle(hBand)->FlushCache(false);
}

/************************************************************************/
/* DropCache() */
/************************************************************************/

/**
* \brief Drop raster data cache : data in cache will be lost.
*
* This call will recover memory used to cache data blocks for this raster
* band, and ensure that new requests are referred to the underlying driver.
*
* This method is the same as the C function GDALDropRasterCache().
*
* @return CE_None on success.
* @since 3.9
*/

CPLErr GDALRasterBand::DropCache()

{
CPLErr result = CE_None;

if (poBandBlockCache)
poBandBlockCache->DisableDirtyBlockWriting();

CPLErr eGlobalErr = eFlushBlockErr;

if (eFlushBlockErr != CE_None)
{
ReportError(
eFlushBlockErr, CPLE_AppDefined,
"An error occurred while writing a dirty block from DropCache");
eFlushBlockErr = CE_None;
}

if (poBandBlockCache == nullptr || !poBandBlockCache->IsInitOK())
result = eGlobalErr;
else
result = poBandBlockCache->FlushCache();

if (poBandBlockCache)
poBandBlockCache->EnableDirtyBlockWriting();

return result;
}

/************************************************************************/
/* GDALDropRasterCache() */
/************************************************************************/

/**
* \brief Drop raster data cache.
*
* @see GDALRasterBand::DropCache()
* @since 3.9
*/

CPLErr CPL_STDCALL GDALDropRasterCache(GDALRasterBandH hBand)

{
VALIDATE_POINTER1(hBand, "GDALDropRasterCache", CE_Failure);

return GDALRasterBand::FromHandle(hBand)->DropCache();
}

/************************************************************************/
/* UnreferenceBlock() */
/* */
Expand Down

0 comments on commit 5489059

Please sign in to comment.