CMS 3D CMS Logo

LocalCacheFile Class Reference

Proxy class to copy a file locally in large chunks. More...

#include <Utilities/StorageFactory/interface/LocalCacheFile.h>

Inheritance diagram for LocalCacheFile:

Storage IOInput IOOutput

List of all members.

Public Member Functions

virtual void close (void)
virtual void flush (void)
 LocalCacheFile (Storage *base, const std::string &tmpdir="")
virtual IOOffset position (IOOffset offset, Relative whence=SET)
virtual bool prefetch (const IOPosBuffer *what, IOSize n)
virtual IOSize read (void *into, IOSize n, IOOffset pos)
virtual IOSize read (void *into, IOSize n)
 Read into into at most n number of bytes.
virtual IOSize readv (IOPosBuffer *into, IOSize n)
virtual IOSize readv (IOBuffer *into, IOSize n)
 Read from the input stream into multiple scattered buffers.
virtual void resize (IOOffset size)
virtual IOSize write (const void *from, IOSize n, IOOffset pos)
virtual IOSize write (const void *from, IOSize n)
 Write n bytes of data starting at address from.
virtual IOSize writev (const IOPosBuffer *from, IOSize n)
virtual IOSize writev (const IOBuffer *from, IOSize n)
 Write to the output stream from multiple buffers.
 ~LocalCacheFile (void)

Private Member Functions

void cache (IOOffset start, IOOffset end)

Private Attributes

Filefile_
IOOffset image_
std::vector< char > present_
Storagestorage_


Detailed Description

Proxy class to copy a file locally in large chunks.

Definition at line 10 of file LocalCacheFile.h.


Constructor & Destructor Documentation

LocalCacheFile::LocalCacheFile ( Storage base,
const std::string &  tmpdir = "" 
)

Definition at line 22 of file LocalCacheFile.cc.

References CHUNK_SIZE, Exception, fd, file_, image_, mkstemp(), p, present_, File::resize(), and pyDBSRunClass::temp.

00023   : image_(base->size()),
00024     file_(0),
00025     storage_(base)
00026 {
00027   present_.resize((image_ + CHUNK_SIZE - 1) / CHUNK_SIZE, 0);
00028 
00029   std::string pattern(tmpdir);
00030   if (pattern.empty())
00031     if (char *p = getenv("TMPDIR"))
00032       pattern = p;
00033   if (pattern.empty())
00034     pattern = "/tmp";
00035   pattern += "/cmssw-shadow-XXXXXX";
00036 
00037   std::vector<char> temp(pattern.c_str(), pattern.c_str()+pattern.size()+1);
00038   int fd = mkstemp(&temp[0]);
00039   if (fd == -1)
00040     throw cms::Exception("LocalCacheFile")
00041       << "Cannot create temporary file '" << pattern << "': "
00042       << strerror(errno) << " (error " << errno << ")";
00043 
00044   unlink(&temp[0]);
00045   file_ = new File(fd);
00046   file_->resize(image_);
00047 }

LocalCacheFile::~LocalCacheFile ( void   ) 

Definition at line 49 of file LocalCacheFile.cc.

References file_, and storage_.

00050 {
00051   delete file_;
00052   delete storage_;
00053 }


Member Function Documentation

void LocalCacheFile::cache ( IOOffset  start,
IOOffset  end 
) [private]

Definition at line 56 of file LocalCacheFile.cc.

References CHUNK_SIZE, e, Exception, IOChannel::fd(), file_, image_, index, len, min, mmap, present_, Storage::read(), storage_, and cms::Exception::what().

Referenced by prefetch(), read(), and readv().

00057 {
00058   start = (start / CHUNK_SIZE) * CHUNK_SIZE;
00059   end = std::min(end, image_);
00060 
00061   IOSize nread = 0;
00062   IOSize index = start / CHUNK_SIZE;
00063 
00064   while (start < end)
00065   {
00066     IOSize len = std::min(image_ - start, CHUNK_SIZE);
00067     if (! present_[index])
00068     {
00069       void *window = mmap(0, len, PROT_READ | PROT_WRITE, MAP_SHARED, file_->fd(), start);
00070       if (window == MAP_FAILED)
00071         throw cms::Exception("LocalCacheFile")
00072           << "Unable to map a window of local cache file: "
00073           << strerror(errno) << " (error " << errno << ")";
00074 
00075       try
00076       {
00077         nread = storage_->read(window, len, start);
00078       }
00079       catch (cms::Exception &e)
00080       {
00081         munmap(window, len);
00082         throw cms::Exception("LocalCacheFile")
00083           << "Unable to cache " << len << " byte file segment at " << start
00084           << ": " << e.what();
00085       }
00086 
00087       munmap(window, len);
00088 
00089       if (nread != len)
00090         throw cms::Exception("LocalCacheFile")
00091           << "Unable to cache " << len << " byte file segment at " << start
00092           << ": got only " << nread << " bytes back";
00093 
00094       present_[index] = 1;
00095     }
00096 
00097     start += len;
00098     ++index;
00099   }
00100 }

