AGX Dynamics  2.37.3.2
Hdf5Journal.h
Go to the documentation of this file.
1 /*
2 Copyright 2007-2024. Algoryx Simulation AB.
3 
4 All AGX source code, intellectual property, documentation, sample code,
5 tutorials, scene files and technical white papers, are copyrighted, proprietary
6 and confidential material of Algoryx Simulation AB. You may not download, read,
7 store, distribute, publish, copy or otherwise disseminate, use or expose this
8 material unless having a written signed agreement with Algoryx Simulation AB, or having been
9 advised so by Algoryx Simulation AB for a time limited evaluation, or having purchased a
10 valid commercial license from Algoryx Simulation AB.
11 
12 Algoryx Simulation AB disclaims all responsibilities for loss or damage caused
13 from using this software, unless otherwise stated in written agreements with
14 Algoryx Simulation AB.
15 */
16 
17 
18 #ifndef AGXDATA_HDF5JOURNAL_H
19 #define AGXDATA_HDF5JOURNAL_H
20 
21 
22 #include <agx/config/AGX_USE_HDF5.h>
23 #include <agx/config.h>
24 #if AGX_USE_HDF5()
25 
26 
27 #include <agxData/JournalArchive.h>
28 #include <agx/AgxH5.h>
29 
30 namespace agxData
31 {
32  class Hdf5FrameReader;
33  class Hdf5FrameWriter;
34 
35  AGX_DECLARE_POINTER_TYPES( Hdf5Journal );
36 
37 
42  class AGXCORE_EXPORT Hdf5Journal : public agxData::JournalArchive
43  {
44  public:
45  static bool isHdf5Journal(const agx::String& path);
46 
47  public:
48  AGX_DECLARE_POINTER_TYPES( Session );
49 
54  Hdf5Journal( const agx::String& diskPath, bool readOnlyIfExists=false);
55 
63  virtual JournalArchive::Session* createNewSession( const agx::Name& name ) override;
64 
70  Hdf5Journal::Session* createNewHdf5Session( const agx::Name& name );
71 
73  agxData::Hdf5Journal::Session* getHdf5Session( const agx::Name& name );
74 
76  virtual bool removeSession(agxData::JournalArchive::Session* session) override;
77 
79  virtual bool renameSession(agxData::JournalArchive::Session* session, const agx::Name& newName) override;
80 
82  virtual agx::String getCustomFilesPath(bool create = true) const override;
83 
85  virtual agx::String getCustomFilesPath( const agx::Name& sessionName, bool create = true ) const override;
86 
88  agx::String getHdf5ArchivePath() const;
89 
90  virtual DiskFrameReader* createFrameReader(JournalArchive::Session* session) override;
91  virtual DiskFrameWriter* createFrameWriter(JournalArchive::Session* session) override;
92 
93  protected:
94  virtual ~Hdf5Journal();
95 
96  virtual agxData::JournalArchive::Session* makeCopy(agxData::JournalArchive::Session* session) override;
97 
98  private:
100  void readJournalContent();
101 
102  agxData::Hdf5Journal::Session* downcastSession( agxData::JournalArchive::Session* abstractSession ) const;
103 
104  private:
105  friend class Session;
106  void unlinkSessionData( const agx::Name& name );
107  void renameSessionData( const agx::Name& name, const agx::Name& newName );
108  H5::Group& getSessionsGroup();
109  H5::H5File* getFile();
110 
111  private:
112  H5::H5File* m_file;
113  H5::Group m_sessions;
114  agx::String m_baseName;
115  agx::String m_parentFolder;
116  };
117 
118 
122  class AGXCORE_EXPORT agxData::Hdf5Journal::Session : public agxData::JournalArchive::Session
123  {
124  public:
125 
141  virtual agx::String getPath() const override;
142 
143 
144  /*
145  Get the path to the unpack location for the scene file for this session.
146  The location will only contain a file if the unpackSceneData method has
147  been called, or if a write to the file has been done explicitly from
148  somewhere else.
149 
150  \return The path to the .agx file containing the initial state of the simulation.
151  */
152  virtual agx::String getScenePath(bool create = true) const override;
153 
154 
155 
156  /*
157  Make a copy of the initial scene configuration data stored in the archive
158  to the location pointed to by getScenePath. Returns false if there is no
159  .agx file in the HDF5 archive, or if the write to the location pointed to
160  by getScenePath fails.
161  */
162  virtual bool unpackSceneFile() override;
163 
164 
165  /*
166  Copy the data found in the stringstream into the initial scene
167  configuration stored in the HDF5 archive.
168  */
169  virtual bool packSceneData(std::stringstream& sceneData) override;
170 
171  /*
172  Copy the initial scene data stored in the HDF5 archive into the given string stream.
173  */
174  virtual bool unpackSceneData(std::stringstream& sceneData) override;
175 
176  /*
177  Copy the data found in the stringstream into the initial scene
178  configuration stored in the HDF5 archive.
179  */
180  virtual bool packSceneData(std::istream& sceneData) override;
181 
182  /*
183  Copy the initial scene data stored in the HDF5 archive into the given string stream.
184  */
185  virtual bool unpackSceneData(std::ostream& sceneData) override;
186 
187  /*
188  Get the path to a disk directory where custom files related to the session
189  may be stored. The folder is created, if necessary, if the 'crate'
190  argument is true.
191  */
192  virtual agx::String getCustomFilesPath(bool create = true) const override;
193 
197  virtual bool truncate( agx::UInt firstFrameToRemove ) override;
198 
202  virtual agxData::Frame::Header getFrameHeader( agx::UInt frameIndex ) const override;
203 
205  Hdf5Journal* getHdf5Journal();
207  const Hdf5Journal* getHdf5Journal() const;
208 
209  virtual bool recordExtraData( const agx::String& key, const agx::String& value ) override;
210  virtual bool retrieveExtraData( const agx::String& key, agx::String& value ) override;
211 
212  virtual void savePlot( const PlotData* plot) override;
213  virtual void removePlot( const agx::String& plotName ) override;
214  virtual PlotData* getPlot( const agx::String& plotName ) override;
215  virtual void getAllPlots( PlotDataRefVector& result ) override;
216  virtual void getPlotList( agx::StringVector& result ) override;
217 
218  virtual agx::UInt getNextFrameIndex(agx::UInt currentFrameIndex, agx::Int offset) const override;
219 
220  virtual void writeHeader() override;
221  virtual void finalizeHeader() override;
222 
223  public:
225  static agxData::Hdf5Journal::Session* create( const agx::Name& name, H5::Group& sessions );
226 
228  static agxData::Hdf5Journal::Session* load( const agx::Name& name, H5::Group& sessionRoot );
229 
230  private:
231  friend class agxData::Hdf5Journal;
232 
233  // Rename the data owned by this session in the HDF5 archive.
234  bool renameDiskData( const agx::Name& newName );
235 
236  bool copySessionFolder( agx::String targetFolderPath, bool deleteSource = false );
237 
240  virtual bool transferDiskData( agxData::JournalArchive* target ) override;
241 
242  bool copyDiskData( agxData::Hdf5Journal* target );
243 
244 
246  Session( const agx::Name& name, H5::Group& session );
247 
248 
249  void fixHeader(H5::Group& sessionRoot, H5::Group& header);
250  void finalizeHeader(
251  agx::UInt64 numFrames, agx::UInt64 startFrameIndex, agx::UInt64 endFrameIndex, double startFrameTime, double endFrameTime);
252 
253  private:
254  friend class agxData::Hdf5FrameReader;
255  friend class agxData::Hdf5FrameWriter;
256 
257  H5::Group& getHdf5Node();
258  H5::H5File* getHdf5File();
259 
262  void flushToFile();
263 
264 
265  private:
267  agx::String getCustomFilesPath( const agx::Name& sessionName, bool create = true ) const;
268 
269  // Assumes that the h5::mutex is held by the current thread.
270  bool unpackSceneDataToStream(std::ostream& sceneData);
271 
272  // Open the extra data HDF5 group. If 'create' is false, then the HDF5
273  // library will Throw an exception if the group doesn't exist. The obvious
274  // action is taken if 'create' is true in this case.
275  H5::Group openExtraData(bool create);
276 
277 
278  struct FrameInfo : public agxData::Frame::Header
279  {
280  agx::String nodeName;
281  agx::UInt sequenceIndex;
282  };
283 
284  void createFrameTable();
285 
286  static herr_t iterateSession(hid_t group_id, const char * member_name, const H5L_info_t *, void* operator_data);
287  static bool compareFrames(const FrameInfo& lhs, const FrameInfo& rhs);
288 
289  protected:
290  virtual ~Session();
291 
292 
293  private:
294  H5::Group m_session;
295  typedef agx::HashVector<agx::UInt64, FrameInfo> FrameTable;
296  FrameTable m_frameTable;
297  agx::Vector<FrameInfo> m_tempFrames;
298  };
299 
300 }
301 
302 /* AGX_USE_HDF5 */
303 #endif
304 
305 /* AGXDATA_HDF5JOURNAL_H */
306 #endif
Session(const agx::Name &name)
The JournalArchive provides an abstract class for inspecting and manipulating the contents of a creat...
int32_t Int
Definition: Integer.h:35
#define AGX_DECLARE_POINTER_TYPES(type)
Definition: Referenced.h:254
Representation of a name string.
Definition: Name.h:32
uint32_t UInt
Definition: Integer.h:34
Contains classes for low level data storage for AGX.
Definition: Container.h:23
uint64_t UInt64
Definition: Integer.h:41
#define AGXCORE_EXPORT