TAO_Notify::Random_File Class Reference

A random file class. More...

#include <Random_File.h>

Inheritance diagram for TAO_Notify::Random_File:

Inheritance graph
[legend]
Collaboration diagram for TAO_Notify::Random_File:

Collaboration graph
[legend]
List of all members.

Public Member Functions

 Random_File ()
 The constructor.
 ~Random_File ()
 The destructor, which closes the open file.
bool open (const ACE_TCHAR *filename, size_t block_size=512)
 Open a file with default permissions.
size_t block_size () const
ACE_OFF_T size () const
 Return the current file size, in number of blocks.
bool write (const size_t block_number, void *buffer, bool atomic=false)
bool read (const size_t block_number, void *buffer)
 Read a block from our file.

Private Member Functions

bool seek (const size_t block_number)
 Seek to a given block number, used by reads and writes.
bool sync ()
 Synchronize the file to disk, used to implement atomic.

Private Attributes

size_t block_size_
TAO_SYNCH_MUTEX lock_

Detailed Description

A random file class.

Derived from ACE_FILE, this class provides access to a file of fixed-size blocks.

Definition at line 45 of file Random_File.h.


Constructor & Destructor Documentation

TAO_Notify::Random_File::Random_File (  ) 

The constructor.

Definition at line 18 of file Random_File.cpp.

00019   : block_size_(512)
00020 {
00021 }

TAO_Notify::Random_File::~Random_File (  ) 

The destructor, which closes the open file.

Definition at line 23 of file Random_File.cpp.

References ACE_FILE::close().

00024 {
00025   this->close();
00026 }


Member Function Documentation

size_t TAO_Notify::Random_File::block_size (  )  const

Accessor for the block size. Note signed size_t is used to be compatible with ACE_FILE.

Definition at line 29 of file Random_File.cpp.

References block_size_.

Referenced by TAO_Notify::Persistent_File_Allocator::block_size(), read(), and write().

00030 {
00031   return this->block_size_;
00032 }

bool TAO_Notify::Random_File::open ( const ACE_TCHAR filename,
size_t  block_size = 512 
)

Open a file with default permissions.

Definition at line 51 of file Random_File.cpp.

References ACE_DEBUG, ACE_DEFAULT_FILE_PERMS, ACE_GUARD_RETURN, ACE_TEXT(), ACE_FILE::addr_, block_size_, ACE_FILE::close(), DEBUG_LEVEL, LM_DEBUG, O_BINARY, ACE_OS::open(), ACE_FILE_Addr::set(), ACE_IO_SAP::set_handle(), and TAO_SYNCH_MUTEX.

Referenced by TAO_Notify::Persistent_File_Allocator::open().

00052 {
00053   ACE_GUARD_RETURN (TAO_SYNCH_MUTEX, ace_mon, this->lock_, false);
00054   this->block_size_ = block_size;
00055   bool result = (this->close() == 0);
00056 
00057   if (result)
00058   {
00059     if (DEBUG_LEVEL > 8) ACE_DEBUG ((LM_DEBUG,
00060       ACE_TEXT ("(%P|%t) Opening file %s\n")
00061       , filename
00062       ));
00063     ACE_HANDLE handle = ACE_OS::open(filename,
00064       O_CREAT | O_RDWR | O_BINARY,
00065       ACE_DEFAULT_FILE_PERMS);
00066 
00067     if (handle == ACE_INVALID_HANDLE)
00068     {
00069       result = false;
00070     }
00071     else
00072     {
00073       this->set_handle(handle);
00074       if (this->get_handle() == 0)
00075       {
00076         result = false;
00077       }
00078       else
00079       {
00080         result = (this->addr_.set(filename) == 0);
00081       }
00082     }
00083   }
00084   return result;
00085 }

bool TAO_Notify::Random_File::read ( const size_t  block_number,
void *  buffer 
)

Read a block from our file.

Definition at line 123 of file Random_File.cpp.

References ACE_DEBUG, ACE_GUARD_RETURN, ACE_TEXT(), block_size(), block_size_, DEBUG_LEVEL, LM_DEBUG, ACE_OS::read(), seek(), and TAO_SYNCH_MUTEX.

Referenced by TAO_Notify::Persistent_File_Allocator::read().

00124 {
00125   ACE_GUARD_RETURN (TAO_SYNCH_MUTEX, ace_mon, this->lock_, false);
00126   if (DEBUG_LEVEL > 8) ACE_DEBUG ((LM_DEBUG,
00127     ACE_TEXT ("(%P|%t) Read block %d\n"),
00128     static_cast<int> (block_number)
00129     ));
00130   bool result = this->seek(block_number);
00131   if (result)
00132   {
00133     ssize_t block_size = this->block_size_;
00134     if (block_size !=
00135       ACE_OS::read(this->get_handle(), buf, block_size))
00136     {
00137       result = false;
00138     }
00139   }
00140   return result;
00141 }

