Loading...
Searching...
No Matches
BucketFile.h
Go to the documentation of this file.
//# BucketFile.h: File object for BucketCache
//# Copyright (C) 1995,1996,1999,2001
//# Associated Universities, Inc. Washington DC, USA.
//#
//# This library is free software; you can redistribute it and/or modify it
//# under the terms of the GNU Library General Public License as published by
//# the Free Software Foundation; either version 2 of the License, or (at your
//# option) any later version.
//#
//# This library is distributed in the hope that it will be useful, but WITHOUT
//# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
//# FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public
//# License for more details.
//#
//# You should have received a copy of the GNU Library General Public License
//# along with this library; if not, write to the Free Software Foundation,
//# Inc., 675 Massachusetts Ave, Cambridge, MA 02139, USA.
//#
//# Correspondence concerning AIPS++ should be addressed as follows:
//# Internet email: casa-feedback@nrao.edu.
//# Postal address: AIPS++ Project Office
//# National Radio Astronomy Observatory
//# 520 Edgemont Road
//# Charlottesville, VA 22903-2475 USA
#ifndef CASA_BUCKETFILE_H
#define CASA_BUCKETFILE_H
//# Includes
#include <casacore/casa/aips.h>
#include <casacore/casa/IO/ByteIO.h>
#include <casacore/casa/IO/MMapfdIO.h>
#include <casacore/casa/IO/FilebufIO.h>
#include <casacore/casa/BasicSL/String.h>
#include <unistd.h>
#include <memory>
namespace casacore { //# NAMESPACE CASACORE - BEGIN
//# Forward Declarations
class MultiFileBase;
// <summary>
// File object for BucketCache.
// </summary>
// <use visibility=local>
// <reviewed reviewer="UNKNOWN" date="before2004/08/25" tests="">
// </reviewed>
//# <prerequisite>
//# Classes you should understand before using this one.
//# </prerequisite>
// <etymology>
// BucketFile represents a data file for the BucketCache class.
// </etymology>
// <synopsis>
// A BucketFile object represents a data file. Currently it is used
// by the Table system, but it can easily be turned into
// a more general storage manager file class.
// <br>
// Creation of a BucketFile object does not open the file yet.
// An explicit open call has to be given before the file can be used.
// <p>
// The file can be opened as an ordinary file (with a file descriptor)
// or as a file in a MultiFileBase object. An ordinary file can be accessed
// in 3 ways:
// <ul>
// <li> In an unbuffered way, where the parent BucketCache class accesses
// a bucket at a time (and possibly keeps it in a cache).
// <li> In a memory-mapped way, where the parent BucketMapped class does
// the access using the MMapfdIO member.
// <li> In a buffered way, where the parent BucketBuffered class does
// the access using the FilebufIO member.
// </ul>
// A MultiFileBase file can only be accessed in the unbuffered way.
// </synopsis>
// <motivation>
// Encapsulate the file creation and access into a single class
// to hide the file IO details.
// </motivation>
// <example>
// <srcblock>
// // Create the file for the given storage manager.
// BucketFile file ("file.name");
// // Open the file and write into it.
// file.open();
// file.write (someBuffer, someLength);
// // Get the length of the file.
// uInt size = file.fileSize();
// </srcblock>
// </example>
// <todo asof="$DATE:$">
// <li> Use the ByteIO classes when they are ready.
// </todo>
class BucketFile
{
public:
// Create a BucketFile object for a new file.
// The file with the given name will be created as a normal file or
// as part of a MultiFileBase (if mfile != 0).
// It can be indicated if a MMapfdIO and/or FilebufIO object must be
// created for the file. If a MultiFileBase is used, memory-mapped IO
// cannot be used and mappedFile is ignored.
explicit 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 an existing file.
// The file should be opened by the <src>open</src>.
// Tell if the file must be opened writable.
// It can be indicated if a MMapfdIO and/or FilebufIO object must be
// created for the file. If a MultiFileBase is used, memory-mapped IO
// cannot be used and mappedFile is ignored.
BucketFile (const String& fileName, Bool writable,
uInt bufSizeFile=0, Bool mappedFile=False,
const std::shared_ptr<MultiFileBase>& mfile=std::shared_ptr<MultiFileBase>());
// The destructor closes the file (if open).
virtual ~BucketFile();
// Forbid copy constructor.
BucketFile (const BucketFile&) = delete;
// Forbid assignment.
BucketFile& operator= (const BucketFile&) = delete;
// Make a (temporary) buffered IO object for this file.
// That object should not close the file.
virtual std::shared_ptr<ByteIO> makeFilebufIO (uInt bufferSize);
// Get the mapped file object.
MMapfdIO* mappedFile()
{ return mappedFile_p; }
// Get the buffered file object.
FilebufIO* bufferedFile()
{ return bufferedFile_p; }
// Open the file if not open yet.
virtual void open();
// Close the file (if open).
virtual void close();
// Remove the file (and close it if needed).
virtual void remove();
// Fsync the file (i.e. force the data to be physically written).
virtual void fsync();
// Set the file to read/write access. It is reopened if not writable.
// It does nothing if the file is already writable.
virtual void setRW();
// Get the file name.
virtual const String& name() const;
// Has the file logically been indicated as writable?
Bool isWritable() const;
// Read bytes from the file.
virtual uInt read (void* buffer, uInt length);
// Write bytes into the file.
virtual uInt write (const void* buffer, uInt length);
// Seek in the file.
// <group>
virtual void seek (Int64 offset);
void seek (Int offset);
// </group>
// Get the (physical) size of the file.
// This is doing a seek and sets the file pointer to end-of-file.
virtual Int64 fileSize() const;
// Is the file cached, mapped, or buffered?
// <group>
Bool isCached() const;
Bool isMapped() const;
Bool isBuffered() const;
// </group>
private:
// The file name.
String name_p;
// The (logical) writability of the file.
Bool isWritable_p;
Bool isMapped_p;
uInt bufSize_p;
int fd_p; // fd (if used) of unbuffered file
// The unbuffered file.
std::shared_ptr<ByteIO> file_p;
// The optional mapped file.
MMapfdIO* mappedFile_p;
// The optional buffered file.
FilebufIO* bufferedFile_p;
// The possibly used MultiFileBase.
std::shared_ptr<MultiFileBase> mfile_p;
// Create the mapped or buffered file object.
void createMapBuf();
// Delete the possible mapped or buffered file object.
void deleteMapBuf();
};
inline const String& BucketFile::name() const
{ return name_p; }
inline Bool BucketFile::isWritable() const
{ return isWritable_p; }
inline void BucketFile::seek (Int offset)
{ seek (Int64(offset)); }
inline Bool BucketFile::isCached() const
{ return !isMapped_p && bufSize_p==0; }
inline Bool BucketFile::isMapped() const
{ return isMapped_p; }
inline Bool BucketFile::isBuffered() const
{ return bufSize_p>0; }
} //# NAMESPACE CASACORE - END
#endif
casacore::BucketFile::fileSize
virtual Int64 fileSize() const
Get the (physical) size of the file.
casacore::BucketFile::isWritable
Bool isWritable() const
Has the file logically been indicated as writable?
Definition BucketFile.h:222
casacore::BucketFile::mfile_p
std::shared_ptr< MultiFileBase > mfile_p
The possibly used MultiFileBase.
Definition BucketFile.h:208
casacore::BucketFile::bufSize_p
uInt bufSize_p
Definition BucketFile.h:199
casacore::BucketFile::write
virtual uInt write(const void *buffer, uInt length)
Write bytes into the file.
casacore::BucketFile::file_p
std::shared_ptr< ByteIO > file_p
The unbuffered file.
Definition BucketFile.h:202
casacore::BucketFile::open
virtual void open()
Open the file if not open yet.
casacore::BucketFile::close
virtual void close()
Close the file (if open).
casacore::BucketFile::isBuffered
Bool isBuffered() const
Definition BucketFile.h:232
casacore::BucketFile::isMapped
Bool isMapped() const
Definition BucketFile.h:230
casacore::BucketFile::isMapped_p
Bool isMapped_p
Definition BucketFile.h:198
casacore::BucketFile::operator=
BucketFile & operator=(const BucketFile &)=delete
Forbid assignment.
casacore::BucketFile::BucketFile
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.
casacore::BucketFile::setRW
virtual void setRW()
Set the file to read/write access.
casacore::BucketFile::createMapBuf
void createMapBuf()
Create the mapped or buffered file object.
casacore::BucketFile::read
virtual uInt read(void *buffer, uInt length)
Read bytes from the file.
casacore::BucketFile::mappedFile
MMapfdIO * mappedFile()
Get the mapped file object.
Definition BucketFile.h:141
casacore::BucketFile::mappedFile_p
MMapfdIO * mappedFile_p
The optional mapped file.
Definition BucketFile.h:204
casacore::BucketFile::~BucketFile
virtual ~BucketFile()
The destructor closes the file (if open).
casacore::BucketFile::isCached
Bool isCached() const
Is the file cached, mapped, or buffered?
Definition BucketFile.h:228
casacore::BucketFile::isWritable_p
Bool isWritable_p
The (logical) writability of the file.
Definition BucketFile.h:197
casacore::BucketFile::bufferedFile
FilebufIO * bufferedFile()
Get the buffered file object.
Definition BucketFile.h:145
casacore::BucketFile::makeFilebufIO
virtual std::shared_ptr< ByteIO > makeFilebufIO(uInt bufferSize)
Make a (temporary) buffered IO object for this file.
casacore::BucketFile::BucketFile
BucketFile(const BucketFile &)=delete
Forbid copy constructor.
casacore::BucketFile::fsync
virtual void fsync()
Fsync the file (i.e.
casacore::BucketFile::name_p
String name_p
The file name.
Definition BucketFile.h:195
casacore::BucketFile::name
virtual const String & name() const
Get the file name.
Definition BucketFile.h:219
casacore::BucketFile::fd_p
int fd_p
Definition BucketFile.h:200
casacore::BucketFile::BucketFile
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.
casacore::BucketFile::bufferedFile_p
FilebufIO * bufferedFile_p
The optional buffered file.
Definition BucketFile.h:206
casacore::BucketFile::deleteMapBuf
void deleteMapBuf()
Delete the possible mapped or buffered file object.
casacore::BucketFile::seek
virtual void seek(Int64 offset)
Seek in the file.
casacore::BucketFile::remove
virtual void remove()
Remove the file (and close it if needed).
casacore::FilebufIO
Definition FilebufIO.h:93
casacore::MMapfdIO
Definition MMapfdIO.h:62
casacore::MultiFileBase
Abstract base class to combine multiple logical files in a single one.
Definition MultiFileBase.h:128
casacore::String
String: the storage and methods of handling collections of characters.
Definition String.h:223
casacore
this file contains all the compiler specific defines
Definition mainpage.dox:28
casacore::False
const Bool False
Definition aipstype.h:42
casacore::offset
int offset(int, int) const
compute a linear offset from array indicies
casacore::uInt
unsigned int uInt
Definition aipstype.h:49
casacore::Int64
long long Int64
Define the extra non-standard types used by Casacore (like proposed uSize, Size).
Definition aipsxtype.h:36
casacore::length
LatticeExprNode length(const LatticeExprNode &expr, const LatticeExprNode &axis)
2-argument function to get the length of an axis.
casacore::Int
int Int
Definition aipstype.h:48
casacore::Bool
bool Bool
Define the standard types used by Casacore.
Definition aipstype.h:40