TPIE

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

Simple class acting both as file and a file::stream. More...

#include <tpie/uncompressed_stream.h>

Inherits tpie::file_stream_base.

Public Types

typedef T item_type
 The type of the items stored in the stream. More...
 
typedef file_base_crtp
< file_stream_base
p_t
 
typedef stream_crtp
< file_stream_base
s_t
 
enum  offset_type
 Type describing how we should interpret the offset supplied to seek. More...
 

Public Member Functions

 uncompressed_stream (double blockFactor=1.0, file_accessor::file_accessor *fileAccessor=NULL)
 Construct a new uncompressed_stream. More...
 
void write (const item_type &item) throw (stream_exception)
 Write an item to the stream. More...
 
template<typename IT >
void write (const IT &start, const IT &end) throw (stream_exception)
 Write several items to the stream. More...
 
const item_typeread () throw (stream_exception)
 Read an item from the stream. More...
 
template<typename IT >
void read (const IT &start, const IT &end) throw (stream_exception)
 Reads several items from the stream. More...
 
const item_typeread_back () throw (stream_exception)
 Read an item from the stream. More...
 
const item_typepeek ()
 Get next item from stream without advancing the position. More...
 
void skip ()
 Advance the stream position to the next item. More...
 
void skip_back ()
 Advance the stream position to the previous item. More...
 
void swap (uncompressed_stream< T > &other)
 
void close () throw (stream_exception)
 Close the file and release resources. More...
 
void truncate (stream_size_type size)
 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...
 
template<typename TT >
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...
 
template<typename TT >
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...
 
void seek (stream_offset_type offset, offset_type whence=beginning) throw (stream_exception)
 Moves the logical offset in the stream. More...
 
stream_size_type offset () const throw ()
 Calculate the current offset in the stream. More...
 
bool can_read () const throw ()
 Check if we can read an item with read(). More...
 
bool can_read_back () const throw ()
 Check if we can read an item with read_back(). More...
 
stream_size_type size () const throw ()
 Get the size of the file measured in items. More...
 

Static Public Member Functions

static memory_size_type memory_usage (float blockFactor=1.0, bool includeDefaultFileAccessor=true) throw ()
 Calculate the amount of memory used by a single uncompressed_stream. 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 swap (file_stream_base &other)
 
void open_inner (const std::string &path, access_type accessType, memory_size_type userDataSize, cache_hint cacheHint) throw (stream_exception)
 
void get_block (stream_size_type block)
 Use file_accessor to fetch indicated block number into m_block. More...
 
void flush_block ()
 Write block to disk. More...
 
void update_vars ()
 
void initialize ()
 
void write_update ()
 
template<typename BT >
void read_block (BT &b, stream_size_type block)
 
void get_block_check (stream_size_type block)
 
void update_block ()
 Fetch block from disk as indicated by m_nextBlock, writing old block to disk if needed. More...
 

Static Protected Member Functions

static void read_array (Stream &stream, const IT &start, const IT &end) throw (stream_exception)
 Reads several items from the stream. More...
 
static void write_array (Stream &stream, const IT &start, const IT &end) throw (stream_exception)
 Write several items to the stream. More...
 

Protected Attributes

block_t m_block
 
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
 
memory_size_type m_index
 Item index into the current block, or maxint if we don't have a block. More...
 
stream_size_type m_nextBlock
 After a cross-block seek: Block index of next block, or maxint if the current block is good enough OR if we haven't read/written anything yet. More...
 
memory_size_type m_nextIndex
 After a cross-block seek: Item index into next block. More...
 
stream_size_type m_blockStartIndex
 The file-level item index of the first item in the current block. More...
 

Friends

struct stream_item_array_operations
 

Detailed Description

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

Simple class acting both as file and a file::stream.

A uncompressed_stream basically supports every operation a file or a file::stream supports. This is used to access a file I/O-efficiently, and is the direct replacement of the old ami::stream.

Template Parameters
TThe type of items stored in the stream.

Definition at line 44 of file uncompressed_stream.h.

Member Typedef Documentation

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

The type of the items stored in the stream.

Definition at line 47 of file uncompressed_stream.h.

Member Enumeration Documentation

Type describing how we should interpret the offset supplied to seek.

Definition at line 38 of file stream_crtp.h.

