TPIE

11a2c2d
file_base_crtp.h
Go to the documentation of this file.
1 // -*- mode: c++; tab-width: 4; indent-tabs-mode: t; eval: (progn (c-set-style "stroustrup") (c-set-offset 'innamespace 0)); -*-
2 // vi:set ts=4 sts=4 sw=4 noet :
3 // Copyright 2012 The TPIE development team
4 //
5 // This file is part of TPIE.
6 //
7 // TPIE is free software: you can redistribute it and/or modify it under
8 // the terms of the GNU Lesser General Public License as published by the
9 // Free Software Foundation, either version 3 of the License, or (at your
10 // option) any later version.
11 //
12 // TPIE is distributed in the hope that it will be useful, but WITHOUT ANY
13 // WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 // FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
15 // License for more details.
16 //
17 // You should have received a copy of the GNU Lesser General Public License
18 // along with TPIE. If not, see <http://www.gnu.org/licenses/>
19 
23 
24 #ifndef __TPIE_FILE_BASE_CRTP_H__
25 #define __TPIE_FILE_BASE_CRTP_H__
26 
27 #include <tpie/types.h>
28 #include <tpie/tpie.h>
29 #include <tpie/exception.h>
30 #include <tpie/memory.h>
31 #include <tpie/access_type.h>
32 #include <tpie/cache_hint.h>
33 #include <tpie/stream_header.h>
35 #include <tpie/tempname.h>
36 #include <cassert>
37 
38 namespace tpie {
39 
49 template <typename child_t>
51 public:
57  bool is_readable() const throw() {
58  return m_canRead;
59  }
60 
66  bool is_writable() const throw() {
67  return m_canWrite;
68  }
69 
70 
79  static inline memory_size_type block_size(double blockFactor) throw () {
80  return static_cast<memory_size_type>(get_block_size() * blockFactor);
81  }
82 
92  static inline double calculate_block_factor(memory_size_type blockSize) throw () {
93  return (double)blockSize / (double)block_size(1.0);
94  }
95 
99  static inline memory_size_type block_memory_usage(double blockFactor) {
100  return block_size(blockFactor);
101  }
102 
106  memory_size_type block_items() const {
107  return m_blockItems;
108  }
109 
113  memory_size_type block_size() const {
114  return m_blockSize;
115  }
116 
125  template <typename TT>
126  void read_user_data(TT & data) {
127  assert(m_open);
128  if (sizeof(TT) != user_data_size()) throw io_exception("Wrong user data size");
129  m_fileAccessor->read_user_data(reinterpret_cast<void*>(&data), sizeof(TT));
130  }
131 
139  memory_size_type read_user_data(void * data, memory_size_type count) {
140  assert(m_open);
141  return m_fileAccessor->read_user_data(data, count);
142  }
143 
152  template <typename TT>
153  void write_user_data(const TT & data) {
154  assert(m_open);
155  if (sizeof(TT) > max_user_data_size()) throw io_exception("Wrong user data size");
156  m_fileAccessor->write_user_data(reinterpret_cast<const void*>(&data), sizeof(TT));
157  }
158 
168  void write_user_data(const void * data, memory_size_type count) {
169  assert(m_open);
170  m_fileAccessor->write_user_data(data, count);
171  }
172 
176  memory_size_type user_data_size() const {
177  assert(m_open);
178  return m_fileAccessor->user_data_size();
179  }
180 
184  memory_size_type max_user_data_size() const {
185  assert(m_open);
186  return m_fileAccessor->max_user_data_size();
187  }
188 
189 
195  inline const std::string & path() const throw() {
196  assert(m_open);
197  return m_fileAccessor->path();
198  }
199 
208  inline void open(const std::string & path,
209  access_type accessType=access_read_write,
210  memory_size_type userDataSize=0,
211  cache_hint cacheHint=access_sequential) {
212  self().close();
213  self().open_inner(path, accessType, userDataSize, cacheHint);
214  }
215 
220  inline void open(memory_size_type userDataSize=0,
221  cache_hint cacheHint=access_sequential) {
222  self().close();
223  m_ownedTempFile.reset(tpie_new<temp_file>());
224  m_tempFile=m_ownedTempFile.get();
225  self().open_inner(m_tempFile->path(), access_read_write, userDataSize, cacheHint);
226  }
227 
233  inline void open(temp_file & file,
234  access_type accessType=access_read_write,
235  memory_size_type userDataSize=0,
236  cache_hint cacheHint=access_sequential) {
237  self().close();
238  m_tempFile=&file;
239  self().open_inner(m_tempFile->path(), accessType, userDataSize, cacheHint);
240  }
241 
247  inline void close() {
248  if (m_open) m_fileAccessor->close();
249  m_open = false;
250  m_tempFile = NULL;
251  m_ownedTempFile.reset();
252  }
253 
257  inline bool is_open() const {
258  return m_open;
259  }
260 
269  inline stream_size_type file_size() const throw() {
270  return m_size;
271  }
272 
273 protected:
274  inline void open_inner(const std::string & path,
275  access_type accessType,
276  memory_size_type userDataSize,
277  cache_hint cacheHint) {
278  m_canRead = accessType == access_read || accessType == access_read_write;
279  m_canWrite = accessType == access_write || accessType == access_read_write;
280  const bool preferCompression = false;
281  m_fileAccessor->open(path, m_canRead, m_canWrite, m_itemSize,
282  m_blockSize, userDataSize, cacheHint,
283  preferCompression);
284  if (m_fileAccessor->get_compressed()) {
285  m_fileAccessor->close();
286  throw stream_exception("Tried to open compressed stream as non-compressed");
287  }
288  m_size = m_fileAccessor->size();
289  m_open = true;
290  }
291 
292 
293  file_base_crtp(memory_size_type itemSize, double blockFactor,
294  file_accessor::file_accessor * fileAccessor);
295 
296 
297  template <typename BT>
298  void read_block(BT & b, stream_size_type block);
299  void get_block_check(stream_size_type block);
300 
301  memory_size_type m_blockItems;
302  memory_size_type m_blockSize;
303  bool m_canRead;
304  bool m_canWrite;
305  bool m_open;
306  memory_size_type m_itemSize;
307  file_accessor::file_accessor * m_fileAccessor;
308  tpie::unique_ptr<temp_file> m_ownedTempFile;
309  temp_file * m_tempFile;
310  stream_size_type m_size;
311 
312 private:
313  child_t & self() {return *static_cast<child_t *>(this);}
314  const child_t & self() const {return *static_cast<const child_t *>(this);}
315 };
316 
317 } // namespace tpie
318 
319 #endif // __TPIE_FILE_BASE_CRTP_H__
tpie::access_write
@ access_write
Open a file for writing only, content is truncated.
Definition: access_type.h:33
tpie::file
Central file abstraction.
Definition: file.h:39
types.h
cache_hint.h
tpie::file_accessor::stream_accessor_base::max_user_data_size
memory_size_type max_user_data_size() const
Maximum size (in bytes) of the user data.
Definition: stream_accessor_base.h:197
file_accessor.h
tpie::file_base_crtp::max_user_data_size
memory_size_type max_user_data_size() const
Get maximum user data size.
Definition: file_base_crtp.h:184
tpie::temp_file::path
const std::string & path()
Get the path of the associated file.
Definition: tempname.h:243
tempname.h
tpie::file_base_crtp::is_writable
bool is_writable() const
Check if we can write to the file.
Definition: file_base_crtp.h:66
tpie::file_base_crtp::is_readable
bool is_readable() const
Check if we can read from the file.
Definition: file_base_crtp.h:57
tpie::file_base_crtp::block_items
memory_size_type block_items() const
Get the number of items per block.
Definition: file_base_crtp.h:106
tpie::unique_ptr
std::unique_ptr< T, tpie_deleter > unique_ptr
like std::unique_ptr, but delete the object with tpie_delete.
Definition: memory.h:306
tpie::access_type
access_type
Type describing how we wish to access a file.
Definition: access_type.h:29
tpie::file_base_crtp::write_user_data
void write_user_data(const void *data, memory_size_type count)
Write variable length user data associated with the file.
Definition: file_base_crtp.h:168
tpie::file_base_crtp::block_size
static memory_size_type block_size(double blockFactor)
Calculate the block size in bytes used by a stream.
Definition: file_base_crtp.h:79
tpie::file_accessor::stream_accessor_base::size
stream_size_type size() const
Number of items in stream.
Definition: stream_accessor_base.h:182
tpie::file_base_crtp::file_size
stream_size_type file_size() const
Get the size of the file measured in items.
Definition: file_base_crtp.h:269
tpie::file_accessor::stream_accessor_base::path
const std::string & path() const
Path of the file currently open.
Definition: stream_accessor_base.h:187
tpie.h
tpie::file_base_crtp::block_size
memory_size_type block_size() const
Get the size of a block in bytes.
Definition: file_base_crtp.h:113
tpie::file_accessor::stream_accessor_base::write_user_data
void write_user_data(const void *data, memory_size_type count)
Write user data to the stream.
tpie::file_accessor::stream_accessor_base::open
void open(const std::string &path, bool read, bool write, memory_size_type itemSize, memory_size_type blockSize, memory_size_type maxUserDataSize, cache_hint cacheHint, int compressionFlags)
Open file for reading and/or writing.
tpie::file_base_crtp::calculate_block_factor
static double calculate_block_factor(memory_size_type blockSize)
Find the block factor that would result in the given block size measured in bytes.
Definition: file_base_crtp.h:92
tpie::file_base_crtp::is_open
bool is_open() const
Check if file is open.
Definition: file_base_crtp.h:257
tpie::file_base_crtp
Base class of classes that access files.
Definition: file_base_crtp.h:50
tpie::cache_hint
cache_hint
Definition: cache_hint.h:28
tpie::file_base_crtp::close
void close()
Close the file.
Definition: file_base_crtp.h:247
tpie::file_base_crtp::read_user_data
void read_user_data(TT &data)
Read the user data associated with the file.
Definition: file_base_crtp.h:126
tpie::file_base_crtp::path
const std::string & path() const
The path of the file opened or the empty string.
Definition: file_base_crtp.h:195
tpie::file_accessor::stream_accessor_base::user_data_size
memory_size_type user_data_size() const
Size (in bytes) of the user data.
Definition: stream_accessor_base.h:192
tpie::file_base_crtp::open
void open(const std::string &path, access_type accessType=access_read_write, memory_size_type userDataSize=0, cache_hint cacheHint=access_sequential)
Open a file.
Definition: file_base_crtp.h:208
tpie::stream_exception
Definition: exception.h:45
tpie::file_base_crtp::open
void open(temp_file &file, access_type accessType=access_read_write, memory_size_type userDataSize=0, cache_hint cacheHint=access_sequential)
Open a temporary file.
Definition: file_base_crtp.h:233
tpie::access_sequential
@ access_sequential
Sequential access is intended.
Definition: cache_hint.h:36
tpie::file_base_crtp::read_user_data
memory_size_type read_user_data(void *data, memory_size_type count)
Read variable length user data associated with the file.
Definition: file_base_crtp.h:139
tpie::file_accessor::stream_accessor_base::read_user_data
memory_size_type read_user_data(void *data, memory_size_type count)
Read user data into the given buffer.
tpie::get_block_size
TPIE_EXPORT memory_size_type get_block_size()
Get the TPIE block size.
tpie::file_base_crtp::write_user_data
void write_user_data(const TT &data)
Write user data to the stream.
Definition: file_base_crtp.h:153
tpie::file_base_crtp::block_memory_usage
static memory_size_type block_memory_usage(double blockFactor)
Amount of memory used by a single block given the block factor.
Definition: file_base_crtp.h:99
tpie::file_base_crtp::user_data_size
memory_size_type user_data_size() const
Get current user data size.
Definition: file_base_crtp.h:176
tpie::temp_file
Class representing a reference to a temporary file.
Definition: tempname.h:201
exception.h
tpie::access_read_write
@ access_read_write
Open a file for reading or writing.
Definition: access_type.h:35
tpie::file_base_crtp::open
void open(memory_size_type userDataSize=0, cache_hint cacheHint=access_sequential)
Open an anonymous temporary file.
Definition: file_base_crtp.h:220
stream_header.h
tpie::io_exception
Definition: exception.h:50
memory.h
access_type.h
tpie
Definition: access_type.h:26
tpie::access_read
@ access_read
Open a file for reading.
Definition: access_type.h:31