Skip to content

Commit

Permalink
Zarr: move code belonging to classes to dedicated files; rename class…
Browse files Browse the repository at this point in the history
…es ZarrGroupVx to ZarrVXGroup (no functional change)
  • Loading branch information
rouault committed May 5, 2023
1 parent 555b3d1 commit 60477b4
Show file tree
Hide file tree
Showing 9 changed files with 2,361 additions and 2,118 deletions.
4 changes: 4 additions & 0 deletions frmts/zarr/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@ add_gdal_driver(
TARGET gdal_Zarr
SOURCES zarr_array.cpp
zarr_attribute.cpp
zarr_dimension.cpp
zarr_group.cpp
zarr_v2_group.cpp
zarr_v3_group.cpp
zarr_sharedresource.cpp
zarrdriver.cpp
STRONG_CXX_WFLAGS
PLUGIN_CAPABLE
Expand Down
31 changes: 18 additions & 13 deletions frmts/zarr/zarr.h
Original file line number Diff line number Diff line change
Expand Up @@ -427,18 +427,18 @@ class ZarrGroupBase CPL_NON_FINAL : public GDALGroup
};

/************************************************************************/
/* ZarrGroupV2 */
/* ZarrV2Group */
/************************************************************************/

class ZarrGroupV2 final : public ZarrGroupBase
class ZarrV2Group final : public ZarrGroupBase
{
void ExploreDirectory() const override;
void LoadAttributes() const override;

std::shared_ptr<ZarrGroupV2>
std::shared_ptr<ZarrV2Group>
GetOrCreateSubGroup(const std::string &osSubGroupFullname);

ZarrGroupV2(const std::shared_ptr<ZarrSharedResource> &poSharedResource,
ZarrV2Group(const std::shared_ptr<ZarrSharedResource> &poSharedResource,
const std::string &osParentName, const std::string &osName)
: ZarrGroupBase(poSharedResource, osParentName, osName)
{
Expand All @@ -447,13 +447,13 @@ class ZarrGroupV2 final : public ZarrGroupBase
void NotifyChildrenOfRenaming();

public:
static std::shared_ptr<ZarrGroupV2>
static std::shared_ptr<ZarrV2Group>
Create(const std::shared_ptr<ZarrSharedResource> &poSharedResource,
const std::string &osParentName, const std::string &osName);

~ZarrGroupV2() override;
~ZarrV2Group() override;

static std::shared_ptr<ZarrGroupV2>
static std::shared_ptr<ZarrV2Group>
CreateOnDisk(const std::shared_ptr<ZarrSharedResource> &poSharedResource,
const std::string &osParentName, const std::string &osName,
const std::string &osDirectoryName);
Expand Down Expand Up @@ -485,25 +485,25 @@ class ZarrGroupV2 final : public ZarrGroupBase
};

/************************************************************************/
/* ZarrGroupV3 */
/* ZarrV3Group */
/************************************************************************/

class ZarrGroupV3 final : public ZarrGroupBase
class ZarrV3Group final : public ZarrGroupBase
{
std::string m_osGroupFilename;
bool m_bNew = false;

void ExploreDirectory() const override;
void LoadAttributes() const override;

ZarrGroupV3(const std::shared_ptr<ZarrSharedResource> &poSharedResource,
ZarrV3Group(const std::shared_ptr<ZarrSharedResource> &poSharedResource,
const std::string &osParentName, const std::string &osName,
const std::string &osRootDirectoryName);

public:
~ZarrGroupV3() override;
~ZarrV3Group() override;

static std::shared_ptr<ZarrGroupV3>
static std::shared_ptr<ZarrV3Group>
Create(const std::shared_ptr<ZarrSharedResource> &poSharedResource,
const std::string &osParentName, const std::string &osName,
const std::string &osRootDirectoryName);
Expand All @@ -516,7 +516,7 @@ class ZarrGroupV3 final : public ZarrGroupBase
OpenZarrGroup(const std::string &osName,
CSLConstList papszOptions = nullptr) const override;

static std::shared_ptr<ZarrGroupV3>
static std::shared_ptr<ZarrV3Group>
CreateOnDisk(const std::shared_ptr<ZarrSharedResource> &poSharedResource,
const std::string &osParentFullName, const std::string &osName,
const std::string &osRootDirectoryName);
Expand Down Expand Up @@ -733,6 +733,11 @@ class ZarrArray final : public GDALPamMDArray
const std::vector<DtypeElt> &aoDtypeElts,
const std::vector<GUInt64> &anBlockSize, bool bFortranOrder);

static bool FillBlockSize(
const std::vector<std::shared_ptr<GDALDimension>> &aoDimensions,
const GDALExtendedDataType &oDataType,
std::vector<GUInt64> &anBlockSize, CSLConstList papszOptions);

bool IsWritable() const override
{
return m_bUpdatable;
Expand Down
64 changes: 63 additions & 1 deletion frmts/zarr/zarr_array.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -350,6 +350,68 @@ void ZarrArray::Flush()
}
}

/************************************************************************/
/* FillBlockSize() */
/************************************************************************/

/* static */
bool ZarrArray::FillBlockSize(
const std::vector<std::shared_ptr<GDALDimension>> &aoDimensions,
const GDALExtendedDataType &oDataType, std::vector<GUInt64> &anBlockSize,
CSLConstList papszOptions)
{
const auto nDims = aoDimensions.size();
anBlockSize.resize(nDims);
for (size_t i = 0; i < nDims; ++i)
anBlockSize[i] = 1;
if (nDims >= 2)
{
anBlockSize[nDims - 2] =
std::min(std::max<GUInt64>(1, aoDimensions[nDims - 2]->GetSize()),
static_cast<GUInt64>(256));
anBlockSize[nDims - 1] =
std::min(std::max<GUInt64>(1, aoDimensions[nDims - 1]->GetSize()),
static_cast<GUInt64>(256));
}
else if (nDims == 1)
{
anBlockSize[0] = std::max<GUInt64>(1, aoDimensions[0]->GetSize());
}

const char *pszBlockSize = CSLFetchNameValue(papszOptions, "BLOCKSIZE");
if (pszBlockSize)
{
const auto aszTokens(
CPLStringList(CSLTokenizeString2(pszBlockSize, ",", 0)));
if (static_cast<size_t>(aszTokens.size()) != nDims)
{
CPLError(CE_Failure, CPLE_AppDefined,
"Invalid number of values in BLOCKSIZE");
return false;
}
size_t nBlockSize = oDataType.GetSize();
for (size_t i = 0; i < nDims; ++i)
{
anBlockSize[i] = static_cast<GUInt64>(CPLAtoGIntBig(aszTokens[i]));
if (anBlockSize[i] == 0)
{
CPLError(CE_Failure, CPLE_AppDefined,
"Values in BLOCKSIZE should be > 0");
return false;
}
if (anBlockSize[i] >
std::numeric_limits<size_t>::max() / nBlockSize)
{
CPLError(CE_Failure, CPLE_AppDefined,
"Too large values in BLOCKSIZE");
return false;
}
nBlockSize *= static_cast<size_t>(anBlockSize[i]);
}
}
return true;
}

/************************************************************************/
/* DeallocateDecodedTileData() */
/************************************************************************/
Expand Down Expand Up @@ -3186,7 +3248,7 @@ ZarrGroupBase::LoadArray(const std::string &osArrayName,
// of this function call.
SetFilenameAdder filenameAdder(oSetFilenamesInLoading, osZarrayFilename);

const bool isZarrV2 = dynamic_cast<const ZarrGroupV2 *>(this) != nullptr;
const bool isZarrV2 = dynamic_cast<const ZarrV2Group *>(this) != nullptr;

if (isZarrV2)
{
Expand Down
82 changes: 82 additions & 0 deletions frmts/zarr/zarr_dimension.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
/******************************************************************************
*
* Project: GDAL
* Purpose: Zarr driver
* Author: Even Rouault <even dot rouault at spatialys.com>
*
******************************************************************************
* Copyright (c) 2021, Even Rouault <even dot rouault at spatialys.com>
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
****************************************************************************/

#include "zarr.h"

/************************************************************************/
/* Rename() */
/************************************************************************/

bool ZarrDimension::Rename(const std::string &osNewName)
{
if (!m_bUpdatable)
{
CPLError(CE_Failure, CPLE_NotSupported,
"Dataset not open in update mode");
return false;
}
if (!IsXArrayDimension())
{
CPLError(CE_Failure, CPLE_NotSupported,
"Cannot rename an implicit dimension "
"(that is one listed in _ARRAY_DIMENSIONS attribute)");
return false;
}
if (!ZarrGroupBase::IsValidObjectName(osNewName))
{
CPLError(CE_Failure, CPLE_NotSupported, "Invalid dimension name");
return false;
}

if (auto poParentGroup = m_poParentGroup.lock())
{
if (!poParentGroup->RenameDimension(m_osName, osNewName))
{
return false;
}
}

m_osFullName.resize(m_osFullName.size() - m_osName.size());
m_osFullName += osNewName;
m_osName = osNewName;

m_bModified = true;

return true;
}

/************************************************************************/
/* ParentRenamed() */
/************************************************************************/

void ZarrDimension::ParentRenamed(const std::string &osNewParentFullName)
{
m_osFullName = osNewParentFullName;
m_osFullName += "/";
m_osFullName += m_osName;
}
Loading

0 comments on commit 60477b4

Please sign in to comment.