-
Notifications
You must be signed in to change notification settings - Fork 55
Expand file tree
/
Copy pathAbstractIOHandlerImplCommon.hpp
More file actions
284 lines (255 loc) · 8.34 KB
/
AbstractIOHandlerImplCommon.hpp
File metadata and controls
284 lines (255 loc) · 8.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
/* Copyright 2018-2025 Franz Poeschel, Axel Huebl
*
* This file is part of openPMD-api.
*
* openPMD-api is free software: you can redistribute it and/or modify
* it under the terms of of either the GNU General Public License or
* the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* openPMD-api is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License and the GNU Lesser General Public License
* for more details.
*
* You should have received a copy of the GNU General Public License
* and the GNU Lesser General Public License along with openPMD-api.
* If not, see <http://www.gnu.org/licenses/>.
*/
#pragma once
#include "openPMD/IO/AbstractFilePosition.hpp"
#include "openPMD/IO/AbstractIOHandler.hpp"
#include "openPMD/IO/AbstractIOHandlerImpl.hpp"
#include "openPMD/IO/InvalidatableFile.hpp"
#include "openPMD/auxiliary/StringManip.hpp"
#include "openPMD/backend/Writable.hpp"
#include <set>
#include <stdexcept>
#include <string>
#include <unordered_map>
namespace openPMD
{
template <typename FilePositionType = AbstractFilePosition>
class AbstractIOHandlerImplCommon : public AbstractIOHandlerImpl
{
// friend struct detail::BufferedActions;
public:
explicit AbstractIOHandlerImplCommon(AbstractIOHandler *handler);
~AbstractIOHandlerImplCommon() override;
protected:
/**
* map each Writable to its associated file contains only the filename,
* without the OS path
*/
std::unordered_map<Writable *, InvalidatableFile> m_files;
// MUST be an ordered set in order to consistently flush on different
// parallel processes (same logic cant apply to m_files since Writable*
// pointers are not predictable)
std::set<InvalidatableFile> m_dirty;
enum PossiblyExisting
{
PE_InvalidatableFile = 0,
PE_Iterator,
PE_NewlyCreated,
};
std::tuple<
InvalidatableFile,
std::unordered_map<Writable *, InvalidatableFile>::iterator,
bool>
getPossiblyExisting(std::string file);
void associateWithFile(Writable *writable, InvalidatableFile file);
/**
*
* @return Full OS path of the file.
*/
std::string fullPath(InvalidatableFile);
std::string fullPath(std::string);
/**
* Get the writable's containing file.
* @param writable The writable whose containing file to figure out.
* @param preferParentFile If true, the file is set to the parent's file if
* present. Otherwise, the parent file is only considered if no own file
* is defined. This is usually needed when switching between iterations
* when opening paths.
* @return The containing file of the writable. If its parent is associated
* with another file, update the writable to match its parent and return
* the refreshed file.
*/
InvalidatableFile
refreshFileFromParent(Writable *writable, bool preferParentFile);
/**
* Figure out the file position of the writable.
* Only modify the writable's fileposition when specified.
* @param writable The writable.
* @param write Whether to refresh the writable's file position.
* @return The current file position.
*/
std::shared_ptr<FilePositionType>
setAndGetFilePosition(Writable *writable, bool write = true);
/**
* Figure out the file position of the writable and extend it.
* @param writable The writable.
* @param extend The extension string.
* @return The current file position.
*/
virtual std::shared_ptr<FilePositionType>
setAndGetFilePosition(Writable *writable, std::string extend);
/**
* @return A string representation of the file position.
*/
virtual std::string
filePositionToString(std::shared_ptr<FilePositionType>) = 0;
/**
* @return A new file position that is extended with the given string.
*/
virtual std::shared_ptr<FilePositionType> extendFilePosition(
std::shared_ptr<FilePositionType> const &, std::string) = 0;
};
template <typename FilePositionType>
AbstractIOHandlerImplCommon<FilePositionType>::AbstractIOHandlerImplCommon(
AbstractIOHandler *handler)
: AbstractIOHandlerImpl{handler}
{}
template <typename FilePositionType>
AbstractIOHandlerImplCommon<FilePositionType>::~AbstractIOHandlerImplCommon() =
default;
template <typename FilePositionType>
std::tuple<
InvalidatableFile,
std::unordered_map<Writable *, InvalidatableFile>::iterator,
bool>
AbstractIOHandlerImplCommon<FilePositionType>::getPossiblyExisting(
std::string file)
{
auto it = std::find_if(
m_files.begin(),
m_files.end(),
[file](
std::unordered_map<Writable *, InvalidatableFile>::value_type const
&entry) {
return *entry.second == file && entry.second.valid();
});
bool newlyCreated;
InvalidatableFile name;
if (it == m_files.end())
{
name = file;
newlyCreated = true;
}
else
{
name = it->second;
newlyCreated = false;
}
return std::tuple<
InvalidatableFile,
std::unordered_map<Writable *, InvalidatableFile>::iterator,
bool>(std::move(name), it, newlyCreated);
}
template <typename FilePositionType>
void AbstractIOHandlerImplCommon<FilePositionType>::associateWithFile(
Writable *writable, InvalidatableFile file)
{
// make sure to overwrite
m_files[writable] = std::move(file);
}
template <typename FilePositionType>
std::string AbstractIOHandlerImplCommon<FilePositionType>::fullPath(
InvalidatableFile fileName)
{
return fullPath(*fileName);
}
template <typename FilePositionType>
std::string
AbstractIOHandlerImplCommon<FilePositionType>::fullPath(std::string fileName)
{
if (auxiliary::ends_with(m_handler->directory, "/"))
{
return m_handler->directory + fileName;
}
else
{
return m_handler->directory + "/" + fileName;
}
}
template <typename FilePositionType>
InvalidatableFile
AbstractIOHandlerImplCommon<FilePositionType>::refreshFileFromParent(
Writable *writable, bool preferParentFile)
{
auto getFileFromParent = [writable, this]() {
auto file_it = m_files.find(writable->parent);
if (file_it == m_files.end())
{
std::stringstream s;
s << "Parent Writable " << writable->parent << " of Writable "
<< writable << " has no associated file.";
throw std::runtime_error(s.str());
}
auto file = m_files.find(writable->parent)->second;
associateWithFile(writable, file);
return file;
};
if (preferParentFile && writable->parent)
{
return getFileFromParent();
}
else
{
auto it = m_files.find(writable);
if (it != m_files.end())
{
return m_files.find(writable)->second;
}
else if (writable->parent)
{
return getFileFromParent();
}
else
{
throw std::runtime_error(
"Internal error: Root object must be opened explicitly.");
}
}
}
template <typename FilePositionType>
std::shared_ptr<FilePositionType>
AbstractIOHandlerImplCommon<FilePositionType>::setAndGetFilePosition(
Writable *writable, bool write)
{
std::shared_ptr<AbstractFilePosition> res;
if (writable->abstractFilePosition)
{
res = writable->abstractFilePosition;
}
else if (writable->parent)
{
res = writable->parent->abstractFilePosition;
}
else
{ // we are root
res = std::make_shared<FilePositionType>();
}
if (write)
{
writable->abstractFilePosition = res;
}
return std::dynamic_pointer_cast<FilePositionType>(res);
}
template <typename FilePositionType>
std::shared_ptr<FilePositionType>
AbstractIOHandlerImplCommon<FilePositionType>::setAndGetFilePosition(
Writable *writable, std::string extend)
{
if (!auxiliary::starts_with(extend, '/'))
{
extend = "/" + extend;
}
auto oldPos = setAndGetFilePosition(writable, false);
auto res = extendFilePosition(oldPos, extend);
writable->abstractFilePosition = res;
return res;
}
} // namespace openPMD