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:
41 
48  static Status Open(const std::string& path, std::shared_ptr<FileOutputStream>* file);
49 
54  static Status Open(const std::string& path, bool append,
55  std::shared_ptr<FileOutputStream>* file);
56 
57  // OutputStream interface
58  Status Close() override;
59  Status Tell(int64_t* position) const override;
60 
61  // Write bytes to the stream. Thread-safe
62  Status Write(const void* data, int64_t nbytes) override;
63 
64  int file_descriptor() const;
65 
66  private:
68 
69  class ARROW_NO_EXPORT FileOutputStreamImpl;
70  std::unique_ptr<FileOutputStreamImpl> impl_;
71 };
72 
73 // Operating system file
74 class ARROW_EXPORT ReadableFile : public RandomAccessFile {
75  public:
76  ~ReadableFile();
77 
82  static Status Open(const std::string& path, std::shared_ptr<ReadableFile>* file);
83 
89  static Status Open(const std::string& path, MemoryPool* pool,
90  std::shared_ptr<ReadableFile>* file);
91 
92  Status Close() override;
93  Status Tell(int64_t* position) const override;
94 
95  // Read bytes from the file. Thread-safe
96  Status Read(int64_t nbytes, int64_t* bytes_read, void* buffer) override;
97  Status Read(int64_t nbytes, std::shared_ptr<Buffer>* out) override;
98 
100  Status ReadAt(int64_t position, int64_t nbytes, int64_t* bytes_read,
101  void* out) override;
102 
104  Status ReadAt(int64_t position, int64_t nbytes, std::shared_ptr<Buffer>* out) override;
105 
106  Status GetSize(int64_t* size) override;
107  Status Seek(int64_t position) override;
108 
109  bool supports_zero_copy() const override;
110 
111  int file_descriptor() const;
112 
113  private:
114  explicit ReadableFile(MemoryPool* pool);
115 
116  class ARROW_NO_EXPORT ReadableFileImpl;
117  std::unique_ptr<ReadableFileImpl> impl_;
118 };
119 
120 // A file interface that uses memory-mapped files for memory interactions,
121 // supporting zero copy reads. The same class is used for both reading and
122 // writing.
123 //
124 // If opening a file in a writeable mode, it is not truncated first as with
125 // FileOutputStream
126 class ARROW_EXPORT MemoryMappedFile : public ReadWriteFileInterface {
127  public:
128  ~MemoryMappedFile();
129 
131  static Status Create(const std::string& path, int64_t size,
132  std::shared_ptr<MemoryMappedFile>* out);
133 
134  static Status Open(const std::string& path, FileMode::type mode,
135  std::shared_ptr<MemoryMappedFile>* out);
136 
137  Status Close() override;
138 
139  Status Tell(int64_t* position) const override;
140 
141  Status Seek(int64_t position) override;
142 
143  // Required by RandomAccessFile, copies memory into out. Not thread-safe
144  Status Read(int64_t nbytes, int64_t* bytes_read, void* out) override;
145 
146  // Zero copy read. Not thread-safe
147  Status Read(int64_t nbytes, std::shared_ptr<Buffer>* out) override;
148 
149  Status ReadAt(int64_t position, int64_t nbytes, int64_t* bytes_read,
150  void* out) override;
151 
153  Status ReadAt(int64_t position, int64_t nbytes, std::shared_ptr<Buffer>* out) override;
154 
155  bool supports_zero_copy() const override;
156 
158  Status Write(const void* data, int64_t nbytes) override;
159 
161  Status WriteAt(int64_t position, const void* data, int64_t nbytes) override;
162 
163  // @return: the size in bytes of the memory source
164  Status GetSize(int64_t* size) override;
165 
166  int file_descriptor() const;
167 
168  private:
170 
171  Status WriteInternal(const void* data, int64_t nbytes);
172 
173  class ARROW_NO_EXPORT MemoryMap;
174  std::shared_ptr<MemoryMap> memory_map_;
175 };
176 
177 } // namespace io
178 } // namespace arrow
179 
180 #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:126
Definition: status.h:106
Definition: interfaces.h:171
Top-level namespace for Apache Arrow C++ API.
Definition: adapter.h:32
Definition: file.h:74
type
Definition: interfaces.h:37
Base class for memory allocation.
Definition: memory_pool.h:34