|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | + * or more contributor license agreements. See the NOTICE file |
| 4 | + * distributed with this work for additional information |
| 5 | + * regarding copyright ownership. The ASF licenses this file |
| 6 | + * to you under the Apache License, Version 2.0 (the |
| 7 | + * "License"); you may not use this file except in compliance |
| 8 | + * with the License. You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | + |
| 20 | +#pragma once |
| 21 | + |
| 22 | +#include <memory> |
| 23 | +#include <string> |
| 24 | +#include <unordered_map> |
| 25 | + |
| 26 | +#include "iceberg/iceberg_export.h" |
| 27 | + |
| 28 | +namespace iceberg { |
| 29 | + |
| 30 | +// Forward declarations |
| 31 | +class Reader; |
| 32 | +class Writer; |
| 33 | + |
| 34 | +/// \brief An interface used to read input files using Reader and AsyncReader |
| 35 | +class ICEBERG_EXPORT InputFile { |
| 36 | + public: |
| 37 | + explicit InputFile(std::string location) : location_(std::move(location)) {} |
| 38 | + |
| 39 | + virtual ~InputFile() = default; |
| 40 | + |
| 41 | + /// \brief Checks whether the file exists. |
| 42 | + virtual bool exists() const = 0; |
| 43 | + /// \brief Returns the total length of the file, in bytes. |
| 44 | + virtual int64_t getLength() const = 0; |
| 45 | + |
| 46 | + /// \brief Get a Reader instance to read bytes from the file. |
| 47 | + virtual std::unique_ptr<Reader> newReader() = 0; |
| 48 | + |
| 49 | + /// \brief Get the file location |
| 50 | + const std::string& location() const { return location_; } |
| 51 | + |
| 52 | + protected: |
| 53 | + std::string location_; |
| 54 | +}; |
| 55 | + |
| 56 | +/// \brief An interface used to write output files using Writer and AsyncWriter |
| 57 | +class ICEBERG_EXPORT OutputFile { |
| 58 | + public: |
| 59 | + explicit OutputFile(std::string location) : location_(std::move(location)) {} |
| 60 | + |
| 61 | + virtual ~OutputFile() = default; |
| 62 | + |
| 63 | + /// \brief Create the file. |
| 64 | + /// |
| 65 | + /// If the file exists and overwrite is true, the file will be replaced, or an error |
| 66 | + /// will be thrown. |
| 67 | + virtual void create(bool overwrite = false) = 0; |
| 68 | + |
| 69 | + /// \brief Get a Writer instance to write bytes to the file. |
| 70 | + virtual std::unique_ptr<Writer> newWriter() = 0; |
| 71 | + |
| 72 | + /// \brief Get the file location |
| 73 | + const std::string& location() const { return location_; } |
| 74 | + |
| 75 | + /// \brief Return an InputFile for the location of this OutputFile. |
| 76 | + virtual std::shared_ptr<InputFile> toInputFile() const = 0; |
| 77 | + |
| 78 | + protected: |
| 79 | + std::string location_; |
| 80 | +}; |
| 81 | + |
| 82 | +/// \brief Pluggable module for reading, writing, and deleting files. |
| 83 | +/// |
| 84 | +/// Both table metadata files and data files can be written and read by this module. |
| 85 | +class ICEBERG_EXPORT FileIO { |
| 86 | + public: |
| 87 | + explicit FileIO(std::string name) : name_(std::move(name)) {} |
| 88 | + |
| 89 | + virtual ~FileIO() = default; |
| 90 | + |
| 91 | + /// \brief Get an InputFile |
| 92 | + /// |
| 93 | + /// Get a InputFile instance to read bytes from the file at the given path. |
| 94 | + virtual std::shared_ptr<InputFile> newInputFile(const std::string& path) = 0; |
| 95 | + |
| 96 | + /// \brief Get an OutputFile |
| 97 | + /// |
| 98 | + /// Get a OutputFile instance to write bytes to the file at the given path. |
| 99 | + virtual std::shared_ptr<OutputFile> newOutputFile(const std::string& path) = 0; |
| 100 | + |
| 101 | + /// \brief Delete file |
| 102 | + /// |
| 103 | + /// Delete the file at the given path. |
| 104 | + virtual void DeleteFile(const std::string& path) = 0; |
| 105 | + void DeleteFile(const InputFile& ifile) { return DeleteFile(ifile.location()); } |
| 106 | + void DeleteFile(const OutputFile& ofile) { return DeleteFile(ofile.location()); } |
| 107 | + |
| 108 | + const std::string& name() const { return name_; } |
| 109 | + |
| 110 | + protected: |
| 111 | + std::string name_; |
| 112 | +}; |
| 113 | + |
| 114 | +} // namespace iceberg |
0 commit comments