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/config.hpp"
33 #include <nlohmann/json.hpp>
43 #include <unordered_map>
44 #include <unordered_set>
64 explicit File(std::string s) : fileState{std::make_shared<FileState>(s)}
71 explicit FileState(std::string s) : name{std::move(s)}
76 bool printedReadmeWarningAlready =
false;
79 std::shared_ptr<FileState> fileState;
83 fileState->valid =
false;
88 return fileState->valid;
91 File &operator=(std::string
const &s)
99 fileState = std::make_shared<FileState>(s);
104 bool operator==(File
const &f)
const
106 return this->fileState == f.fileState;
109 std::string &operator*()
const
111 return fileState->name;
114 std::string *operator->()
const
116 return &fileState->name;
119 explicit operator bool()
const
121 return fileState.operator bool();
132 typedef std::size_t result_type;
134 result_type operator()(
argument_type const &s)
const noexcept
136 return std::hash<shared_ptr<openPMD::File::FileState>>{}(s.fileState);
142 void to_json(nlohmann::json &j,
const std::complex<T> &p)
144 j = nlohmann::json{p.real(), p.imag()};
148 void from_json(
const nlohmann::json &j, std::complex<T> &p)
159 using json = nlohmann::json;
162 enum class FileFormat
172 std::string originalExtension);
180 std::string originalExtension);
243 std::future<void> flush();
247 std::optional<MPI_Comm> m_communicator;
250 using FILEHANDLE = std::fstream;
254 std::unordered_map<Writable *, File> m_files;
256 std::unordered_map<File, std::shared_ptr<nlohmann::json>> m_jsonVals;
259 std::unordered_set<File> m_dirty;
264 FileFormat m_fileFormat{};
266 std::string m_originalExtension;
278 std::tuple<std::unique_ptr<FILEHANDLE>, std::istream *, std::ostream *>
282 std::string fullPath(
File const &);
284 std::string fullPath(std::string
const &);
288 static void parentDir(std::string &);
292 static std::string filepositionOf(
Writable *w);
298 template <
typename T,
typename Visitor>
299 static void syncMultidimensionalJson(
301 Offset
const &offset,
302 Extent
const &extent,
303 Extent
const &multiplicator,
306 size_t currentdim = 0);
312 static Extent getMultiplicators(Extent
const &extent);
314 static Extent getExtent(nlohmann::json &j);
317 static std::string removeSlashes(std::string);
319 template <
typename KeyT>
320 static bool hasKey(nlohmann::json
const &, KeyT &&key);
324 static void ensurePath(nlohmann::json *json, std::string
const &path);
331 std::tuple<File, std::unordered_map<Writable *, File>::iterator,
bool>
332 getPossiblyExisting(std::string
const &file);
336 std::shared_ptr<nlohmann::json> obtainJsonContents(
File const &);
339 nlohmann::json &obtainJsonContents(
Writable *writable);
343 auto putJsonContents(
File const &,
bool unsetDirty =
true)
344 -> decltype(m_jsonVals)::iterator;
349 std::shared_ptr<JSONFilePosition>
350 setAndGetFilePosition(
Writable *, std::string
const &extend);
355 std::shared_ptr<JSONFilePosition>
356 setAndGetFilePosition(
Writable *,
bool write =
true);
366 static bool isGroup(nlohmann::json::const_iterator
const &it);
368 static bool isDataset(nlohmann::json
const &j);
371 template <
typename Param>
372 void verifyDataset(Param
const ¶meters, nlohmann::json &);
374 static nlohmann::json platformSpecifics();
378 template <
typename T>
380 nlohmann::json &json,
383 static constexpr
char const *errorMsg =
"JSON: writeDataset";
388 template <
typename T>
390 nlohmann::json &json,
393 static constexpr
char const *errorMsg =
"JSON: readDataset";
396 struct AttributeWriter
398 template <
typename T>
399 static void call(nlohmann::json &, Attribute::resource
const &);
401 static constexpr
char const *errorMsg =
"JSON: writeAttribute";
404 struct AttributeReader
406 template <
typename T>
410 static constexpr
char const *errorMsg =
"JSON: writeAttribute";
413 template <
typename T>
416 nlohmann::json operator()(
T const &);
419 template <
typename T>
420 struct CppToJSON<std::vector<T>>
422 nlohmann::json operator()(std::vector<T>
const &);
425 template <
typename T,
int n>
426 struct CppToJSON<std::array<T, n>>
428 nlohmann::json operator()(std::array<T, n>
const &);
431 template <
typename T,
typename Enable = T>
434 T operator()(nlohmann::json
const &);
437 template <
typename T>
438 struct JsonToCpp<std::vector<T>>
440 std::vector<T> operator()(nlohmann::json
const &);
443 template <
typename T,
int n>
444 struct JsonToCpp<std::array<T, n>>
446 std::array<T, n> operator()(nlohmann::json
const &);
449 template <
typename T>
452 typename std::enable_if<std::is_floating_point<T>::value>::type>
454 T operator()(nlohmann::json
const &);
Interface for communicating between logical and physically persistent data.
Definition: AbstractIOHandler.hpp:186
Definition: AbstractIOHandlerImpl.hpp:36
Definition: JSONIOHandlerImpl.hpp:158
void createPath(Writable *, Parameter< Operation::CREATE_PATH > const &) override
Create all necessary groups for a path, possibly recursively.
Definition: JSONIOHandlerImpl.cpp:239
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:603
void listAttributes(Writable *, Parameter< Operation::LIST_ATTS > &) override
List all attributes associated with an object.
Definition: JSONIOHandlerImpl.cpp:1016
void checkFile(Writable *, Parameter< Operation::CHECK_FILE > &) override
Check if the file specified by the parameter is already present on disk.
Definition: JSONIOHandlerImpl.cpp:223
void openPath(Writable *, Parameter< Operation::OPEN_PATH > const &) override
Open all contained groups in a path, possibly recursively.
Definition: JSONIOHandlerImpl.cpp:653
void readDataset(Writable *, Parameter< Operation::READ_DATASET > &) override
Read a chunk of data from an existing dataset.
Definition: JSONIOHandlerImpl.cpp:921
void deleteAttribute(Writable *, Parameter< Operation::DELETE_ATT > const &) override
Delete an existing attribute.
Definition: JSONIOHandlerImpl.cpp:851
void openFile(Writable *, Parameter< Operation::OPEN_FILE > &) override
Open an existing file assuming it conforms to openPMD.
Definition: JSONIOHandlerImpl.cpp:613
void openDataset(Writable *, Parameter< Operation::OPEN_DATASET > &) override
Open an existing dataset and determine its datatype and extent.
Definition: JSONIOHandlerImpl.cpp:679
void deletePath(Writable *, Parameter< Operation::DELETE_PATH > const &) override
Delete all objects within an existing path.
Definition: JSONIOHandlerImpl.cpp:731
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:887
void deregister(Writable *, Parameter< Operation::DEREGISTER > const &) override
Notify the backend that the Writable has been / will be deallocated.
Definition: JSONIOHandlerImpl.cpp:1036
void readAttribute(Writable *, Parameter< Operation::READ_ATT > &) override
Read the value of an existing attribute.
Definition: JSONIOHandlerImpl.cpp:940
void createFile(Writable *, Parameter< Operation::CREATE_FILE > const &) override
Create a new file in physical storage, possibly overriding an existing file.
Definition: JSONIOHandlerImpl.cpp:169
void writeDataset(Writable *, Parameter< Operation::WRITE_DATASET > &) override
Write a chunk of data into an existing dataset.
Definition: JSONIOHandlerImpl.cpp:868
void deleteFile(Writable *, Parameter< Operation::DELETE_FILE > const &) override
Delete an existing file from physical storage.
Definition: JSONIOHandlerImpl.cpp:700
void listPaths(Writable *, Parameter< Operation::LIST_PATHS > &) override
List all paths/sub-groups inside a group, non-recursively.
Definition: JSONIOHandlerImpl.cpp:979
void closeFile(Writable *, Parameter< Operation::CLOSE_FILE > const &) override
Close the file corresponding with the writable and release file handles.
Definition: JSONIOHandlerImpl.cpp:635
void listDatasets(Writable *, Parameter< Operation::LIST_DATASETS > &) override
List all datasets inside a group, non-recursively.
Definition: JSONIOHandlerImpl.cpp:998
void createDataset(Writable *, Parameter< Operation::CREATE_DATASET > const &) override
Create a new dataset of given type, extent and storage properties.
Definition: JSONIOHandlerImpl.cpp:274
void deleteDataset(Writable *, Parameter< Operation::DELETE_DATASET > const &) override
Delete an existing dataset.
Definition: JSONIOHandlerImpl.cpp:809
void extendDataset(Writable *, Parameter< Operation::EXTEND_DATASET > const &) override
Increase the extent of an existing dataset.
Definition: JSONIOHandlerImpl.cpp:350
Layer to mirror structure of logical data and persistent data in file.
Definition: Writable.hpp:75
Extend nlohmann::json with tracing of which keys have been accessed by operator[]().
Definition: JSON_internal.hpp:68
Public definitions of openPMD-api.
Definition: Date.cpp:29
Access
File access mode to use during IO.
Definition: Access.hpp:30
Definition: JSONIOHandlerImpl.hpp:70
Definition: JSONIOHandlerImpl.hpp:63
Definition: IOTask.hpp:621
Definition: IOTask.hpp:133
Definition: IOTask.hpp:182
Definition: IOTask.hpp:312
Definition: IOTask.hpp:114
Definition: IOTask.hpp:218
Definition: IOTask.hpp:502
Definition: IOTask.hpp:387
Definition: IOTask.hpp:199
Definition: IOTask.hpp:273
Definition: IOTask.hpp:641
Definition: IOTask.hpp:347
Definition: IOTask.hpp:577
Definition: IOTask.hpp:451
Definition: IOTask.hpp:292
Definition: IOTask.hpp:366
Definition: IOTask.hpp:160
Definition: IOTask.hpp:254
Definition: IOTask.hpp:555
Definition: IOTask.hpp:429
Definition: IOTask.hpp:521
Definition: IOTask.hpp:406