38  {
39  beginning,
40  end,
41  current
42  };

Constructor & Destructor Documentation

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

Construct a new uncompressed_stream.

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 54 of file uncompressed_stream.h.

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

55  :
56  file_stream_base(sizeof(item_type), blockFactor, fileAccessor) {};
T item_type
The type of the items stored in the stream.

Member Function Documentation

template<typename child_t>
memory_size_type tpie::file_base_crtp< child_t >::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  }
template<typename child_t>
static memory_size_type tpie::file_base_crtp< child_t >::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.

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

98  {
99  return block_size(blockFactor);
100  }
memory_size_type block_size() const
Get the size of a block in bytes.
template<typename child_t>
static memory_size_type tpie::file_base_crtp< child_t >::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.
template<typename child_t>
memory_size_type tpie::file_base_crtp< child_t >::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::file_base_crtp< file_stream_base >::block_memory_usage(), and tpie::file_base_crtp< file_stream_base >::calculate_block_factor().

112  {
113  return m_blockSize;
114  }
template<typename child_t>
static double tpie::file_base_crtp< child_t >::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.
bool tpie::stream_crtp< file_stream_base >::can_read ( ) const
throw (
)
inlineinherited

Check if we can read an item with read().

This is logically equivalent to:

return offset() < size();

but it might be faster.

Returns
Whether or not we can read more items from the stream.

Definition at line 108 of file stream_crtp.h.

108  {
109  assert(self().get_file().is_open());
110  if (m_index < self().get_block().size ) return true;
111  return offset() < self().size();
112  }
stream_size_type offset() const
Calculate the current offset in the stream.
Definition: stream_crtp.h:90
memory_size_type m_index
Item index into the current block, or maxint if we don't have a block.
Definition: stream_crtp.h:244
stream_size_type size() const
Get the size of the file measured in items.
Definition: stream_crtp.h:132
bool tpie::stream_crtp< file_stream_base >::can_read_back ( ) const
throw (
)
inlineinherited

Check if we can read an item with read_back().

Returns
Whether or not we can read an item with read_back().

Definition at line 119 of file stream_crtp.h.

119  {
120  assert(self().get_file().is_open());
121  if (m_nextBlock == std::numeric_limits<stream_size_type>::max())
122  return m_index > 0 || m_blockStartIndex > 0;
123  else
124  return m_nextIndex > 0 || m_nextBlock > 0;
125  }
stream_size_type m_nextBlock
After a cross-block seek: Block index of next block, or maxint if the current block is good enough OR...
Definition: stream_crtp.h:248
memory_size_type m_index
Item index into the current block, or maxint if we don't have a block.
Definition: stream_crtp.h:244
memory_size_type m_nextIndex
After a cross-block seek: Item index into next block.
Definition: stream_crtp.h:251
stream_size_type m_blockStartIndex
The file-level item index of the first item in the current block.
Definition: stream_crtp.h:255
void tpie::file_stream_base::close ( )
throw (stream_exception
)
inlineinherited

Close the file and release resources.

This will close the file and resources used by buffers and such.

Definition at line 51 of file file_stream_base.h.

References tpie::file_base_crtp< file_stream_base >::close(), tpie::file_stream_base::flush_block(), and tpie::tpie_delete_array().

51  {
52  if (m_open) flush_block();
53  tpie_delete_array(m_block.data, m_itemSize * m_blockItems);
54  m_block.data = 0;
55  p_t::close();
56  }
void flush_block()
Write block to disk.
void tpie_delete_array(T *a, size_t size)
Delete an array allocated with tpie_new_array.
Definition: memory.h:319
template<typename child_t>
stream_size_type tpie::file_base_crtp< child_t >::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  }
void tpie::file_stream_base::flush_block ( )
inlineprotectedinherited

Write block to disk.

Definition at line 139 of file file_stream_base.h.

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

Referenced by tpie::file_stream_base::close(), and tpie::file_stream_base::truncate().

