|
| 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 <optional> |
| 23 | +#include <string> |
| 24 | +#include <string_view> |
| 25 | + |
| 26 | +#include "iceberg/error.h" |
| 27 | +#include "iceberg/expected.h" |
| 28 | +#include "iceberg/iceberg_export.h" |
| 29 | + |
| 30 | +namespace iceberg { |
| 31 | + |
| 32 | +/// \brief Pluggable module for reading, writing, and deleting files. |
| 33 | +/// |
| 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. |
| 37 | +/// |
| 38 | +/// Note that these functions are not atomic. For example, if a write fails, |
| 39 | +/// the file may be partially written. Implementations should be careful to |
| 40 | +/// avoid corrupting metadata files. |
| 41 | +class ICEBERG_EXPORT FileIO { |
| 42 | + public: |
| 43 | + FileIO() = default; |
| 44 | + virtual ~FileIO() = default; |
| 45 | + |
| 46 | + /// \brief Read the content of the file at the given location. |
| 47 | + /// |
| 48 | + /// \param file_location The location of the file to read. |
| 49 | + /// \param length The number of bytes to read. Some object storage need to specify |
| 50 | + /// the length to read, e.g. S3 `GetObject` has a Range parameter. |
| 51 | + /// \return The content of the file if the read succeeded, an error code if the read |
| 52 | + /// failed. |
| 53 | + virtual expected<std::string, Error> ReadFile(const std::string& file_location, |
| 54 | + std::optional<size_t> length) { |
| 55 | + // We provide a default implementation to avoid Windows linker error LNK2019. |
| 56 | + return unexpected<Error>{ |
| 57 | + {.kind = ErrorKind::kNotImplemented, .message = "ReadFile not implemented"}}; |
| 58 | + } |
| 59 | + |
| 60 | + /// \brief Write the given content to the file at the given location. |
| 61 | + /// |
| 62 | + /// \param file_location The location of the file to write. |
| 63 | + /// \param content The content to write to the file. |
| 64 | + /// \param overwrite If true, overwrite the file if it exists. If false, fail if the |
| 65 | + /// file exists. |
| 66 | + /// \return void if the write succeeded, an error code if the write failed. |
| 67 | + virtual expected<void, Error> WriteFile(const std::string& file_location, |
| 68 | + std::string_view content) { |
| 69 | + return unexpected<Error>{ |
| 70 | + {.kind = ErrorKind::kNotImplemented, .message = "WriteFile not implemented"}}; |
| 71 | + } |
| 72 | + |
| 73 | + /// \brief Delete a file at the given location. |
| 74 | + /// |
| 75 | + /// \param file_location The location of the file to delete. |
| 76 | + /// \return void if the delete succeeded, an error code if the delete failed. |
| 77 | + virtual expected<void, Error> DeleteFile(const std::string& file_location) { |
| 78 | + return unexpected<Error>{ |
| 79 | + {.kind = ErrorKind::kNotImplemented, .message = "DeleteFile not implemented"}}; |
| 80 | + } |
| 81 | +}; |
| 82 | + |
| 83 | +} // namespace iceberg |
0 commit comments