TPIE

11a2c2d
serialization_stream.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 2013, 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 
20 #ifndef TPIE_SERIALIZATION_STREAM_H
21 #define TPIE_SERIALIZATION_STREAM_H
22 
26 
27 #include <tpie/tpie_export.h>
28 #include <tpie/serialization2.h>
30 #include <memory>
31 #include <tpie/access_type.h>
32 #include <tpie/array.h>
33 #include <tpie/tempname.h>
34 #include <algorithm>
35 
36 namespace tpie {
37 
38 namespace bits {
39 
40 class TPIE_EXPORT serialization_writer_base {
41 public:
42  static memory_size_type block_size() {
43  return 2*1024*1024;
44  }
45 
46 private:
47  file_accessor::raw_file_accessor m_fileAccessor;
48  stream_size_type m_blocksWritten;
49  stream_size_type m_size;
50  bool m_open;
51 
52  temp_file * m_tempFile;
53 
54 protected:
56 
57  void open(std::string path, bool reverse);
58  void open(temp_file & tempFile, bool reverse);
59 
60 private:
61  void open_inner(std::string path, bool reverse);
62 
63 protected:
72  void write_block(const char * const s, const memory_size_type n);
73 
74  void close(bool reverse);
75 
76 public:
77  static memory_size_type memory_usage() { return block_size(); }
78 
79  stream_size_type file_size();
80 };
81 
82 } // namespace bits
83 
85 private:
87 
88  tpie::array<char> m_block;
89  memory_size_type m_index;
90 
91  void write_block();
92 
93 public:
94 
95  class serializer {
97 
98  public:
99  serializer(serialization_writer & wr) : wr(wr) {}
100 
101  void write(const char * const s, const memory_size_type n) {
102  const char * i = s;
103  memory_size_type written = 0;
104  while (written != n) {
105  if (wr.m_index >= wr.block_size()) wr.write_block();
106 
107  memory_size_type remaining = n - written;
108  memory_size_type blockRemaining = wr.block_size() - wr.m_index;
109 
110  memory_size_type writeSize = std::min(remaining, blockRemaining);
111 
112  std::copy(i, i + writeSize, &wr.m_block[wr.m_index]);
113  i += writeSize;
114  written += writeSize;
115  wr.m_index += writeSize;
116  }
117  }
118  };
119 
120  friend class serializer;
121 
124 
125  void open(std::string path);
126  void open(temp_file & tempFile);
127 
128  void close();
129 
135  template <typename T>
136  void serialize(const T & v) {
137  using tpie::serialize;
138  serializer s(*this);
139  serialize(s, v);
140  }
141 
149  template <typename IT>
150  void serialize(IT a, IT b) {
151  using tpie::serialize;
152  serializer s(*this);
153  serialize(s, a, b);
154  }
155 };
156 
159 
160  tpie::array<char> m_block;
166  memory_size_type m_index;
167  std::vector<char> m_serializationBuffer;
168 
169  void write_block();
170 
171 public:
172 
173  class serializer {
175 
176  public:
177  serializer(serialization_reverse_writer & wr) : wr(wr) {}
178 
179  void write(const char * const s, const memory_size_type n) {
180  std::vector<char> & data = wr.m_serializationBuffer;
181  memory_size_type offs = data.size();
182  data.resize(data.size() + n);
183  std::copy(s, s + n, data.data() + offs);
184  }
185 
186  ~serializer() {
187  std::vector<char> & data = wr.m_serializationBuffer;
188  const memory_size_type n = data.size();
189  const char * const s = data.data();
190  if (wr.m_index + n <= wr.block_size()) {
191  std::copy(s, s + n, &wr.m_block[block_size() - wr.m_index - n]);
192  wr.m_index += n;
193  } else {
194  const char * i = s + n;
195  memory_size_type written = 0;
196  while (written != n) {
197  if (wr.m_index >= wr.block_size()) wr.write_block();
198 
199  memory_size_type remaining = n - written;
200  memory_size_type blockRemaining = wr.block_size() - wr.m_index;
201 
202  memory_size_type writeSize = std::min(remaining, blockRemaining);
203 
204  std::copy(i - writeSize, i, &wr.m_block[block_size() - wr.m_index - writeSize]);
205  i -= writeSize;
206  written += writeSize;
207  wr.m_index += writeSize;
208  }
209  }
210  data.resize(0);
211  }
212  };
213 
214  friend class serializer;
215 
218 
219  void open(std::string path);
220  void open(temp_file & tempFile);
221 
222  void close();
223 
229  template <typename T>
230  void serialize(const T & v) {
231  using tpie::serialize;
232  serializer s(*this);
233  serialize(s, v);
234  }
235 
243  template <typename IT>
244  void serialize(IT a, IT b) {
245  using tpie::serialize;
246  serializer s(*this);
247  serialize(s, a, b);
248  }
249 };
250 
251 namespace bits {
252 
253 class TPIE_EXPORT serialization_reader_base {
254 public:
255  static memory_size_type block_size() {
256  return serialization_writer_base::block_size();
257  }
258 
259 private:
260  file_accessor::raw_file_accessor m_fileAccessor;
261  bool m_open;
262 
263 protected:
264  tpie::array<char> m_block;
265  stream_size_type m_size;
266  memory_size_type m_index;
267  memory_size_type m_blockSize;
268 
270 
271  void open(std::string path, bool reverse);
272 
273  void read_block(const stream_size_type blk);
274 
275  // Check if EOF is reached, call read_block(blk) to reset m_index/m_blockSize.
276  virtual void next_block() = 0;
277 
278 public:
279  void close();
280 
287  void read(char * const s, const memory_size_type n) {
288  // TODO: inline some of this
289  char * i = s;
290  memory_size_type written = 0;
291  while (written != n) {
292  if (m_index >= m_blockSize) {
293  // virtual invocation
294  next_block();
295  }
296 
297  memory_size_type remaining = n - written;
298  memory_size_type blockRemaining = m_blockSize - m_index;
299 
300  memory_size_type readSize = std::min(remaining, blockRemaining);
301 
302  i = std::copy(m_block.get() + m_index,
303  m_block.get() + (m_index + readSize),
304  i);
305 
306  written += readSize;
307  m_index += readSize;
308  }
309  }
310 
320  template <typename T>
321  void unserialize(T & v) {
322  using tpie::unserialize;
323  unserialize(*this, v);
324  }
325 
335  template <typename IT>
336  void unserialize(IT a, IT b) {
337  using tpie::unserialize;
338  unserialize(*this, a, b);
339  }
340 
341  static memory_size_type memory_usage() { return block_size(); }
342 
346  stream_size_type file_size();
347 
353  stream_size_type size();
354 };
355 
356 } // namespace bits
357 
360  stream_size_type m_blockNumber;
361 
362 protected:
363  void next_block() override;
364 
365 public:
367  virtual ~serialization_reader() = default;
368 
369  void open(std::string path);
370  void open(temp_file & tempFile);
371 
372  bool can_read() {
373  if (m_index < m_blockSize) return true;
374  return m_blockNumber * (stream_size_type)block_size() + m_index < m_size;
375  }
376 
382  stream_size_type offset();
383 };
384 
387  stream_size_type m_blockNumber;
388 
389 protected:
390  void next_block() override;
391 
392 public:
394 
395  void open(std::string path);
396  void open(temp_file & tempFile);
397 
398  bool can_read() {
399  if (m_index < m_blockSize) return true;
400  return m_blockNumber > 0;
401  }
402 
408  stream_size_type offset();
409 };
410 
411 }
412 
413 #endif // TPIE_SERIALIZATION_STREAM_H
tpie::serialization_reverse_writer::serialize
void serialize(IT a, IT b)
Serialize a sequence of serializable items and write them to the stream.
Definition: serialization_stream.h:244
tpie::bits::serialization_writer_base
Definition: serialization_stream.h:40
tpie::serialization_reverse_writer
Definition: serialization_stream.h:157
file_accessor.h
tpie::open
Definition: stream.h:38
tpie::array< char >
tempname.h
tpie::bits::serialization_reader_base::unserialize
void unserialize(T &v)
Unserialize an unserializable item from the stream.
Definition: serialization_stream.h:321
array.h
tpie::serialization_reverse_reader
Definition: serialization_stream.h:385
tpie::unserialize
void unserialize(S &src, foo &v)
Sample tpie::unserialize prototype.
serialization2.h
tpie::serialize
void serialize(D &dst, const foo &v)
Sample tpie::serialize prototype.
tpie::serialization_writer::serialize
void serialize(const T &v)
Serialize a serializable item and write it to the stream.
Definition: serialization_stream.h:136
tpie::bits::serialization_reader_base
Definition: serialization_stream.h:253
tpie::array::get
T * get()
Return a raw pointer to the array content.
Definition: array.h:536
tpie::serialization_writer::serialize
void serialize(IT a, IT b)
Serialize a sequence of serializable items and write them to the stream.
Definition: serialization_stream.h:150
tpie::serialization_writer::serializer
Definition: serialization_stream.h:95
tpie::serialization_reverse_writer::serialize
void serialize(const T &v)
Serialize a serializable item and write it to the stream.
Definition: serialization_stream.h:230
tpie::bits::serialization_reader_base::unserialize
void unserialize(IT a, IT b)
Unserialize a sequence of unserializable items from the stream.
Definition: serialization_stream.h:336
tpie::serialization_reader
Definition: serialization_stream.h:358
tpie::temp_file
Class representing a reference to a temporary file.
Definition: tempname.h:201
tpie::serialization_writer
Definition: serialization_stream.h:84
tpie::bits::serialization_reader_base::read
void read(char *const s, const memory_size_type n)
Read n bytes from stream into buffer starting at s.
Definition: serialization_stream.h:287
access_type.h
tpie
Definition: access_type.h:26
tpie::file_accessor::posix
POSIX-style file accessor.
Definition: posix.h:35
tpie::serialization_reverse_writer::serializer
Definition: serialization_stream.h:173