TPIE

2362a60
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 #include <algorithm>
43 
44 namespace tpie {
45 
46 class file_base: public file_base_crtp<file_base> {
48 protected:
53 #ifdef WIN32
54 #pragma warning( push )
55 #pragma warning( disable : 4200 )
56 #endif
57  struct block_t : public boost::intrusive::list_base_hook<> {
58  memory_size_type size;
59  memory_size_type usage;
60  stream_size_type number;
61  bool dirty;
62  char data[0];
63  };
64 #ifdef WIN32
65 #pragma warning( pop )
66 #endif
67 
68  inline void update_size(stream_size_type size) {
69  m_size = std::max(m_size, size);
70  if (m_tempFile)
71  m_tempFile->update_recorded_size(m_fileAccessor->byte_size());
72  }
73 
74 public:
75  inline stream_size_type size() const throw() {
76  return file_size();
77  }
78 
79  void close();
80 
84  class stream: public stream_crtp<stream> {
85  private:
86  typedef stream_crtp<stream> p_t;
87 
88  friend class stream_crtp<stream>;
89 
91  file_base * m_file;
92 
93  protected:
94  block_t & get_block() {return *m_block;}
95  const block_t & get_block() const {return *m_block;}
96  inline file_base & get_file() {assert(m_file != 0); return *m_file;}
97  inline const file_base & get_file() const {assert(m_file != 0); return *m_file;}
98 
99  void update_block_core();
100 
101  inline void update_vars() {}
102 
106 
107  public:
111  inline bool attached() const { return 0 != m_file; }
112 
113  protected:
117  void attach_inner(file_base & f);
118 
122  void detach_inner();
123 
124  public:
128  inline memory_size_type block_items() const {return get_file().m_blockItems;}
129 
130  protected:
138  inline void write_update() {
139  m_block->dirty = true;
140  m_block->size = std::max(m_block->size, m_index);
141  get_file().update_size(static_cast<stream_size_type>(m_index)+m_blockStartIndex);
142  }
143 
144  public:
150  stream(file_base & file, stream_size_type offset=0);
151 
152  stream() : m_file(0) {}
153 
154  private:
158  void free();
159 
160  public:
161  inline ~stream() {free();}
162 
163 
164  protected:
168  inline void initialize() {
169  if (m_block != &get_file().m_emptyBlock) get_file().free_block(m_block);
170  p_t::initialize();
171  m_block = &get_file().m_emptyBlock;
172  }
173  };
174 
179  void truncate(stream_size_type s) throw(stream_exception) {
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  }
189 
193  ~file_base();
194 
195 
196 protected:
197  file_base(memory_size_type item_size,
198  double blockFactor=1.0,
199  file_accessor::file_accessor * fileAccessor=NULL);
200 
201  void create_block();
202  void delete_block();
203  block_t * get_block(stream_size_type block);
204  void free_block(block_t * block);
205 
206 
207  static block_t m_emptyBlock;
208  // TODO This should really be a hash map
209  boost::intrusive::list<block_t> m_used;
210  boost::intrusive::list<block_t> m_free;
211 };
212 
213 } // namespace tpie
214 
215 #endif //__TPIE_FILE_BASE_H__
Different hints for OS file caching.
Typesafe bitflags.
Memory management subsystem.
CRTP base of file_base.
memory_size_type block_items() const
Fetch number of items per block.
Definition: file_base.h:128
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:168
Stream in file. We support multiple streams per file.
Definition: file_base.h:84
This is the type of our block buffers.
Definition: file_base.h:57
void truncate(stream_size_type s)
Truncate file to given size.
Definition: file_base.h:179
void write_update()
Call whenever the current block buffer is modified.
Definition: file_base.h:138
CRTP base of file::stream and file_stream.
bool attached() const
True if we are attached to a tpie::file.
Definition: file_base.h:111
Temporary file names.
Describes how to acces a file.
block_t * m_block
Current block.
Definition: file_base.h:105
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.