139  {
140  if (m_block.dirty) {
141  assert(m_canWrite);
142  update_vars();
143  m_fileAccessor->write_block(m_block.data, m_block.number, m_block.size);
144  if (m_tempFile)
145  m_tempFile->update_recorded_size(m_fileAccessor->byte_size());
146  }
147  m_block.dirty = false;
148  }
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...
virtual void write_block(const void *data, stream_size_type blockNumber, memory_size_type itemCount)=0
Write the given number of items from the given buffer into the given block.
void tpie::file_stream_base::get_block ( stream_size_type  block)
protectedinherited

Use file_accessor to fetch indicated block number into m_block.

template<typename child_t>
bool tpie::file_base_crtp< child_t >::is_open ( ) const
inlineinherited

Check if file is open.

Definition at line 256 of file file_base_crtp.h.

256  {
257  return m_open;
258  }
template<typename child_t>
bool tpie::file_base_crtp< child_t >::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  }
template<typename child_t>
bool tpie::file_base_crtp< child_t >::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::uncompressed_stream< T >::write().

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

Get maximum user data size.

Definition at line 183 of file file_base_crtp.h.

Referenced by tpie::file_base_crtp< file_stream_base >::write_user_data().

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::uncompressed_stream< T >::memory_usage ( float  blockFactor = 1.0,
bool  includeDefaultFileAccessor = true 
)
throw (
)
inlinestatic

Calculate the amount of memory used by a single uncompressed_stream.

Parameters
blockFactorThe block factor you pass to open.
includeDefaultFileAccessorUnless you are supplying your own file accessor to open, leave this to be true.
Returns
The amount of memory maximally used by the count file_streams.

Definition at line 156 of file uncompressed_stream.h.

References tpie::file_base_crtp< child_t >::block_memory_usage(), tpie::file_accessor::stream_accessor_base< file_accessor_t >::memory_usage(), and tpie::uncompressed_stream< T >::uncompressed_stream().

Referenced by tpie::ami::stream_old< T >::main_memory_usage(), and tpie::ami::stream_old< T >::memory_usage().

158  {
159  // TODO
160  memory_size_type x = sizeof(uncompressed_stream);
161  x += block_memory_usage(blockFactor); // allocated in constructor
162  if (includeDefaultFileAccessor)
164  return x;
165  }
static memory_size_type block_memory_usage(double blockFactor)
Amount of memory used by a single block given the block factor.
uncompressed_stream(double blockFactor=1.0, file_accessor::file_accessor *fileAccessor=NULL)
Construct a new uncompressed_stream.
static memory_size_type memory_usage()
Return memory usage of this file accessor.
stream_size_type tpie::stream_crtp< file_stream_base >::offset ( ) const
throw (
)
inlineinherited

Calculate the current offset in the stream.

Returns
The current offset in the stream

Definition at line 90 of file stream_crtp.h.

Referenced by tpie::uncompressed_stream< T >::peek(), and tpie::file_stream_base::truncate().

90  {
91  assert(self().get_file().is_open());
92  if (m_nextBlock == std::numeric_limits<stream_size_type>::max())
93  return m_index + m_blockStartIndex;
94  return m_nextIndex + m_nextBlock * self().get_file().block_items();
95  }
stream_size_type m_nextBlock
After a cross-block seek: Block index of next block, or maxint if the current block is good enough OR...
Definition: stream_crtp.h:248
memory_size_type m_index
Item index into the current block, or maxint if we don't have a block.
Definition: stream_crtp.h:244
memory_size_type m_nextIndex
After a cross-block seek: Item index into next block.
Definition: stream_crtp.h:251
stream_size_type m_blockStartIndex
The file-level item index of the first item in the current block.
Definition: stream_crtp.h:255
template<typename child_t>
void tpie::file_base_crtp< child_t >::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.
template<typename child_t>
void tpie::file_base_crtp< child_t >::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
template<typename child_t>
void tpie::file_base_crtp< child_t >::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
template<typename child_t>
const std::string& tpie::file_base_crtp< child_t >::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.

Referenced by tpie::pipelining::serialization_bits::reverser_input_t< T >::begin(), and tpie::pipelining::serialization_bits::buffer_input_t< T >::begin().

194  {
195  assert(m_open);
196  return m_fileAccessor->path();
197  }
const std::string & path() const
Path of the file currently open.
template<typename T>
const item_type& tpie::uncompressed_stream< T >::peek ( )
inline

Get next item from stream without advancing the position.

Definition at line 123 of file uncompressed_stream.h.

