Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-08-28 08:26:59

0001 // Licensed to the Apache Software Foundation (ASF) under one
0002 // or more contributor license agreements.  See the NOTICE file
0003 // distributed with this work for additional information
0004 // regarding copyright ownership.  The ASF licenses this file
0005 // to you under the Apache License, Version 2.0 (the
0006 // "License"); you may not use this file except in compliance
0007 // with the License.  You may obtain a copy of the License at
0008 //
0009 //   http://www.apache.org/licenses/LICENSE-2.0
0010 //
0011 // Unless required by applicable law or agreed to in writing,
0012 // software distributed under the License is distributed on an
0013 // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
0014 // KIND, either express or implied.  See the License for the
0015 // specific language governing permissions and limitations
0016 // under the License.
0017 
0018 // IO interface implementations for OS files
0019 
0020 #pragma once
0021 
0022 #include <cstdint>
0023 #include <memory>
0024 #include <string>
0025 #include <vector>
0026 
0027 #include "arrow/io/concurrency.h"
0028 #include "arrow/io/interfaces.h"
0029 #include "arrow/util/visibility.h"
0030 
0031 namespace arrow {
0032 
0033 class Buffer;
0034 class MemoryPool;
0035 class Status;
0036 
0037 namespace io {
0038 
0039 /// \brief An operating system file open in write-only mode.
0040 class ARROW_EXPORT FileOutputStream : public OutputStream {
0041  public:
0042   ~FileOutputStream() override;
0043 
0044   /// \brief Open a local file for writing, truncating any existing file
0045   /// \param[in] path with UTF8 encoding
0046   /// \param[in] append append to existing file, otherwise truncate to 0 bytes
0047   /// \return an open FileOutputStream
0048   ///
0049   /// When opening a new file, any existing file with the indicated path is
0050   /// truncated to 0 bytes, deleting any existing data
0051   static Result<std::shared_ptr<FileOutputStream>> Open(const std::string& path,
0052                                                         bool append = false);
0053 
0054   /// \brief Open a file descriptor for writing.  The underlying file isn't
0055   /// truncated.
0056   /// \param[in] fd file descriptor
0057   /// \return an open FileOutputStream
0058   ///
0059   /// The file descriptor becomes owned by the OutputStream, and will be closed
0060   /// on Close() or destruction.
0061   static Result<std::shared_ptr<FileOutputStream>> Open(int fd);
0062 
0063   // OutputStream interface
0064   Status Close() override;
0065   bool closed() const override;
0066   Result<int64_t> Tell() const override;
0067 
0068   // Write bytes to the stream. Thread-safe
0069   Status Write(const void* data, int64_t nbytes) override;
0070   /// \cond FALSE
0071   using Writable::Write;
0072   /// \endcond
0073 
0074   int file_descriptor() const;
0075 
0076  private:
0077   FileOutputStream();
0078 
0079   class ARROW_NO_EXPORT FileOutputStreamImpl;
0080   std::unique_ptr<FileOutputStreamImpl> impl_;
0081 };
0082 
0083 /// \brief An operating system file open in read-only mode.
0084 ///
0085 /// Reads through this implementation are unbuffered.  If many small reads
0086 /// need to be issued, it is recommended to use a buffering layer for good
0087 /// performance.
0088 class ARROW_EXPORT ReadableFile
0089     : public internal::RandomAccessFileConcurrencyWrapper<ReadableFile> {
0090  public:
0091   ~ReadableFile() override;
0092 
0093   /// \brief Open a local file for reading
0094   /// \param[in] path with UTF8 encoding
0095   /// \param[in] pool a MemoryPool for memory allocations
0096   /// \return ReadableFile instance
0097   static Result<std::shared_ptr<ReadableFile>> Open(
0098       const std::string& path, MemoryPool* pool = default_memory_pool());
0099 
0100   /// \brief Open a local file for reading
0101   /// \param[in] fd file descriptor
0102   /// \param[in] pool a MemoryPool for memory allocations
0103   /// \return ReadableFile instance
0104   ///
0105   /// The file descriptor becomes owned by the ReadableFile, and will be closed
0106   /// on Close() or destruction.
0107   static Result<std::shared_ptr<ReadableFile>> Open(
0108       int fd, MemoryPool* pool = default_memory_pool());
0109 
0110   bool closed() const override;
0111 
0112   int file_descriptor() const;
0113 
0114   Status WillNeed(const std::vector<ReadRange>& ranges) override;
0115 
0116  private:
0117   friend RandomAccessFileConcurrencyWrapper<ReadableFile>;
0118 
0119   explicit ReadableFile(MemoryPool* pool);
0120 
0121   Status DoClose();
0122   Result<int64_t> DoTell() const;
0123   Result<int64_t> DoRead(int64_t nbytes, void* buffer);
0124   Result<std::shared_ptr<Buffer>> DoRead(int64_t nbytes);
0125 
0126   /// \brief Thread-safe implementation of ReadAt
0127   Result<int64_t> DoReadAt(int64_t position, int64_t nbytes, void* out);
0128 
0129   /// \brief Thread-safe implementation of ReadAt
0130   Result<std::shared_ptr<Buffer>> DoReadAt(int64_t position, int64_t nbytes);
0131 
0132   Result<int64_t> DoGetSize();
0133   Status DoSeek(int64_t position);
0134 
0135   class ARROW_NO_EXPORT ReadableFileImpl;
0136   std::unique_ptr<ReadableFileImpl> impl_;
0137 };
0138 
0139 /// \brief A file interface that uses memory-mapped files for memory interactions
0140 ///
0141 /// This implementation supports zero-copy reads. The same class is used
0142 /// for both reading and writing.
0143 ///
0144 /// If opening a file in a writable mode, it is not truncated first as with
0145 /// FileOutputStream.
0146 class ARROW_EXPORT MemoryMappedFile : public ReadWriteFileInterface {
0147  public:
0148   ~MemoryMappedFile() override;
0149 
0150   /// Create new file with indicated size, return in read/write mode
0151   static Result<std::shared_ptr<MemoryMappedFile>> Create(const std::string& path,
0152                                                           int64_t size);
0153 
0154   // mmap() with whole file
0155   static Result<std::shared_ptr<MemoryMappedFile>> Open(const std::string& path,
0156                                                         FileMode::type mode);
0157 
0158   // mmap() with a region of file, the offset must be a multiple of the page size
0159   static Result<std::shared_ptr<MemoryMappedFile>> Open(const std::string& path,
0160                                                         FileMode::type mode,
0161                                                         const int64_t offset,
0162                                                         const int64_t length);
0163 
0164   Status Close() override;
0165 
0166   bool closed() const override;
0167 
0168   Result<int64_t> Tell() const override;
0169 
0170   Status Seek(int64_t position) override;
0171 
0172   // Required by RandomAccessFile, copies memory into out. Not thread-safe
0173   Result<int64_t> Read(int64_t nbytes, void* out) override;
0174 
0175   // Zero copy read, moves position pointer. Not thread-safe
0176   Result<std::shared_ptr<Buffer>> Read(int64_t nbytes) override;
0177 
0178   // Zero-copy read, leaves position unchanged. Acquires a reader lock
0179   // for the duration of slice creation (typically very short). Is thread-safe.
0180   Result<std::shared_ptr<Buffer>> ReadAt(int64_t position, int64_t nbytes) override;
0181 
0182   // Raw copy of the memory at specified position. Thread-safe, but
0183   // locks out other readers for the duration of memcpy. Prefer the
0184   // zero copy method
0185   Result<int64_t> ReadAt(int64_t position, int64_t nbytes, void* out) override;
0186 
0187   // Synchronous ReadAsync override
0188   Future<std::shared_ptr<Buffer>> ReadAsync(const IOContext&, int64_t position,
0189                                             int64_t nbytes) override;
0190 
0191   Status WillNeed(const std::vector<ReadRange>& ranges) override;
0192 
0193   bool supports_zero_copy() const override;
0194 
0195   /// Write data at the current position in the file. Thread-safe
0196   Status Write(const void* data, int64_t nbytes) override;
0197   /// \cond FALSE
0198   using Writable::Write;
0199   /// \endcond
0200 
0201   /// Set the size of the map to new_size.
0202   Status Resize(int64_t new_size);
0203 
0204   /// Write data at a particular position in the file. Thread-safe
0205   Status WriteAt(int64_t position, const void* data, int64_t nbytes) override;
0206 
0207   Result<int64_t> GetSize() override;
0208 
0209   int file_descriptor() const;
0210 
0211  private:
0212   MemoryMappedFile();
0213 
0214   Status WriteInternal(const void* data, int64_t nbytes);
0215 
0216   class ARROW_NO_EXPORT MemoryMap;
0217   std::shared_ptr<MemoryMap> memory_map_;
0218 };
0219 
0220 }  // namespace io
0221 }  // namespace arrow