24#include "openPMD/IO/AbstractIOHandler.hpp"
25#include "openPMD/IO/AbstractIOHandlerImpl.hpp"
26#include "openPMD/IO/Access.hpp"
27#include "openPMD/IO/JSON/JSONFilePosition.hpp"
28#include "openPMD/auxiliary/Filesystem.hpp"
29#include "openPMD/auxiliary/JSON_internal.hpp"
30#include "openPMD/backend/Variant_internal.hpp"
31#include "openPMD/config.hpp"
34#include <nlohmann/json.hpp>
44#include <unordered_map>
45#include <unordered_set>
65 explicit File(std::string s) : fileState{std::make_shared<FileState>(s)}
72 explicit FileState(std::string s) : name{std::move(s)}
77 bool printedReadmeWarningAlready =
false;
80 std::shared_ptr<FileState> fileState;
84 fileState->valid =
false;
89 return fileState->valid;
92 File &operator=(std::string
const &s)
100 fileState = std::make_shared<FileState>(s);
105 bool operator==(File
const &f)
const
107 return this->fileState == f.fileState;
110 std::string &operator*()
const
112 return fileState->name;
115 std::string *operator->()
const
117 return &fileState->name;
120 explicit operator bool()
const
122 return fileState.operator bool();
133 typedef std::size_t result_type;
135 result_type operator()(argument_type
const &s)
const noexcept
137 return std::hash<shared_ptr<openPMD::File::FileState>>{}(s.fileState);
143void to_json(nlohmann::json &j,
const std::complex<T> &p)
145 j = nlohmann::json{p.real(), p.imag()};
149void from_json(
const nlohmann::json &j, std::complex<T> &p)
158class JSONIOHandlerImpl :
public AbstractIOHandlerImpl
160 using json = nlohmann::json;
163 enum class FileFormat
169 explicit JSONIOHandlerImpl(
177 std::string originalExtension);
182 ~JSONIOHandlerImpl()
override;
244 std::future<void> flush();
248 std::optional<MPI_Comm> m_communicator;
251 using FILEHANDLE = std::fstream;
255 std::unordered_map<Writable *, File> m_files;
257 std::unordered_map<File, std::shared_ptr<nlohmann::json>> m_jsonVals;
260 std::unordered_set<File> m_dirty;
265 FileFormat m_fileFormat{};
271 std::string backendConfigKey()
const;
277 std::pair<std::string, std::optional<openPMD::json::TracingJSON>>
280 std::string m_originalExtension;
286 enum class SpecificationVia
296 enum class DatasetMode
310 DatasetMode m_mode{};
311 SpecificationVia m_specificationVia = SpecificationVia::DefaultValue;
312 bool m_skipWarnings =
false;
314 template <
typename A,
typename B,
typename C>
315 operator std::tuple<A, B, C>()
317 return std::tuple<A, B, C>{
318 m_mode, m_specificationVia, m_skipWarnings};
321 DatasetMode_s m_datasetMode;
328 enum class AttributeMode
334 struct AttributeMode_s
338 AttributeMode m_mode{};
339 SpecificationVia m_specificationVia = SpecificationVia::DefaultValue;
341 AttributeMode_s m_attributeMode;
356 std::tuple<std::unique_ptr<FILEHANDLE>, std::istream *, std::ostream *>
360 std::string fullPath(
File const &);
362 std::string fullPath(std::string
const &);
366 static void parentDir(std::string &);
370 static std::string filepositionOf(
Writable *w);
376 template <
typename T,
typename Visitor>
377 static void syncMultidimensionalJson(
379 Offset
const &offset,
380 Extent
const &extent,
381 Extent
const &multiplicator,
384 size_t currentdim = 0);
390 static Extent getMultiplicators(Extent
const &extent);
392 static std::pair<Extent, DatasetMode> getExtent(nlohmann::json &j);
395 static std::string removeSlashes(std::string);
397 template <
typename KeyT>
398 static bool hasKey(nlohmann::json
const &, KeyT &&key);
402 static void ensurePath(nlohmann::json *json, std::string
const &path);
409 std::tuple<File, std::unordered_map<Writable *, File>::iterator,
bool>
410 getPossiblyExisting(std::string
const &file);
414 std::shared_ptr<nlohmann::json> obtainJsonContents(
File const &);
417 nlohmann::json &obtainJsonContents(
Writable *writable);
421 auto putJsonContents(
File const &,
bool unsetDirty =
true)
427 std::shared_ptr<JSONFilePosition>
428 setAndGetFilePosition(
Writable *, std::string
const &extend);
433 std::shared_ptr<JSONFilePosition>
434 setAndGetFilePosition(
Writable *,
bool write =
true);
444 static bool isGroup(nlohmann::json::const_iterator
const &it);
446 static bool isDataset(nlohmann::json
const &j);
449 template <
typename Param>
450 DatasetMode verifyDataset(Param
const ¶meters, nlohmann::json &);
452 static nlohmann::json platformSpecifics();
456 template <
typename T>
458 nlohmann::json &json,
461 static constexpr char const *errorMsg =
"JSON: writeDataset";
466 template <
typename T>
468 nlohmann::json &json,
471 static constexpr char const *errorMsg =
"JSON: readDataset";
474 struct AttributeWriter
476 template <
typename T>
477 static void call(nlohmann::json &, attribute_types
const &);
479 static constexpr char const *errorMsg =
"JSON: writeAttribute";
482 struct AttributeReader
484 template <
typename T>
488 static constexpr char const *errorMsg =
"JSON: writeAttribute";
491 template <
typename T>
494 nlohmann::json operator()(
T const &);
497 template <
typename T>
498 struct CppToJSON<
std::vector<T>>
500 nlohmann::json operator()(std::vector<T>
const &);
503 template <
typename T,
int n>
504 struct CppToJSON<
std::array<T, n>>
506 nlohmann::json operator()(std::array<T, n>
const &);
509 template <
typename T,
typename Enable = T>
512 T operator()(nlohmann::json
const &);
515 template <
typename T>
516 struct JsonToCpp<
std::vector<T>>
518 std::vector<T> operator()(nlohmann::json
const &);
521 template <
typename T,
int n>
522 struct JsonToCpp<
std::array<T, n>>
524 std::array<T, n> operator()(nlohmann::json
const &);
527 template <
typename T>
528 struct JsonToCpp<
T, typename
std::enable_if_t<std::is_floating_point_v<T>>>
530 T operator()(nlohmann::json
const &);
Interface for communicating between logical and physically persistent data.
Definition AbstractIOHandler.hpp:206
void createPath(Writable *, Parameter< Operation::CREATE_PATH > const &) override
Create all necessary groups for a path, possibly recursively.
Definition JSONIOHandlerImpl.cpp:552
void availableChunks(Writable *, Parameter< Operation::AVAILABLE_CHUNKS > &) override
Report chunks that are available for loading from the dataset represented by this writable.
Definition JSONIOHandlerImpl.cpp:880
void touch(Writable *, Parameter< Operation::TOUCH > const &) override
Treat this writable's file as open/active/dirty.
Definition JSONIOHandlerImpl.cpp:1643
void listAttributes(Writable *, Parameter< Operation::LIST_ATTS > &) override
List all attributes associated with an object.
Definition JSONIOHandlerImpl.cpp:1617
void checkFile(Writable *, Parameter< Operation::CHECK_FILE > &) override
Check if the file specified by the parameter is already present on disk.
Definition JSONIOHandlerImpl.cpp:536
void openPath(Writable *, Parameter< Operation::OPEN_PATH > const &) override
Open all contained groups in a path, possibly recursively.
Definition JSONIOHandlerImpl.cpp:930
void readDataset(Writable *, Parameter< Operation::READ_DATASET > &) override
Read a chunk of data from an existing dataset.
Definition JSONIOHandlerImpl.cpp:1240
void deleteAttribute(Writable *, Parameter< Operation::DELETE_ATT > const &) override
Delete an existing attribute.
Definition JSONIOHandlerImpl.cpp:1126
void openFile(Writable *, Parameter< Operation::OPEN_FILE > &) override
Open an existing file assuming it conforms to openPMD.
Definition JSONIOHandlerImpl.cpp:890
void openDataset(Writable *, Parameter< Operation::OPEN_DATASET > &) override
Open an existing dataset and determine its datatype and extent.
Definition JSONIOHandlerImpl.cpp:956
void deletePath(Writable *, Parameter< Operation::DELETE_PATH > const &) override
Delete all objects within an existing path.
Definition JSONIOHandlerImpl.cpp:1008
void writeAttribute(Writable *, Parameter< Operation::WRITE_ATT > const &) override
Create a single attribute and fill the value, possibly overwriting an existing attribute.
Definition JSONIOHandlerImpl.cpp:1174
void deregister(Writable *, Parameter< Operation::DEREGISTER > const &) override
Notify the backend that the Writable has been / will be deallocated.
Definition JSONIOHandlerImpl.cpp:1637
void readAttribute(Writable *, Parameter< Operation::READ_ATT > &) override
Read the value of an existing attribute.
Definition JSONIOHandlerImpl.cpp:1532
void createFile(Writable *, Parameter< Operation::CREATE_FILE > const &) override
Create a new file in physical storage, possibly overriding an existing file.
Definition JSONIOHandlerImpl.cpp:463
void writeDataset(Writable *, Parameter< Operation::WRITE_DATASET > &) override
Write a chunk of data into an existing dataset.
Definition JSONIOHandlerImpl.cpp:1142
void deleteFile(Writable *, Parameter< Operation::DELETE_FILE > const &) override
Delete an existing file from physical storage.
Definition JSONIOHandlerImpl.cpp:977
void listPaths(Writable *, Parameter< Operation::LIST_PATHS > &) override
List all paths/sub-groups inside a group, non-recursively.
Definition JSONIOHandlerImpl.cpp:1580
void closeFile(Writable *, Parameter< Operation::CLOSE_FILE > const &) override
Close the file corresponding with the writable and release file handles.
Definition JSONIOHandlerImpl.cpp:912
void listDatasets(Writable *, Parameter< Operation::LIST_DATASETS > &) override
List all datasets inside a group, non-recursively.
Definition JSONIOHandlerImpl.cpp:1599
void createDataset(Writable *, Parameter< Operation::CREATE_DATASET > const &) override
Create a new dataset of given type, extent and storage properties.
Definition JSONIOHandlerImpl.cpp:587
void deleteDataset(Writable *, Parameter< Operation::DELETE_DATASET > const &) override
Delete an existing dataset.
Definition JSONIOHandlerImpl.cpp:1085
void extendDataset(Writable *, Parameter< Operation::EXTEND_DATASET > const &) override
Increase the extent of an existing dataset.
Definition JSONIOHandlerImpl.cpp:707
Layer to mirror structure of logical data and persistent data in file.
Definition Writable.hpp:76
Extend nlohmann::json with tracing of which keys have been accessed by operator[]().
Definition JSON_internal.hpp:69
Public definitions of openPMD-api.
Definition Date.cpp:29
Access
File access mode to use during IO.
Definition Access.hpp:58
@ T
time
Definition UnitDimension.hpp:41
Definition JSONIOHandlerImpl.hpp:64
Typesafe description of all required arguments for a specified Operation.
Definition IOTask.hpp:148