Skip to content

Commit

Permalink
added DropCache to GDALDataset
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 tp FlushCache(true) but does not assume that the dataset will be closed.
  • Loading branch information
chacha21 committed Dec 8, 2023
1 parent ffd100b commit 27c6a37
Show file tree
Hide file tree
Showing 6 changed files with 127 additions and 5 deletions.
1 change: 1 addition & 0 deletions gcore/gdal.h
Original file line number Diff line number Diff line change
Expand Up @@ -1545,6 +1545,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_bWriteDirtyBlocksDisabled = 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_bWriteDirtyBlocksDisabled;
}
void DisableDirtyBlockWriting()
{
m_bWriteDirtyBlocks = false;
++m_bWriteDirtyBlocksDisabled;
}
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_bWriteDirtyBlocksDisabled && 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
*/

CPLErr GDALDataset::DropCache()

{
CPLErr eErr = CE_None;
// This sometimes happens if a dataset is destroyed before completely
// built.

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
*/

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_bWriteDirtyBlocksDisabled && 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_bWriteDirtyBlocksDisabled && bWriteDirtyBlock && poBlock->GetDirty())
eErr = poBlock->Write();

delete poBlock;
Expand Down
62 changes: 62 additions & 0 deletions gcore/gdalrasterband.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1140,6 +1140,68 @@ 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.
*/

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()
*/

CPLErr CPL_STDCALL GDALDropRasterCache(GDALRasterBandH hBand)

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

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

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

0 comments on commit 27c6a37

Please sign in to comment.