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::CHECK_FILE:
62  checkFile(
63  i.writable,
64  deref_dynamic_cast<Parameter<Operation::CHECK_FILE> >(
65  i.parameter.get()));
66  break;
67  case O::CREATE_PATH:
68  createPath(
69  i.writable,
70  deref_dynamic_cast<Parameter<O::CREATE_PATH> >(
71  i.parameter.get()));
72  break;
73  case O::CREATE_DATASET:
75  i.writable,
76  deref_dynamic_cast<Parameter<O::CREATE_DATASET> >(
77  i.parameter.get()));
78  break;
79  case O::EXTEND_DATASET:
81  i.writable,
82  deref_dynamic_cast<Parameter<O::EXTEND_DATASET> >(
83  i.parameter.get()));
84  break;
85  case O::OPEN_FILE:
86  openFile(
87  i.writable,
88  deref_dynamic_cast<Parameter<O::OPEN_FILE> >(
89  i.parameter.get()));
90  break;
91  case O::CLOSE_FILE:
92  closeFile(
93  i.writable,
94  deref_dynamic_cast<Parameter<O::CLOSE_FILE> >(
95  i.parameter.get()));
96  break;
97  case O::OPEN_PATH:
98  openPath(
99  i.writable,
100  deref_dynamic_cast<Parameter<O::OPEN_PATH> >(
101  i.parameter.get()));
102  break;
103  case O::CLOSE_PATH:
104  closePath(
105  i.writable,
106  deref_dynamic_cast<Parameter<O::CLOSE_PATH> >(
107  i.parameter.get()));
108  break;
109  case O::OPEN_DATASET:
110  openDataset(
111  i.writable,
112  deref_dynamic_cast<Parameter<O::OPEN_DATASET> >(
113  i.parameter.get()));
114  break;
115  case O::DELETE_FILE:
116  deleteFile(
117  i.writable,
118  deref_dynamic_cast<Parameter<O::DELETE_FILE> >(
119  i.parameter.get()));
120  break;
121  case O::DELETE_PATH:
122  deletePath(
123  i.writable,
124  deref_dynamic_cast<Parameter<O::DELETE_PATH> >(
125  i.parameter.get()));
126  break;
127  case O::DELETE_DATASET:
129  i.writable,
130  deref_dynamic_cast<Parameter<O::DELETE_DATASET> >(
131  i.parameter.get()));
132  break;
133  case O::DELETE_ATT:
135  i.writable,
136  deref_dynamic_cast<Parameter<O::DELETE_ATT> >(
137  i.parameter.get()));
138  break;
139  case O::WRITE_DATASET:
140  writeDataset(
141  i.writable,
142  deref_dynamic_cast<Parameter<O::WRITE_DATASET> >(
143  i.parameter.get()));
144  break;
145  case O::WRITE_ATT:
147  i.writable,
148  deref_dynamic_cast<Parameter<O::WRITE_ATT> >(
149  i.parameter.get()));
150  break;
151  case O::READ_DATASET:
152  readDataset(
153  i.writable,
154  deref_dynamic_cast<Parameter<O::READ_DATASET> >(
155  i.parameter.get()));
156  break;
157  case O::GET_BUFFER_VIEW:
159  i.writable,
160  deref_dynamic_cast<Parameter<O::GET_BUFFER_VIEW> >(
161  i.parameter.get()));
162  break;
163  case O::READ_ATT:
165  i.writable,
166  deref_dynamic_cast<Parameter<O::READ_ATT> >(
167  i.parameter.get()));
168  break;
169  case O::LIST_PATHS:
170  listPaths(
171  i.writable,
172  deref_dynamic_cast<Parameter<O::LIST_PATHS> >(
173  i.parameter.get()));
174  break;
175  case O::LIST_DATASETS:
176  listDatasets(
177  i.writable,
178  deref_dynamic_cast<Parameter<O::LIST_DATASETS> >(
179  i.parameter.get()));
180  break;
181  case O::LIST_ATTS:
183  i.writable,
184  deref_dynamic_cast<Parameter<O::LIST_ATTS> >(
185  i.parameter.get()));
186  break;
187  case O::ADVANCE:
188  advance(
189  i.writable,
190  deref_dynamic_cast<Parameter<O::ADVANCE> >(
191  i.parameter.get()));
192  break;
193  case O::AVAILABLE_CHUNKS:
195  i.writable,
196  deref_dynamic_cast<Parameter<O::AVAILABLE_CHUNKS> >(
197  i.parameter.get()));
198  break;
199  case O::KEEP_SYNCHRONOUS:
201  i.writable,
202  deref_dynamic_cast<Parameter<O::KEEP_SYNCHRONOUS> >(
203  i.parameter.get()));
204  break;
205  case O::DEREGISTER:
206  deregister(
207  i.writable,
208  deref_dynamic_cast<Parameter<O::DEREGISTER> >(
209  i.parameter.get()));
210  break;
211  }
212  }
213  catch (...)
214  {
215  std::cerr << "[AbstractIOHandlerImpl] IO Task "
216  << internal::operationAsString(i.operation)
217  << " failed with exception. Clearing IO queue and "
218  "passing on the exception."
219  << std::endl;
220  while (!m_handler->m_work.empty())
221  {
222  m_handler->m_work.pop();
223  }
224  throw;
225  }
226  (*m_handler).m_work.pop();
227  }
228  return std::future<void>();
229  }
230 
235  virtual void
237 
247  virtual void checkFile(Writable *, Parameter<Operation::CHECK_FILE> &) = 0;
248 
264  virtual void advance(Writable *, Parameter<Operation::ADVANCE> &parameters)
265  {
266  *parameters.status = AdvanceStatus::RANDOMACCESS;
267  }
268 
282  {}
283 
290  virtual void
292 
309  virtual void
320  virtual void
339  virtual void
350  virtual void
363  virtual void openFile(Writable *, Parameter<Operation::OPEN_FILE> &) = 0;
376  virtual void
389  virtual void
402  virtual void
417  virtual void
431  virtual void
442  virtual void
458  virtual void
460 
486  virtual void
488  {
489  // default implementation: operation unsupported by backend
490  parameters.out->backendManagedBuffer = false;
491  }
509  virtual void
524  virtual void
538  virtual void
547  virtual void listPaths(Writable *, Parameter<Operation::LIST_PATHS> &) = 0;
555  virtual void
564  virtual void
566 
572  void
574 
582  virtual void
584 
585  AbstractIOHandler *m_handler;
586 }; // AbstractIOHandlerImpl
587 } // namespace openPMD
virtual void deregister(Writable *, Parameter< Operation::DEREGISTER > const &param)=0
Notify the backend that the Writable has been / will be deallocated.
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.
virtual void checkFile(Writable *, Parameter< Operation::CHECK_FILE > &)=0
Check if the file specified by the parameter is already present on disk.
Self-contained description of a single IO operation.
Definition: IOTask.hpp:695
virtual void openFile(Writable *, Parameter< Operation::OPEN_FILE > &)=0
Open an existing file assuming it conforms to openPMD.
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.
std::shared_ptr< AdvanceStatus > status
output parameter
Definition: IOTask.hpp:629
virtual void advance(Writable *, Parameter< Operation::ADVANCE > &parameters)
Advance the file/stream that this writable belongs to.
Definition: AbstractIOHandlerImpl.hpp:264
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.
there is no stream, it will never be over
Interface for communicating between logical and physically persistent data.
Definition: AbstractIOHandler.hpp:179
virtual void deleteFile(Writable *, Parameter< Operation::DELETE_FILE > const &)=0
Delete an existing file from physical storage.
Public definitions of openPMD-api.
Layer to mirror structure of logical data and persistent data in file.
Definition: Writable.hpp:64
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:120
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 closePath(Writable *, Parameter< Operation::CLOSE_PATH > const &)
Close an openPMD group.
Definition: AbstractIOHandlerImpl.hpp:281
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:487
virtual void deletePath(Writable *, Parameter< Operation::DELETE_PATH > const &)=0
Delete all objects within an existing path.
void keepSynchronous(Writable *, Parameter< Operation::KEEP_SYNCHRONOUS > param)
Treat the current Writable as equivalent to that in the parameter object.
Definition: AbstractIOHandlerImpl.cpp:27
virtual void writeDataset(Writable *, Parameter< Operation::WRITE_DATASET > &)=0
Write a chunk of data into an existing dataset.
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 openPath(Writable *, Parameter< Operation::OPEN_PATH > const &)=0
Open all contained groups in a path, possibly recursively.