References tpie::stream_crtp< file_stream_base >::m_index, tpie::stream_crtp< file_stream_base >::offset(), tpie::stream_crtp< file_stream_base >::size(), and tpie::stream_crtp< file_stream_base >::update_block().

Referenced by tpie::uncompressed_stream< T >::read(), and tpie::uncompressed_stream< T >::read_back().

123  {
124  assert(m_open);
125  if (m_index >= m_block.size) {
126  update_block();
127  if (offset() >= size()) {
128  throw end_of_stream_exception();
129  }
130  }
131  return reinterpret_cast<item_type*>(m_block.data)[m_index];
132  }
stream_size_type offset() const
Calculate the current offset in the stream.
Definition: stream_crtp.h:90
T item_type
The type of the items stored in the stream.
memory_size_type m_index
Item index into the current block, or maxint if we don't have a block.
Definition: stream_crtp.h:244
void update_block()
Fetch block from disk as indicated by m_nextBlock, writing old block to disk if needed.
stream_size_type size() const
Get the size of the file measured in items.
Definition: stream_crtp.h:132
template<typename T>
const item_type& tpie::uncompressed_stream< T >::read ( )
throw (stream_exception
)
inline

Read an item from the stream.

Read current item from the stream, and increment the offset by one item.

This will throw an end_of_stream_exception if there are no more items left in the stream.

To ensure that no exception is thrown, check that can_read() returns true.

Returns
The item read from the stream.
See also
file<T>::stream::read()

Definition at line 91 of file uncompressed_stream.h.

References tpie::stream_crtp< file_stream_base >::m_index, and tpie::uncompressed_stream< T >::peek().

91  {
92  assert(m_open);
93  const item_type & x = peek();
94  ++m_index;
95  return x;
96  }
T item_type
The type of the items stored in the stream.
memory_size_type m_index
Item index into the current block, or maxint if we don't have a block.
Definition: stream_crtp.h:244
const item_type & peek()
Get next item from stream without advancing the position.
template<typename T>
template<typename IT >
void tpie::uncompressed_stream< T >::read ( const IT &  start,
const IT &  end 
)
throw (stream_exception
)
inline

Reads several items from the stream.

Implementation note: If your iterator type is efficiently copyable with std::copy, then this will also read efficiently from the internal TPIE buffer.

Template Parameters
ITThe type of Random Access Iterators used to supply the items.
Parameters
startIterator to the first spot to write to.
endIterator past the last spot to write to.
Exceptions
end_of_stream_exceptionIf there are not enough elements in the stream to fill all the spots between start and end.
See also
file<T>::stream::read(const IT & start, const IT & end)

Definition at line 104 of file uncompressed_stream.h.

References tpie::stream_crtp< file_stream_base >::read_array().

104  {
105  assert(m_open);
106  read_array(*this, start, end);
107  }
static void read_array(Stream &stream, const IT &start, const IT &end)
Reads several items from the stream.
Definition: stream_crtp.h:165
static void tpie::stream_crtp< file_stream_base >::read_array ( Stream &  stream,
const IT &  start,
const IT &  end 
)
throw (stream_exception
)
inlinestaticprotectedinherited

Reads several items from the stream.

Implementation note: If your iterator type is efficiently copyable with std::copy, then this will also read efficiently from the internal TPIE buffer.

Template Parameters
ITThe type of Random Access Iterators used to supply the items.
Parameters
startIterator to the first spot to write to.
endIterator past the last spot to write to.
Exceptions
end_of_stream_exceptionIf there are not enough elements in the stream to fill all the spots between start and end.

Definition at line 165 of file stream_crtp.h.

Referenced by tpie::uncompressed_stream< T >::read().

165  {
166  typedef typename Stream::item_type T;
167  IT i = start;
168  while (i != end) {
169  if (stream.m_index >= stream.block_items()) {
170  // check to make sure we have enough items in the stream
171  stream_size_type offs = stream.offset();
172  if (offs >= stream.size()
173  || offs + (end-i) > stream.size()) {
174 
175  throw end_of_stream_exception();
176  }
177 
178  // fetch next block from disk
179  stream.update_block();
180  }
181 
182  T * src = reinterpret_cast<T*>(stream.get_block().data) + stream.m_index;
183 
184  // either read the rest of the block or until `end'
185  memory_size_type count = std::min(stream.block_items()-stream.m_index, static_cast<memory_size_type>(end-i));
186 
187  std::copy(src, src + count, i);
188 
189  // advance output iterator
190  i += count;
191 
192  // advance input position
193  stream.m_index += count;
194  }
195  }
template<typename T>
const item_type& tpie::uncompressed_stream< T >::read_back ( )
throw (stream_exception
)
inline

