24 #ifndef __TPIE_FILE_STREAM_BASE_H__
25 #define __TPIE_FILE_STREAM_BASE_H__
40 memory_size_type size;
41 stream_size_type number;
67 stream_size_type o=
offset();
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();
74 m_fileAccessor->truncate(size);
76 m_tempFile->update_recorded_size(m_fileAccessor->
byte_size());
77 seek(std::min(o, size));
81 file_stream_base(memory_size_type itemSize,
85 inline ~file_stream_base() {
89 void swap(file_stream_base & other) {
95 swap(m_blockItems, other.m_blockItems);
96 swap(m_blockSize, other.m_blockSize);
97 swap(m_size, other.m_size);
98 swap(m_canRead, other.m_canRead);
99 swap(m_canWrite, other.m_canWrite);
100 swap(m_itemSize, other.m_itemSize);
101 swap(m_open, other.m_open);
102 swap(m_fileAccessor, other.m_fileAccessor);
103 swap(m_block.size, other.m_block.size);
104 swap(m_block.number, other.m_block.number);
105 swap(m_block.dirty, other.m_block.dirty);
106 swap(m_block.data, other.m_block.data);
107 swap(m_ownedTempFile, other.m_ownedTempFile);
108 swap(m_tempFile, other.m_tempFile);
111 inline void open_inner(
const std::string &
path,
113 memory_size_type userDataSize,
114 cache_hint cacheHint)
throw (stream_exception) {
115 p_t::open_inner(
path, accessType, userDataSize, cacheHint);
118 m_nextBlock = std::numeric_limits<stream_size_type>::max();
119 m_nextIndex = std::numeric_limits<memory_size_type>::max();
120 m_index = std::numeric_limits<memory_size_type>::max();
123 m_block.number = std::numeric_limits<stream_size_type>::max();
124 m_block.dirty =
false;
125 m_block.data = tpie_new_array<char>(m_blockItems * m_itemSize);
143 m_fileAccessor->
write_block(m_block.data, m_block.number, m_block.size);
145 m_tempFile->update_recorded_size(m_fileAccessor->
byte_size());
147 m_block.dirty =
false;
150 inline void update_vars() {
151 if (m_block.dirty &&
m_index != std::numeric_limits<memory_size_type>::max()) {
152 assert(
m_index <= m_blockItems);
153 m_block.size = std::max(m_block.size,
m_index);
158 inline void initialize() {
163 inline void write_update() {
164 m_block.dirty =
true;
171 friend class stream_crtp<file_stream_base>;
172 file_stream_base & get_file() {
return *
this;}
173 const file_stream_base & get_file()
const {
return *
this;}
175 const block_t &
get_block()
const {
return m_block;}
176 void update_block_core();
181 #endif // __TPIE_FILE_STREAM_BASE_H__
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...
const std::string & path() const
The path of the file opened or the empty string.
stream_size_type offset() const
Calculate the current offset in the stream.
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.
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 truncate(stream_size_type size)
Truncate file to given size.
memory_size_type m_nextIndex
After a cross-block seek: Item index into next block.
Base class of classes that access files.
void close()
Close the file.
void seek(stream_offset_type offset, offset_type whence=beginning)
Moves the logical offset in the stream.
void flush_block()
Write block to disk.
CRTP base of file::stream and file_stream.
access_type
Type describing how we wish to access a file.
stream_size_type m_blockStartIndex
The file-level item index of the first item in the current block.
void get_block(stream_size_type block)
Use file_accessor to fetch indicated block number into m_block.
stream_size_type size() const
Get the size of the file measured in items.
void close()
Close the file and release resources.
void tpie_delete_array(T *a, size_t size)
Delete an array allocated with tpie_new_array.