|
| 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 <functional> |
| 23 | +#include <memory> |
| 24 | +#include <mutex> |
| 25 | +#include <string> |
| 26 | +#include <unordered_map> |
| 27 | + |
| 28 | +#include "iceberg/file_io.h" |
| 29 | +#include "iceberg/iceberg_export.h" |
| 30 | +#include "iceberg/result.h" |
| 31 | + |
| 32 | +namespace iceberg { |
| 33 | + |
| 34 | +/// \brief Registry for FileIO implementations. |
| 35 | +/// |
| 36 | +/// Provides a mechanism to register and load FileIO implementations by name. |
| 37 | +/// This allows the REST catalog (and others) to resolve FileIO implementations |
| 38 | +/// at runtime based on configuration properties like "io-impl". |
| 39 | +class ICEBERG_EXPORT FileIORegistry { |
| 40 | + public: |
| 41 | + /// Well-known implementation names |
| 42 | + static constexpr const char* kArrowLocalFileIO = |
| 43 | + "org.apache.iceberg.arrow.ArrowFileIO"; |
| 44 | + static constexpr const char* kArrowS3FileIO = |
| 45 | + "org.apache.iceberg.arrow.ArrowS3FileIO"; |
| 46 | + |
| 47 | + /// Factory function type for creating FileIO instances. |
| 48 | + using Factory = std::function<Result<std::shared_ptr<FileIO>>( |
| 49 | + const std::string& warehouse, |
| 50 | + const std::unordered_map<std::string, std::string>& properties)>; |
| 51 | + |
| 52 | + /// \brief Register a FileIO factory under the given name. |
| 53 | + /// |
| 54 | + /// \param name The implementation name (e.g., "org.apache.iceberg.arrow.ArrowFileIO") |
| 55 | + /// \param factory The factory function that creates the FileIO instance. |
| 56 | + static void Register(const std::string& name, Factory factory) { |
| 57 | + std::lock_guard lock(Mutex()); |
| 58 | + Registry()[name] = std::move(factory); |
| 59 | + } |
| 60 | + |
| 61 | + /// \brief Load a FileIO implementation by name. |
| 62 | + /// |
| 63 | + /// \param name The implementation name to look up. |
| 64 | + /// \param warehouse The warehouse location URI. |
| 65 | + /// \param properties Configuration properties to pass to the factory. |
| 66 | + /// \return A shared_ptr to the FileIO instance, or an error if not found. |
| 67 | + static Result<std::shared_ptr<FileIO>> Load( |
| 68 | + const std::string& name, const std::string& warehouse, |
| 69 | + const std::unordered_map<std::string, std::string>& properties) { |
| 70 | + Factory factory; |
| 71 | + { |
| 72 | + std::lock_guard lock(Mutex()); |
| 73 | + auto it = Registry().find(name); |
| 74 | + if (it == Registry().end()) { |
| 75 | + return std::unexpected<Error>( |
| 76 | + {.kind = ErrorKind::kNotFound, |
| 77 | + .message = "FileIO implementation not found: " + name}); |
| 78 | + } |
| 79 | + factory = it->second; |
| 80 | + } |
| 81 | + // Invoke factory outside the lock to avoid blocking other Register/Load |
| 82 | + // calls and to prevent deadlocks if the factory calls back into the registry. |
| 83 | + return factory(warehouse, properties); |
| 84 | + } |
| 85 | + |
| 86 | + private: |
| 87 | + static std::unordered_map<std::string, Factory>& Registry() { |
| 88 | + static std::unordered_map<std::string, Factory> registry; |
| 89 | + return registry; |
| 90 | + } |
| 91 | + |
| 92 | + static std::mutex& Mutex() { |
| 93 | + static std::mutex mutex; |
| 94 | + return mutex; |
| 95 | + } |
| 96 | +}; |
| 97 | + |
| 98 | +/// \brief Property keys for FileIO configuration. |
| 99 | +struct FileIOProperties { |
| 100 | + /// The FileIO implementation class name (e.g., "org.apache.iceberg.arrow.ArrowFileIO") |
| 101 | + static constexpr const char* kImpl = "io-impl"; |
| 102 | +}; |
| 103 | + |
| 104 | +} // namespace iceberg |
0 commit comments