Read an item from the stream.

Decrement the offset by one, and read current item from the stream.

This will throw an end_of_stream_exception if there are no more items left in the stream.

To ensure that no exception is thrown, check that can_read_back() returns true.

Returns
The item read from the stream.
See also
file<T>::stream::read_back()

Definition at line 114 of file uncompressed_stream.h.

References tpie::uncompressed_stream< T >::peek(), and tpie::uncompressed_stream< T >::skip_back().

114  {
115  assert(m_open);
116  skip_back();
117  return peek();
118  }
void skip_back()
Advance the stream position to the previous item.
const item_type & peek()
Get next item from stream without advancing the position.
template<typename child_t>
template<typename TT >
void tpie::file_base_crtp< child_t >::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.
template<typename child_t>
memory_size_type tpie::file_base_crtp< child_t >::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::stream_crtp< file_stream_base >::seek ( stream_offset_type  offset,
offset_type  whence = beginning 
)
throw (stream_exception
)
inlineinherited

Moves the logical offset in the stream.

Parameters
offsetWhere to move the logical offset to.
whenceMove the offset relative to what.

Definition at line 50 of file stream_crtp.h.

Referenced by tpie::uncompressed_stream< T >::skip(), tpie::uncompressed_stream< T >::skip_back(), and tpie::file_stream_base::truncate().

50  {
51  assert(self().get_file().is_open());
52  if (whence == end)
53  offset += self().size();
54  else if (whence == current) {
55  // are we seeking into the current block?
56  if (offset >= 0 || static_cast<stream_size_type>(-offset) <= m_index) {
57  stream_size_type new_index = static_cast<stream_offset_type>(offset+m_index);
58 
59  if (new_index < self().get_file().block_items()) {
60  self().update_vars();
61  m_index = static_cast<memory_size_type>(new_index);
62  return;
63  }
64  }
65 
66  offset += self().offset();
67  }
68  if (0 > offset || (stream_size_type)offset > self().size())
69  throw io_exception("Tried to seek out of file");
70  self().update_vars();
71  stream_size_type b = static_cast<stream_size_type>(offset) / self().get_file().block_items();
72  m_index = static_cast<memory_size_type>(offset - b* self().get_file().block_items());
73  if (b == self().get_block().number) {
74  m_nextBlock = std::numeric_limits<stream_size_type>::max();
75  m_nextIndex = std::numeric_limits<memory_size_type>::max();
76  assert(self().offset() == (stream_size_type)offset);
77  return;
78  }
79  m_nextBlock = b;
81  m_index = std::numeric_limits<memory_size_type>::max();
82  assert(self().offset() == (stream_size_type)offset);
83  }
stream_size_type m_nextBlock
After a cross-block seek: Block index of next block, or maxint if the current block is good enough OR...
Definition: stream_crtp.h:248
stream_size_type offset() const
Calculate the current offset in the stream.
Definition: stream_crtp.h:90
memory_size_type m_index
Item index into the current block, or maxint if we don't have a block.
Definition: stream_crtp.h:244
memory_size_type m_nextIndex
After a cross-block seek: Item index into next block.
Definition: stream_crtp.h:251
stream_size_type size() const
Get the size of the file measured in items.
Definition: stream_crtp.h:132
stream_size_type tpie::stream_crtp< file_stream_base >::size ( ) const
throw (
)
inlineinherited

Get the size of the file measured in items.

Returns
The number of items in the file.

Definition at line 132 of file stream_crtp.h.

Referenced by tpie::uncompressed_stream< T >::peek(), and tpie::file_stream_base::truncate().

132  {
133  // XXX update_vars changes internal state in a way that is not visible
134  // through the class interface.
135  // therefore, a const_cast is warranted.
136  const_cast<child_t&>(self()).update_vars();
137  return self().get_file().file_size();
138  }
template<typename T>
void tpie::uncompressed_stream< T >::skip ( )
inline

