|
19 | 19 |
|
20 | 20 | #pragma once |
21 | 21 |
|
22 | | -#include <memory> |
| 22 | +#include <optional> |
23 | 23 | #include <string> |
| 24 | +#include <string_view> |
24 | 25 |
|
| 26 | +#include "iceberg/error.h" |
| 27 | +#include "iceberg/expected.h" |
25 | 28 | #include "iceberg/iceberg_export.h" |
26 | | -#include "iceberg/reader.h" |
27 | | -#include "iceberg/writer.h" |
28 | 29 |
|
29 | 30 | namespace iceberg { |
30 | 31 |
|
31 | | -/// \brief An interface used to read input files using Reader and AsyncReader |
32 | | -class ICEBERG_EXPORT InputFile { |
33 | | - public: |
34 | | - explicit InputFile(std::string location) : location_(std::move(location)) {} |
35 | | - |
36 | | - virtual ~InputFile() = default; |
37 | | - |
38 | | - /// \brief Checks whether the file exists. |
39 | | - virtual bool exists() const = 0; |
40 | | - /// \brief Returns the total length of the file, in bytes. |
41 | | - virtual int64_t getLength() const = 0; |
42 | | - |
43 | | - /// \brief Get a Reader instance to read bytes from the file. |
44 | | - virtual std::unique_ptr<Reader> newReader() = 0; |
45 | | - |
46 | | - /// \brief Get the file location |
47 | | - const std::string& location() const { return location_; } |
48 | | - |
49 | | - protected: |
50 | | - std::string location_; |
51 | | -}; |
52 | | - |
53 | | -/// \brief An interface used to write output files using Writer and AsyncWriter |
54 | | -class ICEBERG_EXPORT OutputFile { |
55 | | - public: |
56 | | - explicit OutputFile(std::string location) : location_(std::move(location)) {} |
57 | | - |
58 | | - virtual ~OutputFile() = default; |
59 | | - |
60 | | - /// \brief Create the file. |
61 | | - /// |
62 | | - /// If the file exists, or an error will be thrown. |
63 | | - virtual void create() = 0; |
64 | | - |
65 | | - /// \brief Get a Writer instance to write bytes to the file. |
66 | | - virtual std::unique_ptr<Writer> newWriter() = 0; |
67 | | - |
68 | | - /// \brief Get the file location |
69 | | - const std::string& location() const { return location_; } |
70 | | - |
71 | | - /// \brief Return an InputFile for the location of this OutputFile. |
72 | | - virtual std::shared_ptr<InputFile> toInputFile() const = 0; |
73 | | - |
74 | | - protected: |
75 | | - std::string location_; |
76 | | -}; |
77 | | - |
78 | | -/// \brief Pluggable module for reading, writing, and deleting files. |
| 32 | +/// \brief Pluggable module for reading, writing, and deleting metadata files. |
79 | 33 | /// |
80 | | -/// Both table metadata files and data files can be written and read by this module. |
| 34 | +/// This module only handle metadata files, not data files. The metadata files |
| 35 | +/// are typically small and are used to store schema, partition information, |
| 36 | +/// and other metadata about the table. |
81 | 37 | class ICEBERG_EXPORT FileIO { |
82 | 38 | public: |
83 | | - explicit FileIO(std::string name) : name_(std::move(name)) {} |
84 | | - |
| 39 | + FileIO() = default; |
85 | 40 | virtual ~FileIO() = default; |
86 | 41 |
|
87 | | - /// \brief Get an InputFile |
| 42 | + /// \brief Read the content of the file at the given location. |
88 | 43 | /// |
89 | | - /// Get a InputFile instance to read bytes from the file at the given location. |
90 | | - virtual std::shared_ptr<InputFile> newInputFile(const std::string& location) = 0; |
91 | | - |
92 | | - /// \brief Get an OutputFile |
| 44 | + /// \param file_location The location of the file to read. |
| 45 | + /// \param length The number of bytes to read. Some object storage need to specify |
| 46 | + /// the length to read, e.g. S3 `GetObject` has a Range parameter. |
| 47 | + /// \return The content of the file if the read succeeded, an error code if the read |
| 48 | + /// failed. |
| 49 | + virtual expected<std::string, Error> ReadFile(const std::string& file_location, |
| 50 | + std::optional<size_t> length) = 0; |
| 51 | + |
| 52 | + /// \brief Write the given content to the file at the given location. |
93 | 53 | /// |
94 | | - /// Get a OutputFile instance to write bytes to the file at the given location. |
95 | | - virtual std::shared_ptr<OutputFile> newOutputFile(const std::string& location) = 0; |
96 | | - |
97 | | - /// \brief Delete file |
| 54 | + /// \param file_location The location of the file to write. |
| 55 | + /// \param content The content to write to the file. |
| 56 | + /// \param overwrite If true, overwrite the file if it exists. If false, fail if the |
| 57 | + /// file exists. |
| 58 | + /// \return void if the write succeeded, an error code if the write failed. |
| 59 | + virtual expected<void, Error> WriteFile(const std::string& file_location, |
| 60 | + std::string_view content, bool overwrite) = 0; |
| 61 | + |
| 62 | + /// \brief Delete a file at the given location. |
98 | 63 | /// |
99 | | - /// Delete the file at the given location. |
100 | | - virtual void DeleteFile(const std::string& location) = 0; |
101 | | - void DeleteFile(const InputFile& ifile) { return DeleteFile(ifile.location()); } |
102 | | - void DeleteFile(const OutputFile& ofile) { return DeleteFile(ofile.location()); } |
103 | | - |
104 | | - const std::string& name() const { return name_; } |
105 | | - |
106 | | - protected: |
107 | | - std::string name_; |
| 64 | + /// \param file_location The location of the file to delete. |
| 65 | + /// \return void if the delete succeeded, an error code if the delete failed. |
| 66 | + virtual expected<void, Error> DeleteFile(const std::string& file_location) = 0; |
108 | 67 | }; |
109 | 68 |
|
110 | 69 | } // namespace iceberg |
0 commit comments