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 
64  static Status Open(int fd, std::shared_ptr<OutputStream>* out);
65 
72  static Status Open(const std::string& path, std::shared_ptr<FileOutputStream>* file);
73 
78  static Status Open(const std::string& path, bool append,
79  std::shared_ptr<FileOutputStream>* file);
80 
88  static Status Open(int fd, std::shared_ptr<FileOutputStream>* out);
89 
90  // OutputStream interface
91  Status Close() override;
92  Status Tell(int64_t* position) const override;
93 
94  // Write bytes to the stream. Thread-safe
95  Status Write(const void* data, int64_t nbytes) override;
96 
97  int file_descriptor() const;
98 
99  private:
101 
102  class ARROW_NO_EXPORT FileOutputStreamImpl;
103  std::unique_ptr<FileOutputStreamImpl> impl_;
104 };
105 
106 // Operating system file
107 class ARROW_EXPORT ReadableFile : public RandomAccessFile {
108  public:
109  ~ReadableFile() override;
110 
115  static Status Open(const std::string& path, std::shared_ptr<ReadableFile>* file);
116 
122  static Status Open(const std::string& path, MemoryPool* pool,
123  std::shared_ptr<ReadableFile>* file);
124 
132  static Status Open(int fd, std::shared_ptr<ReadableFile>* file);
133 
142  static Status Open(int fd, MemoryPool* pool, std::shared_ptr<ReadableFile>* file);
143 
144  Status Close() override;
145  Status Tell(int64_t* position) const override;
146 
147  // Read bytes from the file. Thread-safe
148  Status Read(int64_t nbytes, int64_t* bytes_read, void* buffer) override;
149  Status Read(int64_t nbytes, std::shared_ptr<Buffer>* out) override;
150 
152  Status ReadAt(int64_t position, int64_t nbytes, int64_t* bytes_read,
153  void* out) override;
154 
156  Status ReadAt(int64_t position, int64_t nbytes, std::shared_ptr<Buffer>* out) override;
157 
158  Status GetSize(int64_t* size) override;
159  Status Seek(int64_t position) override;
160 
161  bool supports_zero_copy() const override;
162 
163  int file_descriptor() const;
164 
165  private:
166  explicit ReadableFile(MemoryPool* pool);
167 
168  class ARROW_NO_EXPORT ReadableFileImpl;
169  std::unique_ptr<ReadableFileImpl> impl_;
170 };
171 
172 // A file interface that uses memory-mapped files for memory interactions,
173 // supporting zero copy reads. The same class is used for both reading and
174 // writing.
175 //
176 // If opening a file in a writable mode, it is not truncated first as with
177 // FileOutputStream
178 class ARROW_EXPORT MemoryMappedFile : public ReadWriteFileInterface {
179  public:
180  ~MemoryMappedFile() override;
181 
183  static Status Create(const std::string& path, int64_t size,
184  std::shared_ptr<MemoryMappedFile>* out);
185 
186  static Status Open(const std::string& path, FileMode::type mode,
187  std::shared_ptr<MemoryMappedFile>* out);
188 
189  Status Close() override;
190 
191  Status Tell(int64_t* position) const override;
192 
193  Status Seek(int64_t position) override;
194 
195  // Required by RandomAccessFile, copies memory into out. Not thread-safe
196  Status Read(int64_t nbytes, int64_t* bytes_read, void* out) override;
197 
198  // Zero copy read, moves position pointer. Not thread-safe
199  Status Read(int64_t nbytes, std::shared_ptr<Buffer>* out) override;
200 
201  // Zero-copy read, leaves position unchanged. Acquires a reader lock
202  // for the duration of slice creation (typically very short). Is thread-safe.
203  Status ReadAt(int64_t position, int64_t nbytes, std::shared_ptr<Buffer>* out) override;
204 
205  // Raw copy of the memory at specified position. Thread-safe, but
206  // locks out other readers for the duration of memcpy. Prefer the
207  // zero copy method
208  Status ReadAt(int64_t position, int64_t nbytes, int64_t* bytes_read,
209  void* out) override;
210 
211  bool supports_zero_copy() const override;
212 
214  Status Write(const void* data, int64_t nbytes) override;
215 
217  Status Resize(int64_t new_size);
218 
220  Status WriteAt(int64_t position, const void* data, int64_t nbytes) override;
221 
222  // @return: the size in bytes of the memory source
223  Status GetSize(int64_t* size) override;
224 
225  int file_descriptor() const;
226 
227  private:
229 
230  Status WriteInternal(const void* data, int64_t nbytes);
231 
232  class ARROW_NO_EXPORT MemoryMap;
233  std::shared_ptr<MemoryMap> memory_map_;
234 };
235 
236 } // namespace io
237 } // namespace arrow
238 
239 #endif // ARROW_IO_FILE_H
Definition: interfaces.h:111
Definition: file.h:38
Definition: interfaces.h:127
Definition: file.h:178
Definition: status.h:95
Definition: interfaces.h:182
Top-level namespace for Apache Arrow C++ API.
Definition: adapter.h:32
#define ARROW_NO_EXPORT
Definition: visibility.h:42
Definition: file.h:107
type
Definition: interfaces.h:37
Base class for memory allocation.
Definition: memory_pool.h:34
::arrow::Buffer Buffer
Definition: memory.h:54