|
| 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 | +#include "iceberg/resolving_file_io.h" |
| 21 | + |
| 22 | +#include <utility> |
| 23 | + |
| 24 | +#include "iceberg/file_io_registry.h" |
| 25 | +#include "iceberg/resolving_file_io_internal.h" |
| 26 | +#include "iceberg/util/macros.h" |
| 27 | + |
| 28 | +namespace iceberg { |
| 29 | + |
| 30 | +ResolvingFileIO::ResolvingFileIO(std::unordered_map<std::string, std::string> properties) |
| 31 | + : properties_(std::move(properties)) {} |
| 32 | + |
| 33 | +ResolvingFileIO::~ResolvingFileIO() = default; |
| 34 | + |
| 35 | +Result<std::string_view> ResolveFileIOName(std::string_view location) { |
| 36 | + const auto pos = location.find("://"); |
| 37 | + if (pos == std::string_view::npos) { |
| 38 | + return FileIORegistry::kArrowLocalFileIO; |
| 39 | + } |
| 40 | + |
| 41 | + const auto scheme = location.substr(0, pos); |
| 42 | + if (scheme == "file") { |
| 43 | + return FileIORegistry::kArrowLocalFileIO; |
| 44 | + } |
| 45 | + // S3-compatible schemes served by the S3 FileIO (Java: SCHEME_TO_FILE_IO). |
| 46 | + // Keep in sync with CanonicalizeS3Scheme in arrow_s3_file_io.cc. |
| 47 | + if (scheme == "s3" || scheme == "s3a" || scheme == "s3n" || scheme == "oss") { |
| 48 | + return FileIORegistry::kArrowS3FileIO; |
| 49 | + } |
| 50 | + |
| 51 | + return NotSupported("URI scheme '{}' is not supported for FileIO resolution", scheme); |
| 52 | +} |
| 53 | + |
| 54 | +Result<FileIO*> ResolvingFileIO::FileIOForPath(std::string_view location) { |
| 55 | + ICEBERG_ASSIGN_OR_RAISE(const auto name, ResolveFileIOName(location)); |
| 56 | + |
| 57 | + std::lock_guard lock(mutex_); |
| 58 | + auto it = io_by_name_.find(name); |
| 59 | + if (it == io_by_name_.end()) { |
| 60 | + ICEBERG_ASSIGN_OR_RAISE(auto io, |
| 61 | + FileIORegistry::Load(std::string(name), properties_)); |
| 62 | + // Forward all credentials; each implementation applies the prefixes it |
| 63 | + // understands. |
| 64 | + if (!storage_credentials_.empty()) { |
| 65 | + if (auto* credentialed = io->AsSupportsStorageCredentials()) { |
| 66 | + ICEBERG_RETURN_UNEXPECTED( |
| 67 | + credentialed->SetStorageCredentials(storage_credentials_)); |
| 68 | + } |
| 69 | + } |
| 70 | + it = io_by_name_.emplace(std::string(name), std::move(io)).first; |
| 71 | + } |
| 72 | + return it->second.get(); |
| 73 | +} |
| 74 | + |
| 75 | +Result<std::unique_ptr<InputFile>> ResolvingFileIO::NewInputFile( |
| 76 | + std::string file_location) { |
| 77 | + ICEBERG_ASSIGN_OR_RAISE(auto* io, FileIOForPath(file_location)); |
| 78 | + return io->NewInputFile(std::move(file_location)); |
| 79 | +} |
| 80 | + |
| 81 | +Result<std::unique_ptr<InputFile>> ResolvingFileIO::NewInputFile( |
| 82 | + std::string file_location, size_t length) { |
| 83 | + ICEBERG_ASSIGN_OR_RAISE(auto* io, FileIOForPath(file_location)); |
| 84 | + return io->NewInputFile(std::move(file_location), length); |
| 85 | +} |
| 86 | + |
| 87 | +Result<std::unique_ptr<OutputFile>> ResolvingFileIO::NewOutputFile( |
| 88 | + std::string file_location) { |
| 89 | + ICEBERG_ASSIGN_OR_RAISE(auto* io, FileIOForPath(file_location)); |
| 90 | + return io->NewOutputFile(std::move(file_location)); |
| 91 | +} |
| 92 | + |
| 93 | +Status ResolvingFileIO::DeleteFile(const std::string& file_location) { |
| 94 | + ICEBERG_ASSIGN_OR_RAISE(auto* io, FileIOForPath(file_location)); |
| 95 | + return io->DeleteFile(file_location); |
| 96 | +} |
| 97 | + |
| 98 | +Status ResolvingFileIO::DeleteFiles(const std::vector<std::string>& file_locations) { |
| 99 | + std::unordered_map<FileIO*, std::vector<std::string>> locations_by_io; |
| 100 | + for (const auto& file_location : file_locations) { |
| 101 | + ICEBERG_ASSIGN_OR_RAISE(auto* io, FileIOForPath(file_location)); |
| 102 | + locations_by_io[io].push_back(file_location); |
| 103 | + } |
| 104 | + for (auto& [io, locations] : locations_by_io) { |
| 105 | + ICEBERG_RETURN_UNEXPECTED(io->DeleteFiles(locations)); |
| 106 | + } |
| 107 | + return {}; |
| 108 | +} |
| 109 | + |
| 110 | +Status ResolvingFileIO::SetStorageCredentials( |
| 111 | + const std::vector<StorageCredential>& storage_credentials) { |
| 112 | + // Rebuild delegates lazily with the new credentials. Updating live delegates |
| 113 | + // instead would leave the resolver inconsistent if one of them rejected them. |
| 114 | + std::lock_guard lock(mutex_); |
| 115 | + storage_credentials_ = storage_credentials; |
| 116 | + io_by_name_.clear(); |
| 117 | + return {}; |
| 118 | +} |
| 119 | + |
| 120 | +const std::vector<StorageCredential>& ResolvingFileIO::credentials() const { |
| 121 | + return storage_credentials_; |
| 122 | +} |
| 123 | + |
| 124 | +} // namespace iceberg |
0 commit comments