TPIE

v1.1rc1-6-g0c97303
stream_accessor.h
Go to the documentation of this file.
1 // -*- mode: c++; tab-width: 4; indent-tabs-mode: t; c-file-style: "stroustrup"; -*-
2 // vi:set ts=4 sts=4 sw=4 noet :
3 // Copyright 2009, 2010, 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 #ifndef _TPIE_FILE_ACCESSOR_FILE_ACCESSOR_CRTP_H
20 #define _TPIE_FILE_ACCESSOR_FILE_ACCESSOR_CRTP_H
21 
26 
28 #include <tpie/stream_header.h>
29 #include <tpie/cache_hint.h>
30 
31 namespace tpie {
32 namespace file_accessor {
33 
34 template <typename file_accessor_t>
36 private:
37  inline void validate_header(const stream_header_t & header);
38  inline void fill_header(stream_header_t & header, bool clean);
39 
40  bool m_open;
41  bool m_write;
42 
43  file_accessor_t m_fileAccessor;
44 
46  stream_size_type m_size;
47 
49  memory_size_type m_userDataSize;
50 
52  memory_size_type m_maxUserDataSize;
53 
55  memory_size_type m_itemSize;
56 
58  memory_size_type m_blockSize;
59 
61  memory_size_type m_blockItems;
62 
64  std::string m_path;
65 
70  inline void read_header();
71 
75  inline void write_header(bool clean);
76 
80  inline memory_size_type boundary() const { return 4096; }
81 
86  inline memory_size_type align_to_boundary(memory_size_type z) const { return (z+boundary()-1)/boundary()*boundary(); }
87 
92  inline memory_size_type header_size() const { return align_to_boundary(sizeof(stream_header_t)+m_maxUserDataSize); }
93 public:
94  inline stream_accessor()
95  : m_open(false)
96  , m_write(false)
97  {
98  }
99 
100  inline ~stream_accessor() {close();}
101 
105  inline void open(const std::string & path,
106  bool read,
107  bool write,
108  memory_size_type itemSize,
109  memory_size_type blockSize,
110  memory_size_type maxUserDataSize,
111  cache_hint cacheHint);
112 
113  inline void close();
114 
124  inline memory_size_type read_block(void * data, stream_size_type blockNumber, memory_size_type itemCount);
125 
135  inline void write_block(const void * data, stream_size_type blockNumber, memory_size_type itemCount);
136 
145  inline memory_size_type read_user_data(void * data, memory_size_type count);
146 
153  inline void write_user_data(const void * data, memory_size_type count);
154 
158  static inline memory_size_type memory_usage() {return sizeof(stream_accessor<file_accessor_t>);}
159 
163  inline stream_size_type size() const {return m_size;}
164 
168  inline const std::string & path() const {return m_path;}
169 
173  inline memory_size_type user_data_size() const {return m_userDataSize;}
174 
178  inline memory_size_type max_user_data_size() const {return m_maxUserDataSize;}
179 
185  inline stream_size_type byte_size() const {
186  return ((m_size + m_blockItems - 1)/m_blockItems) * m_blockSize + header_size();
187  }
188 
189  inline void truncate(stream_size_type items);
190 };
191 
192 }
193 }
194 
195 #include <tpie/file_accessor/stream_accessor.inl>
196 #endif //_TPIE_FILE_ACCESSOR_FILE_ACCESSOR_CRTP_H
Different hints for OS file caching.
cache_hint
Definition: cache_hint.h:28
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...
Declare default file accessor.
const std::string & path() const
Path of the file currently open.
stream_size_type size() const
Number of items in stream.
static memory_size_type memory_usage()
Return memory usage of this file accessor.
memory_size_type user_data_size() const
Size (in bytes) of the user data.
void write_user_data(const void *data, memory_size_type count)
Write user data to the stream.
memory_size_type read_block(void *data, stream_size_type blockNumber, memory_size_type itemCount)
Read the given number of items from the given block into the given buffer.
void write_block(const void *data, stream_size_type blockNumber, memory_size_type itemCount)
Write the given number of items from the given buffer into the given block.
memory_size_type read_user_data(void *data, memory_size_type count)
Read user data into the given buffer.
Header of streams.
memory_size_type max_user_data_size() const
Maximum size (in bytes) of the user data.
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)
Open file for reading and/or writing.