openPMD-api
AbstractIOHandlerImpl.hpp
1 /* Copyright 2018-2021 Fabian Koller
2  *
3  * This file is part of openPMD-api.
4  *
5  * openPMD-api is free software: you can redistribute it and/or modify
6  * it under the terms of of either the GNU General Public License or
7  * the GNU Lesser General Public License as published by
8  * the Free Software Foundation, either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * openPMD-api is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License and the GNU Lesser General Public License
15  * for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * and the GNU Lesser General Public License along with openPMD-api.
19  * If not, see <http://www.gnu.org/licenses/>.
20  */
21 #pragma once
22 
23 #include "openPMD/IO/AbstractIOHandler.hpp"
24 #include "openPMD/IO/IOTask.hpp"
25 #include "openPMD/auxiliary/DerefDynamicCast.hpp"
26 
27 #include <future>
28 #include <iostream>
29 
30 namespace openPMD
31 {
32 // class AbstractIOHandler;
33 class Writable;
34 
36 {
37 public:
38  AbstractIOHandlerImpl(AbstractIOHandler *handler) : m_handler{handler}
39  {}
40 
41  virtual ~AbstractIOHandlerImpl() = default;
42 
43  std::future<void> flush()
44  {
45  using namespace auxiliary;
46 
47  while (!(*m_handler).m_work.empty())
48  {
49  IOTask &i = (*m_handler).m_work.front();
50  try
51  {
52  switch (i.operation)
53  {
54  using O = Operation;
55  case O::CREATE_FILE:
56  createFile(
57  i.writable,
58  deref_dynamic_cast<Parameter<Operation::CREATE_FILE> >(
59  i.parameter.get()));
60  break;
61  case O::CREATE_PATH:
62  createPath(
63  i.writable,
64  deref_dynamic_cast<Parameter<O::CREATE_PATH> >(
65  i.parameter.get()));
66  break;
67  case O::CREATE_DATASET:
69  i.writable,
70  deref_dynamic_cast<Parameter<O::CREATE_DATASET> >(
71  i.parameter.get()));
72  break;
73  case O::EXTEND_DATASET:
75  i.writable,
76  deref_dynamic_cast<Parameter<O::EXTEND_DATASET> >(
77  i.parameter.get()));
78  break;
79  case O::OPEN_FILE:
80  openFile(
81  i.writable,
82  deref_dynamic_cast<Parameter<O::OPEN_FILE> >(
83  i.parameter.get()));
84  break;
85  case O::CLOSE_FILE:
86  closeFile(
87  i.writable,
88  deref_dynamic_cast<Parameter<O::CLOSE_FILE> >(
89  i.parameter.get()));
90  break;
91  case O::OPEN_PATH:
92  openPath(
93  i.writable,
94  deref_dynamic_cast<Parameter<O::OPEN_PATH> >(
95  i.parameter.get()));
96  break;
97  case O::CLOSE_PATH:
98  closePath(
99  i.writable,
100  deref_dynamic_cast<Parameter<O::CLOSE_PATH> >(
101  i.parameter.get()));
102  break;
103  case O::OPEN_DATASET:
104  openDataset(
105  i.writable,
106  deref_dynamic_cast<Parameter<O::OPEN_DATASET> >(
107  i.parameter.get()));
108  break;
109  case O::DELETE_FILE:
110  deleteFile(
111  i.writable,
112  deref_dynamic_cast<Parameter<O::DELETE_FILE> >(
113  i.parameter.get()));
114  break;
115  case O::DELETE_PATH:
116  deletePath(
117  i.writable,
118  deref_dynamic_cast<Parameter<O::DELETE_PATH> >(
119  i.parameter.get()));
120  break;
121  case O::DELETE_DATASET:
123  i.writable,
124  deref_dynamic_cast<Parameter<O::DELETE_DATASET> >(
125  i.parameter.get()));
126  break;
127  case O::DELETE_ATT:
129  i.writable,
130  deref_dynamic_cast<Parameter<O::DELETE_ATT> >(
131  i.parameter.get()));
132  break;
133  case O::WRITE_DATASET:
134  writeDataset(
135  i.writable,
136  deref_dynamic_cast<Parameter<O::WRITE_DATASET> >(
137  i.parameter.get()));
138  break;
139  case O::WRITE_ATT:
141  i.writable,
142  deref_dynamic_cast<Parameter<O::WRITE_ATT> >(
143  i.parameter.get()));
144  break;
145  case O::READ_DATASET:
146  readDataset(
147  i.writable,
148  deref_dynamic_cast<Parameter<O::READ_DATASET> >(
149  i.parameter.get()));
150  break;
151  case O::GET_BUFFER_VIEW:
153  i.writable,
154  deref_dynamic_cast<Parameter<O::GET_BUFFER_VIEW> >(
155  i.parameter.get()));
156  break;
157  case O::READ_ATT:
159  i.writable,
160  deref_dynamic_cast<Parameter<O::READ_ATT> >(
161  i.parameter.get()));
162  break;
163  case O::LIST_PATHS:
164  listPaths(
165  i.writable,
166  deref_dynamic_cast<Parameter<O::LIST_PATHS> >(
167  i.parameter.get()));
168  break;
169  case O::LIST_DATASETS:
170  listDatasets(
171  i.writable,
172  deref_dynamic_cast<Parameter<O::LIST_DATASETS> >(
173  i.parameter.get()));
174  break;
175  case O::LIST_ATTS:
177  i.writable,
178  deref_dynamic_cast<Parameter<O::LIST_ATTS> >(
179  i.parameter.get()));
180  break;
181  case O::ADVANCE:
182  advance(
183  i.writable,
184  deref_dynamic_cast<Parameter<O::ADVANCE> >(
185  i.parameter.get()));
186  break;
187  case O::AVAILABLE_CHUNKS:
189  i.writable,
190  deref_dynamic_cast<Parameter<O::AVAILABLE_CHUNKS> >(
191  i.parameter.get()));
192  break;
193  }
194  }
195  catch (...)
196  {
197  std::cerr << "[AbstractIOHandlerImpl] IO Task "
198  << internal::operationAsString(i.operation)
199  << " failed with exception. Removing task"
200  << " from IO queue and passing on the exception."
201  << std::endl;
202  (*m_handler).m_work.pop();
203  throw;
204  }
205  (*m_handler).m_work.pop();
206  }
207  return std::future<void>();
208  }
209 
214  virtual void
216 
233  {}
234 
248  {}
249 
256  virtual void
258 
272  virtual void
283  virtual void
302  virtual void
313  virtual void
326  virtual void
340  virtual void
353  virtual void
366  virtual void
381  virtual void
395  virtual void
406  virtual void
422  virtual void
424 
450  virtual void
452  {
453  // default implementation: operation unsupported by backend
454  parameters.out->backendManagedBuffer = false;
455  }
468  virtual void
483  virtual void
497  virtual void
506  virtual void listPaths(Writable *, Parameter<Operation::LIST_PATHS> &) = 0;
514  virtual void
523  virtual void
525 
526  AbstractIOHandler *m_handler;
527 }; // AbstractIOHandlerImpl
528 } // namespace openPMD
virtual void writeDataset(Writable *, Parameter< Operation::WRITE_DATASET > const &)=0
Write a chunk of data into an existing dataset.
virtual void deleteAttribute(Writable *, Parameter< Operation::DELETE_ATT > const &)=0
Delete an existing attribute.
virtual void writeAttribute(Writable *, Parameter< Operation::WRITE_ATT > const &)=0
Create a single attribute and fill the value, possibly overwriting an existing attribute.
Self-contained description of a single IO operation.
Definition: IOTask.hpp:615
virtual void deleteDataset(Writable *, Parameter< Operation::DELETE_DATASET > const &)=0
Delete an existing dataset.
virtual void createDataset(Writable *, Parameter< Operation::CREATE_DATASET > const &)=0
Create a new dataset of given type, extent and storage properties.
virtual void listPaths(Writable *, Parameter< Operation::LIST_PATHS > &)=0
List all paths/sub-groups inside a group, non-recursively.
virtual void createFile(Writable *, Parameter< Operation::CREATE_FILE > const &)=0
Create a new file in physical storage, possibly overriding an existing file.
virtual void extendDataset(Writable *, Parameter< Operation::EXTEND_DATASET > const &)=0
Increase the extent of an existing dataset.
Interface for communicating between logical and physically persistent data.
Definition: AbstractIOHandler.hpp:122
virtual void deleteFile(Writable *, Parameter< Operation::DELETE_FILE > const &)=0
Delete an existing file from physical storage.
Public definitions of openPMD-api.
Definition: Date.cpp:28
Layer to mirror structure of logical data and persistent data in file.
Definition: Writable.hpp:63
virtual void readDataset(Writable *, Parameter< Operation::READ_DATASET > &)=0
Read a chunk of data from an existing dataset.
virtual void listAttributes(Writable *, Parameter< Operation::LIST_ATTS > &)=0
List all attributes associated with an object.
Typesafe description of all required arguments for a specified Operation.
Definition: IOTask.hpp:93
virtual void openDataset(Writable *, Parameter< Operation::OPEN_DATASET > &)=0
Open an existing dataset and determine its datatype and extent.
virtual void closeFile(Writable *, Parameter< Operation::CLOSE_FILE > const &)=0
Close the file corresponding with the writable and release file handles.
virtual void openFile(Writable *, Parameter< Operation::OPEN_FILE > const &)=0
Open an existing file assuming it conforms to openPMD.
virtual void closePath(Writable *, Parameter< Operation::CLOSE_PATH > const &)
Close an openPMD group.
Definition: AbstractIOHandlerImpl.hpp:247
virtual void getBufferView(Writable *, Parameter< Operation::GET_BUFFER_VIEW > &parameters)
Get a view into a dataset buffer that can be filled by a user.
Definition: AbstractIOHandlerImpl.hpp:451
virtual void deletePath(Writable *, Parameter< Operation::DELETE_PATH > const &)=0
Delete all objects within an existing path.
virtual void listDatasets(Writable *, Parameter< Operation::LIST_DATASETS > &)=0
List all datasets inside a group, non-recursively.
virtual void availableChunks(Writable *, Parameter< Operation::AVAILABLE_CHUNKS > &)=0
Report chunks that are available for loading from the dataset represented by this writable...
Definition: AbstractIOHandlerImpl.hpp:35
virtual void createPath(Writable *, Parameter< Operation::CREATE_PATH > const &)=0
Create all necessary groups for a path, possibly recursively.
virtual void readAttribute(Writable *, Parameter< Operation::READ_ATT > &)=0
Read the value of an existing attribute.
virtual void advance(Writable *, Parameter< Operation::ADVANCE > &)
Advance the file/stream that this writable belongs to.
Definition: AbstractIOHandlerImpl.hpp:232
virtual void openPath(Writable *, Parameter< Operation::OPEN_PATH > const &)=0
Open all contained groups in a path, possibly recursively.