bool TAO_Notify::Random_File::seek ( const size_t  block_number  )  [private]

Seek to a given block number, used by reads and writes.

Definition at line 144 of file Random_File.cpp.

References block_size_, and ACE_FILE::seek().

Referenced by read(), and write().

00145 {
00146   ssize_t destloc = block_number * this->block_size_;
00147   bool result = (destloc == this->ACE_FILE::seek(destloc, SEEK_SET));
00148   return result;
00149 }

ACE_OFF_T TAO_Notify::Random_File::size (  )  const

Return the current file size, in number of blocks.

Definition at line 35 of file Random_File.cpp.

References ACE_GUARD_RETURN, block_size_, TAO_SYNCH_MUTEX, and ACE_FILE::tell().

Referenced by TAO_Notify::Persistent_File_Allocator::file_size().

00036 {
00037   Random_File * const mutable_this = const_cast<Random_File *> (this);
00038   ACE_GUARD_RETURN (TAO_SYNCH_MUTEX, ace_mon, this->lock_, 0);
00039   ACE_OFF_T original_pos = mutable_this->tell ();
00040   mutable_this->ACE_FILE::seek(0, SEEK_END);
00041   ACE_OFF_T cursize = mutable_this->tell();
00042   mutable_this->ACE_FILE::seek (original_pos, SEEK_SET);
00043   if ((cursize % this->block_size_) != 0)
00044   {
00045     cursize += this->block_size_;
00046   }
00047   return cursize / this->block_size_;
00048 }

bool TAO_Notify::Random_File::sync (  )  [private]

Synchronize the file to disk, used to implement atomic.

Definition at line 152 of file Random_File.cpp.

References ACE_OS::fsync().

Referenced by write().

00153 {
00154   bool result = false;
00155   result = (0 == ACE_OS::fsync(this->get_handle()));
00156   return result;
00157 }

bool TAO_Notify::Random_File::write ( const size_t  block_number,
void *  buffer,
bool  atomic = false 
)

Write a block to our file, potentially as an "atomic" write. If the atomic argument is true, then the operating system's write-through cache for this file is flushed both before and after the write. The flush before ensures that any record pointers in this block will point to records that actually appear in the file. The flush after provides the caller with a guarantee that the data will appear in the file even if the system fails immediately after this method returns.

Definition at line 88 of file Random_File.cpp.

References ACE_DEBUG, ACE_GUARD_RETURN, ACE_TEXT(), block_size(), block_size_, DEBUG_LEVEL, LM_DEBUG, seek(), sync(), TAO_SYNCH_MUTEX, and ACE_OS::write().

Referenced by TAO_Notify::Persistent_File_Allocator::run().

00089 {
00090   ACE_GUARD_RETURN (TAO_SYNCH_MUTEX, ace_mon, this->lock_, false);
00091   if (DEBUG_LEVEL > 8) ACE_DEBUG ((LM_DEBUG,
00092     ACE_TEXT ("(%P|%t) Write block %d %c\n"),
00093     static_cast<int> (block_number),
00094     (atomic ? '*' : ' ')
00095     ));
00096   bool result = this->seek(block_number);
00097   if (result)
00098   {
00099     if (atomic)
00100     {
00101       // sync before so that any block pointed to from this block
00102       // will be there when this block is written.
00103       result = sync();
00104     }
00105     // ACE uses an ssize_t for buffer size, so we do this to make it happy.
00106     ssize_t block_size = this->block_size_;
00107     if (result && (block_size !=
00108       ACE_OS::write(this->get_handle(), buf, block_size)))
00109     {
00110       result = false;
00111     }
00112     if (result && atomic)
00113     {
00114       // sync after to provide the caller with a guarantee that
00115       // this block is physically written to the storage device.
00116       result = sync();
00117     }
00118   }
00119   return result;
00120 }


Member Data Documentation

size_t TAO_Notify::Random_File::block_size_ [private]

Definition at line 87 of file Random_File.h.

Referenced by block_size(), open(), read(), seek(), size(), and write().

TAO_SYNCH_MUTEX TAO_Notify::Random_File::lock_ [mutable, private]

Definition at line 88 of file Random_File.h.


The documentation for this class was generated from the following files:
Generated on Tue Feb 2 17:46:42 2010 for TAO_CosNotification by  doxygen 1.4.7