RandomAccessFileDevice Class Reference

RandomAccessFileDevice is an implementation of RandomAccessDevice in terms of a FileDevice. More...

#include <RandomAccessFileDevice.h>

Inheritance diagram for RandomAccessFileDevice:

RandomAccessDevice FileDevice List of all members.

Public Member Functions

 RandomAccessFileDevice (std::string filename, DeviceMode mode, FileSize initialSize)
 Opens a file device for random access, specifying an initial size on creation.
 RandomAccessFileDevice (std::string filename, DeviceMode mode)
 Opens a file device for random access.
virtual FileSize getSizeInBytes ()
virtual void setSizeInBytes (FileSize cbNew)
virtual void transfer (RandomAccessRequest const &request)
 Executes a synchronous transfer request for a single random access binding.
virtual void prepareTransfer (RandomAccessRequest &request)
 Prepares for an asynchronous transfer by associating required information about this device (e.g.
virtual void flush ()
virtual int getHandle ()
 
Returns:
OS-defined handle representing this device, or -1 for none

void close ()
bool isOpen () const
 
Returns:
whether the device file is currently open


Protected Attributes

FileSize cbFile
 current file size in bytes
int handle
 the opened OS file
DeviceMode mode
 mode in which file was opened
std::string filename
 path to file in file system
StrictMutex mutex
 On Cygwin, there's no pread/pwrite, so all I/O has to be synchronized per device.

Detailed Description

RandomAccessFileDevice is an implementation of RandomAccessDevice in terms of a FileDevice.

Definition at line 38 of file RandomAccessFileDevice.h.


Constructor & Destructor Documentation

RandomAccessFileDevice::RandomAccessFileDevice ( std::string  filename,
DeviceMode  mode,
FileSize  initialSize 
) [explicit]

Opens a file device for random access, specifying an initial size on creation.

Parameters:
filename path to file
mode modifiers for how to open file
initialSize the initial size (in bytes) of the device, if creating a new file

Definition at line 30 of file RandomAccessFileDevice.cpp.

00032     : FileDevice(filename,mode,initialSize)
00033 {
00034 }

RandomAccessFileDevice::RandomAccessFileDevice ( std::string  filename,
DeviceMode  mode 
) [explicit]

Opens a file device for random access.

Parameters:
filename path to file
mode modifiers for how to open file

Definition at line 36 of file RandomAccessFileDevice.cpp.

00038     : FileDevice(filename,mode,FileSize(0))
00039 {
00040 }


Member Function Documentation

FileSize RandomAccessFileDevice::getSizeInBytes (  )  [virtual]

Reimplemented from FileDevice.

Definition at line 46 of file RandomAccessFileDevice.cpp.

References FileDevice::getSizeInBytes().

00047 {
00048     return FileDevice::getSizeInBytes();
00049 }

void RandomAccessFileDevice::setSizeInBytes ( FileSize  cbNew  )  [virtual]

Reimplemented from FileDevice.

Definition at line 51 of file RandomAccessFileDevice.cpp.

References FileDevice::setSizeInBytes().

00052 {
00053     FileDevice::setSizeInBytes(cbNew);
00054 }

void RandomAccessFileDevice::transfer ( RandomAccessRequest const &  request  )  [virtual]

Executes a synchronous transfer request for a single random access binding.

This is an all-or-nothing request, so unless the result size is equal to the requested transfer size, the request is considered a failure. However, no exception is thrown when failure occurs; instead, the binding notification method is called.

Parameters:
request transfer specification; must have exactly one binding

Reimplemented from FileDevice.

Definition at line 56 of file RandomAccessFileDevice.cpp.

References RandomAccessRequest::pDevice, and FileDevice::transfer().

00057 {
00058     assert(request.pDevice == this);
00059     FileDevice::transfer(request);
00060 }

void RandomAccessFileDevice::prepareTransfer ( RandomAccessRequest request  )  [virtual]

Prepares for an asynchronous transfer by associating required information about this device (e.g.

file handle) with the given request. The actual asynchronous transfer is initiated by a calling DeviceAccessScheduler rather than this RandomAccessDevice itself.

Parameters:
request the encapsulated request parameters

Implements RandomAccessDevice.

Definition at line 62 of file RandomAccessFileDevice.cpp.

References RandomAccessRequest::bindingList, RandomAccessRequest::cbOffset, RandomAccessRequest::cbTransfer, RandomAccessRequestBinding::getBuffer(), RandomAccessRequestBinding::getBufferSize(), FileDevice::handle, RandomAccessRequest::pDevice, RandomAccessRequest::READ, and RandomAccessRequest::type.

00063 {
00064     assert(request.pDevice == this);
00065 
00066 #ifdef USE_AIO_H
00067     int aio_lio_opcode =
00068         (request.type == RandomAccessRequest::READ)
00069         ? LIO_READ : LIO_WRITE;
00070     FileSize cbOffset = request.cbOffset;
00071     for (RandomAccessRequest::BindingListIter
00072              bindingIter(request.bindingList);
00073          bindingIter; ++bindingIter)
00074     {
00075         RandomAccessRequestBinding *pBinding = bindingIter;
00076         pBinding->aio_fildes = handle;
00077         pBinding->aio_lio_opcode = aio_lio_opcode;
00078         // TODO:  initialize constant fields below only once
00079         pBinding->aio_buf = pBinding->getBuffer();
00080         pBinding->aio_nbytes = pBinding->getBufferSize();
00081         pBinding->aio_reqprio = 0;
00082         pBinding->aio_offset = cbOffset;
00083         cbOffset += pBinding->aio_nbytes;
00084     }
00085     assert(cbOffset == request.cbOffset + request.cbTransfer);
00086 #endif
00087 
00088 #ifdef USE_LIBAIO_H
00089     FileSize cbOffset = request.cbOffset;
00090     for (RandomAccessRequest::BindingListIter
00091              bindingIter(request.bindingList);
00092          bindingIter; ++bindingIter)
00093     {
00094         RandomAccessRequestBinding *pBinding = bindingIter;
00095 
00096         if (request.type == RandomAccessRequest::READ) {
00097             io_prep_pread(
00098                 pBinding, handle, pBinding->getBuffer(),
00099                 pBinding->getBufferSize(), cbOffset);
00100         } else {
00101             io_prep_pwrite(
00102                 pBinding, handle, pBinding->getBuffer(),
00103                 pBinding->getBufferSize(), cbOffset);
00104         }
00105         cbOffset += pBinding->getBufferSize();
00106     }
00107     assert(cbOffset == request.cbOffset + request.cbTransfer);
00108 #endif
00109 }

void RandomAccessFileDevice::flush (  )  [virtual]

Reimplemented from FileDevice.

Definition at line 111 of file RandomAccessFileDevice.cpp.

References FileDevice::flush().

00112 {
00113     FileDevice::flush();
00114 }

int RandomAccessFileDevice::getHandle (  )  [virtual]

Returns:
OS-defined handle representing this device, or -1 for none

Implements RandomAccessDevice.

Definition at line 116 of file RandomAccessFileDevice.cpp.

References FileDevice::handle.

00117 {
00118     return handle;
00119 }

void FileDevice::close (  )  [inherited]

Definition at line 168 of file FileDevice.cpp.

References FileDevice::filename, FileDevice::handle, FileDevice::isOpen(), FileDevice::mode, and DeviceMode::temporary.

Referenced by FileDevice::~FileDevice().

00169 {
00170     assert(isOpen());
00171 #ifdef __MSVC__
00172     CloseHandle(HANDLE(handle));
00173 #else
00174     ::close(handle);
00175     if (mode.temporary) {
00176         ::unlink(filename.c_str());
00177     }
00178 #endif
00179     handle = -1;
00180 }

bool FileDevice::isOpen (  )  const [inline, inherited]

Returns:
whether the device file is currently open

Definition at line 100 of file FileDevice.h.

Referenced by FileDevice::close(), FileDevice::FileDevice(), and FileDevice::~FileDevice().

00101     {
00102         return handle == -1 ? 0 : 1;
00103     }


Member Data Documentation

FileSize FileDevice::cbFile [protected, inherited]

current file size in bytes

Definition at line 45 of file FileDevice.h.

Referenced by FileDevice::FileDevice(), and FileDevice::setSizeInBytes().

int FileDevice::handle [protected, inherited]

the opened OS file

Definition at line 50 of file FileDevice.h.

Referenced by FileDevice::close(), FileDevice::FileDevice(), FileDevice::flush(), getHandle(), prepareTransfer(), FileDevice::setSizeInBytes(), and FileDevice::transfer().

DeviceMode FileDevice::mode [protected, inherited]

mode in which file was opened

Definition at line 55 of file FileDevice.h.

Referenced by FileDevice::close(), FileDevice::FileDevice(), and FileDevice::flush().

std::string FileDevice::filename [protected, inherited]

path to file in file system

Definition at line 60 of file FileDevice.h.

Referenced by FileDevice::close(), and FileDevice::FileDevice().

StrictMutex FileDevice::mutex [protected, inherited]

On Cygwin, there's no pread/pwrite, so all I/O has to be synchronized per device.

Definition at line 66 of file FileDevice.h.


The documentation for this class was generated from the following files:
Generated on Mon Jun 22 04:00:44 2009 for Fennel by  doxygen 1.5.1