TPIE

2362a60
tpie::file< T > Class Template Reference

Central file abstraction. More...

#include <tpie/file.h>

Inherits tpie::file_base.

Classes

class  stream
 Central stream abstraction. More...
 

Public Types

typedef T item_type
 Type of items stored in the file. More...
 

Public Member Functions

 file (double blockFactor=1.0, file_accessor::file_accessor *fileAccessor=NULL)
 Construct a file object with the given block factor and file accessor. More...
 
stream_size_type size () const throw ()
 
void close ()
 
void truncate (stream_size_type s) throw (stream_exception)
 Truncate file to given size. More...
 
bool is_readable () const throw ()
 Check if we can read from the file. More...
 
bool is_writable () const throw ()
 Check if we can write to the file. More...
 
memory_size_type block_size () const
 Get the size of a block in bytes. More...
 
memory_size_type block_items () const
 Get the number of items per block. More...
 
void read_user_data (TT &data) throw (stream_exception)
 Read the user data associated with the file. More...
 
memory_size_type read_user_data (void *data, memory_size_type count)
 Read variable length user data associated with the file. More...
 
void write_user_data (const TT &data) throw (stream_exception)
 Write user data to the stream. More...
 
void write_user_data (const void *data, memory_size_type count)
 Write variable length user data associated with the file. More...
 
memory_size_type user_data_size () const
 Get current user data size. More...
 
memory_size_type max_user_data_size () const
 Get maximum user data size. More...
 
const std::string & path () const throw ()
 The path of the file opened or the empty string. More...
 
void open (const std::string &path, access_type accessType=access_read_write, memory_size_type userDataSize=0, cache_hint cacheHint=access_sequential) throw (stream_exception)
 Open a file. More...
 
void open (memory_size_type userDataSize=0, cache_hint cacheHint=access_sequential) throw (stream_exception)
 Open an anonymous temporary file. More...
 
void open (temp_file &file, access_type accessType=access_read_write, memory_size_type userDataSize=0, cache_hint cacheHint=access_sequential) throw (stream_exception)
 Open a temporary file. More...
 
bool is_open () const
 Check if file is open. More...
 
stream_size_type file_size () const throw ()
 Get the size of the file measured in items. More...
 

Static Public Member Functions

static memory_size_type memory_usage (bool includeDefaultFileAccessor=true)
 Calculate the memory usage of a file. More...
 
static memory_size_type block_size (double blockFactor) throw ()
 Calculate the block size in bytes used by a stream. More...
 
static double calculate_block_factor (memory_size_type blockSize) throw ()
 Find the block factor that would result in the given block size measured in bytes. More...
 
static memory_size_type block_memory_usage (double blockFactor)
 Amount of memory used by a single block given the block factor. More...
 

Protected Member Functions

void update_size (stream_size_type size)
 
void create_block ()
 
void delete_block ()
 
block_tget_block (stream_size_type block)
 
void free_block (block_t *block)
 
void open_inner (const std::string &path, access_type accessType, memory_size_type userDataSize, cache_hint cacheHint) throw (stream_exception)
 
void read_block (BT &b, stream_size_type block)
 
void get_block_check (stream_size_type block)
 

Protected Attributes

boost::intrusive::list< block_tm_used
 
boost::intrusive::list< block_tm_free
 
memory_size_type m_blockItems
 
memory_size_type m_blockSize
 
bool m_canRead
 
bool m_canWrite
 
bool m_open
 
memory_size_type m_itemSize
 
file_accessor::file_accessorm_fileAccessor
 
tpie::unique_ptr< temp_filem_ownedTempFile
 
temp_filem_tempFile
 
stream_size_type m_size
 

Static Protected Attributes

static block_t m_emptyBlock
 

Detailed Description

template<typename T>
class tpie::file< T >

Central file abstraction.

Definition at line 44 of file file.h.

Member Typedef Documentation

template<typename T>
typedef T tpie::file< T >::item_type

Type of items stored in the file.

Definition at line 47 of file file.h.

Constructor & Destructor Documentation

template<typename T>
tpie::file< T >::file ( double  blockFactor = 1.0,
file_accessor::file_accessor fileAccessor = NULL 
)
inline

Construct a file object with the given block factor and file accessor.

Parameters
blockFactorThe relative size of a block compared to the default. To find the block factor corresponding to an absolute block size, use file_base::calculate_block_factor.
fileAccessorThe file accessor to use, if none is supplied a default will be used.

Definition at line 68 of file file.h.

Referenced by tpie::file< T >::memory_usage().

69  :
70  file_base(sizeof(T), blockFactor, fileAccessor) {};

Member Function Documentation

memory_size_type tpie::file_base_crtp< file_base >::block_items ( ) const
inlineinherited

Get the number of items per block.

Definition at line 105 of file file_base_crtp.h.

105  {
106  return m_blockItems;
107  }
static memory_size_type tpie::file_base_crtp< file_base >::block_memory_usage ( double  blockFactor)
inlinestaticinherited

Amount of memory used by a single block given the block factor.

Definition at line 98 of file file_base_crtp.h.

