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