void LocalCacheFile::close ( void   )  [virtual]

Reimplemented from Storage.

Definition at line 172 of file LocalCacheFile.cc.

References Storage::close(), File::close(), file_, and storage_.

00173 {
00174   storage_->close();
00175   file_->close();
00176 }

void LocalCacheFile::flush ( void   )  [virtual]

Reimplemented from Storage.

Definition at line 168 of file LocalCacheFile.cc.

References nowrite().

00169 { nowrite("flush"); }

IOOffset LocalCacheFile::position ( IOOffset  offset,
Relative  whence = SET 
) [virtual]

Implements Storage.

Definition at line 160 of file LocalCacheFile.cc.

References file_, and File::position().

00161 { return file_->position(offset, whence); }

bool LocalCacheFile::prefetch ( const IOPosBuffer what,
IOSize  n 
) [virtual]

Reimplemented from Storage.

Definition at line 179 of file LocalCacheFile.cc.

References cache(), end, file_, i, IOPosBuffer::offset(), File::prefetch(), and IOPosBuffer::size().

00180 {
00181   for (IOSize i = 0; i < n; ++i)
00182   {
00183     IOOffset start = what[i].offset();
00184     IOOffset end = start + what[i].size();
00185     cache(start, end);
00186   }
00187   
00188   return file_->prefetch(what, n);
00189 }

IOSize LocalCacheFile::read ( void into,
IOSize  n,
IOOffset  pos 
) [virtual]

Reimplemented from Storage.

Definition at line 112 of file LocalCacheFile.cc.

References cache(), file_, and File::read().

00113 {
00114   cache(pos, pos + n);
00115   return file_->read(into, n, pos);
00116 }

IOSize LocalCacheFile::read ( void into,
IOSize  n 
) [virtual]

Read into into at most n number of bytes.

If this is a blocking stream, the call will block until some data can be read, end of input is reached, or an exception is thrown. For a non-blocking stream the available input is returned. If none is available, an exception is thrown.

Returns:
The number of bytes actually read. This is less or equal to the size of the buffer. Zero indicates that the end of the input has been reached: end of file, or remote end closing for a connected channel like a pipe or a socket. Otherwise the value can be less than requested if limited amount of input is currently available for platform or implementation reasons.
Exceptions:
In case of error, a IOError exception is thrown. This includes the situation where the input stream is in non-blocking mode and no input is currently available (FIXME: make this simpler; clarify which exception).

Implements IOInput.

Definition at line 103 of file LocalCacheFile.cc.

References cache(), file_, File::position(), and File::read().

00104 {
00105   IOOffset here = file_->position();
00106   cache(here, here + n);
00107 
00108   return file_->read(into, n);
00109 }

IOSize LocalCacheFile::readv ( IOPosBuffer into,
IOSize  n 
) [virtual]

Reimplemented from Storage.

Definition at line 131 of file LocalCacheFile.cc.

References cache(), end, i, IOPosBuffer::offset(), Storage::readv(), IOPosBuffer::size(), and storage_.

00132 {
00133   for (IOSize i = 0; i < n; ++i)
00134   {
00135     IOOffset start = into[i].offset();
00136     IOOffset end = start + into[i].size();
00137     cache(start, end);
00138   }
00139 
00140   return storage_->readv(into, n);
00141 }

IOSize LocalCacheFile::readv ( IOBuffer into,
IOSize  buffers 
) [virtual]

Read from the input stream into multiple scattered buffers.

There are buffers to fill in an array starting at into; the memory those buffers occupy does not need to be contiguous. The buffers are filled in the order given, eac buffer is filled fully before the subsequent buffers.

If this is a blocking stream, the call will block until some data can be read, end of input is reached, or an exception is thrown. For a non-blocking stream the available input is returned. If none is available, an exception is thrown.

The base class implementation uses read(void *, IOSize) method, but derived classes may implement a more efficient alternative.

