casacore
Loading...
Searching...
No Matches
MultiHDF5.h
Go to the documentation of this file.
1
2// # MultiHDF5.h: Class to combine multiple files in a single HDF5 file
3// # Copyright (C) 2015
4// # Associated Universities, Inc. Washington DC, USA.
5// #
6// # This library is free software; you can redistribute it and/or modify it
7// # under the terms of the GNU Library General Public License as published by
8// # the Free Software Foundation; either version 2 of the License, or (at your
9// # option) any later version.
10// #
11// # This library is distributed in the hope that it will be useful, but WITHOUT
12// # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13// # FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public
14// # License for more details.
15// #
16// # You should have received a copy of the GNU Library General Public License
17// # along with this library; if not, write to the Free Software Foundation,
18// # Inc., 675 Massachusetts Ave, Cambridge, MA 02139, USA.
19// #
20// # Correspondence concerning AIPS++ should be addressed as follows:
21// # Internet email: casa-feedback@nrao.edu.
22// # Postal address: AIPS++ Project Office
23// # National Radio Astronomy Observatory
24// # 520 Edgemont Road
25// # Charlottesville, VA 22903-2475 USA
26
27#ifndef CASA_MULTIHDF5_H
28#define CASA_MULTIHDF5_H
29
30// # Includes
31#include <casacore/casa/aips.h>
32#include <casacore/casa/IO/MultiFile.h>
33#include <casacore/casa/HDF5/HDF5File.h>
34#include <casacore/casa/HDF5/HDF5Group.h>
35#include <memory>
36
37namespace casacore { // # NAMESPACE CASACORE - BEGIN
38
39// <summary>
40// Class to combine multiple files in a single HDF5 file.
41// </summary>
42
43// <use visibility=export>
44
45// <reviewed reviewer="" date="" tests="tMultiHDF5" demos="">
46// </reviewed>
47
48// <synopsis>
49// This class is a container file holding multiple virtual files. It is
50// primarily meant as a container file for the storage manager files of a
51// table to reduce the number of files used (especially for Lustre) and to
52// reduce the number of open files (especially when concatenating tables).
53// <br>A secondary goal is offering the ability to use an IO buffer size
54// that matches the file system well (large buffer size for e.g. ZFS).
55//
56// The SetupNewTable constructor has a StorageOption argument to define
57// if a MultiFile has to be used and if so, the buffer size to use.
58// It is also possible to specify that through aipsrc variables.
59//
60// A virtual file is spread over multiple (fixed size) data blocks in the
61// MultiFile. A data block is never shared by multiple files.
62// For each virtual file MultiFile keeps a MultiFileInfo object telling
63// the file size and the blocks numbers used for the file. When flushing
64// the MultiFile, this meta info is written into a header block and,
65// if needed, continuation blocks. On open and resync, it is read back.
66// <br>
67//
68// A virtual file is represented by an MFFileIO object, which is derived
69// from ByteIO and as such part of the casacore IO framework. It makes it
70// possible for applications to access a virtual file in the same way as
71// a regular file.
72//
73// It is possible to delete a virtual file. Its blocks will be added to
74// the free block list (which is also stored in the meta info).
75// </synopsis>
76
77// <example>
78// In principle it is possible to use the MultiFile functions directly.
79// However, in general it is much easier to use an MFFileIO object
80// per virtual file as shown below.
81// <srcblock>
82// // Create a new MultiFile using a block size of 1 MB.
83// MultiFile mfile("file.mf', ByteIO::New, 1048576);
84// // Create a virtual file in it.
85// MFFileIO mf1(mfile, "mf1", ByteIO::New);
86// // Use it (for example) as the sink of AipsIO.
87// AipsIO stream (&mf1);
88// // Write values.
89// stream << (Int)10;
90// stream << True;
91// // Seek to beginning of file and read data in.
92// stream.setpos (0);
93// Int vali;
94// Bool valb;
95// stream >> vali >> valb;
96// </srcblock>
97// </example>
99class MultiHDF5 : public MultiFileBase {
100 public:
101 // Open or create a MultiHDF5 with the given name.
102 // Upon creation the block size can be given. If 0, it uses the block size
103 // of the file system the file is on.
104 explicit MultiHDF5(const String& name, ByteIO::OpenOption, Int blockSize = 0);
105
106 // Open or create a MultiHDF5 which is nested in the given parent.
107 // The data are read/written in a group with the given name in the parent.
108 // Upon creation the block size can be given. If 0, it uses the block size
109 // of the parent.
110 explicit MultiHDF5(const String& name, const std::shared_ptr<MultiFileBase>& parent,
112
113 // The destructor flushes and closes the file.
114 ~MultiHDF5() override;
115
116 // Copy constructor and assignment not possible.
117 MultiHDF5(const MultiHDF5&) = delete;
118 MultiHDF5& operator=(const MultiHDF5&) = delete;
119
120 // Make the correct MultiFileBase object for a nested file.
121 // It creates a new group under which the virtual files are created.
122 std::shared_ptr<MultiFileBase> makeNested(const std::shared_ptr<MultiFileBase>& parent,
124 Int blockSize) const override;
125
126 // Open the given logical file and return its file id.
127 // If the name is unknown, an exception is thrown.
128 // It creates the HDF5 group and dataset opbject.
129 // Reopen the underlying file for read/write access.
130 // Nothing will be done if the file is writable already.
131 // Otherwise it will be reopened and an exception will be thrown
132 // if it is not possible to reopen it for read/write access.
133 void reopenRW() override;
134
135 // Fsync the file (i.e., force the data to be physically written).
136 void fsync() override;
137
138 // Get the HDF5File object.
139 const std::shared_ptr<HDF5File>& getHDF5File() const { return itsFile; }
140 const HDF5Object& getHDF5Object() const { return *itsHDF5; }
141
142 private:
143 // Initialize the MultiFile object.
144 void init(ByteIO::OpenOption option);
145 // Do the class-specific actions on opening a file.
146 void doOpenFile(MultiFileInfo&) override;
147 // Do the class-specific actions on closing a file.
148 void doCloseFile(MultiFileInfo&) override;
149 // Do the class-specific actions on adding a file.
150 void doAddFile(MultiFileInfo&) override;
151 // Do the class-specific actions on deleting a file.
152 void doDeleteFile(MultiFileInfo&) override;
153 // Truncate the file to <src>nrblk</src> blocks (does nothing).
154 void doTruncateFile(MultiFileInfo& info, uInt64 nrblk) override;
155 // Flush the file itself.
156 void doFlushFile() override;
157 // Flush and close the file.
158 void close() override;
159 // Write the header info.
160 void writeHeader() override;
161 // Read the header info. If always==False, the info is only read if the
162 // header counter has changed.
163 void readHeader(Bool always = True) override;
164 // Extend the virtual file to fit lastblk.
165 void extend(MultiFileInfo& info, Int64 lastblk) override;
166 // Read a data block.
167 void readBlock(MultiFileInfo& info, Int64 blknr, void* buffer) override;
168 // Write a data block.
169 void writeBlock(MultiFileInfo& info, Int64 blknr, const void* buffer) override;
170
171 // # Data members
172 std::shared_ptr<HDF5File> itsFile;
173 std::shared_ptr<HDF5Group> itsGroup;
174 // This points to the HDF5Group if present, otherwise to the HDF5File.
175 // It tells where the HDF5 data are accessed.
177};
178
179} // namespace casacore
180
181#endif
OpenOption
Define the possible ByteIO open options.
Definition ByteIO.h:60
Int64 blockSize() const
Get the block size used.
MultiFileBase(const String &name, Int blockSize, Bool useODirect)
Create a MultiFileBase object with the given name.
const std::vector< MultiFileInfo > & info() const
Get the info object (for test purposes mainly).
~MultiHDF5() override
The destructor flushes and closes the file.
std::shared_ptr< HDF5Group > itsGroup
Definition MultiHDF5.h:172
const std::shared_ptr< HDF5File > & getHDF5File() const
Get the HDF5File object.
Definition MultiHDF5.h:138
void doOpenFile(MultiFileInfo &) override
Do the class-specific actions on opening a file.
std::shared_ptr< MultiFileBase > makeNested(const std::shared_ptr< MultiFileBase > &parent, const String &name, ByteIO::OpenOption, Int blockSize) const override
Make the correct MultiFileBase object for a nested file.
void fsync() override
Fsync the file (i.e., force the data to be physically written).
MultiHDF5 & operator=(const MultiHDF5 &)=delete
void reopenRW() override
Open the given logical file and return its file id.
const HDF5Object & getHDF5Object() const
Definition MultiHDF5.h:139
void readHeader(Bool always=True) override
Read the header info.
void doCloseFile(MultiFileInfo &) override
Do the class-specific actions on closing a file.
void doFlushFile() override
Flush the file itself.
std::shared_ptr< HDF5File > itsFile
Definition MultiHDF5.h:171
void doTruncateFile(MultiFileInfo &info, uInt64 nrblk) override
Truncate the file to nrblk blocks (does nothing).
void close() override
Flush and close the file.
void doAddFile(MultiFileInfo &) override
Do the class-specific actions on adding a file.
HDF5Object * itsHDF5
This points to the HDF5Group if present, otherwise to the HDF5File.
Definition MultiHDF5.h:175
void writeHeader() override
Write the header info.
void readBlock(MultiFileInfo &info, Int64 blknr, void *buffer) override
Read a data block.
void extend(MultiFileInfo &info, Int64 lastblk) override
Extend the virtual file to fit lastblk.
void writeBlock(MultiFileInfo &info, Int64 blknr, const void *buffer) override
Write a data block.
void doDeleteFile(MultiFileInfo &) override
Do the class-specific actions on deleting a file.
MultiHDF5(const String &name, ByteIO::OpenOption, Int blockSize=0)
Open or create a MultiHDF5 with the given name.
String: the storage and methods of handling collections of characters.
Definition String.h:355
For temporary backward namespace compatibility, use casa as alias for casacore.
Definition mainpage.dox:28
long long Int64
Define the extra non-standard types used by Casacore (like proposed uSize, Size).
Definition aipsxtype.h:36
String name() const
Return the name of the field.
void init()
Other internal helper function(s).
int Int
Definition aipstype.h:48
bool Bool
Define the standard types used by Casacore.
Definition aipstype.h:40
const Bool True
Definition aipstype.h:41
unsigned long long uInt64
Definition aipsxtype.h:37
Helper class for MultiFileBase containing info per logical file.