TPIE

2362a60
stream_old.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 2008, 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 #ifndef _TPIE_AMI_STREAM_H
24 #define _TPIE_AMI_STREAM_H
25 
26 
27 // Include the configuration header.
28 #include <tpie/config.h>
29 
30 // Get definitions for working with Unix and Windows
31 #include <tpie/portability.h>
32 
33 // Get the error codes.
34 #include <tpie/err.h>
35 #include <tpie/persist.h>
36 
37 #include <tpie/tempname.h>
39 
40 #include <tpie/tpie_log.h>
41 
42 #include <tpie/stream_usage.h>
43 
44 #include <tpie/tpie_assert.h>
45 
46 #include <tpie/file_manager.h>
47 
48 namespace tpie {
49 
50  namespace ami {
51 
53  enum stream_type {
54  READ_STREAM = 1, // Open existing stream for reading
55  WRITE_STREAM, // Open for writing. Create if non-existent
56  APPEND_STREAM, // Open for writing at end. Create if needed.
57  READ_WRITE_STREAM // Open to read and write.
58  };
59 
66  };
67 
68  } // ami namespace
69 
70 } // tpie namespace
71 
72 
73 namespace tpie {
74 
75  namespace ami {
76 
99 template<class T >
100 class stream_old {
101 
102 public:
103  typedef T item_type;
104 
105  // We have a variety of constructors for different uses.
106 
112  stream_old();
113 
119  stream_old(const std::string& path_name,
120  stream_type st = READ_WRITE_STREAM);
121 
142  stream_offset_type sub_begin,
143  stream_offset_type sub_end,
144  stream_old<T> **sub_stream);
145 
153  inline stream_status status() const {
154  return m_status;
155  }
156 
161  inline bool is_valid() const {
162  return m_status == STREAM_STATUS_VALID;
163  }
164 
169  inline bool operator!() const {
170  return !is_valid();
171  }
172 
180  inline err read_item(T **elt);
181 
187  inline err write_item(const T &elt);
188 
195  err read_array(T *mm_space, stream_offset_type *len);
196 
202  err read_array(T *mm_space, memory_size_type & len);
203 
209  err write_array(const T *mm_space, memory_size_type len);
210 
214  inline stream_offset_type stream_len(void) const {
215  return m_stream.size();
216  }
217 
221  inline std::string name() const;
222 
226  inline err seek(stream_offset_type offset);
227 
232  inline stream_offset_type tell() const {
233  return m_stream.offset();
234  }
235 
245  inline err truncate(stream_offset_type offset);
246 
264  err main_memory_usage(size_type *usage,
265  stream_usage usage_type) const;
266 
267 
268 
272  static memory_size_type memory_usage(memory_size_type count);
273 
274  // This method used to return a statistics object, but this is no longer
275  // supported.
276  //struct stats_stream & stats() const {
277  // tp_assert(0, "stream::stats() is no longer supported");
278  // return *((stats_stream *)0);
279  //}
280 
281  // This method used to return a statistics object, but this is no longer
282  // supported.
283  //static stats_stream & gstats() {
284  // tp_assert(0, "stream::stats() is no longer supported");
285  // return *((stats_stream *)0);
286  //}
287 
294  size_t available_streams(void) {
295  return get_file_manager().available();
296  }
297 
302  memory_size_type chunk_size(void) const {
303  return file_base::block_size(1.0) / sizeof(T);
304  }
305 
310  void persist(persistence p) {
311  m_temp.set_persistent(p == PERSIST_PERSISTENT);
312  }
313 
318  persistence persist() const {
319  return m_temp.is_persistent() ? PERSIST_PERSISTENT : PERSIST_DELETE;
320  }
321 
324  // This function gives easy access to the stream's file name and its length.
326  std::string& sprint();
327 
332  return m_stream;
333  }
334 
335 private:
336 
338  stream_old(const stream_old<T>& other);
340  stream_old<T>& operator=(const stream_old<T>& other);
341 
342  temp_file m_temp;
343  uncompressed_stream<T> m_stream;
344 
347  //bool m_destructBTEStream;
348  stream_status m_status;
349 
350  static inline float block_factor() {
351 #ifndef STREAM_UFS_BLOCK_FACTOR
352  return 1.0;
353 #else
354 # ifdef WIN32
355  return static_cast<float>(STREAM_UFS_BLOCK_FACTOR)/32;
356 # else
357  return static_cast<float>(STREAM_UFS_BLOCK_FACTOR)/512;
358 # endif
359 #endif
360  }
361 };
362 
363 // Create a temporary AMI stream on one of the devices in the default
364 // device description. Persistence is PERSIST_DELETE by default. We
365 // are given the index of the string describing the desired device.
366  template<class T>
367  stream_old<T>::stream_old(): m_stream(block_factor()), m_status(STREAM_STATUS_INVALID)
368  {
369  TP_LOG_DEBUG_ID("Temporary stream in file: ");
370  TP_LOG_DEBUG_ID( m_temp.path() );
371  try {
372  m_stream.open( m_temp );
373  } catch(const stream_exception &e) {
374  tpie::unused(e);
375  TP_LOG_FATAL_ID("Open failed: " << e.what());
376  return;
377  }
378  // Set status to VALID.
379  m_status = STREAM_STATUS_VALID;
380  };
381 
382 
383 // A stream created with this constructor will persist on disk at the
384 // location specified by the path name.
385  template<class T>
386  stream_old<T>::stream_old(const std::string& path_name, stream_type st) :
387  m_temp(path_name, true), m_stream(block_factor()), m_status(STREAM_STATUS_INVALID) {
388  try {
389  m_stream.open(m_temp, st==READ_STREAM ? access_read: access_read_write);
390  if (st == APPEND_STREAM) m_stream.seek(0, file_stream_base::end);
391  } catch(const stream_exception &e) {
392  tpie::unused(e);
393  TP_LOG_FATAL_ID("Open failed: " << e.what());
394  return;
395  }
396  m_status = STREAM_STATUS_VALID;
397  };
398 
399 
400  // *stream::new_substream* //
401  template<class T>
403  stream_offset_type sub_begin,
404  stream_offset_type sub_end,
405  stream_old<T> **sub_stream)
406  {
407  unused(st);
408  unused(sub_begin);
409  unused(sub_end);
410  unused(sub_stream);
411  return BTE_ERROR;
412  }
413 
414 
415  template<class T>
416  inline std::string stream_old<T>::name() const {
417  return m_stream.path();
418  }
419 
420 // Move to a specific offset.
421  template<class T>
422  inline err stream_old<T>::seek(stream_offset_type offset) {
423  try {
424  m_stream.seek(offset);
425  } catch(const stream_exception &e) {
426  tpie::unused(e);
427  TP_LOG_WARNING_ID("BTE error - seek failed: " << e.what());
428  return BTE_ERROR;
429  }
430  return NO_ERROR;
431  }
432 
433 // Truncate
434  template<class T>
435  inline err stream_old<T>::truncate(stream_offset_type offset) {
436  try {
437  m_stream.truncate(offset);
438  } catch(const stream_exception & e) {
439  tpie::unused(e);
440  TP_LOG_WARNING_ID("BTE error - truncate failed: " << e.what());
441  return BTE_ERROR;
442  }
443  return NO_ERROR;
444  }
445 
446 template<class T>
447 memory_size_type stream_old<T>::memory_usage(memory_size_type count) {
448  return count*(uncompressed_stream<T>::memory_usage(block_factor()) + sizeof(stream_old<T>));
449 }
450 
451 
452 // Query memory usage
453  template<class T>
454  err stream_old<T>::main_memory_usage(memory_size_type *usage,
455  stream_usage usage_type) const {
456 
457  switch (usage_type) {
459  *usage = sizeof(*this) + uncompressed_stream<T>::memory_usage(0.0);
460  return NO_ERROR;
464  *usage = memory_usage(1);
465  return NO_ERROR;
466  case STREAM_USAGE_BUFFER:
468  return NO_ERROR;
469  }
470  return BTE_ERROR;
471  }
472 
473  template<class T>
474  inline err stream_old<T>::read_item(T **elt) {
475  if (!m_stream.can_read())
476  return END_OF_STREAM;
477 
478  *elt = &(const_cast<T &>(m_stream.read()));
479  return NO_ERROR;
480  }
481 
482  template<class T>
483  inline err stream_old<T>::write_item(const T &elt) {
484  m_stream.write(elt);
485  return NO_ERROR;
486  }
487 
488  template<class T>
489  err stream_old<T>::read_array(T *mm_space, stream_offset_type *len) {
490  size_type l=(size_t)*len;
491  err e = read_array(mm_space, l);
492  *len = l;
493  return e;
494  }
495 
496  template<class T>
497  err stream_old<T>::read_array(T *mm_space, memory_size_type & len) {
498  size_type l = static_cast<size_type>(std::min(
499  static_cast<stream_size_type>(len),
500  static_cast<stream_size_type>(m_stream.size() - m_stream.offset())));
501  m_stream.read(mm_space, mm_space+l);
502  return (l == len)?NO_ERROR:END_OF_STREAM;
503  }
504 
505  template<class T>
506  err stream_old<T>::write_array(const T *mm_space, size_type len) {
507  m_stream.write(mm_space, mm_space+len);
508  return NO_ERROR;
509  }
510 
511  template<class T>
512  std::string& stream_old<T>::sprint() {
513  static std::string buf;
514  std::stringstream ss;
515  ss << "STREAM " << name() << " " << static_cast<long>(stream_len());
516  ss >> buf;
517  return buf;
518  }
519 
520  } // ami namespace
521 
522 } // tpie namespace
523 
524 #endif // _TPIE_AMI_STREAM_H
A Stream object stores an ordered collection of objects of type T on external memory.
Definition: stream_old.h:100
err main_memory_usage(size_type *usage, stream_usage usage_type) const
This function is used for obtaining the amount of main memory used by an Stream object (in bytes)...
Definition: stream_old.h:454
Defines the tp_assert macro.
err truncate(stream_offset_type offset)
Resize the stream to off items.
Definition: stream_old.h:435
Amount currently in use.
Definition: stream_usage.h:34
Maximum additional amount used by each substream created.
Definition: stream_usage.h:38
void persist(persistence p)
Set the stream's persistence flag to p, which can have one of two values: PERSIST_DELETE or PERSIST_P...
Definition: stream_old.h:310
bool operator!() const
Returns true if the block's status is not BLOCK_STATUS_VALID.
Definition: stream_old.h:169
stream_offset_type stream_len(void) const
Returns the number of items in the stream.
Definition: stream_old.h:214
PERSIST_DELETE
Delete the stream from the disk when it is destructed.
Definition: persist.h:39
Max amount that will ever be used.
Definition: stream_usage.h:36
err write_array(const T *mm_space, memory_size_type len)
Writes len items from array |mm_array to the stream, starting in the current position.
Definition: stream_old.h:506
std::string name() const
Returns the path name of this stream in newly allocated space.
Definition: stream_old.h:416
void unused(const T &x)
Declare that a variable is unused on purpose.
Definition: util.h:42
std::string & sprint()
Return a string describing the stream.
Definition: stream_old.h:512
No error occurred.
Definition: err.h:47
This file contains a few deprecated definitions for legacy code.
Open a file for reading.
Definition: access_type.h:31
Overhead of the object without the buffer.
Definition: stream_usage.h:30
void set_persistent(bool p)
Set persistence.
Definition: tempname.h:230
Logging functionality and log_level codes for different priorities of log messages.
persistence persist() const
Set the stram's persistence flag to PERSIST_PERSISTENT, thereby ensuring it is not deleted when destr...
Definition: stream_old.h:318
err new_substream(stream_type st, stream_offset_type sub_begin, stream_offset_type sub_end, stream_old< T > **sub_stream)
A substream is a TPIE stream that is part of another TPIE stream.
Definition: stream_old.h:402
memory_size_type chunk_size(void) const
Returns the maximum number of items (of type T) that can be stored in one block.
Definition: stream_old.h:302
stream_status status() const
Returns the status of the stream instance; the result is either STREAM_STATUS_VALID or STREAM_STATUS_...
Definition: stream_old.h:153
uncompressed_stream< T > & underlying_stream()
Get the underlying uncompressed_stream
Definition: stream_old.h:331
stream_status
AMI stream status.
Definition: stream_old.h:61
stream_usage enum
bool is_valid() const
Returns wether the status of the stream is STREAM_STATUS_VALID.
Definition: stream_old.h:161
const std::string & path()
Get the path of the associated file.
Definition: tempname.h:244
file_manager & get_file_manager()
Return a reference to the file manager.
Class representing a reference to a temporary file.
Definition: tempname.h:202
Legacy AMI error types.
Persistence tags for deprecated TPIE AMI streams.
err read_array(T *mm_space, stream_offset_type *len)
Reads *len items from the current position of the stream into the array mm_array. ...
Definition: stream_old.h:489
err seek(stream_offset_type offset)
Move the current position to off (measured in terms of items.
Definition: stream_old.h:422
size_t available() const noexcept
Return the amount of the resource still available to be assigned.
stream_type
AMI stream types passed to constructors.
Definition: stream_old.h:53
stream_offset_type tell() const
Returns the current position in the stream measured of items from the beginning of the stream...
Definition: stream_old.h:232
memory_size_type block_size() const
Get the size of a block in bytes.
PERSIST_PERSISTENT
Do not delete the stream from the disk when it is destructed.
Definition: persist.h:41
static memory_size_type memory_usage(float blockFactor=1.0, bool includeDefaultFileAccessor=true)
Calculate the amount of memory used by a single uncompressed_stream.
Simple class acting both as file and a file::stream.
bool is_persistent() const
Definition: tempname.h:222
stream_old()
A new stream of type READ_WRITE_STREAM is constructed on the given device as a file with a randomly g...
Definition: stream_old.h:367
err write_item(const T &elt)
Writes elt to the stream in the current position.
Definition: stream_old.h:483
err read_item(T **elt)
Reads the current item from the stream and advance the "current item" pointer to the next item...
Definition: stream_old.h:474
err
Legacy TPIE error codes.
Definition: err.h:45
size_t available_streams(void)
Returns the number of globally available streams.
Definition: stream_old.h:294
An error occurred at the BTE level.
Definition: err.h:63
Temporary file names.
Simple class acting both as a tpie::file and a tpie::file::stream.
stream_usage
Definition: stream_usage.h:28
An attempt was made to read past the end of a stream or write past the end of a substream.
Definition: err.h:52
static memory_size_type memory_usage(memory_size_type count)
Returns the number of bytes that count streams will maximaly consume.
Definition: stream_old.h:447
Max amount ever used by a buffer.
Definition: stream_usage.h:32
Open a file for reading or writing.
Definition: access_type.h:35