libfoedus-core
FOEDUS Core Library
direct_io_file.hpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2014-2015, Hewlett-Packard Development Company, LP.
3  * This program is free software; you can redistribute it and/or modify it
4  * under the terms of the GNU General Public License as published by the Free
5  * Software Foundation; either version 2 of the License, or (at your option)
6  * any later version.
7  *
8  * This program is distributed in the hope that it will be useful, but WITHOUT
9  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
11  * more details. You should have received a copy of the GNU General Public
12  * License along with this program; if not, write to the Free Software
13  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
14  *
15  * HP designates this particular file as subject to the "Classpath" exception
16  * as provided by HP in the LICENSE.txt file that accompanied this code.
17  */
18 #ifndef FOEDUS_FS_DIRECT_IO_FILE_HPP_
19 #define FOEDUS_FS_DIRECT_IO_FILE_HPP_
20 #include <stdint.h>
21 
22 #include <iosfwd>
23 #include <string>
24 
25 #include "foedus/cxx11.hpp"
26 #include "foedus/error_code.hpp"
28 #include "foedus/fs/path.hpp"
29 #include "foedus/memory/fwd.hpp"
30 
31 namespace foedus {
32 namespace fs {
33 
48 class DirectIoFile {
49  public:
51  typedef int file_descriptor;
53  enum Constants {
56  };
57 
59  enum SeekType {
66  };
67 
74  const Path &path,
75  const DeviceEmulationOptions &emulation = DeviceEmulationOptions());
76 
78  ~DirectIoFile();
79 
80  // Disable default constructors
83  DirectIoFile& operator=(const DirectIoFile &) CXX11_FUNC_DELETE;
84 
92  ErrorCode open(bool read, bool write, bool append, bool create);
93 
95  bool is_opened() const { return descriptor_ != kInvalidDescriptor; }
96 
102  bool close();
103 
113  ErrorCode read(uint64_t desired_bytes, foedus::memory::AlignedMemory* buffer);
115  ErrorCode read(uint64_t desired_bytes, const foedus::memory::AlignedMemorySlice& slice);
117  ErrorCode read_raw(uint64_t desired_bytes, void* buffer);
118 
128  ErrorCode write(uint64_t desired_bytes, const foedus::memory::AlignedMemory& buffer);
130  ErrorCode write(uint64_t desired_bytes, const foedus::memory::AlignedMemorySlice& slice);
132  ErrorCode write_raw(uint64_t desired_bytes, const void* buffer);
133 
144  ErrorCode truncate(uint64_t new_length, bool sync = false);
145 
150  ErrorCode seek(uint64_t offset, SeekType seek_type);
151 
152 
167  ErrorCode sync();
168 
169 
170  Path get_path() const { return path_; }
171  DeviceEmulationOptions get_emulation() const { return emulation_; }
172  file_descriptor get_descriptor() const { return descriptor_; }
173  uint64_t get_current_offset() const { return current_offset_; }
174  bool is_read() const { return read_; }
175  bool is_write() const { return write_; }
176 
177  std::string to_string() const;
178  friend std::ostream& operator<<(std::ostream& o, const DirectIoFile& v);
179 
180  private:
182  Path path_;
183 
185  DeviceEmulationOptions emulation_;
186 
188  file_descriptor descriptor_;
189 
191  bool read_;
192 
194  bool write_;
195 
197  uint64_t current_offset_;
198 };
199 } // namespace fs
200 } // namespace foedus
201 #endif // FOEDUS_FS_DIRECT_IO_FILE_HPP_
202 
The offset is set to its current location plus offset bytes.
ErrorCode truncate(uint64_t new_length, bool sync=false)
Discard the content of the file after the given offset.
ErrorCode write(uint64_t desired_bytes, const foedus::memory::AlignedMemory &buffer)
Sequentially write the given amount of contents from the current position.
Root package of FOEDUS (Fast Optimistic Engine for Data Unification Services).
Definition: assert_nd.hpp:44
bool close()
Close the file if not yet closed.
ErrorCode open(bool read, bool write, bool append, bool create)
Tries to open the file for the specified volume.
uint64_t get_current_offset() const
~DirectIoFile()
Automatically closes the file if it is opened.
ErrorCode read_raw(uint64_t desired_bytes, void *buffer)
A version that receives a raw pointer that has to be aligned (be careful to use this ver)...
Analogue of boost::filesystem::path.
Definition: path.hpp:37
The offset is set to the size of the file plus offset bytes.
Constants
Constant values.
bool is_opened() const
Whether the file is already and successfully opened.
SeekType
Analogue of SEEK_SET/SEEK_CUR/SEEK_END in POSIX.
ErrorCode sync()
Analogues of POSIX fsync().
ErrorCode seek(uint64_t offset, SeekType seek_type)
Sets the position of the next byte to be written/extracted from/to the stream.
ErrorCode read(uint64_t desired_bytes, foedus::memory::AlignedMemory *buffer)
Sequentially read the given amount of contents from the current position.
A slice of foedus::memory::AlignedMemory.
ErrorCode write_raw(uint64_t desired_bytes, const void *buffer)
A version that receives a raw pointer that has to be aligned (be careful to use this ver)...
Forward declarations of classes in memory package.
int file_descriptor
Represents low-level file descriptor.
file_descriptor get_descriptor() const
Set of configurations to emulate slower devices for some experiments.
#define CXX11_FUNC_DELETE
Used in public headers in place of " = delete" of C++11.
Definition: cxx11.hpp:128
Represents an I/O stream on one file without filesystem caching.
friend std::ostream & operator<<(std::ostream &o, const DirectIoFile &v)
Represents one memory block aligned to actual OS/hardware pages.
POSIX open() semantics says -1 is invalid or not-yet-opened.
The offset is set to offset bytes.
ErrorCode
Enum of error codes defined in error_code.xmacro.
Definition: error_code.hpp:85
std::string to_string() const
DeviceEmulationOptions get_emulation() const