Apache Arrow (C++)
A columnar in-memory analytics layer designed to accelerate big data.
file.h
Go to the documentation of this file.
1 // Licensed to the Apache Software Foundation (ASF) under one
2 // or more contributor license agreements. See the NOTICE file
3 // distributed with this work for additional information
4 // regarding copyright ownership. The ASF licenses this file
5 // to you under the Apache License, Version 2.0 (the
6 // "License"); you may not use this file except in compliance
7 // with the License. You may obtain a copy of the License at
8 //
9 // http://www.apache.org/licenses/LICENSE-2.0
10 //
11 // Unless required by applicable law or agreed to in writing,
12 // software distributed under the License is distributed on an
13 // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14 // KIND, either express or implied. See the License for the
15 // specific language governing permissions and limitations
16 // under the License.
17 
18 // IO interface implementations for OS files
19 
20 #ifndef ARROW_IO_FILE_H
21 #define ARROW_IO_FILE_H
22 
23 #include <cstdint>
24 #include <memory>
25 #include <string>
26 
27 #include "arrow/io/interfaces.h"
28 #include "arrow/util/visibility.h"
29 
30 namespace arrow {
31 
32 class Buffer;
33 class MemoryPool;
34 class Status;
35 
36 namespace io {
37 
38 class ARROW_EXPORT FileOutputStream : public OutputStream {
39  public:
40  ~FileOutputStream() override;
41 
48  static Status Open(const std::string& path, std::shared_ptr<OutputStream>* out);
49 
54  static Status Open(const std::string& path, bool append,
55  std::shared_ptr<OutputStream>* out);
56 
63  static Status Open(const std::string& path, std::shared_ptr<FileOutputStream>* file);
64 
69  static Status Open(const std::string& path, bool append,
70  std::shared_ptr<FileOutputStream>* file);
71 
72  // OutputStream interface
73  Status Close() override;
74  Status Tell(int64_t* position) const override;
75 
76  // Write bytes to the stream. Thread-safe
77  Status Write(const void* data, int64_t nbytes) override;
78 
79  int file_descriptor() const;
80 
81  private:
83 
84  class ARROW_NO_EXPORT FileOutputStreamImpl;
85  std::unique_ptr<FileOutputStreamImpl> impl_;
86 };
87 
88 // Operating system file
89 class ARROW_EXPORT ReadableFile : public RandomAccessFile {
90  public:
91  ~ReadableFile() override;
92 
97  static Status Open(const std::string& path, std::shared_ptr<ReadableFile>* file);
98 
104  static Status Open(const std::string& path, MemoryPool* pool,
105  std::shared_ptr<ReadableFile>* file);
106 
107  Status Close() override;
108  Status Tell(int64_t* position) const override;
109 
110  // Read bytes from the file. Thread-safe
111  Status Read(int64_t nbytes, int64_t* bytes_read, void* buffer) override;
112  Status Read(int64_t nbytes, std::shared_ptr<Buffer>* out) override;
113 
115  Status ReadAt(int64_t position, int64_t nbytes, int64_t* bytes_read,
116  void* out) override;
117 
119  Status ReadAt(int64_t position, int64_t nbytes, std::shared_ptr<Buffer>* out) override;
120 
121  Status GetSize(int64_t* size) override;
122  Status Seek(int64_t position) override;
123 
124  bool supports_zero_copy() const override;
125 
126  int file_descriptor() const;
127 
128  private:
129  explicit ReadableFile(MemoryPool* pool);
130 
131  class ARROW_NO_EXPORT ReadableFileImpl;
132  std::unique_ptr<ReadableFileImpl> impl_;
133 };
134 
135 // A file interface that uses memory-mapped files for memory interactions,
136 // supporting zero copy reads. The same class is used for both reading and
137 // writing.
138 //
139 // If opening a file in a writeable mode, it is not truncated first as with
140 // FileOutputStream
141 class ARROW_EXPORT MemoryMappedFile : public ReadWriteFileInterface {
142  public:
143  ~MemoryMappedFile() override;
144 
146  static Status Create(const std::string& path, int64_t size,
147  std::shared_ptr<MemoryMappedFile>* out);
148 
149  static Status Open(const std::string& path, FileMode::type mode,
150  std::shared_ptr<MemoryMappedFile>* out);
151 
152  Status Close() override;
153 
154  Status Tell(int64_t* position) const override;
155 
156  Status Seek(int64_t position) override;
157 
158  // Required by RandomAccessFile, copies memory into out. Not thread-safe
159  Status Read(int64_t nbytes, int64_t* bytes_read, void* out) override;
160 
161  // Zero copy read. Not thread-safe
162  Status Read(int64_t nbytes, std::shared_ptr<Buffer>* out) override;
163 
164  Status ReadAt(int64_t position, int64_t nbytes, int64_t* bytes_read,
165  void* out) override;
166 
168  Status ReadAt(int64_t position, int64_t nbytes, std::shared_ptr<Buffer>* out) override;
169 
170  bool supports_zero_copy() const override;
171 
173  Status Write(const void* data, int64_t nbytes) override;
174 
176  Status WriteAt(int64_t position, const void* data, int64_t nbytes) override;
177 
178  // @return: the size in bytes of the memory source
179  Status GetSize(int64_t* size) override;
180 
181  int file_descriptor() const;
182 
183  private:
185 
186  Status WriteInternal(const void* data, int64_t nbytes);
187 
188  class ARROW_NO_EXPORT MemoryMap;
189  std::shared_ptr<MemoryMap> memory_map_;
190 };
191 
192 } // namespace io
193 } // namespace arrow
194 
195 #endif // ARROW_IO_FILE_H
Definition: interfaces.h:111
#define ARROW_NO_EXPORT
Definition: visibility.h:42
Definition: file.h:38
Definition: interfaces.h:121
Definition: file.h:141
Definition: status.h:106
Definition: interfaces.h:171
Top-level namespace for Apache Arrow C++ API.
Definition: adapter.h:32
Definition: file.h:89
type
Definition: interfaces.h:37
Base class for memory allocation.
Definition: memory_pool.h:34