A random file class. More...
#include <Random_File.h>
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_ |
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.
TAO_Notify::Random_File::Random_File | ( | ) |
TAO_Notify::Random_File::~Random_File | ( | ) |
The destructor, which closes the open file.
Definition at line 23 of file Random_File.cpp.
{ this->close(); }
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.
{ return this->block_size_; }
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.
{ ACE_GUARD_RETURN (TAO_SYNCH_MUTEX, ace_mon, this->lock_, false); this->block_size_ = block_size; bool result = (this->close() == 0); if (result) { if (DEBUG_LEVEL > 8) ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("(%P|%t) Opening file %s\n") , filename )); ACE_HANDLE handle = ACE_OS::open(filename, O_CREAT | O_RDWR | O_BINARY, ACE_DEFAULT_FILE_PERMS); if (handle == ACE_INVALID_HANDLE) { result = false; } else { this->set_handle(handle); if (this->get_handle() == 0) { result = false; } else { result = (this->addr_.set(filename) == 0); } } } return result; }
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.
{ ACE_GUARD_RETURN (TAO_SYNCH_MUTEX, ace_mon, this->lock_, false); if (DEBUG_LEVEL > 8) ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("(%P|%t) Read block %d\n"), static_cast<int> (block_number) )); bool result = this->seek(block_number); if (result) { ssize_t block_size = this->block_size_; if (block_size != ACE_OS::read(this->get_handle(), buf, block_size)) { result = false; } } return result; }
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.
{ ssize_t destloc = block_number * this->block_size_; bool result = (destloc == this->ACE_FILE::seek(destloc, SEEK_SET)); return result; }
ACE_OFF_T TAO_Notify::Random_File::size | ( | void | ) | const |
Return the current file size, in number of blocks.
Definition at line 35 of file Random_File.cpp.
{ Random_File * const mutable_this = const_cast<Random_File *> (this); ACE_GUARD_RETURN (TAO_SYNCH_MUTEX, ace_mon, this->lock_, 0); ACE_OFF_T original_pos = mutable_this->tell (); mutable_this->ACE_FILE::seek(0, SEEK_END); ACE_OFF_T cursize = mutable_this->tell(); mutable_this->ACE_FILE::seek (original_pos, SEEK_SET); if ((cursize % this->block_size_) != 0) { cursize += this->block_size_; } return cursize / this->block_size_; }
bool TAO_Notify::Random_File::sync | ( | void | ) | [private] |
Synchronize the file to disk, used to implement atomic.
Definition at line 152 of file Random_File.cpp.
{ bool result = false; result = (0 == ACE_OS::fsync(this->get_handle())); return result; }
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.
{ ACE_GUARD_RETURN (TAO_SYNCH_MUTEX, ace_mon, this->lock_, false); if (DEBUG_LEVEL > 8) ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("(%P|%t) Write block %d %c\n"), static_cast<int> (block_number), (atomic ? '*' : ' ') )); bool result = this->seek(block_number); if (result) { if (atomic) { // sync before so that any block pointed to from this block // will be there when this block is written. result = sync(); } // ACE uses an ssize_t for buffer size, so we do this to make it happy. ssize_t block_size = this->block_size_; if (result && (block_size != ACE_OS::write(this->get_handle(), buf, block_size))) { result = false; } if (result && atomic) { // sync after to provide the caller with a guarantee that // this block is physically written to the storage device. result = sync(); } } return result; }
size_t TAO_Notify::Random_File::block_size_ [private] |
Definition at line 87 of file Random_File.h.
TAO_SYNCH_MUTEX TAO_Notify::Random_File::lock_ [mutable, private] |
Definition at line 88 of file Random_File.h.