98  {
99  return block_size(blockFactor);
100  }
memory_size_type block_size() const
Get the size of a block in bytes.
static memory_size_type tpie::file_base_crtp< file_base >::block_size ( double  blockFactor)
throw (
)
inlinestaticinherited

Calculate the block size in bytes used by a stream.

We have block_size(calculate_block_factor(b)) ~= b.

Parameters
blockFactorFactor of the global block size to use.
Returns
Size in bytes.

Definition at line 78 of file file_base_crtp.h.

78  {
79  return static_cast<memory_size_type>(get_block_size() * blockFactor);
80  }
memory_size_type get_block_size()
Get the TPIE block size.
memory_size_type tpie::file_base_crtp< file_base >::block_size ( ) const
inlineinherited

Get the size of a block in bytes.

Definition at line 112 of file file_base_crtp.h.

Referenced by tpie::ami::stream_old< T >::chunk_size(), and tpie::file< T >::stream::memory_usage().

112  {
113  return m_blockSize;
114  }
static double tpie::file_base_crtp< file_base >::calculate_block_factor ( memory_size_type  blockSize)
throw (
)
inlinestaticinherited

Find the block factor that would result in the given block size measured in bytes.

We have calculate_block_factor(block_size(f)) ~= f.

Parameters
blockSizeThe sought block size.
Returns
The block factor needed to achieve this block size.

Definition at line 91 of file file_base_crtp.h.

91  {
92  return (double)blockSize / (double)block_size(1.0);
93  }
memory_size_type block_size() const
Get the size of a block in bytes.
stream_size_type tpie::file_base_crtp< file_base >::file_size ( ) const
throw (
)
inlineinherited

Get the size of the file measured in items.

If there are streams of this file that have extended the stream length but have not yet flushed these writes, we might report an incorrect size.

Returns
The number of items in the file.

Definition at line 268 of file file_base_crtp.h.

268  {
269  return m_size;
270  }
bool tpie::file_base_crtp< file_base >::is_open ( ) const
inlineinherited

Check if file is open.

Definition at line 256 of file file_base_crtp.h.

Referenced by tpie::file< T >::stream::read(), tpie::file< T >::stream::read_back(), tpie::file< T >::stream::read_mutable(), and tpie::file< T >::stream::write().

256  {
257  return m_open;
258  }
bool tpie::file_base_crtp< file_base >::is_readable ( ) const
throw (
)
inlineinherited

Check if we can read from the file.

Returns
True if we can read from the file.

Definition at line 56 of file file_base_crtp.h.

56  {
57  return m_canRead;
58  }
bool tpie::file_base_crtp< file_base >::is_writable ( ) const
throw (
)
inlineinherited

Check if we can write to the file.

Returns
True if we can write to the file.

Definition at line 65 of file file_base_crtp.h.

Referenced by tpie::file< T >::stream::write().

65  {
66  return m_canWrite;
67  }
memory_size_type tpie::file_base_crtp< file_base >::max_user_data_size ( ) const
inlineinherited

Get maximum user data size.

Definition at line 183 of file file_base_crtp.h.

183  {
184  assert(m_open);
185  return m_fileAccessor->max_user_data_size();
186  }
memory_size_type max_user_data_size() const
Maximum size (in bytes) of the user data.
template<typename T>
static memory_size_type tpie::file< T >::memory_usage ( bool  includeDefaultFileAccessor = true)
inlinestatic

Calculate the memory usage of a file.

Definition at line 52 of file file.h.

References tpie::file< T >::file(), and tpie::file_accessor::stream_accessor_base< file_accessor_t >::memory_usage().

52  {
53  memory_size_type x = sizeof(file);
54  if (includeDefaultFileAccessor)
56  return x;
57  }
file(double blockFactor=1.0, file_accessor::file_accessor *fileAccessor=NULL)
Construct a file object with the given block factor and file accessor.
Definition: file.h:68
static memory_size_type memory_usage()
Return memory usage of this file accessor.
void tpie::file_base_crtp< file_base >::open ( const std::string &  path,
access_type  accessType = access_read_write,
memory_size_type  userDataSize = 0,
cache_hint  cacheHint = access_sequential 
)
throw (stream_exception
)
inlineinherited

Open a file.

Parameters
pathThe path of the file to open.
accessTypeThe mode of operation.
userDataSizeThe size of the user data we want to store in the file.

Definition at line 207 of file file_base_crtp.h.

210  {
211  self().close();
212  self().open_inner(path, accessType, userDataSize, cacheHint);
213  }
const std::string & path() const
The path of the file opened or the empty string.
void close()
Close the file.
void tpie::file_base_crtp< file_base >::open ( memory_size_type  userDataSize = 0,
cache_hint  cacheHint = access_sequential 
)
throw (stream_exception
)
inlineinherited

Open an anonymous temporary file.

The temporary file is deleted when this file is closed.

Definition at line 219 of file file_base_crtp.h.

