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
103
105{
106public:
107 // Create a BucketFile object for a new file.
108 // The file with the given name will be created as a normal file or
109 // as part of a MultiFileBase (if mfile != 0).
110 // It can be indicated if a MMapfdIO and/or FilebufIO object must be
111 // created for the file. If a MultiFileBase is used, memory-mapped IO
112 // cannot be used and mappedFile is ignored.
113 explicit BucketFile (const String& fileName,
114 uInt bufSizeFile=0, Bool mappedFile=False,
115 const std::shared_ptr<MultiFileBase>& mfile=std::shared_ptr<MultiFileBase>());
116
117 // Create a BucketFile object for an existing file.
118 // The file should be opened by the <src>open</src>.
119 // Tell if the file must be opened writable.
120 // It can be indicated if a MMapfdIO and/or FilebufIO object must be
121 // created for the file. If a MultiFileBase is used, memory-mapped IO
122 // cannot be used and mappedFile is ignored.
123 BucketFile (const String& fileName, Bool writable,
124 uInt bufSizeFile=0, Bool mappedFile=False,
125 const std::shared_ptr<MultiFileBase>& mfile=std::shared_ptr<MultiFileBase>());
126
127 // The destructor closes the file (if open).
128 virtual ~BucketFile();
129
130 // Forbid copy constructor.
131 BucketFile (const BucketFile&) = delete;
132
133 // Forbid assignment.
135
136 // Make a (temporary) buffered IO object for this file.
137 // That object should not close the file.
138 virtual std::shared_ptr<ByteIO> makeFilebufIO (uInt bufferSize);
139
140 // Get the mapped file object.
142 { return mappedFile_p; }
143
144 // Get the buffered file object.
147
148 // Open the file if not open yet.
149 virtual void open();
150
151 // Close the file (if open).
152 virtual void close();
153
154 // Remove the file (and close it if needed).
155 virtual void remove();
156
157 // Fsync the file (i.e. force the data to be physically written).
158 virtual void fsync();
159
160 // Set the file to read/write access. It is reopened if not writable.
161 // It does nothing if the file is already writable.
162 virtual void setRW();
163
164 // Get the file name.
165 virtual const String& name() const;
166
167 // Has the file logically been indicated as writable?
168 Bool isWritable() const;
169
170 // Read bytes from the file.
171 virtual uInt read (void* buffer, uInt length);
172
173 // Write bytes into the file.
174 virtual uInt write (const void* buffer, uInt length);
175
176 // Seek in the file.
177 // <group>
178 virtual void seek (Int64 offset);
179 void seek (Int offset);
180 // </group>
181
182 // Get the (physical) size of the file.
183 // This is doing a seek and sets the file pointer to end-of-file.
184 virtual Int64 fileSize() const;
185
186 // Is the file cached, mapped, or buffered?
187 // <group>
188 Bool isCached() const;
189 Bool isMapped() const;
190 Bool isBuffered() const;
191 // </group>
192
193private:
194 // The file name.
196 // The (logical) writability of the file.
200 int fd_p; // fd (if used) of unbuffered file
201 // The unbuffered file.
202 std::shared_ptr<ByteIO> file_p;
203 // The optional mapped file.
205 // The optional buffered file.
207 // The possibly used MultiFileBase.
208 std::shared_ptr<MultiFileBase> mfile_p;
209
210
211 // Create the mapped or buffered file object.
213
214 // Delete the possible mapped or buffered file object.
216};
217
218
219inline const String& BucketFile::name() const
220 { return name_p; }
221
223 { return isWritable_p; }
224
225inline void BucketFile::seek (Int offset)
226 { seek (Int64(offset)); }
227
229 { return !isMapped_p && bufSize_p==0; }
231 { return isMapped_p; }
233 { return bufSize_p>0; }
234
235
236} //# NAMESPACE CASACORE - END
237
238#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:222
std::shared_ptr< MultiFileBase > mfile_p
The possibly used MultiFileBase.
Definition BucketFile.h:208
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:202
virtual void open()
Open the file if not open yet.
virtual void close()
Close the file (if open).
Bool isBuffered() const
Definition BucketFile.h:232
Bool isMapped() const
Definition BucketFile.h:230
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:141
MMapfdIO * mappedFile_p
The optional mapped file.
Definition BucketFile.h:204
virtual ~BucketFile()
The destructor closes the file (if open).
Bool isCached() const
Is the file cached, mapped, or buffered?
Definition BucketFile.h:228
Bool isWritable_p
The (logical) writability of the file.
Definition BucketFile.h:197
FilebufIO * bufferedFile()
Get the buffered file object.
Definition BucketFile.h:145
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:195
virtual const String & name() const
Get the file name.
Definition BucketFile.h:219
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:206
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).
String: the storage and methods of handling collections of characters.
Definition String.h:223
this file contains all the compiler specific defines
Definition mainpage.dox:28
const Bool False
Definition aipstype.h:42
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