Advance the stream position to the next item.

Definition at line 137 of file uncompressed_stream.h.

References tpie::stream_crtp< file_stream_base >::seek().

137  {
138  seek(1, current);
139  }
void seek(stream_offset_type offset, offset_type whence=beginning)
Moves the logical offset in the stream.
Definition: stream_crtp.h:50
template<typename T>
void tpie::uncompressed_stream< T >::skip_back ( )
inline

Advance the stream position to the previous item.

Definition at line 144 of file uncompressed_stream.h.

References tpie::stream_crtp< file_stream_base >::seek().

Referenced by tpie::uncompressed_stream< T >::read_back().

144  {
145  seek(-1, current);
146  }
void seek(stream_offset_type offset, offset_type whence=beginning)
Moves the logical offset in the stream.
Definition: stream_crtp.h:50
void tpie::file_stream_base::truncate ( stream_size_type  size)
inlineinherited

Truncate file to given size.

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

See also
file_base::truncate()

Note that when using a file_stream the stream will automatically be rewound if it is beyond the new end of the file.

Definition at line 66 of file file_stream_base.h.

References tpie::file_accessor::stream_accessor_base< file_accessor_t >::byte_size(), tpie::file_stream_base::flush_block(), tpie::stream_crtp< file_stream_base >::m_index, tpie::stream_crtp< file_stream_base >::m_nextBlock, tpie::stream_crtp< file_stream_base >::m_nextIndex, tpie::stream_crtp< file_stream_base >::offset(), tpie::stream_crtp< file_stream_base >::seek(), and tpie::stream_crtp< file_stream_base >::size().

66  {
67  stream_size_type o=offset();
68  flush_block();
69  m_block.number = std::numeric_limits<stream_size_type>::max();
70  m_nextBlock = std::numeric_limits<stream_size_type>::max();
71  m_nextIndex = std::numeric_limits<memory_size_type>::max();
72  m_index = std::numeric_limits<memory_size_type>::max();
73  m_size = size;
74  m_fileAccessor->truncate(size);
75  if (m_tempFile)
76  m_tempFile->update_recorded_size(m_fileAccessor->byte_size());
77  seek(std::min(o, size));
78  }
stream_size_type m_nextBlock
After a cross-block seek: Block index of next block, or maxint if the current block is good enough OR...
Definition: stream_crtp.h:248
stream_size_type offset() const
Calculate the current offset in the stream.
Definition: stream_crtp.h:90
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 m_index
Item index into the current block, or maxint if we don't have a block.
Definition: stream_crtp.h:244
memory_size_type m_nextIndex
After a cross-block seek: Item index into next block.
Definition: stream_crtp.h:251
void seek(stream_offset_type offset, offset_type whence=beginning)
Moves the logical offset in the stream.
Definition: stream_crtp.h:50
void flush_block()
Write block to disk.
stream_size_type size() const
Get the size of the file measured in items.
Definition: stream_crtp.h:132
void tpie::stream_crtp< file_stream_base >::update_block ( )
protectedinherited

Fetch block from disk as indicated by m_nextBlock, writing old block to disk if needed.

Update m_block, m_index, m_nextBlock and m_nextIndex. If m_nextBlock is maxint, use next block is the one numbered m_block->number+1. m_index is updated with the value of m_nextIndex.

Referenced by tpie::uncompressed_stream< T >::peek(), and tpie::uncompressed_stream< T >::write().

template<typename child_t>
memory_size_type tpie::file_base_crtp< child_t >::user_data_size ( ) const
inlineinherited

Get current user data size.

Definition at line 175 of file file_base_crtp.h.

Referenced by tpie::file_base_crtp< file_stream_base >::read_user_data().

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.
template<typename T>
void tpie::uncompressed_stream< T >::write ( const item_type item)
throw (stream_exception
)
inline

Write an item to the stream.

Parameters
itemThe item to write to the stream.
See also
file<T>::stream::write(const item_type & item)

Definition at line 64 of file uncompressed_stream.h.

References tpie::file_base_crtp< child_t >::is_writable(), tpie::stream_crtp< file_stream_base >::m_index, and tpie::stream_crtp< file_stream_base >::update_block().

