TPIE

v1.1rc1-6-g0c97303
file_base.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 2009, 2011, 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_H__
25 #define __TPIE_FILE_BASE_H__
26 #include <tpie/file_base_crtp.h>
27 #include <tpie/stream_crtp.h>
28 #include <tpie/exception.h>
30 #ifndef WIN32
32 #else
33 #include <tpie/file_accessor/win32.h>
34 #endif //WIN32
35 #include <boost/intrusive/list.hpp>
36 #include <tpie/tempname.h>
37 #include <memory>
38 #include <tpie/memory.h>
39 #include <tpie/cache_hint.h>
40 #include <tpie/access_type.h>
41 #include <tpie/types.h>
42 
43 namespace tpie {
44 
45 class file_base: public file_base_crtp<file_base> {
47 protected:
52 #ifdef WIN32
53 #pragma warning( push )
54 #pragma warning( disable : 4200 )
55 #endif
56  struct block_t : public boost::intrusive::list_base_hook<> {
57  memory_size_type size;
58  memory_size_type usage;
59  stream_size_type number;
60  bool dirty;
61  char data[0];
62  };
63 #ifdef WIN32
64 #pragma warning( pop )
65 #endif
66 
67  inline void update_size(stream_size_type size) {
68  m_size = std::max(m_size, size);
69  if (m_tempFile)
70  m_tempFile->update_recorded_size(m_fileAccessor->byte_size());
71  }
72 
73 public:
74  inline stream_size_type size() const throw() {
75  return file_size();
76  }
77 
78  void close();
79 
83  class stream: public stream_crtp<stream> {
84  private:
85  typedef stream_crtp<stream> p_t;
86 
87  friend class stream_crtp<stream>;
88 
90  file_base * m_file;
91 
92  protected:
93  block_t & __block() {return *m_block;}
94  const block_t & __block() const {return *m_block;}
95  inline file_base & __file() {assert(m_file != 0); return *m_file;}
96  inline const file_base & __file() const {assert(m_file != 0); return *m_file;}
97 
98  void update_block_core();
99 
100  inline void update_vars() {}
101 
105 
106  public:
110  inline bool attached() const { return 0 != m_file; }
111 
112  protected:
116  void attach_inner(file_base & f);
117 
121  void detach_inner();
122 
123  public:
127  inline memory_size_type block_items() const {return __file().m_blockItems;}
128 
129  protected:
137  inline void write_update() {
138  m_block->dirty = true;
139  m_block->size = std::max(m_block->size, m_index);
140  __file().update_size(static_cast<stream_size_type>(m_index)+m_blockStartIndex);
141  }
142 
143  public:
149  stream(file_base & file, stream_size_type offset=0);
150 
151  stream() : m_file(0) {}
152 
153  private:
157  void free();
158 
159  public:
160  inline ~stream() {free();}
161 
162 
163  protected:
167  inline void initialize() {
168  if (m_block != &__file().m_emptyBlock) __file().free_block(m_block);
169  p_t::initialize();
170  m_block = &__file().m_emptyBlock;
171  }
172  };
173 
178  void truncate(stream_size_type s) throw(stream_exception) {
179  assert(m_open);
180  if (!m_used.empty()) {
181  throw io_exception("Tried to truncate a file with one or more open streams");
182  }
183  m_size = s;
184  m_fileAccessor->truncate(s);
185  if (m_tempFile)
186  m_tempFile->update_recorded_size(m_fileAccessor->byte_size());
187  }
188 
192  ~file_base();
193 
194 
195 protected:
196  file_base(memory_size_type item_size,
197  double blockFactor=1.0,
198  file_accessor::file_accessor * fileAccessor=NULL);
199 
200  void create_block();
201  void delete_block();
202  block_t * get_block(stream_size_type block);
203  void free_block(block_t * block);
204 
205 
206  static block_t m_emptyBlock;
207  // TODO This should really be a hash map
208  boost::intrusive::list<block_t> m_used;
209  boost::intrusive::list<block_t> m_free;
210 };
211 
212 } // namespace tpie
213 
214 #endif //__TPIE_FILE_BASE_H__
Different hints for OS file caching.
Standard types.
Memory management subsystem.
CRTP base of file_base.
memory_size_type block_items() const
Fetch number of items per block.
Definition: file_base.h:127
Central file abstraction.
Definition: file.h:44
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
Exception classes.
void attach_inner(file_base &f)
Attach to the given tpie::file. If necessary, detach first.
void detach_inner()
Detach from a tpie::file.
Declare default file accessor.
Base class of classes that access files.
stream_size_type file_size() const
Get the size of the file measured in items.
void initialize()
Set up block buffers and offsets.
Definition: file_base.h:167
Stream in file. We support multiple streams per file.
Definition: file_base.h:83
This is the type of our block buffers.
Definition: file_base.h:56
void truncate(stream_size_type s)
Truncate file to given size.
Definition: file_base.h:178
void write_update()
Call whenever the current block buffer is modified.
Definition: file_base.h:137
CRTP base of file::stream and file_stream.
bool attached() const
True if we are attached to a tpie::file.
Definition: file_base.h:110
Temporary file names.
Describes how to acces a file.
block_t * m_block
Current block.
Definition: file_base.h:104
POSIX-style file accessor.
stream_size_type m_blockStartIndex
The file-level item index of the first item in the current block.
Definition: stream_crtp.h:255
~file_base()
file_base destructor.