Returns:
The number of bytes actually read. This is less or equal to the size of the buffer. Zero indicates that the end of the input has been reached: end of file, or remote end closing for a connected channel like a pipe or a socket. Otherwise the value can be less than requested if limited amount of input is currently available for platform or implementation reasons. Note that the return value indicates the number of bytes read, not the number of buffers; it is the sum total of bytes filled into all the buffers.
Exceptions:
In case of error, a IOError exception is thrown. However if some data has already been read, the error is swallowed and the method returns the data read so far. It is assumed that persistent errors will occur anyway on the next read and sporadic errors like stream becoming unvailable can be ignored. Use xread() if a different policy is desirable.

Reimplemented from IOInput.

Definition at line 119 of file LocalCacheFile.cc.

References cache(), end, file_, i, File::position(), File::readv(), and Storage::size().

00120 {
00121   IOOffset start = file_->position();
00122   IOOffset end = start;
00123   for (IOSize i = 0; i < n; ++i)
00124     end += into[i].size();
00125   cache(start, end);
00126 
00127   return file_->readv(into, n);
00128 }

void LocalCacheFile::resize ( IOOffset  size  )  [virtual]

Implements Storage.

Definition at line 164 of file LocalCacheFile.cc.

References nowrite().

00165 { nowrite("resize"); }

IOSize LocalCacheFile::write ( const void from,
IOSize  n,
IOOffset  pos 
) [virtual]

Reimplemented from Storage.

Definition at line 148 of file LocalCacheFile.cc.

References nowrite().

00149 { nowrite("write"); return 0; }

IOSize LocalCacheFile::write ( const void from,
IOSize  n 
) [virtual]

Write n bytes of data starting at address from.

Returns:
The number of bytes written. Normally this will be n, but can be less, even zero, for example if the stream is non-blocking mode and cannot accept input at this time.
Exceptions:
In case of error, an exception is thrown. However if the stream is in non-blocking mode and cannot accept output, it will not throw an exception -- the return value will be less than requested.

Implements IOOutput.

Definition at line 144 of file LocalCacheFile.cc.

References nowrite().

00145 { nowrite("write"); return 0; }

IOSize LocalCacheFile::writev ( const IOPosBuffer from,
IOSize  n 
) [virtual]

Reimplemented from Storage.

Definition at line 156 of file LocalCacheFile.cc.

References nowrite().

00157 { nowrite("writev"); return 0; }

IOSize LocalCacheFile::writev ( const IOBuffer from,
IOSize  buffers 
) [virtual]

Write to the output stream from multiple buffers.

There are buffers to fill in an array starting at from. The buffers are filled in the order given, each buffer fully before the subsequent buffers. The method uses write(const void *, IOSize), but may be implemented more efficiently in derived classes.

Note that derived classes should not normally call this method, as it simply routes the call back to derived class through the other virtual functions. Use this method only at the "outside edge" when transferring calls from one object to another, not in up/down calls in the inheritance tree.

Returns:
The number of bytes actually written. This is less or equal to the size of the buffers. The value can be less than requested if the stream is unable to accept all the output for platform or implementation reasons. Note that the return value indicates the number of bytes written, not the number of buffers; it is the sum total of bytes written from all the buffers.
Exceptions:
In case of error, an exception is thrown. However if the stream is in non-blocking mode and cannot accept output, it will not throw an exception -- the return value will be less than requested.

Reimplemented from IOOutput.

Definition at line 152 of file LocalCacheFile.cc.

References nowrite().

00153 { nowrite("writev"); return 0; }


Member Data Documentation

File* LocalCacheFile::file_ [private]

Definition at line 39 of file LocalCacheFile.h.

Referenced by cache(), close(), LocalCacheFile(), position(), prefetch(), read(), readv(), and ~LocalCacheFile().

IOOffset LocalCacheFile::image_ [private]

Definition at line 37 of file LocalCacheFile.h.

Referenced by cache(), and LocalCacheFile().

std::vector<char> LocalCacheFile::present_ [private]

Definition at line 38 of file LocalCacheFile.h.

Referenced by cache(), and LocalCacheFile().

Storage* LocalCacheFile::storage_ [private]

Definition at line 40 of file LocalCacheFile.h.

Referenced by cache(), close(), readv(), and ~LocalCacheFile().


The documentation for this class was generated from the following files:
Generated on Tue Jun 9 18:27:54 2009 for CMSSW by  doxygen 1.5.4