|
23 | 23 | #include <ranges> |
24 | 24 | #include <string> |
25 | 25 |
|
| 26 | +#include <nlohmann/json.hpp> |
| 27 | + |
| 28 | +#include "iceberg/file_io.h" |
| 29 | +#include "iceberg/json_internal.h" |
26 | 30 | #include "iceberg/partition_spec.h" |
27 | 31 | #include "iceberg/result.h" |
28 | 32 | #include "iceberg/schema.h" |
29 | 33 | #include "iceberg/sort_order.h" |
| 34 | +#include "iceberg/util/macros.h" |
30 | 35 | namespace iceberg { |
31 | 36 |
|
32 | 37 | std::string ToString(const SnapshotLogEntry& entry) { |
@@ -69,4 +74,35 @@ Result<std::shared_ptr<SortOrder>> TableMetadata::SortOrder() const { |
69 | 74 | return *iter; |
70 | 75 | } |
71 | 76 |
|
| 77 | +Result<MetadataFileCodecType> TableMetadataUtil::FromFileName( |
| 78 | + std::string_view file_name) { |
| 79 | + if (file_name.find(".metadata.json") == std::string::npos) { |
| 80 | + return InvalidArgument("{} is not a valid metadata file", file_name); |
| 81 | + } |
| 82 | + |
| 83 | + // We have to be backward-compatible with .metadata.json.gz files |
| 84 | + if (file_name.ends_with(".metadata.json.gz")) { |
| 85 | + return MetadataFileCodecType::kGzip; |
| 86 | + } |
| 87 | + |
| 88 | + std::string_view file_name_without_suffix = |
| 89 | + file_name.substr(0, file_name.find_last_of(".metadata.json")); |
| 90 | + if (file_name_without_suffix.ends_with(".gz")) { |
| 91 | + return MetadataFileCodecType::kGzip; |
| 92 | + } |
| 93 | + return MetadataFileCodecType::kNone; |
| 94 | +} |
| 95 | + |
| 96 | +Result<std::unique_ptr<TableMetadata>> TableMetadataUtil::Read( |
| 97 | + FileIO& io, const std::string& location, std::optional<size_t> length) { |
| 98 | + ICEBERG_ASSIGN_OR_RAISE(auto codec_type, FromFileName(location)); |
| 99 | + if (codec_type == MetadataFileCodecType::kGzip) { |
| 100 | + return NotImplemented("Reading gzip-compressed metadata files is not supported yet"); |
| 101 | + } |
| 102 | + |
| 103 | + ICEBERG_ASSIGN_OR_RAISE(auto content, io.ReadFile(location, length)); |
| 104 | + ICEBERG_ASSIGN_OR_RAISE(auto json, FromJsonString(content)); |
| 105 | + return TableMetadataFromJson(json); |
| 106 | +} |
| 107 | + |
72 | 108 | } // namespace iceberg |
0 commit comments