casacore
Loading...
Searching...
No Matches
BucketFile.h
Go to the documentation of this file.
1// # BucketFile.h: File object for BucketCache
2// # Copyright (C) 1995,1996,1999,2001
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_BUCKETFILE_H
27#define CASA_BUCKETFILE_H
28
29// # Includes
30#include <casacore/casa/aips.h>
31#include <casacore/casa/IO/ByteIO.h>
32#include <casacore/casa/IO/MMapfdIO.h>
33#include <casacore/casa/IO/FilebufIO.h>
34#include <casacore/casa/BasicSL/String.h>
35#include <unistd.h>
36#include <memory>
37
38namespace casacore { // # NAMESPACE CASACORE - BEGIN
39
40// # Forward Declarations
41class MultiFileBase;
42
43// <summary>
44// File object for BucketCache.
45// </summary>
46
47// <use visibility=local>
48
49// <reviewed reviewer="UNKNOWN" date="before2004/08/25" tests="">
50// </reviewed>
51
52// # <prerequisite>
53// # Classes you should understand before using this one.
54// # </prerequisite>
55
56// <etymology>
57// BucketFile represents a data file for the BucketCache class.
58// </etymology>
59
60// <synopsis>
61// A BucketFile object represents a data file. Currently it is used
62// by the Table system, but it can easily be turned into
63// a more general storage manager file class.
64// <br>
65// Creation of a BucketFile object does not open the file yet.
66// An explicit open call has to be given before the file can be used.
67// <p>
68// The file can be opened as an ordinary file (with a file descriptor)
69// or as a file in a MultiFileBase object. An ordinary file can be accessed
70// in 3 ways:
71// <ul>
72// <li> In an unbuffered way, where the parent BucketCache class accesses
73// a bucket at a time (and possibly keeps it in a cache).
74// <li> In a memory-mapped way, where the parent BucketMapped class does
75// the access using the MMapfdIO member.
76// <li> In a buffered way, where the parent BucketBuffered class does
77// the access using the FilebufIO member.
78// </ul>
79// A MultiFileBase file can only be accessed in the unbuffered way.
80// </synopsis>
81
82// <motivation>
83// Encapsulate the file creation and access into a single class
84// to hide the file IO details.
85// </motivation>
86
87// <example>
88// <srcblock>
89// // Create the file for the given storage manager.
90// BucketFile file ("file.name");
91// // Open the file and write into it.
92// file.open();
93// file.write (someBuffer, someLength);
94// // Get the length of the file.
95// uInt size = file.fileSize();
96// </srcblock>
97// </example>
98
99// <todo asof="$DATE:$">
100// <li> Use the ByteIO classes when they are ready.
101// </todo>
102
104 public:
105 // Create a BucketFile object for a new file.
106 // The file with the given name will be created as a normal file or
107 // as part of a MultiFileBase (if mfile != 0).
108 // It can be indicated if a MMapfdIO and/or FilebufIO object must be
109 // created for the file. If a MultiFileBase is used, memory-mapped IO
110 // cannot be used and mappedFile is ignored.
111 explicit BucketFile(
112 const String& fileName, uInt bufSizeFile = 0, Bool mappedFile = False,
113 const std::shared_ptr<MultiFileBase>& mfile = std::shared_ptr<MultiFileBase>());
114
115 // Create a BucketFile object for an existing file.
116 // The file should be opened by the <src>open</src>.
117 // Tell if the file must be opened writable.
118 // It can be indicated if a MMapfdIO and/or FilebufIO object must be
119 // created for the file. If a MultiFileBase is used, memory-mapped IO
120 // cannot be used and mappedFile is ignored.
121 BucketFile(const String& fileName, Bool writable, uInt bufSizeFile = 0, Bool mappedFile = False,
122 const std::shared_ptr<MultiFileBase>& mfile = std::shared_ptr<MultiFileBase>());
123
124 // The destructor closes the file (if open).
125 virtual ~BucketFile();
126
127 // Forbid copy constructor.
128 BucketFile(const BucketFile&) = delete;
129
130 // Forbid assignment.
131 BucketFile& operator=(const BucketFile&) = delete;
132
133 // Make a (temporary) buffered IO object for this file.
134 // That object should not close the file.
135 virtual std::shared_ptr<ByteIO> makeFilebufIO(uInt bufferSize);
136
137 // Get the mapped file object.
139
140 // Get the buffered file object.
142
143 // Open the file if not open yet.
144 virtual void open();
145
146 // Close the file (if open).
147 virtual void close();
148
149 // Remove the file (and close it if needed).
150 virtual void remove();
151
152 // Fsync the file (i.e. force the data to be physically written).
153 virtual void fsync();
154
155 // Set the file to read/write access. It is reopened if not writable.
156 // It does nothing if the file is already writable.
157 virtual void setRW();
158
159 // Get the file name.
160 virtual const String& name() const;
161
162 // Has the file logically been indicated as writable?
163 Bool isWritable() const;
164
165 // Read bytes from the file.
166 virtual uInt read(void* buffer, uInt length);
167
168 // Write bytes into the file.
169 virtual uInt write(const void* buffer, uInt length);
170
171 // Seek in the file.
172 // <group>
173 virtual void seek(Int64 offset);
174 void seek(Int offset);
175 // </group>
176
177 // Get the (physical) size of the file.
178 // This is doing a seek and sets the file pointer to end-of-file.
179 virtual Int64 fileSize() const;
180
181 // Is the file cached, mapped, or buffered?
182 // <group>
183 Bool isCached() const;
184 Bool isMapped() const;
185 Bool isBuffered() const;
186 // </group>
187
188 private:
189 // The file name.
191 // The (logical) writability of the file.
195 int fd_p; // fd (if used) of unbuffered file
196 // The unbuffered file.
197 std::shared_ptr<ByteIO> file_p;
198 // The optional mapped file.
200 // The optional buffered file.
202 // The possibly used MultiFileBase.
203 std::shared_ptr<MultiFileBase> mfile_p;
204
205 // Create the mapped or buffered file object.
207
208 // Delete the possible mapped or buffered file object.
210};
211
212inline const String& BucketFile::name() const { return name_p; }
213
214inline Bool BucketFile::isWritable() const { return isWritable_p; }
215
217
218inline Bool BucketFile::isCached() const { return !isMapped_p && bufSize_p == 0; }
219inline Bool BucketFile::isMapped() const { return isMapped_p; }
220inline Bool BucketFile::isBuffered() const { return bufSize_p > 0; }
221
222} // namespace casacore
223
224#endif
virtual Int64 fileSize() const
Get the (physical) size of the file.
Bool isWritable() const
Has the file logically been indicated as writable?
Definition BucketFile.h:214
std::shared_ptr< MultiFileBase > mfile_p
The possibly used MultiFileBase.
Definition BucketFile.h:203
virtual uInt write(const void *buffer, uInt length)
Write bytes into the file.
std::shared_ptr< ByteIO > file_p
The unbuffered file.
Definition BucketFile.h:197
virtual void open()
Open the file if not open yet.
virtual void close()
Close the file (if open).
Bool isBuffered() const
Definition BucketFile.h:220
Bool isMapped() const
Definition BucketFile.h:219
BucketFile & operator=(const BucketFile &)=delete
Forbid assignment.
BucketFile(const String &fileName, uInt bufSizeFile=0, Bool mappedFile=False, const std::shared_ptr< MultiFileBase > &mfile=std::shared_ptr< MultiFileBase >())
Create a BucketFile object for a new file.
virtual void setRW()
Set the file to read/write access.
void createMapBuf()
Create the mapped or buffered file object.
virtual uInt read(void *buffer, uInt length)
Read bytes from the file.
MMapfdIO * mappedFile()
Get the mapped file object.
Definition BucketFile.h:138
MMapfdIO * mappedFile_p
The optional mapped file.
Definition BucketFile.h:199
virtual ~BucketFile()
The destructor closes the file (if open).
Bool isCached() const
Is the file cached, mapped, or buffered?
Definition BucketFile.h:218
Bool isWritable_p
The (logical) writability of the file.
Definition BucketFile.h:192
FilebufIO * bufferedFile()
Get the buffered file object.
Definition BucketFile.h:141
virtual std::shared_ptr< ByteIO > makeFilebufIO(uInt bufferSize)
Make a (temporary) buffered IO object for this file.
BucketFile(const BucketFile &)=delete
Forbid copy constructor.
virtual void fsync()
Fsync the file (i.e.
String name_p
The file name.
Definition BucketFile.h:190
virtual const String & name() const
Get the file name.
Definition BucketFile.h:212
BucketFile(const String &fileName, Bool writable, uInt bufSizeFile=0, Bool mappedFile=False, const std::shared_ptr< MultiFileBase > &mfile=std::shared_ptr< MultiFileBase >())
Create a BucketFile object for an existing file.
FilebufIO * bufferedFile_p
The optional buffered file.
Definition BucketFile.h:201
void deleteMapBuf()
Delete the possible mapped or buffered file object.
virtual void seek(Int64 offset)
Seek in the file.
virtual void remove()
Remove the file (and close it if needed).
Abstract base class to combine multiple logical files in a single one.
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
const Bool False
Definition aipstype.h:42
int offset(int, int) const
compute a linear offset from array indicies
unsigned int uInt
Definition aipstype.h:49
long long Int64
Define the extra non-standard types used by Casacore (like proposed uSize, Size).
Definition aipsxtype.h:36
LatticeExprNode length(const LatticeExprNode &expr, const LatticeExprNode &axis)
2-argument function to get the length of an axis.
int Int
Definition aipstype.h:48
bool Bool
Define the standard types used by Casacore.
Definition aipstype.h:40