casacore
Loading...
Searching...
No Matches
MultiFile.h
Go to the documentation of this file.
1// # MultiFile.h: Class to combine multiple files in a single one
2// # Copyright (C) 2014
3// # Associated Universities, Inc. Washington DC, USA.
4// #
5// # This library is free software; you can redistribute it and/or modify it
6// # under the terms of the GNU Library General Public License as published by
7// # the Free Software Foundation; either version 2 of the License, or (at your
8// # option) any later version.
9// #
10// # This library is distributed in the hope that it will be useful, but WITHOUT
11// # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12// # FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public
13// # License for more details.
14// #
15// # You should have received a copy of the GNU Library General Public License
16// # along with this library; if not, write to the Free Software Foundation,
17// # Inc., 675 Massachusetts Ave, Cambridge, MA 02139, USA.
18// #
19// # Correspondence concerning AIPS++ should be addressed as follows:
20// # Internet email: casa-feedback@nrao.edu.
21// # Postal address: AIPS++ Project Office
22// # National Radio Astronomy Observatory
23// # 520 Edgemont Road
24// # Charlottesville, VA 22903-2475 USA
25
26#ifndef CASA_MULTIFILE_H
27#define CASA_MULTIFILE_H
28
29// # Includes
30#include <casacore/casa/aips.h>
31#include <casacore/casa/IO/MultiFileBase.h>
32#include <memory>
33
34namespace casacore { // # NAMESPACE CASACORE - BEGIN
35
36// # Forward declarations.
37class ByteIO;
38class CanonicalIO;
39class MemoryIO;
40
41// <summary>
42// Class to combine multiple files in a single one.
43// </summary>
44
45// <use visibility=export>
46
47// <reviewed reviewer="" date="" tests="tMultiFile" demos="">
48// </reviewed>
49
50// <synopsis>
51// This class (derived from MultiFileBase) is a container file holding
52// multiple virtual files in a regular file.
53// It is primarily meant as a container file for the storage manager files
54// of a table to reduce the number of files used (especially for Lustre) and
55// to reduce the number of open files (especially when concatenating tables).
56// <br> MultiFile has the following properties:
57// <ul>
58// <li> It can choose an IO buffer size that matches the file system well
59// (e.g., to support a large buffer size on ZFS or Lustre).
60// <li> O_DIRECT (if supported by the OS) can be used to tell the OS kernel
61// to bypass its file cache. It does not speed up the I/O, but it makes
62// I/O behaviour more predictable which a real-time system might need.
63// <li> Often the data to be read from MultiFile will not exactly match the
64// block size and offset. MultiFile will buffer the data and copy the
65// part that is needed (similar to stdio). However, when matching
66// block size and offset are used, data will directly be read into the
67// user's buffer to achieve zero-copy behaviour.
68// <li> It is possible to nest MultiFile's. Thus a MultiFile can be a file
69// in a parent MultiFile. In this way it is easily possible to store
70// a main table and its subtables (such as an MS) in a single file.
71// <li> Optionally each block is stored with a 32-bit CRC to check if the
72// data in a block are correctly read. The CRC values are stored as
73// part of the header, thus not in each individual block. This is done
74// to make the zero-copy behaviour possible (as described above).
75// <li> The header and the index are stored in the first block. If too large,
76// continuation blocks are used. There are two sets of continuation
77// blocks between which is alternated. This is done for robustness
78// purposes; there is always a valid one in case of a crash in the
79// middle of writing the continuation blocks. Note that the first
80// header block is written after the continuation blocks, so it always
81// points to a valid set of continuation blocks.
82// </ul>
83//
84// The SetupNewTable constructor has a StorageOption argument to define
85// if a MultiFile has to be used and if so, the buffer size to use.
86// It is also possible to specify that through aipsrc variables.
87//
88// A virtual file is spread over multiple (fixed size) data blocks in the
89// MultiFile. A data block is never shared by multiple files.
90// For each virtual file MultiFile keeps a MultiFileInfo object telling
91// the file size and the block numbers used for the file. When flushing
92// the MultiFile, this meta info is written into the header block. If it
93// does not fit in the header block, the rest is written in continuation blocks.
94// On open and resync, it is read back. There are two sets of continuation
95// blocks which are alternately used when the header is written. This is done
96// to have a valid header in case of a crash in the middle of writing the header.
97//
98// A virtual file is represented by an MFFileIO object, which is derived
99// from ByteIO and as such part of the casacore IO framework. It makes it
100// possible for applications to access a virtual file in the same way as
101// a regular file.
102//
103// It is possible to delete a virtual file. Its blocks will be added to
104// the free block list (which is also stored in the meta info).
105// The MultiFile is truncated when blocks are deleted at the end of the file.
106// </synopsis>
107
108// <example>
109// In principle it is possible to use the MultiFile functions directly.
110// However, in general it is much easier to use an MFFileIO object
111// per virtual file as shown below.
112// <srcblock>
113// // Create a new MultiFile using a block size of 1 MB.
114// std::shared_ptr<MultiFileBase> mfile
115// (new MultiFile("file.mf", ByteIO::New, 1048576));
116// // Create a virtual file in it.
117// MFFileIO mf1(mfile, "mf1", ByteIO::New);
118// // Use it (for example) as the sink of AipsIO.
119// AipsIO stream (&mf1);
120// // Write values.
121// stream << (Int)10;
122// stream << True;
123// // Seek to beginning of file and read data in.
124// stream.setpos (0);
125// Int vali;
126// Bool valb;
127// stream >> vali >> valb;
128// </srcblock>
129// </example>
130
131// <todo>
132// <li> MultiFile can be optimized how cont.blocks are used. In case of
133// file truncation, it could check if only cont.blocks are present
134// after the blocks to be removed. In such a case they can be moved
135// backwards. Also the nr of cont.blocks can shrink. In such a case
136// the unused blocks are not added to the free list. Only the nr of
137// actually used cont.blocks is decremented. They could be added to
138// the free list later.
139// The reason for above is that the free list is written into the
140// header blocks before the required nr of continuation blocks is known.
141// <li> Keep a journal file telling which files are created and which
142// blocks are allocated for a virtual file.
143// </todo>
144
145class MultiFile : public MultiFileBase {
146 public:
147 // Open or create a MultiFile with the given name.
148 // Upon creation the block size can be given. If 0, it uses the block size
149 // of the file system the file is on.
150 // <br>If useODirect=True, the O_DIRECT flag is used (if supported).
151 // It tells the kernel to bypass its file cache to have more predictable
152 // I/O behaviour.
153 // <br>If useCRC=True, 32-bit CRC values are calculated and stored for
154 // each data block. Note that useCRC is only used for new files.
156 Bool useODirect = False, Bool useCRC = False);
157
158 // Open or create a MultiFile with the given name which is nested in the
159 // given parent. Thus data are read/written in the parent file.
160 // Upon creation the block size can be given. If 0, it uses the block size
161 // of the parent.
162 explicit MultiFile(const String& name, const std::shared_ptr<MultiFileBase>& parent,
164
165 // The destructor flushes and closes the file.
166 ~MultiFile() override;
167
168 // Copy constructor and assignment not possible.
169 MultiFile(const MultiFile&) = delete;
170 MultiFile& operator=(const MultiFile&) = delete;
171
172 // Make a nested MultiFile.
173 std::shared_ptr<MultiFileBase> makeNested(const std::shared_ptr<MultiFileBase>& parent,
175 Int blockSize) const override;
176
177 // Reopen the underlying file for read/write access.
178 // Nothing will be done if the file is writable already.
179 // Otherwise it will be reopened and an exception will be thrown
180 // if it is not possible to reopen it for read/write access.
181 void reopenRW() override;
182
183 // Fsync the file (i.e., force the data to be physically written).
184 void fsync() override;
185
186 // Show some info.
187 void show(std::ostream&) const;
188
189 // Compress a block index by looking for subsequent block numbers.
190 static std::vector<Int64> packIndex(const std::vector<Int64>& blockNrs);
191
192 // Decompress a block index by inserting subsequent block numbers.
193 static std::vector<Int64> unpackIndex(const std::vector<Int64>& blockNrs);
194
195 private:
196 // Initialize the MultiFile object.
198 // Read the file info for the new version 2.
199 void getInfoVersion2(Int64 contBlockNr, CanonicalIO& aio);
200 // Write a vector of Int64.
201 void writeVector(CanonicalIO& cio, const std::vector<Int64>& index);
202 void writeVector(CanonicalIO& cio, const std::vector<uInt>& index);
203 // Read a vector of Int64.
204 void readVector(CanonicalIO& cio, std::vector<Int64>& index);
205 void readVector(CanonicalIO& cio, std::vector<uInt>& index);
206 // Write the remainder of the header (in case exceeding 1 block).
207 // <src>iobuf</src> should be large enough
209 // Read the remainder of the header into the buffer.
210 void readRemainder(Int64 headerSize, Int64 blockNr, std::vector<char>& buf);
211 // Truncate the file if blocks are freed at the end.
213 // Header writing hooks (meant for derived test classes).
214 virtual void writeHeaderShow(Int64 ncont, Int64 todo) const;
215 virtual void writeHeaderTest();
216 // </group>
217
218 // Do the class-specific actions on opening a file.
219 void doOpenFile(MultiFileInfo&) override;
220 // Do the class-specific actions on closing a file.
221 void doCloseFile(MultiFileInfo&) override;
222 // Do the class-specific actions on adding a file.
223 void doAddFile(MultiFileInfo&) override;
224 // Do the class-specific actions on deleting a file.
226 // Truncate the file to <src>nrblk</src> blocks.
227 void doTruncateFile(MultiFileInfo& info, uInt64 nrblk) override;
228 // Flush the file itself.
229 void doFlushFile() override;
230 // Flush and close the file.
231 void close() override;
232 // Write the header info.
233 void writeHeader() override;
234 // Read the header info. If always==False, the info is only read if the
235 // header counter has changed.
236 void readHeader(Bool always = True) override;
237 // Extend the virtual file to fit lastblk.
238 void extend(MultiFileInfo& info, Int64 lastblk) override;
239
240 protected:
241 // Store the CRC of a data block in the index.
242 void storeCRC(const void* buffer, Int64 blknr);
243 // Check the CRC of a data block read.
244 void checkCRC(const void* buffer, Int64 blknr) const;
245 // Calculate the CRC of a data block.
246 uInt calcCRC(const void* buffer, Int64 size) const;
247 // Extend the virtual file to fit lastblk.
248 // Optionally the free blocks are not used.
249 virtual void extendVF(MultiFileInfo& info, Int64 lastblk, Bool useFreeBlocks);
250 // Write a data block.
251 void writeBlock(MultiFileInfo& info, Int64 blknr, const void* buffer) override;
252 // Read a data block.
253 void readBlock(MultiFileInfo& info, Int64 blknr, void* buffer) override;
254 // Read the version 1 header.
255 void readHeaderVersion1(Int64 headerSize, std::vector<char>& buf);
256 // Read the version 2 and higher header.
257 void readHeaderVersion2(std::vector<char>& buf);
258
259 // # Data members
260 // Define two continuation sets where the header overflow can be stored
262 uInt itsNrContUsed[2]; // nr of cont.blocks actually used
263 uInt itsHdrContInx; // Continuation set last used (0 or 1)
265 std::vector<uInt> itsCRC; // CRC value per block (empty if useCRC=False)
266 std::unique_ptr<ByteIO> itsIO; // A regular file or nested MFFileIO
267};
268
269} // namespace casacore
270
271#endif
OpenOption
Define the possible ByteIO open options.
Definition ByteIO.h:60
virtual void doCloseFile(MultiFileInfo &)=0
Do the class-specific actions on closing a logical file.
Int64 blockSize() const
Get the block size used.
virtual void extend(MultiFileInfo &info, Int64 lastblk)=0
Extend a logical file to fit lastblk.
virtual void doFlushFile()=0
Flush the container file.
virtual void writeHeader()=0
Write the header info.
MultiFileBase(const String &name, Int blockSize, Bool useODirect)
Create a MultiFileBase object with the given name.
virtual void readHeader(Bool always=True)=0
Read the header info.
virtual void doTruncateFile(MultiFileInfo &info, uInt64 nrblk)=0
Truncate the container file to nrblk blocks.
const std::vector< MultiFileInfo > & info() const
Get the info object (for test purposes mainly).
virtual void doOpenFile(MultiFileInfo &)=0
Do the class-specific actions on opening a logical file.
Bool useODirect() const
Is O_DIRECT used?
virtual void doDeleteFile(MultiFileInfo &)=0
Do the class-specific actions on deleting a logical file.
virtual void writeBlock(MultiFileInfo &info, Int64 blknr, const void *buffer)=0
Write a data block of a logical file into the container file.
virtual void readBlock(MultiFileInfo &info, Int64 blknr, void *buffer)=0
Read a data block of a logical file from the container file.
virtual void close()=0
Flush and close the container file.
virtual void doAddFile(MultiFileInfo &)=0
Do the class-specific actions on adding a logical file.
void readRemainder(Int64 headerSize, Int64 blockNr, std::vector< char > &buf)
Read the remainder of the header into the buffer.
void fsync() override
Fsync the file (i.e., force the data to be physically written).
static std::vector< Int64 > packIndex(const std::vector< Int64 > &blockNrs)
Compress a block index by looking for subsequent block numbers.
void writeVector(CanonicalIO &cio, const std::vector< Int64 > &index)
Write a vector of Int64.
MultiFile(const String &name, ByteIO::OpenOption, Int blockSize=0, Bool useODirect=False, Bool useCRC=False)
Open or create a MultiFile with the given name.
MultiFile(const String &name, const std::shared_ptr< MultiFileBase > &parent, ByteIO::OpenOption, Int blockSize=0)
Open or create a MultiFile with the given name which is nested in the given parent.
void readVector(CanonicalIO &cio, std::vector< Int64 > &index)
Read a vector of Int64.
void init(ByteIO::OpenOption option)
Initialize the MultiFile object.
MultiFile & operator=(const MultiFile &)=delete
std::shared_ptr< MultiFileBase > makeNested(const std::shared_ptr< MultiFileBase > &parent, const String &name, ByteIO::OpenOption, Int blockSize) const override
Make a nested MultiFile.
static std::vector< Int64 > unpackIndex(const std::vector< Int64 > &blockNrs)
Decompress a block index by inserting subsequent block numbers.
virtual void writeHeaderShow(Int64 ncont, Int64 todo) const
Header writing hooks (meant for derived test classes).
void reopenRW() override
Reopen the underlying file for read/write access.
~MultiFile() override
The destructor flushes and closes the file.
void show(std::ostream &) const
Show some info.
void writeRemainder(MemoryIO &mio, CanonicalIO &, MultiFileBuffer &mfbuf)
Write the remainder of the header (in case exceeding 1 block).
MultiFile(const MultiFile &)=delete
Copy constructor and assignment not possible.
void getInfoVersion2(Int64 contBlockNr, CanonicalIO &aio)
Read the file info for the new version 2.
void writeVector(CanonicalIO &cio, const std::vector< uInt > &index)
virtual void writeHeaderTest()
void readVector(CanonicalIO &cio, std::vector< uInt > &index)
void truncateIfNeeded()
Truncate the file if blocks are freed at the end.
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
uInt itsNrContUsed[2]
Definition MultiFile.h:262
void checkCRC(const void *buffer, Int64 blknr) const
Check the CRC of a data block read.
const Bool False
Definition aipstype.h:42
uInt itsHdrContInx
Definition MultiFile.h:263
std::vector< uInt > itsCRC
Definition MultiFile.h:265
uInt calcCRC(const void *buffer, Int64 size) const
Calculate the CRC of a data block.
void readHeader(Bool always=True) override
Read the header info.
void doDeleteFile(MultiFileInfo &) override
Do the class-specific actions on deleting a file.
void storeCRC(const void *buffer, Int64 blknr)
Store the CRC of a data block in the index.
void doAddFile(MultiFileInfo &) override
Do the class-specific actions on adding a file.
unsigned int uInt
Definition aipstype.h:49
void doTruncateFile(MultiFileInfo &info, uInt64 nrblk) override
Truncate the file to nrblk blocks.
long long Int64
Define the extra non-standard types used by Casacore (like proposed uSize, Size).
Definition aipsxtype.h:36
void close() override
Flush and close the file.
String name() const
Return the name of the field.
void readBlock(MultiFileInfo &info, Int64 blknr, void *buffer) override
Read a data block.
void readHeaderVersion2(std::vector< char > &buf)
Read the version 2 and higher header.
std::unique_ptr< ByteIO > itsIO
Definition MultiFile.h:266
int Int
Definition aipstype.h:48
bool Bool
Define the standard types used by Casacore.
Definition aipstype.h:40
void readHeaderVersion1(Int64 headerSize, std::vector< char > &buf)
Read the version 1 header.
void writeHeader() override
Write the header info.
size_t size() const
Definition Block.h:566
Bool itsUseCRC
Definition MultiFile.h:264
virtual void extendVF(MultiFileInfo &info, Int64 lastblk, Bool useFreeBlocks)
Extend the virtual file to fit lastblk.
void doFlushFile() override
Flush the file itself.
const Bool True
Definition aipstype.h:41
void writeBlock(MultiFileInfo &info, Int64 blknr, const void *buffer) override
Write a data block.
void doCloseFile(MultiFileInfo &) override
Do the class-specific actions on closing a file.
void extend(MultiFileInfo &info, Int64 lastblk) override
Extend the virtual file to fit lastblk.
void doOpenFile(MultiFileInfo &) override
Do the class-specific actions on opening a file.
MultiFileInfo itsHdrCont[2]
Define two continuation sets where the header overflow can be stored.
Definition MultiFile.h:261
unsigned long long uInt64
Definition aipsxtype.h:37
Helper class for MultiFileBase containing info per logical file.