TPIE

2362a60
stream_accessor_base.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_STREAM_ACCESSOR_BASE_H
20 #define TPIE_FILE_ACCESSOR_STREAM_ACCESSOR_BASE_H
21 
26 
27 #include <tpie/stream_header.h>
28 #include <tpie/cache_hint.h>
29 
30 namespace tpie {
31 namespace file_accessor {
32 
33 template <typename file_accessor_t>
35 private:
36  inline void validate_header(const stream_header_t & header);
37  inline void fill_header(stream_header_t & header, bool clean);
38 
39  bool m_open;
40  bool m_write;
41 
42 protected:
43  file_accessor_t m_fileAccessor;
44 
45 private:
47  stream_size_type m_size;
48 
50  memory_size_type m_userDataSize;
51 
53  memory_size_type m_maxUserDataSize;
54 
56  memory_size_type m_itemSize;
57 
59  memory_size_type m_blockSize;
60 
62  memory_size_type m_blockItems;
63 
65  stream_size_type m_lastBlockReadOffset;
66 
68  int m_compressionFlags;
69 
71  bool m_useCompression;
72 
74  std::string m_path;
75 
80  inline void read_header();
81 
85  inline void write_header(bool clean);
86 
87 protected:
91  inline memory_size_type boundary() const { return 4096; }
92 
97  inline memory_size_type align_to_boundary(memory_size_type z) const { return (z+boundary()-1)/boundary()*boundary(); }
98 
103  inline memory_size_type header_size() const { return align_to_boundary(sizeof(stream_header_t)+m_maxUserDataSize); }
104 
105  memory_size_type block_size() const { return m_blockSize; }
106  memory_size_type block_items() const { return m_blockItems; }
107  memory_size_type item_size() const { return m_itemSize; }
108 
109  void set_size(stream_size_type s) { m_size = s; }
110 
111 public:
112  inline stream_accessor_base()
113  : m_open(false)
114  , m_write(false)
115  {
116  }
117 
118  virtual ~stream_accessor_base() {close();}
119 
123  inline void open(const std::string & path,
124  bool read,
125  bool write,
126  memory_size_type itemSize,
127  memory_size_type blockSize,
128  memory_size_type maxUserDataSize,
129  cache_hint cacheHint,
130  int compressionFlags);
131 
132  inline void close();
133 
143  virtual memory_size_type read_block(void * data, stream_size_type blockNumber, memory_size_type itemCount) = 0;
144 
154  virtual void write_block(const void * data, stream_size_type blockNumber, memory_size_type itemCount) = 0;
155 
164  inline memory_size_type read_user_data(void * data, memory_size_type count);
165 
172  inline void write_user_data(const void * data, memory_size_type count);
173 
177  static inline memory_size_type memory_usage() {return sizeof(stream_accessor_base);}
178 
182  inline stream_size_type size() const {return m_size;}
183 
187  inline const std::string & path() const {return m_path;}
188 
192  inline memory_size_type user_data_size() const {return m_userDataSize;}
193 
197  inline memory_size_type max_user_data_size() const {return m_maxUserDataSize;}
198 
204  inline stream_size_type byte_size() const {
205  return ((m_size + m_blockItems - 1)/m_blockItems) * m_blockSize + header_size();
206  }
207 
208  inline void truncate(stream_size_type items);
209 
210  void set_last_block_read_offset(stream_size_type n) { m_lastBlockReadOffset = n; }
211  stream_size_type get_last_block_read_offset() { return m_lastBlockReadOffset; }
212 
213  bool get_compressed() { return m_useCompression; }
214 
215  int get_compression_flags() { return m_compressionFlags; }
216 };
217 
218 }
219 }
220 
221 #include <tpie/file_accessor/stream_accessor_base.inl>
222 #endif // TPIE_FILE_ACCESSOR_STREAM_ACCESSOR_BASE_H
memory_size_type read_user_data(void *data, memory_size_type count)
Read user data into the given buffer.
Different hints for OS file caching.
virtual memory_size_type read_block(void *data, stream_size_type blockNumber, memory_size_type itemCount)=0
Read the given number of items from the given block into the given buffer.
void write_user_data(const void *data, memory_size_type count)
Write user data to the stream.
stream_size_type size() const
Number of items in stream.
cache_hint
Definition: cache_hint.h:28
memory_size_type header_size() const
The size of header and user data with padding included.
memory_size_type boundary() const
Returns the boundary on which we align blocks.
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...
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 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.
memory_size_type user_data_size() const
Size (in bytes) of the user data.
memory_size_type align_to_boundary(memory_size_type z) const
Given a memory offset, rounds up to the nearest alignment boundary.
Header of streams.
static memory_size_type memory_usage()
Return memory usage of this file accessor.
memory_size_type max_user_data_size() const
Maximum size (in bytes) of the user data.
const std::string & path() const
Path of the file currently open.