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

added DropCache to GDALDataset #8940

Merged
merged 9 commits into from
Dec 11, 2023
Merged
Show file tree
Hide file tree
Changes from 2 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
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();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

does that need to be virtual ?

Copy link
Contributor Author

@chacha21 chacha21 Dec 8, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Honestly it is just copy/paste from FlushCache().
I think virtual is harmless and make it ready for future specific implementations (if any)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok


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;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

'b' is for boolean. So this should be "m_nWriteDirtyBlocksDisabled"


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();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

virtual needed ?

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
*/
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
* @return CE_None in case of success
*/
* @return CE_None in case of success
* @since 3.9
*/


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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
* @return CE_None in case of success
* @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_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.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
* @return CE_None on success.
* @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()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
* @see GDALRasterBand::DropCache()
* @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
Loading