64  {
65  assert(m_open);
66 #ifndef NDEBUG
67  if (!is_writable())
68  throw io_exception("Cannot write to read only stream");
69 #endif
70  if (m_index >= m_blockItems) update_block();
71  reinterpret_cast<item_type*>(m_block.data)[m_index++] = item;
72  write_update();
73  }
T item_type
The type of the items stored in the stream.
memory_size_type m_index
Item index into the current block, or maxint if we don't have a block.
Definition: stream_crtp.h:244
bool is_writable() const
Check if we can write to the file.
void update_block()
Fetch block from disk as indicated by m_nextBlock, writing old block to disk if needed.
template<typename T>
template<typename IT >
void tpie::uncompressed_stream< T >::write ( const IT &  start,
const IT &  end 
)
throw (stream_exception
)
inline

Write several items to the stream.

Implementation note: If your iterator type is efficiently copyable with std::copy, then this will also write efficiently into the internal TPIE buffer.

Template Parameters
ITThe type of Random Access Iterators used to supply the items.
Parameters
startIterator to the first item to write.
endIterator past the last item to write.
See also
file<T>::stream::write(const IT & start, const IT & end)

Definition at line 81 of file uncompressed_stream.h.

References tpie::stream_crtp< file_stream_base >::write_array().

81  {
82  assert(m_open);
83  write_array(*this, start, end);
84  }
static void write_array(Stream &stream, const IT &start, const IT &end)
Write several items to the stream.
Definition: stream_crtp.h:211
static void tpie::stream_crtp< file_stream_base >::write_array ( Stream &  stream,
const IT &  start,
const IT &  end 
)
throw (stream_exception
)
inlinestaticprotectedinherited

Write several items to the stream.

Implementation note: If your iterator type is efficiently copyable with std::copy, then this will also write efficiently into the internal TPIE buffer.

Template Parameters
ITThe type of Random Access Iterators used to supply the items.
Parameters
startIterator to the first item to write.
endIterator past the last item to write.

Definition at line 211 of file stream_crtp.h.

Referenced by tpie::uncompressed_stream< T >::write().

211  {
212  typedef typename Stream::item_type T;
213  IT i = start;
214  while (i != end) {
215  if (stream.m_index >= stream.block_items()) stream.update_block();
216 
217  size_t streamRemaining = end - i;
218  size_t blockRemaining = stream.block_items()-stream.m_index;
219 
220  IT till = (blockRemaining < streamRemaining) ? (i + blockRemaining) : end;
221 
222  T * dest = reinterpret_cast<T*>(stream.get_block().data) + stream.m_index;
223 
224  std::copy(i, till, dest);
225 
226  stream.m_index += till - i;
227  stream.write_update();
228  i = till;
229  }
230  }
template<typename child_t>
template<typename TT >
void tpie::file_base_crtp< child_t >::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.
template<typename child_t>
void tpie::file_base_crtp< child_t >::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.

Member Data Documentation

stream_size_type tpie::stream_crtp< file_stream_base >::m_blockStartIndex
protectedinherited

The file-level item index of the first item in the current block.

When m_block is not the null block, this should be equal to m_block->number * block_items().

Definition at line 255 of file stream_crtp.h.

memory_size_type tpie::stream_crtp< file_stream_base >::m_index
protectedinherited

Item index into the current block, or maxint if we don't have a block.

Definition at line 244 of file stream_crtp.h.

Referenced by tpie::uncompressed_stream< T >::peek(), tpie::uncompressed_stream< T >::read(), tpie::file_stream_base::truncate(), and tpie::uncompressed_stream< T >::write().

stream_size_type tpie::stream_crtp< file_stream_base >::m_nextBlock
protectedinherited

After a cross-block seek: Block index of next block, or maxint if the current block is good enough OR if we haven't read/written anything yet.

Definition at line 248 of file stream_crtp.h.

Referenced by tpie::file_stream_base::truncate().

memory_size_type tpie::stream_crtp< file_stream_base >::m_nextIndex
protectedinherited

After a cross-block seek: Item index into next block.

Otherwise, maxint as with m_nextBlock.

Definition at line 251 of file stream_crtp.h.

Referenced by tpie::file_stream_base::truncate().


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