LSSTApplications  1.1.2+25,10.0+13,10.0+132,10.0+133,10.0+224,10.0+41,10.0+8,10.0-1-g0f53050+14,10.0-1-g4b7b172+19,10.0-1-g61a5bae+98,10.0-1-g7408a83+3,10.0-1-gc1e0f5a+19,10.0-1-gdb4482e+14,10.0-11-g3947115+2,10.0-12-g8719d8b+2,10.0-15-ga3f480f+1,10.0-2-g4f67435,10.0-2-gcb4bc6c+26,10.0-28-gf7f57a9+1,10.0-3-g1bbe32c+14,10.0-3-g5b46d21,10.0-4-g027f45f+5,10.0-4-g86f66b5+2,10.0-4-gc4fccf3+24,10.0-40-g4349866+2,10.0-5-g766159b,10.0-5-gca2295e+25,10.0-6-g462a451+1
LSSTDataManagementBasePackage
FileIo.h
Go to the documentation of this file.
1 // -*- lsst-c++ -*-
2 
3 /*
4  * LSST Data Management System
5  * Copyright 2008, 2009, 2010 LSST Corporation.
6  *
7  * This product includes software developed by the
8  * LSST Project (http://www.lsst.org/).
9  *
10  * This program is free software: you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation, either version 3 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the LSST License Statement and
21  * the GNU General Public License along with this program. If not,
22  * see <http://www.lsstcorp.org/LegalNotices/>.
23  */
24 
25 
33 #ifndef LSST_AP_IO_FILE_IO_H
34 #define LSST_AP_IO_FILE_IO_H
35 
36 #include <aio.h>
37 #include <zlib.h>
38 
39 #include <string>
40 
41 #include "boost/noncopyable.hpp"
42 #include "boost/scoped_array.hpp"
43 
44 #include "../Common.h"
45 
46 
47 namespace lsst { namespace ap { namespace io {
48 
51 
52 public :
53 
54  enum State {
58  };
59 
61 
62  virtual ~SequentialIoBase() = 0;
63 
65  bool finished() const { return _state == FINISHED; }
66 
68  bool failed() const { return _state == FAILED; }
69 
71  State getState() const { return _state; }
72 
73 protected :
74 
76 };
77 
78 
81 
82 public :
83 
88  virtual std::size_t read(unsigned char * const buf, std::size_t const len) = 0;
89 };
90 
91 
94 
95 public :
96 
98  virtual void write(unsigned char const * const buf, std::size_t const len) = 0;
99 
101  virtual void finish() = 0;
102 };
103 
104 
107  public SequentialReader,
108  private boost::noncopyable
109 {
110 
111 public:
112 
113  explicit SequentialFileReader(std::string const & fileName);
114 
115  virtual ~SequentialFileReader();
116  virtual std::size_t read(unsigned char * const buf, std::size_t const size);
117 
118 private :
119 
120  int _fd;
121 
122  void cleanup();
123  void cleanup(State const state) {
124  cleanup();
125  _state = state;
126  }
127 };
128 
129 
132  public SequentialWriter,
133  private boost::noncopyable
134 {
135 
136 public :
137 
138  explicit SequentialFileWriter(
139  std::string const & fileName,
140  bool const overwrite = false
141  );
142 
143  virtual ~SequentialFileWriter();
144  virtual void write(unsigned char const * const buf, std::size_t const len);
145  virtual void finish();
146 
147 private :
148 
149  int _fd;
150 
151  void cleanup();
152  void cleanup(State const state) {
153  cleanup();
154  _state = state;
155  }
156 };
157 
158 
166 
167 public :
168 
169  explicit CompressedFileReader(
170  std::string const & fileName,
171  std::size_t const blockSize = 262144
172  );
173 
174  virtual ~CompressedFileReader();
175  virtual std::size_t read(unsigned char * const buf, std::size_t const size);
176 
177  std::size_t getBlockSize() const { return _blockSize; }
178 
179 private :
180 
181  boost::scoped_array<unsigned char> _memory;
182  unsigned char * _buffers;
183  ::z_stream _stream;
184  ::aiocb _request;
185  std::size_t const _blockSize;
186  std::size_t _fileSize;
187  std::size_t _remaining;
188  int _fd;
189 
190  void cleanup();
191  void cleanup(State const state) {
192  cleanup();
193  _state = state;
194  }
195 };
196 
197 
205 
206 public :
207 
208  explicit CompressedFileWriter(
209  std::string const & fileName,
210  bool const overwrite = false,
211  std::size_t const blockSize = 262144
212  );
213 
214  virtual ~CompressedFileWriter();
215 
216  virtual void write(unsigned char const * const buf, std::size_t const size);
217  virtual void finish();
218 
219  std::size_t getBlockSize() const { return _blockSize; }
220 
221 private :
222 
223  boost::scoped_array<unsigned char> _memory;
224  unsigned char * _buffers;
225  ::z_stream _stream;
226  ::aiocb _request;
227  std::size_t const _blockSize;
228  int _fd;
229  bool _started;
230 
231  void cleanup();
232  void cleanup(State const state) {
233  cleanup();
234  _state = state;
235  }
236 };
237 
238 
239 }}} // end of namespace lsst::ap::io
240 
241 #endif // LSST_AP_IO_FILE_IO_H
::z_stream _stream
zlib state
Definition: FileIo.h:225
virtual void finish()=0
Moves modified data to the underlying storage device and marks the SequentialWriter as finished...
boost::scoped_array< unsigned char > _memory
Definition: FileIo.h:223
bool failed() const
Returns true if a read operation failed.
Definition: FileIo.h:68
bool finished() const
Returns true if there are no more bytes available for reading.
Definition: FileIo.h:65
virtual ~SequentialIoBase()=0
Definition: FileIo.cc:85
int _fd
file descriptor
Definition: FileIo.h:188
void cleanup(State const state)
Definition: FileIo.h:152
void cleanup(State const state)
Definition: FileIo.h:123
A sequential reader for compressed files that uses asynchronous IO to overlap IO with decompression...
Definition: FileIo.h:165
virtual void write(unsigned char const *const buf, std::size_t const size)
Writes len bytes from buf to the underlying storage device.
Definition: FileIo.cc:515
CompressedFileReader(std::string const &fileName, std::size_t const blockSize=262144)
Definition: FileIo.cc:225
std::size_t getBlockSize() const
Definition: FileIo.h:177
virtual void write(unsigned char const *const buf, std::size_t const len)=0
Writes len bytes from buf to the underlying storage device.
virtual void finish()
Moves modified data to the underlying storage device and marks the SequentialWriter as finished...
Definition: FileIo.cc:606
virtual std::size_t read(unsigned char *const buf, std::size_t const size)
Definition: FileIo.cc:115
State getState() const
Returns the state of the SequentialReader.
Definition: FileIo.h:71
::aiocb _request
Outstanding IO request.
Definition: FileIo.h:184
std::size_t getBlockSize() const
Definition: FileIo.h:219
A sequential writer for compressed files that uses asynchronous IO to overlap IO with compression...
Definition: FileIo.h:204
::aiocb _request
Outstanding IO request.
Definition: FileIo.h:226
std::size_t const _blockSize
read granularity
Definition: FileIo.h:185
Abstract base class for writing a stream of data in sequential fashion.
Definition: FileIo.h:93
void cleanup(State const state)
Definition: FileIo.h:232
boost::scoped_array< unsigned char > _memory
Definition: FileIo.h:181
std::size_t _fileSize
Size of the file being read.
Definition: FileIo.h:186
A sequential reader for uncompressed files. Uses standard (blocking) IO calls.
Definition: FileIo.h:106
virtual std::size_t read(unsigned char *const buf, std::size_t const size)
Definition: FileIo.cc:313
virtual void finish()
Moves modified data to the underlying storage device and marks the SequentialWriter as finished...
Definition: FileIo.cc:204
A sequential writer for uncompressed files. Uses standard (blocking) IO calls.
Definition: FileIo.h:131
std::size_t const _blockSize
read granularity
Definition: FileIo.h:227
virtual void write(unsigned char const *const buf, std::size_t const len)
Writes len bytes from buf to the underlying storage device.
Definition: FileIo.cc:173
Abstract base class for reading a stream of data in sequential fashion.
Definition: FileIo.h:80
unsigned char * _buffers
aligned input buffers
Definition: FileIo.h:182
::z_stream _stream
zlib state
Definition: FileIo.h:183
CompressedFileWriter(std::string const &fileName, bool const overwrite=false, std::size_t const blockSize=262144)
Definition: FileIo.cc:450
std::size_t _remaining
Bytes that haven&#39;t yet been read.
Definition: FileIo.h:187
void cleanup(State const state)
Definition: FileIo.h:191
SequentialFileWriter(std::string const &fileName, bool const overwrite=false)
Definition: FileIo.cc:147
virtual std::size_t read(unsigned char *const buf, std::size_t const len)=0
unsigned char * _buffers
aligned output buffers
Definition: FileIo.h:224
Abstract base class for sequential I/O classes.
Definition: FileIo.h:50
SequentialFileReader(std::string const &fileName)
Definition: FileIo.cc:90