220  {
221  self().close();
222  m_ownedTempFile.reset(tpie_new<temp_file>());
223  m_tempFile=m_ownedTempFile.get();
224  self().open_inner(m_tempFile->path(), access_read_write, userDataSize, cacheHint);
225  }
void close()
Close the file.
const std::string & path()
Get the path of the associated file.
Definition: tempname.h:244
Open a file for reading or writing.
Definition: access_type.h:35
void tpie::file_base_crtp< file_base >::open ( temp_file file,
access_type  accessType = access_read_write,
memory_size_type  userDataSize = 0,
cache_hint  cacheHint = access_sequential 
)
throw (stream_exception
)
inlineinherited

Open a temporary file.

The temporary file is not deleted when this file is closed, so several tpie::file objects may use the same temporary file consecutively.

Definition at line 232 of file file_base_crtp.h.

235  {
236  self().close();
237  m_tempFile=&file;
238  self().open_inner(m_tempFile->path(), accessType, userDataSize, cacheHint);
239  }
void close()
Close the file.
const std::string & path()
Get the path of the associated file.
Definition: tempname.h:244
const std::string& tpie::file_base_crtp< file_base >::path ( ) const
throw (
)
inlineinherited

The path of the file opened or the empty string.

Returns
The path of the currently opened file.

Definition at line 194 of file file_base_crtp.h.

194  {
195  assert(m_open);
196  return m_fileAccessor->path();
197  }
const std::string & path() const
Path of the file currently open.
void tpie::file_base_crtp< file_base >::read_user_data ( TT &  data)
throw (stream_exception
)
inlineinherited

Read the user data associated with the file.

Parameters
dataWhere to store the user data.
Template Parameters
TTThe type of user data. sizeof(TT) must be less than or equal to the maximum user data size of the stream. TT must be trivially copyable.

Definition at line 125 of file file_base_crtp.h.

125  {
126  assert(m_open);
127  if (sizeof(TT) != user_data_size()) throw io_exception("Wrong user data size");
128  m_fileAccessor->read_user_data(reinterpret_cast<void*>(&data), sizeof(TT));
129  }
memory_size_type read_user_data(void *data, memory_size_type count)
Read user data into the given buffer.
memory_size_type user_data_size() const
Get current user data size.
memory_size_type tpie::file_base_crtp< file_base >::read_user_data ( void *  data,
memory_size_type  count 
)
inlineinherited

Read variable length user data associated with the file.

Parameters
dataThe buffer in which to write data.
countThe size of the buffer.
Returns
Number of bytes of user data actually read.

Definition at line 138 of file file_base_crtp.h.

138  {
139  assert(m_open);
140  return m_fileAccessor->read_user_data(data, count);
141  }
memory_size_type read_user_data(void *data, memory_size_type count)
Read user data into the given buffer.
void tpie::file_base::truncate ( stream_size_type  s)
throw (stream_exception
)
inlineinherited

Truncate file to given size.

May only be used when no streams are opened to this file.

Definition at line 179 of file file_base.h.

References tpie::file_accessor::stream_accessor_base< file_accessor_t >::byte_size().

179  {
180  assert(m_open);
181  if (!m_used.empty()) {
182  throw io_exception("Tried to truncate a file with one or more open streams");
183  }
184  m_size = s;
185  m_fileAccessor->truncate(s);
186  if (m_tempFile)
187  m_tempFile->update_recorded_size(m_fileAccessor->byte_size());
188  }
stream_size_type byte_size() const
Size (in bytes) of entire stream as laid out on disk after padding the final block to alignment bound...
memory_size_type tpie::file_base_crtp< file_base >::user_data_size ( ) const
inlineinherited

Get current user data size.

Definition at line 175 of file file_base_crtp.h.

175  {
176  assert(m_open);
177  return m_fileAccessor->user_data_size();
178  }
memory_size_type user_data_size() const
Size (in bytes) of the user data.
void tpie::file_base_crtp< file_base >::write_user_data ( const TT &  data)
throw (stream_exception
)
inlineinherited

Write user data to the stream.

Parameters
dataThe user data to store in the stream.
Template Parameters
TTThe type of user data. sizeof(TT) must be less than or equal to the maximum user data size of the stream. TT must be trivially copyable.

Definition at line 152 of file file_base_crtp.h.

152  {
153  assert(m_open);
154  if (sizeof(TT) > max_user_data_size()) throw io_exception("Wrong user data size");
155  m_fileAccessor->write_user_data(reinterpret_cast<const void*>(&data), sizeof(TT));
156  }
void write_user_data(const void *data, memory_size_type count)
Write user data to the stream.
memory_size_type max_user_data_size() const
Get maximum user data size.
void tpie::file_base_crtp< file_base >::write_user_data ( const void *  data,
memory_size_type  count 
)
inlineinherited

Write variable length user data associated with the file.

Throws a stream_exception if the size of the user data exceeds the maximum user data size of the stream.

Parameters
dataThe buffer from which to read data.
countThe size of the user data.

Definition at line 167 of file file_base_crtp.h.

167  {
168  assert(m_open);
169  m_fileAccessor->write_user_data(data, count);
170  }
void write_user_data(const void *data, memory_size_type count)
Write user data to the stream.

The documentation for this class was generated from the following file: